github.com/kotovmak/go-admin@v1.1.1/plugins/admin/controller/edit.go (about) 1 package controller 2 3 import ( 4 "fmt" 5 template2 "html/template" 6 "net/http" 7 "net/url" 8 9 "github.com/kotovmak/go-admin/modules/logger" 10 11 "github.com/kotovmak/go-admin/template" 12 13 "github.com/kotovmak/go-admin/plugins/admin/modules/response" 14 15 "github.com/kotovmak/go-admin/context" 16 "github.com/kotovmak/go-admin/modules/auth" 17 "github.com/kotovmak/go-admin/modules/file" 18 "github.com/kotovmak/go-admin/modules/language" 19 "github.com/kotovmak/go-admin/plugins/admin/modules" 20 "github.com/kotovmak/go-admin/plugins/admin/modules/constant" 21 form2 "github.com/kotovmak/go-admin/plugins/admin/modules/form" 22 "github.com/kotovmak/go-admin/plugins/admin/modules/guard" 23 "github.com/kotovmak/go-admin/plugins/admin/modules/parameter" 24 "github.com/kotovmak/go-admin/template/types" 25 "github.com/kotovmak/go-admin/template/types/form" 26 ) 27 28 // ShowForm show form page. 29 func (h *Handler) ShowForm(ctx *context.Context) { 30 param := guard.GetShowFormParam(ctx) 31 h.showForm(ctx, "", param.Prefix, param.Param, false) 32 } 33 34 func (h *Handler) showForm(ctx *context.Context, alert template2.HTML, prefix string, param parameter.Parameters, isEdit bool, animation ...bool) { 35 36 panel := h.table(prefix, ctx) 37 38 if panel.GetForm().HasError() { 39 if panel.GetForm().PageErrorHTML != template2.HTML("") { 40 h.HTML(ctx, auth.Auth(ctx), 41 types.Panel{Content: panel.GetForm().PageErrorHTML}, template.ExecuteOptions{Animation: param.Animation}) 42 return 43 } 44 h.HTML(ctx, auth.Auth(ctx), 45 template.WarningPanel(panel.GetForm().PageError.Error(), 46 template.GetPageTypeFromPageError(panel.GetForm().PageError)), template.ExecuteOptions{Animation: param.Animation}) 47 return 48 } 49 50 var ( 51 user = auth.Auth(ctx) 52 paramStr = param.GetRouteParamStr() 53 newUrl = modules.AorEmpty(panel.GetCanAdd(), h.routePathWithPrefix("show_new", prefix)+paramStr) 54 footerKind = "edit" 55 ) 56 57 if newUrl == "" || !user.CheckPermissionByUrlMethod(newUrl, h.route("show_new").Method(), url.Values{}) { 58 footerKind = "edit_only" 59 } 60 61 formInfo, err := panel.GetDataWithId(param) 62 63 if err != nil { 64 logger.Error("receive data error: ", err) 65 h.HTML(ctx, user, template. 66 WarningPanelWithDescAndTitle(err.Error(), panel.GetForm().Description, panel.GetForm().Title), 67 template.ExecuteOptions{Animation: alert == "" || ((len(animation) > 0) && animation[0])}) 68 69 if isEdit { 70 ctx.AddHeader(constant.PjaxUrlHeader, h.routePathWithPrefix("show_edit", prefix)+ 71 param.DeletePK().GetRouteParamStr()) 72 } 73 return 74 } 75 76 showEditUrl := h.routePathWithPrefix("show_edit", prefix) + param.DeletePK().GetRouteParamStr() 77 infoUrl := h.routePathWithPrefix("info", prefix) + param.DeleteField(constant.EditPKKey).GetRouteParamStr() 78 editUrl := h.routePathWithPrefix("edit", prefix) 79 referer := ctx.Referer() 80 81 if referer != "" && !isInfoUrl(referer) && !isEditUrl(referer, ctx.Query(constant.PrefixKey)) { 82 infoUrl = referer 83 } 84 85 f := panel.GetForm() 86 87 isNotIframe := ctx.Query(constant.IframeKey) != "true" 88 89 hiddenFields := map[string]string{ 90 form2.TokenKey: h.authSrv().AddToken(), 91 form2.PreviousKey: infoUrl, 92 } 93 94 if ctx.Query(constant.IframeKey) != "" { 95 hiddenFields[constant.IframeKey] = ctx.Query(constant.IframeKey) 96 } 97 98 if ctx.Query(constant.IframeIDKey) != "" { 99 hiddenFields[constant.IframeIDKey] = ctx.Query(constant.IframeIDKey) 100 } 101 102 content := formContent(aForm(). 103 SetContent(formInfo.FieldList). 104 SetFieldsHTML(f.HTMLContent). 105 SetTabContents(formInfo.GroupFieldList). 106 SetTabHeaders(formInfo.GroupFieldHeaders). 107 SetPrefix(h.config.PrefixFixSlash()). 108 SetInputWidth(f.InputWidth). 109 SetHeadWidth(f.HeadWidth). 110 SetPrimaryKey(panel.GetPrimaryKey().Name). 111 SetUrl(editUrl). 112 SetTitle(f.FormEditTitle). 113 SetAjax(f.AjaxSuccessJS, f.AjaxErrorJS). 114 SetLayout(f.Layout). 115 SetHiddenFields(hiddenFields). 116 SetOperationFooter(formFooter(footerKind, 117 f.IsHideContinueEditCheckBox, 118 f.IsHideContinueNewCheckBox, 119 f.IsHideResetButton, f.FormEditBtnWord)). 120 SetHeader(f.HeaderHtml). 121 SetFooter(f.FooterHtml), len(formInfo.GroupFieldHeaders) > 0, !isNotIframe, f.IsHideBackButton, f.Header) 122 123 if f.Wrapper != nil { 124 content = f.Wrapper(content) 125 } 126 127 h.HTML(ctx, user, types.Panel{ 128 Content: alert + content, 129 Description: template2.HTML(formInfo.Description), 130 Title: modules.AorBHTML(isNotIframe, template2.HTML(formInfo.Title), ""), 131 MiniSidebar: f.HideSideBar, 132 }, template.ExecuteOptions{Animation: alert == "" || ((len(animation) > 0) && animation[0]), NoCompress: f.NoCompress}) 133 134 if isEdit { 135 ctx.AddHeader(constant.PjaxUrlHeader, showEditUrl) 136 } 137 } 138 139 func (h *Handler) EditForm(ctx *context.Context) { 140 141 param := guard.GetEditFormParam(ctx) 142 143 if len(param.MultiForm.File) > 0 { 144 err := file.GetFileEngine(h.config.FileUploadEngine.Name).Upload(param.MultiForm) 145 if err != nil { 146 logger.Error("get file engine error: ", err) 147 if ctx.WantJSON() { 148 response.Error(ctx, err.Error()) 149 } else { 150 h.showForm(ctx, aAlert().Warning(err.Error()), param.Prefix, param.Param, true) 151 } 152 return 153 } 154 } 155 156 formPanel := param.Panel.GetForm() 157 158 for i := 0; i < len(formPanel.FieldList); i++ { 159 if formPanel.FieldList[i].FormType == form.File && 160 len(param.MultiForm.File[formPanel.FieldList[i].Field]) == 0 && 161 len(param.MultiForm.Value[formPanel.FieldList[i].Field+"__delete_flag"]) > 0 && 162 param.MultiForm.Value[formPanel.FieldList[i].Field+"__delete_flag"][0] != "1" { 163 param.MultiForm.Value[formPanel.FieldList[i].Field] = []string{""} 164 } 165 if formPanel.FieldList[i].FormType == form.File && 166 len(param.MultiForm.Value[formPanel.FieldList[i].Field+"__change_flag"]) > 0 && 167 param.MultiForm.Value[formPanel.FieldList[i].Field+"__change_flag"][0] != "1" { 168 delete(param.MultiForm.Value, formPanel.FieldList[i].Field) 169 } 170 } 171 172 err := param.Panel.UpdateData(param.Value()) 173 if err != nil { 174 logger.Error("update data error: ", err) 175 if ctx.WantJSON() { 176 response.Error(ctx, err.Error(), map[string]interface{}{ 177 "token": h.authSrv().AddToken(), 178 }) 179 } else { 180 h.showForm(ctx, aAlert().Warning(err.Error()), param.Prefix, param.Param, true) 181 } 182 return 183 } 184 185 if formPanel.Responder != nil { 186 formPanel.Responder(ctx) 187 return 188 } 189 190 if ctx.WantJSON() && !param.IsIframe { 191 response.OkWithData(ctx, map[string]interface{}{ 192 "url": param.PreviousPath, 193 "token": h.authSrv().AddToken(), 194 }) 195 return 196 } 197 198 if !param.FromList { 199 200 if isNewUrl(param.PreviousPath, param.Prefix) { 201 h.showNewForm(ctx, param.Alert, param.Prefix, param.Param.DeleteEditPk().GetRouteParamStr(), true) 202 return 203 } 204 205 if isEditUrl(param.PreviousPath, param.Prefix) { 206 h.showForm(ctx, param.Alert, param.Prefix, param.Param, true, false) 207 return 208 } 209 210 ctx.HTML(http.StatusOK, fmt.Sprintf(`<script>location.href="%s"</script>`, param.PreviousPath)) 211 ctx.AddHeader(constant.PjaxUrlHeader, param.PreviousPath) 212 return 213 } 214 215 if param.IsIframe { 216 ctx.HTML(http.StatusOK, fmt.Sprintf(`<script> 217 swal('%s', '', 'success'); 218 setTimeout(function(){ 219 $("#%s", window.parent.document).hide(); 220 $('.modal-backdrop.fade.in', window.parent.document).hide(); 221 }, 1000) 222 </script>`, language.Get("success"), param.IframeID)) 223 return 224 } 225 226 buf := h.showTable(ctx, param.Prefix, param.Param.DeletePK().DeleteEditPk(), nil) 227 228 ctx.HTML(http.StatusOK, buf.String()) 229 ctx.AddHeader(constant.PjaxUrlHeader, param.PreviousPath) 230 }