github.com/kotovmak/go-admin@v1.1.1/plugins/admin/modules/guard/edit.go (about) 1 package guard 2 3 import ( 4 tmpl "html/template" 5 "mime/multipart" 6 "regexp" 7 "strings" 8 9 "github.com/kotovmak/go-admin/template/types" 10 11 "github.com/kotovmak/go-admin/context" 12 "github.com/kotovmak/go-admin/modules/auth" 13 "github.com/kotovmak/go-admin/modules/config" 14 "github.com/kotovmak/go-admin/modules/db" 15 "github.com/kotovmak/go-admin/modules/errors" 16 "github.com/kotovmak/go-admin/plugins/admin/modules/constant" 17 "github.com/kotovmak/go-admin/plugins/admin/modules/form" 18 "github.com/kotovmak/go-admin/plugins/admin/modules/parameter" 19 "github.com/kotovmak/go-admin/plugins/admin/modules/response" 20 "github.com/kotovmak/go-admin/plugins/admin/modules/table" 21 "github.com/kotovmak/go-admin/template" 22 ) 23 24 type ShowFormParam struct { 25 Panel table.Table 26 Id string 27 Prefix string 28 Param parameter.Parameters 29 } 30 31 func (g *Guard) ShowForm(ctx *context.Context) { 32 33 panel, prefix := g.table(ctx) 34 35 if !panel.GetEditable() { 36 alert(ctx, panel, errors.OperationNotAllow, g.conn, g.navBtns) 37 ctx.Abort() 38 return 39 } 40 41 if panel.GetOnlyInfo() { 42 ctx.Redirect(config.Url("/info/" + prefix)) 43 ctx.Abort() 44 return 45 } 46 47 if panel.GetOnlyDetail() { 48 ctx.Redirect(config.Url("/info/" + prefix + "/detail")) 49 ctx.Abort() 50 return 51 } 52 53 if panel.GetOnlyNewForm() { 54 ctx.Redirect(config.Url("/info/" + prefix + "/new")) 55 ctx.Abort() 56 return 57 } 58 59 id := ctx.Query(constant.EditPKKey) 60 61 if id == "" { 62 id = "1" 63 } 64 65 ctx.SetUserValue(showFormParamKey, &ShowFormParam{ 66 Panel: panel, 67 Id: id, 68 Prefix: prefix, 69 Param: parameter.GetParam(ctx.Request.URL, panel.GetInfo().DefaultPageSize, panel.GetInfo().SortField, 70 panel.GetInfo().GetSort()).WithPKs(id), 71 }) 72 ctx.Next() 73 } 74 75 func GetShowFormParam(ctx *context.Context) *ShowFormParam { 76 return ctx.UserValue[showFormParamKey].(*ShowFormParam) 77 } 78 79 type EditFormParam struct { 80 Panel table.Table 81 Id string 82 Prefix string 83 Param parameter.Parameters 84 Path string 85 MultiForm *multipart.Form 86 PreviousPath string 87 Alert tmpl.HTML 88 FromList bool 89 IsIframe bool 90 IframeID string 91 } 92 93 func (e EditFormParam) Value() form.Values { 94 return e.MultiForm.Value 95 } 96 97 func (g *Guard) EditForm(ctx *context.Context) { 98 99 panel, prefix := g.table(ctx) 100 101 if !panel.GetEditable() { 102 alert(ctx, panel, errors.OperationNotAllow, g.conn, g.navBtns) 103 ctx.Abort() 104 return 105 } 106 token := ctx.FormValue(form.TokenKey) 107 108 if !auth.GetTokenService(g.services.Get(auth.TokenServiceKey)).CheckToken(token) { 109 alert(ctx, panel, errors.EditFailWrongToken, g.conn, g.navBtns) 110 ctx.Abort() 111 return 112 } 113 114 var ( 115 previous = ctx.FormValue(form.PreviousKey) 116 fromList = isInfoUrl(previous) 117 param = parameter.GetParamFromURL(previous, panel.GetInfo().DefaultPageSize, 118 panel.GetInfo().GetSort(), panel.GetPrimaryKey().Name) 119 ) 120 121 if fromList { 122 previous = config.Url("/info/" + prefix + param.GetRouteParamStr()) 123 } 124 125 var ( 126 multiForm = ctx.Request.MultipartForm 127 id = multiForm.Value[panel.GetPrimaryKey().Name][0] 128 values = ctx.Request.MultipartForm.Value 129 ) 130 131 ctx.SetUserValue(editFormParamKey, &EditFormParam{ 132 Panel: panel, 133 Id: id, 134 Prefix: prefix, 135 Param: param.WithPKs(id), 136 Path: strings.Split(previous, "?")[0], 137 MultiForm: multiForm, 138 IsIframe: form.Values(values).Get(constant.IframeKey) == "true", 139 IframeID: form.Values(values).Get(constant.IframeIDKey), 140 PreviousPath: previous, 141 FromList: fromList, 142 }) 143 ctx.Next() 144 } 145 146 func isInfoUrl(s string) bool { 147 reg, _ := regexp.Compile("(.*?)info/(.*?)$") 148 sub := reg.FindStringSubmatch(s) 149 return len(sub) > 2 && !strings.Contains(sub[2], "/") 150 } 151 152 func GetEditFormParam(ctx *context.Context) *EditFormParam { 153 return ctx.UserValue[editFormParamKey].(*EditFormParam) 154 } 155 156 func alert(ctx *context.Context, panel table.Table, msg string, conn db.Connection, btns *types.Buttons) { 157 if ctx.WantJSON() { 158 response.BadRequest(ctx, msg) 159 } else { 160 response.Alert(ctx, panel.GetInfo().Description, panel.GetInfo().Title, msg, conn, btns) 161 } 162 } 163 164 func alertWithTitleAndDesc(ctx *context.Context, title, desc, msg string, conn db.Connection, btns *types.Buttons) { 165 response.Alert(ctx, desc, title, msg, conn, btns) 166 } 167 168 func getAlert(msg string) tmpl.HTML { 169 return template.Get(config.GetTheme()).Alert().Warning(msg) 170 }