forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_form_data.go
312 lines (284 loc) · 8.96 KB
/
get_form_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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package uadmin
import (
"fmt"
"html/template"
//"github.com/jinzhu/gorm"
"net/http"
"reflect"
"strconv"
"strings"
"time"
)
func getFormData(a interface{}, r *http.Request, session *Session, s *ModelSchema, user *User) {
// This holds the formatted value of the field
var value interface{}
var f *F
var err error
// Get the type of model
t := reflect.TypeOf(a)
// Get the value of the model
modelValue := reflect.ValueOf(a)
// Get the primary key
newForm := r.FormValue("ModelID") == "0"
ModelID64, _ := strconv.ParseUint(r.FormValue("ModelID"), 10, 64)
// Loop over the fields of the model and get schema information
for index := 0; index < t.NumField(); index++ {
// Read field value
fieldValue := modelValue.Field(index)
// Get the field from schema
f = &F{}
fName := t.Field(index).Name
if t.Field(index).Anonymous {
fName = "ID"
}
for i := range s.Fields {
if s.Fields[i].Name == fName {
f = &s.Fields[i]
break
}
}
if f.Hidden || !f.FormDisplay {
continue
}
if f.Type == cFK {
fieldValue = modelValue.FieldByName(f.Name + "ID")
}
// Check if the field was not found in schema
if f.Name == "" {
continue
}
// For new records:
// Overide field value with any values passed in request
// If not available check if there is a default value for the field
//if newForm {
if r.FormValue(t.Field(index).Name) != "" {
if f.Type == cNUMBER || f.Type == cLIST {
fValue, _ := strconv.ParseInt(r.FormValue(t.Field(index).Name), 10, 64)
fieldValue = reflect.ValueOf(fValue)
} else if f.Type == cDATE {
var tm time.Time
if t.Field(index).Type.Kind() == reflect.Ptr {
var ptm *time.Time
if r.FormValue(t.Field(index).Name) == "" {
fieldValue = reflect.ValueOf(ptm)
} else {
tm, err = time.Parse("2006-01-02 15:04", r.FormValue(t.Field(index).Name))
if err != nil {
Trail(ERROR, "getFormData unable to parse time format(%s). %s", r.FormValue(t.Field(index).Name), err)
}
fieldValue = reflect.ValueOf(&tm)
}
} else {
tm, err = time.Parse("2006-01-02 15:04", r.FormValue(t.Field(index).Name))
if err != nil {
Trail(ERROR, "getFormData unable to parse time format(%s). %s", r.FormValue(t.Field(index).Name), err)
}
fieldValue = reflect.ValueOf(tm)
}
} else if f.Type == cBOOL {
fieldValue = reflect.ValueOf(r.FormValue(t.Field(index).Name) == "on")
} else {
fieldValue = reflect.ValueOf(r.FormValue(t.Field(index).Name))
}
} else if f.DefaultValue != "" && newForm {
DefaultValue := f.DefaultValue
DefaultValue = strings.Replace(DefaultValue, "{NOW}", time.Now().Format("2006-01-02 15:04:05"), -1)
fieldValue = reflect.ValueOf(DefaultValue)
}
// Check for approval
if f.Approval {
// Check if there is an approval record
approvals := []Approval{}
Filter(&approvals, "model_name = ? AND column_name = ? AND model_pk = ?", strings.ToLower(t.Name()), f.Name, ModelID64)
if len(approvals) != 0 {
// Get the last approval
lastA := approvals[len(approvals)-1]
f.ApprovalAction = lastA.ApprovalAction
f.NewValue = lastA.NewValueDescription
f.ChangedBy = lastA.ChangedBy
f.ChangeDate = &lastA.ChangeDate
f.ApprovalDate = lastA.ApprovalDate
f.ApprovalBy = lastA.ApprovalBy
f.ApprovalID = lastA.ID
f.OldValue = lastA.OldValue
// Remove required if the field has a pending approval
f.Required = f.Required && (f.ApprovalAction != ApprovalAction(0))
}
}
// Check the data type
if f.Type == cID {
m, ok := fieldValue.Interface().(Model)
if !ok {
Trail(ERROR, "Unable tp parse value of ID for %s.%s (%#v)", t.Name(), f.Name, fieldValue.Interface())
}
value = m.ID
} else if f.Type == cNUMBER {
if f.Format != "" {
value = fmt.Sprintf(f.Format, fieldValue.Interface())
} else {
value = fieldValue.Interface()
}
} else if f.Type == cFK {
// Get selected items's ID
fkValue, _ := strconv.ParseUint(fmt.Sprint(fieldValue.Interface()), 10, 64)
value = fkValue
if f.LimitChoicesTo == nil {
fkType := t.Field(index).Type.Name()
if t.Field(index).Type.Kind() == reflect.Ptr {
fkType = t.Field(index).Type.Elem().Name()
}
fkList, _ := NewModelArray(strings.ToLower(fkType), false)
All(fkList.Addr().Interface())
// Build choices
f.Choices = []Choice{
{
K: 0,
V: "-",
Selected: uint(fkValue) == 0,
},
}
for i := 0; i < fkList.Len(); i++ {
f.Choices = append(f.Choices, Choice{
K: GetID(fkList.Index(i)),
V: GetString(fkList.Index(i).Interface()),
Selected: uint(fkValue) == GetID(fkList.Index(i)),
})
}
} else {
f.Choices = f.LimitChoicesTo(a, &session.User)
for i := 0; i < len(f.Choices); i++ {
f.Choices[i].Selected = uint(fkValue) == f.Choices[i].K
}
}
} else if f.Type == cM2M {
if fmt.Sprint(reflect.TypeOf(fieldValue.Interface())) == "string" {
continue
}
fKType := reflect.TypeOf(fieldValue.Interface()).Elem()
m, ok := NewModelArray(strings.ToLower(fKType.Name()), false)
if !ok {
Trail(ERROR, "GetListSchema.NewModelArray. No model name (%s)", s.ModelName)
}
if f.LimitChoicesTo == nil {
All(m.Addr().Interface())
f.Choices = []Choice{}
for i := 0; i < m.Len(); i++ {
item := m.Index(i).Interface()
id := GetID(m.Index(i))
// if id == myID {
// continue
// }
f.Choices = append(f.Choices, Choice{
K: id,
V: GetString(item),
})
}
} else {
f.Choices = f.LimitChoicesTo(a, &session.User)
}
for i := 0; i < fieldValue.Len(); i++ {
for counter, val := range f.Choices {
itemID := GetID(fieldValue.Index(i))
if val.K == itemID {
f.Choices[counter].Selected = true
}
}
}
} else if f.Type == cDATE {
if newForm && t.Field(index).Type.Kind() != reflect.Ptr {
value = time.Now().Format("2006-01-02 15:04:05")
} else {
var d *time.Time
// If the date is not a pointer to date make it a pointer
if t.Field(index).Type.Kind() != reflect.Ptr {
tempD, _ := fieldValue.Interface().(time.Time)
d = &tempD
} else {
d, _ = fieldValue.Interface().(*time.Time)
}
if d == nil {
value = ""
} else {
value = d.Format("2006-01-02 15:04:05") //2006-01-02 15:04:05
}
}
} else if f.Type == cBOOL {
d, ok := fieldValue.Interface().(bool)
if !ok {
Trail(ERROR, "Unable to parse bool value for %s.%s (%#v)", t.Name(), f.Name, fieldValue.Interface())
}
value = d
} else if f.Type == cLIST {
value = fieldValue.Int()
if f.LimitChoicesTo != nil {
f.Choices = append([]Choice{{"-", 0, false}}, f.LimitChoicesTo(a, &session.User)...)
}
for i := range f.Choices {
f.Choices[i].Selected = f.Choices[i].K == uint(fieldValue.Int())
}
} else if f.Type == cMULTILINGUAL {
value = fieldValue.Interface()
for i := range activeLangs {
f.Translations[i].Value = Translate(fmt.Sprint(value), activeLangs[i].Code, false)
if f.ChangedBy != "" {
f.Translations[i].NewValue = Translate(fmt.Sprint(f.NewValue), activeLangs[i].Code, false)
f.Translations[i].OldValue = Translate(fmt.Sprint(f.OldValue), activeLangs[i].Code, false)
}
}
} else if f.Type == cLINK {
URL := fieldValue.String()
if URL != "" {
if strings.Contains(URL, "?") {
URL += "&"
} else {
URL += "?"
}
URL += "x-csrf-token=" + session.Key
}
value = URL
} else {
value = fieldValue.Interface()
}
f.Value = value
}
// Get data from method fields
for index := 0; index < t.NumMethod(); index++ {
// Check if the method should be included in the field list
if strings.Contains(t.Method(index).Name, "__Form") {
if strings.ToLower(string(t.Method(index).Name[0])) == string(t.Method(index).Name[0]) {
continue
}
in := []reflect.Value{}
ret := modelValue.Method(index).Call(in)
s.FieldByName(t.Method(index).Name).Value = template.HTML(stripHTMLScriptTag(fmt.Sprint(ret[0].Interface())))
}
}
inlineData := []listData{}
if uint(ModelID64) != 0 {
for _, inlineS := range s.Inlines {
inlineModel, _ := NewModel(strings.ToLower(inlineS.ModelName), false)
//inlineQ := fmt.Sprintf("%s = %d", foreignKeys[s.ModelName][strings.ToLower(inlineS.ModelName)], ModelID64)
//r.Form.Set("inline_id", inlineQ)
// Check if there the inline has a ListModifier
query := ""
args := []interface{}{}
if inlineS.ListModifier != nil {
query, args = inlineS.ListModifier(inlineS, user)
}
// Add the fk for the inline
if query != "" {
query += " AND "
}
query += fmt.Sprintf("%s = ?", foreignKeys[s.ModelName][strings.ToLower(inlineS.ModelName)])
args = append(args, ModelID64)
rows := getListData(inlineModel.Interface(), PageLength, r, session, query, inlineS, args...)
r.Form.Del("inline_id")
if rows.Count == 0 {
rows.Rows = [][]interface{}{}
}
inlineData = append(inlineData, *rows)
}
}
s.InlinesData = inlineData
s.ModelID = uint(ModelID64)
}