forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_list_data.go
258 lines (237 loc) · 8.33 KB
/
get_list_data.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package uadmin
import (
"fmt"
"html"
"html/template"
"net/http"
"path"
"reflect"
"strconv"
"strings"
"time"
"github.com/PesTospertnyj/uadmin/helper"
)
// GetListSchema returns a schema for list view
func getListData(a interface{}, PageLength int, r *http.Request, session *Session, query string, schema *ModelSchema, args ...interface{}) (l *listData) {
l = &listData{}
//schema, _ := getSchema(a)
language := getLanguage(r)
TranslateSchema(schema, language.Code)
t := reflect.TypeOf(a)
// For Order By and Pagination
o := r.FormValue("o")
asc := !strings.HasPrefix(o, "-")
if !asc {
o = strings.Replace(o, "-", "", 1)
}
p := r.FormValue("p")
page, _ := strconv.ParseInt(p, 10, 32)
m, ok := NewModelArray(schema.ModelName, false)
if !ok {
Trail(ERROR, "getListSchema.NewModelArray. No model name", schema.ModelName)
return
}
newModel, _ := NewModel(schema.ModelName, false)
iPager, isPager := newModel.Interface().(adminPager)
iCounter, isCounter := newModel.Interface().(counter)
var (
_query interface{}
_args []interface{}
)
_query, _args = getFilter(r, session, schema)
if _query.(string) != "" {
if query == "" {
query = _query.(string)
} else {
query += " AND " + _query.(string)
}
args = append(args, _args...)
}
if !isPager {
if OptimizeSQLQuery {
FilterList(schema, o, asc, int(page-1)*PageLength, PageLength, m.Addr().Interface(), query, args...)
} else {
AdminPage(o, asc, int(page-1)*PageLength, PageLength, m.Addr().Interface(), query, args...)
}
} else {
iPager.AdminPage(o, asc, int(page-1)*PageLength, PageLength, m.Addr().Interface(), query, args...)
}
if !isCounter {
l.Count = Count(m.Interface(), query, args...)
} else {
l.Count = iCounter.Count(m.Interface(), query, args...)
}
for i := 0; i < m.Len(); i++ {
l.Rows = append(l.Rows, evaluateObject(m.Index(i).Interface(), t, schema, language.Code, session))
}
return
}
// evaluateObject !
func evaluateObject(obj interface{}, t reflect.Type, s *ModelSchema, lang string, session *Session) (y []interface{}) {
value := reflect.ValueOf(obj)
for index := 0; index < len(s.Fields); index++ {
if s.Fields[index].IsMethod {
if strings.Contains(s.Fields[index].Name, "__List") {
in := []reflect.Value{}
method := value.MethodByName(s.Fields[index].Name)
ret := method.Call(in)
y = append(y, template.HTML(stripHTMLScriptTag(fmt.Sprint(ret[0].Interface()))))
}
continue
}
field, _ := t.FieldByName(s.Fields[index].Name)
if strings.ToLower(string(field.Name[0])) == string(field.Name[0]) {
continue
}
if !s.Fields[index].ListDisplay {
continue
}
v := value.FieldByName(field.Name)
if s.Fields[index].Type == cID {
id, ok := v.Interface().(uint)
if !ok {
Trail(ERROR, "evaluateObject.Interface.(uadmin.Model) ID NOT OK. %#v", v.Interface())
}
temp := template.HTML(fmt.Sprintf("<a class='clickable Row_id no-style bold' data-id='%d' href='%s%s/%d'>%s</a>", id, RootURL, s.ModelName, id, html.EscapeString(GetString(obj))))
y = append(y, temp)
} else if s.Fields[index].Type == cNUMBER {
temp := v.Interface()
y = append(y, temp)
} else if s.Fields[index].Type == cPROGRESSBAR {
tempProgressValue, _ := strconv.ParseFloat(fmt.Sprint(v.Interface()), 64) // 10
tempProgressColor := ""
maxThreshold := 0.0
var maxColor string
if len(s.Fields[index].ProgressBar) == 1 {
for tempThreshold, tempColor := range s.Fields[index].ProgressBar {
tempProgressColor = tempColor
maxThreshold = tempThreshold
}
} else {
for tempThreshold, tempColor := range s.Fields[index].ProgressBar {
if tempThreshold > maxThreshold {
maxThreshold = tempThreshold
maxColor = tempColor
}
}
currentThreshold := maxThreshold
tempProgressColor = maxColor
for tempThreshold, tempColor := range s.Fields[index].ProgressBar {
if tempThreshold > tempProgressValue && tempThreshold < currentThreshold {
tempProgressColor = tempColor
currentThreshold = tempThreshold
}
}
}
tempProgressWidth := tempProgressValue / maxThreshold * 100.0
if tempProgressValue > maxThreshold {
tempProgressWidth = 100.0
tempProgressColor = maxColor
}
tempColor := helper.GetRGB(tempProgressColor)
DarkerFactor := 0.67
tempDarker := []int{
int(float64(tempColor[0]) * DarkerFactor),
int(float64(tempColor[1]) * DarkerFactor),
int(float64(tempColor[2]) * DarkerFactor),
}
tempGradient1 := fmt.Sprintf("#%02x%02x%02x", tempColor[0], tempColor[1], tempColor[2])
tempGradient2 := fmt.Sprintf("#%02x%02x%02x", tempDarker[0], tempDarker[1], tempDarker[2])
temp := template.HTML(fmt.Sprintf("<div style='border:solid 1px;'><div style='width:%d%%; background-image:linear-gradient(%s,%s); text-align:center;'>%.2f</div></div>", int(tempProgressWidth), tempGradient1, tempGradient2, tempProgressValue))
y = append(y, temp)
} else if s.Fields[index].Type == cMONEY {
temp := commaf(v.Interface())
y = append(y, temp)
} else if s.Fields[index].Type == cLINK {
if fmt.Sprint(v) != "" {
URL := v.String()
if URL != "" {
if !strings.Contains(URL, "?") {
URL += "?"
} else {
URL += "&"
}
URL += "x-csrf-token=" + session.Key
}
temp := template.HTML(fmt.Sprintf("<a class='btn btn-primary uadmin-link' href='%s'>%s</a>", URL, s.Fields[index].Name))
y = append(y, temp)
} else {
temp := template.HTML("<span></span>")
y = append(y, temp)
}
} else if s.Fields[index].Type == cDATE {
if fmt.Sprint(v.Type())[0] == '*' {
if v.IsNil() {
y = append(y, "")
} else {
v = v.Elem()
d, _ := v.Interface().(time.Time)
y = append(y, d.Format("2006-01-02 15:04:05"))
}
} else {
d, _ := v.Interface().(time.Time)
y = append(y, d.Format("2006-01-02 15:04:05"))
}
} else if s.Fields[index].Type == cFK {
vID := value.FieldByName(field.Name + "ID")
cIndex, _ := vID.Interface().(uint)
fkFieldName := strings.ToLower(value.FieldByName(s.Fields[index].Name).Type().Name())
if value.FieldByName(s.Fields[index].Name).Type().Kind() == reflect.Ptr {
fkFieldName = strings.ToLower(value.FieldByName(s.Fields[index].Name).Type().Elem().Name())
}
// Fetch that record from DB
fkModel, _ := NewModel(fkFieldName, true)
GetStringer(fkModel.Interface(), "id = ?", cIndex)
temp := template.HTML(fmt.Sprintf("<a class='clickable no-style bold' href='%s%s/%d'>%s</a>", RootURL, fkFieldName, cIndex, html.EscapeString(GetString(fkModel.Interface()))))
y = append(y, temp)
} else if s.Fields[index].Type == cBOOL {
var temp template.HTML
tempValue, _ := v.Interface().(bool)
if tempValue {
temp = template.HTML(`<i class="fa fa-check-circle" aria-hidden=TRUE style="color:green;"></i>`)
} else {
temp = template.HTML(`<i class="fa fa-times-circle" aria-hidden=TRUE style="color:red;"></i>`)
}
y = append(y, temp)
} else if s.Fields[index].Type == cLIST {
cIndex := v.Int()
choiceAdded := false
if s.Fields[index].LimitChoicesTo != nil {
s.Fields[index].Choices = s.Fields[index].LimitChoicesTo(obj, &session.User)
}
for cCounter := 0; cCounter < len(s.Fields[index].Choices); cCounter++ {
if uint(cIndex) == s.Fields[index].Choices[cCounter].K {
y = append(y, s.Fields[index].Choices[uint(cCounter)].V)
choiceAdded = true
break
}
}
if !choiceAdded {
y = append(y, cIndex)
}
} else if s.Fields[index].Type == cIMAGE || s.Fields[index].Type == cIMAGE_MINIO {
temp := template.HTML(fmt.Sprintf(`<img class="hvr-grow pointer image_trigger" style="max-width: 50px; height: auto;" src="%s" />`, v.Interface()))
y = append(y, temp)
} else if s.Fields[index].Type == cFILE {
if v.Interface() != "" {
fileLocation := v.Interface().(string)
fileName := path.Base(fileLocation)
temp := template.HTML(fmt.Sprintf(`<a href="%s">%s</a>`, v.Interface(), fileName))
y = append(y, temp)
} else {
y = append(y, "")
}
} else if s.Fields[index].Type == cCODE {
temp := template.HTML(fmt.Sprintf(`<pre style="width: 200px; white-space: pre-wrap;">%s</pre>`, v.Interface()))
y = append(y, temp)
} else if s.Fields[index].Type == cMULTILINGUAL {
y = append(y, Translate(fmt.Sprint(v), lang, true))
} else if s.Fields[index].Type == cHTML {
str := helper.StripTags(fmt.Sprint(v))
y = append(y, str)
} else {
y = append(y, v)
}
}
return
}