forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_form_data.go
241 lines (218 loc) · 6.57 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
package uadmin
import (
"fmt"
"net/http"
"reflect"
"strconv"
"strings"
"time"
"encoding/json"
)
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
// 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.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 r.FormValue(t.Field(index).Name) != "" {
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)
}
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 == cIMAGELIST {
var arr = make([]string, 0)
if obj := fieldValue.Interface(); obj != nil {
if jsonStr, ok := obj.(string); ok {
json.Unmarshal([]byte(jsonStr), &arr)
}
}
value = arr
} 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)
}
} 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 = 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)
}
rows := getListData(inlineModel.Interface(), PageLength, r, session, query, args...)
r.Form.Del("inline_id")
if rows.Count == 0 {
rows.Rows = [][]interface{}{}
}
inlineData = append(inlineData, *rows)
}
}
s.InlinesData = inlineData
s.ModelID = uint(ModelID64)
return
}