forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
process_form.go
261 lines (242 loc) · 7.73 KB
/
process_form.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
package uadmin
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"reflect"
"strconv"
"strings"
"time"
)
// processForm verifies form data and stores it to DB
func processForm(modelName string, w http.ResponseWriter, r *http.Request, session *Session, s *ModelSchema) reflect.Value { //(errMap map[string]string) {
user := session.User
//errMap = map[string]string{}
now := time.Now()
DType := reflect.TypeOf(now)
DType1 := reflect.TypeOf(&now)
tempID, _ := strconv.ParseUint(r.FormValue("ID"), 10, 64)
ID := uint(tempID)
isNew := ID == 0
m, ok := NewModel(modelName, true)
if !ok {
Trail(ERROR, "processForm.NewModel model not found (%s)", modelName)
page404Handler(w, r, session)
return m
}
// Fetch record from DB if not new
if ID != 0 {
Get(m.Interface(), "id = ?", ID)
}
// Get Type
t := reflect.TypeOf(m.Interface()).Elem()
// Check if there is a field name Createdby
_, hasCreatedBy := t.FieldByName("CreatedBy")
_, hasUpdatedBy := t.FieldByName("UpdatedBy")
_, isValidate := t.MethodByName("Validate")
// Process Fields
for index := 0; index < t.NumField(); index++ {
// Ignore private fields
if strings.ToLower(string(t.Field(index).Name[0])) == string(t.Field(index).Name[0]) {
continue
}
f := s.FieldByName(t.Field(index).Name)
if f.ReadOnly == "true" || (strings.Contains(f.ReadOnly, "new") && isNew) || (strings.Contains(f.ReadOnly, "edit") && !isNew) {
continue
}
if t.Field(index).Type.Kind() == reflect.Int {
_v := r.FormValue(t.Field(index).Name)
i, _ := strconv.ParseInt(_v, 10, 64)
m.Elem().FieldByName(t.Field(index).Name).SetInt(i)
} else if t.Field(index).Type.Kind() == reflect.String {
// Check if Multi lingual
val := ""
if f.Type == cMULTILINGUAL {
tVal := map[string]string{}
langs := []Language{}
Filter(&langs, "`active` = ?", true)
for _, lang := range langs {
tVal[lang.Code] = fmt.Sprint(r.FormValue(lang.Code + "-" + t.Field(index).Name))
}
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
_ = encoder.Encode(tVal)
val = string(buffer.Bytes())
} else if f.Type == cIMAGELIST || f.Type == cIMAGE || f.Type == cFILE {
val = DoProcessUpload(r, f, modelName, session, s)
if val == "" || val == "[]" {
continue
}
} else {
val = fmt.Sprint(r.FormValue(t.Field(index).Name))
}
m.Elem().FieldByName(t.Field(index).Name).SetString(val)
} else if t.Field(index).Type.Kind() == reflect.Bool {
var val bool
val = false
if string(r.FormValue(t.Field(index).Name)) == "on" {
val = true
}
m.Elem().FieldByName(t.Field(index).Name).SetBool(val)
} else if t.Field(index).Type.Kind() == reflect.Uint {
_v := r.FormValue(t.Field(index).Name)
i, _ := strconv.ParseInt(_v, 10, 64)
val := uint(i)
m.Elem().FieldByName(t.Field(index).Name).Set(reflect.ValueOf(val))
} else if t.Field(index).Type.Kind() == reflect.Float64 {
_v := r.FormValue(t.Field(index).Name)
i, _ := strconv.ParseFloat(_v, 10)
//val := uint(i)
m.Elem().FieldByName(t.Field(index).Name).Set(reflect.ValueOf(i))
} else if t.Field(index).Type.Kind() == reflect.Slice {
Field := m.Elem().Field(index)
_v := r.Form[t.Field(index).Name]
// Initialize the list and item
m2mListType := reflect.TypeOf(Field.Interface())
m2mList := reflect.New(m2mListType).Elem()
m2mItemType := reflect.TypeOf(Field.Interface()).Elem()
// DeleteM2m(m.Elem().Addr().Interface(), m2mItemType.Name())
//DeleteM2m(newType.Addr().Interface(), m2mListType.Name())
// Append the selected items from the form
for _, m2mID := range _v {
m2mItem := reflect.Zero(m2mItemType)
m2mItem = reflect.New(m2mItemType).Elem()
// params := []reflect.Value{
// reflect.ValueOf("id = ?"),
// reflect.ValueOf(m2mID),
// }
Get(m2mItem.Addr().Interface(), "id="+fmt.Sprint(m2mID))
// m2mItemInstance := m2mItem.MethodByName("Get").Call(params)[0].Elem()
m2mList = reflect.Append(m2mList, m2mItem)
}
// Set the list to the field
m.Elem().FieldByName(t.Field(index).Name).Set(m2mList)
} else if t.Field(index).Type == DType {
if r.FormValue(t.Field(index).Name) == "" {
continue
}
tm, err := time.Parse("2006-01-02 15:04", r.FormValue(t.Field(index).Name))
if err != nil {
tm, err = time.Parse("2006-01-02T15:04", r.FormValue(t.Field(index).Name))
}
if err != nil {
tm, err = time.Parse("2006-01-02T15:04:05", r.FormValue(t.Field(index).Name))
}
if err != nil {
tm, err = time.Parse("2006-01-02 15:04:05", r.FormValue(t.Field(index).Name))
}
if err != nil {
Trail(WARNING, "Unable to parse date: %s (%s)", r.FormValue(t.Field(index).Name), err)
continue
}
tm = time.Date(tm.Year(), tm.Month(), tm.Day(), tm.Hour(), tm.Minute(), tm.Second(), tm.Nanosecond(), time.Local)
m.Elem().FieldByName(t.Field(index).Name).Set(reflect.ValueOf(tm))
} else if t.Field(index).Type == DType1 {
if r.FormValue(t.Field(index).Name) == "" {
// TODO: Remove value on empty
//m.Elem().FieldByName(t.Field(index).Name).Set(reflect.ValueOf(nil))
continue
} else {
tm, err := time.Parse("2006-01-02 15:04", r.FormValue(t.Field(index).Name))
if err != nil {
tm, err = time.Parse("2006-01-02T15:04", r.FormValue(t.Field(index).Name))
}
if err != nil {
tm, err = time.Parse("2006-01-02T15:04:05", r.FormValue(t.Field(index).Name))
}
if err != nil {
tm, err = time.Parse("2006-01-02 15:04:05", r.FormValue(t.Field(index).Name))
}
if err != nil {
Trail(WARNING, "Unable to parse date: %s (%s)", r.FormValue(t.Field(index).Name), err)
continue
}
tm = time.Date(tm.Year(), tm.Month(), tm.Day(), tm.Hour(), tm.Minute(), tm.Second(), tm.Nanosecond(), time.Local)
m.Elem().FieldByName(t.Field(index).Name).Set(reflect.ValueOf(&tm))
}
} else {
}
}
if isValidate {
in := []reflect.Value{}
validate := m.MethodByName("Validate")
ret := validate.Call(in)
if ret[0].Len() > 0 {
tempErrMap, _ := ret[0].Interface().(map[string]string)
for k, v := range tempErrMap {
for i := range s.Fields {
if s.Fields[i].Name == k {
s.Fields[i].ErrMsg = v
}
}
//s.FieldByName(k).ErrMsg = v
}
}
}
formError := false
for _, f := range s.Fields {
if f.ErrMsg != "" {
formError = true
break
}
}
if formError {
// ERROR OCCURRED THEN RETURN
newURL := "new?"
for i := 0; i < t.NumField(); i++ {
newURL += t.Field(i).Name + "=" + fmt.Sprint(m.Elem().FieldByName(t.Field(i).Name)) + "&"
}
r.Form.Set("new_url", newURL)
return m
}
// Create Log before changing anything
if !isNew {
func() {
log := &Log{}
log.ParseRecord(m, modelName, ID, &user, log.Action.Modified(), r)
log.Save()
}()
if hasUpdatedBy {
m.Elem().FieldByName("UpdatedBy").SetString(user.Username)
}
} else {
if hasCreatedBy {
m.Elem().FieldByName("CreatedBy").SetString(user.Username)
}
}
// Save the record
var saverI saver
saverI, ok = m.Interface().(saver)
if !ok {
Save(m.Elem().Addr().Interface())
} else {
saverI.Save()
}
// Store the log for a new record
if isNew {
ID = GetID(m)
log := &Log{}
log.ParseRecord(m, modelName, ID, &user, log.Action.Added(), r)
log.Save()
}
// Redirect the user to the proper URL
newURL := strings.TrimPrefix(r.URL.Path, RootURL)
if r.FormValue("save") == "" {
newURL = RootURL + strings.Split(newURL, "/")[0]
if r.FormValue("return_url") != "" {
newURL = r.FormValue("return_url")
}
http.Redirect(w, r, newURL, 303)
return m
}
if r.FormValue("save") == "another" {
newURL = RootURL + strings.Split(newURL, "/")[0] + "/new"
http.Redirect(w, r, newURL, 303)
return m
}
newURL = RootURL + strings.Split(newURL, "/")[0] + "/" + fmt.Sprint(ID)
http.Redirect(w, r, newURL, 303)
return m
}