forked from GoAdminGroup/go-admin
/
edit.go
170 lines (141 loc) · 4.22 KB
/
edit.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
package guard
import (
tmpl "html/template"
"mime/multipart"
"regexp"
"strings"
"github.com/MarErm27/go-admin/template/types"
"github.com/MarErm27/go-admin/context"
"github.com/MarErm27/go-admin/modules/auth"
"github.com/MarErm27/go-admin/modules/config"
"github.com/MarErm27/go-admin/modules/db"
"github.com/MarErm27/go-admin/modules/errors"
"github.com/MarErm27/go-admin/plugins/admin/modules/constant"
"github.com/MarErm27/go-admin/plugins/admin/modules/form"
"github.com/MarErm27/go-admin/plugins/admin/modules/parameter"
"github.com/MarErm27/go-admin/plugins/admin/modules/response"
"github.com/MarErm27/go-admin/plugins/admin/modules/table"
"github.com/MarErm27/go-admin/template"
)
type ShowFormParam struct {
Panel table.Table
Id string
Prefix string
Param parameter.Parameters
}
func (g *Guard) ShowForm(ctx *context.Context) {
panel, prefix := g.table(ctx)
if !panel.GetEditable() {
alert(ctx, panel, errors.OperationNotAllow, g.conn, g.navBtns)
ctx.Abort()
return
}
if panel.GetOnlyInfo() {
ctx.Redirect(config.Url("/info/" + prefix))
ctx.Abort()
return
}
if panel.GetOnlyDetail() {
ctx.Redirect(config.Url("/info/" + prefix + "/detail"))
ctx.Abort()
return
}
if panel.GetOnlyNewForm() {
ctx.Redirect(config.Url("/info/" + prefix + "/new"))
ctx.Abort()
return
}
id := ctx.Query(constant.EditPKKey)
if id == "" {
id = "1"
}
ctx.SetUserValue(showFormParamKey, &ShowFormParam{
Panel: panel,
Id: id,
Prefix: prefix,
Param: parameter.GetParam(ctx.Request.URL, panel.GetInfo().DefaultPageSize, panel.GetInfo().SortField,
panel.GetInfo().GetSort()).WithPKs(id),
})
ctx.Next()
}
func GetShowFormParam(ctx *context.Context) *ShowFormParam {
return ctx.UserValue[showFormParamKey].(*ShowFormParam)
}
type EditFormParam struct {
Panel table.Table
Id string
Prefix string
Param parameter.Parameters
Path string
MultiForm *multipart.Form
PreviousPath string
Alert tmpl.HTML
FromList bool
IsIframe bool
IframeID string
}
func (e EditFormParam) Value() form.Values {
return e.MultiForm.Value
}
func (g *Guard) EditForm(ctx *context.Context) {
panel, prefix := g.table(ctx)
if !panel.GetEditable() {
alert(ctx, panel, errors.OperationNotAllow, g.conn, g.navBtns)
ctx.Abort()
return
}
token := ctx.FormValue(form.TokenKey)
if !auth.GetTokenService(g.services.Get(auth.TokenServiceKey)).CheckToken(token) {
alert(ctx, panel, errors.EditFailWrongToken, g.conn, g.navBtns)
ctx.Abort()
return
}
var (
previous = ctx.FormValue(form.PreviousKey)
fromList = isInfoUrl(previous)
param = parameter.GetParamFromURL(previous, panel.GetInfo().DefaultPageSize,
panel.GetInfo().GetSort(), panel.GetPrimaryKey().Name)
)
if fromList {
previous = config.Url("/info/" + prefix + param.GetRouteParamStr())
}
var (
multiForm = ctx.Request.MultipartForm
id = multiForm.Value[panel.GetPrimaryKey().Name][0]
values = ctx.Request.MultipartForm.Value
)
ctx.SetUserValue(editFormParamKey, &EditFormParam{
Panel: panel,
Id: id,
Prefix: prefix,
Param: param.WithPKs(id),
Path: strings.Split(previous, "?")[0],
MultiForm: multiForm,
IsIframe: form.Values(values).Get(constant.IframeKey) == "true",
IframeID: form.Values(values).Get(constant.IframeIDKey),
PreviousPath: previous,
FromList: fromList,
})
ctx.Next()
}
func isInfoUrl(s string) bool {
reg, _ := regexp.Compile("(.*?)info/(.*?)$")
sub := reg.FindStringSubmatch(s)
return len(sub) > 2 && !strings.Contains(sub[2], "/")
}
func GetEditFormParam(ctx *context.Context) *EditFormParam {
return ctx.UserValue[editFormParamKey].(*EditFormParam)
}
func alert(ctx *context.Context, panel table.Table, msg string, conn db.Connection, btns *types.Buttons) {
if ctx.WantJSON() {
response.BadRequest(ctx, msg)
} else {
response.Alert(ctx, panel.GetInfo().Description, panel.GetInfo().Title, msg, conn, btns)
}
}
func alertWithTitleAndDesc(ctx *context.Context, title, desc, msg string, conn db.Connection, btns *types.Buttons) {
response.Alert(ctx, desc, title, msg, conn, btns)
}
func getAlert(msg string) tmpl.HTML {
return template.Get(config.GetTheme()).Alert().Warning(msg)
}