github.com/kotovmak/go-admin@v1.1.1/plugins/admin/controller/new.go (about) 1 package controller 2 3 import ( 4 "fmt" 5 template2 "html/template" 6 "net/http" 7 8 "github.com/kotovmak/go-admin/template" 9 10 "github.com/kotovmak/go-admin/modules/logger" 11 12 "github.com/kotovmak/go-admin/plugins/admin/modules/response" 13 14 "github.com/kotovmak/go-admin/context" 15 "github.com/kotovmak/go-admin/modules/auth" 16 "github.com/kotovmak/go-admin/modules/file" 17 "github.com/kotovmak/go-admin/modules/language" 18 "github.com/kotovmak/go-admin/plugins/admin/modules" 19 "github.com/kotovmak/go-admin/plugins/admin/modules/constant" 20 form2 "github.com/kotovmak/go-admin/plugins/admin/modules/form" 21 "github.com/kotovmak/go-admin/plugins/admin/modules/guard" 22 "github.com/kotovmak/go-admin/template/types" 23 ) 24 25 // ShowNewForm show a new form page. 26 func (h *Handler) ShowNewForm(ctx *context.Context) { 27 param := guard.GetShowNewFormParam(ctx) 28 h.showNewForm(ctx, "", param.Prefix, param.Param.GetRouteParamStr(), false) 29 } 30 31 func (h *Handler) showNewForm(ctx *context.Context, alert template2.HTML, prefix, paramStr string, isNew bool) { 32 33 var ( 34 user = auth.Auth(ctx) 35 panel = h.table(prefix, ctx) 36 formInfo = panel.GetNewFormInfo() 37 infoUrl = h.routePathWithPrefix("info", prefix) + paramStr 38 newUrl = h.routePathWithPrefix("new", prefix) 39 showNewUrl = h.routePathWithPrefix("show_new", prefix) + paramStr 40 referer = ctx.Referer() 41 f = panel.GetActualNewForm() 42 isNotIframe = ctx.Query(constant.IframeKey) != "true" 43 ) 44 45 if referer != "" && !isInfoUrl(referer) && !isNewUrl(referer, ctx.Query(constant.PrefixKey)) { 46 infoUrl = referer 47 } 48 49 hiddenFields := map[string]string{ 50 form2.TokenKey: h.authSrv().AddToken(), 51 form2.PreviousKey: infoUrl, 52 } 53 54 if ctx.Query(constant.IframeKey) != "" { 55 hiddenFields[constant.IframeKey] = ctx.Query(constant.IframeKey) 56 } 57 58 if ctx.Query(constant.IframeIDKey) != "" { 59 hiddenFields[constant.IframeIDKey] = ctx.Query(constant.IframeIDKey) 60 } 61 62 content := formContent(aForm(). 63 SetPrefix(h.config.PrefixFixSlash()). 64 SetFieldsHTML(f.HTMLContent). 65 SetContent(formInfo.FieldList). 66 SetTabContents(formInfo.GroupFieldList). 67 SetTabHeaders(formInfo.GroupFieldHeaders). 68 SetUrl(newUrl). 69 SetAjax(f.AjaxSuccessJS, f.AjaxErrorJS). 70 SetInputWidth(f.InputWidth). 71 SetHeadWidth(f.HeadWidth). 72 SetLayout(f.Layout). 73 SetPrimaryKey(panel.GetPrimaryKey().Name). 74 SetHiddenFields(hiddenFields). 75 SetTitle(f.FormNewTitle). 76 SetOperationFooter(formFooter("new", f.IsHideContinueEditCheckBox, f.IsHideContinueNewCheckBox, 77 f.IsHideResetButton, f.FormNewBtnWord)). 78 SetHeader(f.HeaderHtml). 79 SetFooter(f.FooterHtml), len(formInfo.GroupFieldHeaders) > 0, !isNotIframe, f.IsHideBackButton, f.Header) 80 81 if f.Wrapper != nil { 82 content = f.Wrapper(content) 83 } 84 85 h.HTML(ctx, user, types.Panel{ 86 Content: alert + content, 87 Description: template2.HTML(f.Description), 88 Title: modules.AorBHTML(isNotIframe, template2.HTML(f.Title), ""), 89 MiniSidebar: f.HideSideBar, 90 }, template.ExecuteOptions{Animation: alert == ""}) 91 92 if isNew { 93 ctx.AddHeader(constant.PjaxUrlHeader, showNewUrl) 94 } 95 } 96 97 // NewForm insert a table row into database. 98 func (h *Handler) NewForm(ctx *context.Context) { 99 100 param := guard.GetNewFormParam(ctx) 101 102 // process uploading files, only support local storage 103 if len(param.MultiForm.File) > 0 { 104 err := file.GetFileEngine(h.config.FileUploadEngine.Name).Upload(param.MultiForm) 105 if err != nil { 106 logger.Error("get file engine error: ", err) 107 if ctx.WantJSON() { 108 response.Error(ctx, err.Error()) 109 } else { 110 h.showNewForm(ctx, aAlert().Warning(err.Error()), param.Prefix, param.Param.GetRouteParamStr(), true) 111 } 112 return 113 } 114 } 115 116 err := param.Panel.InsertData(param.Value()) 117 if err != nil { 118 logger.Error("insert data error: ", err) 119 if ctx.WantJSON() { 120 response.Error(ctx, err.Error(), map[string]interface{}{ 121 "token": h.authSrv().AddToken(), 122 }) 123 } else { 124 h.showNewForm(ctx, aAlert().Warning(err.Error()), param.Prefix, param.Param.GetRouteParamStr(), true) 125 } 126 return 127 } 128 129 f := param.Panel.GetActualNewForm() 130 131 if f.Responder != nil { 132 f.Responder(ctx) 133 return 134 } 135 136 if ctx.WantJSON() && !param.IsIframe { 137 response.OkWithData(ctx, map[string]interface{}{ 138 "url": param.PreviousPath, 139 "token": h.authSrv().AddToken(), 140 }) 141 return 142 } 143 144 if !param.FromList { 145 146 if isNewUrl(param.PreviousPath, param.Prefix) { 147 h.showNewForm(ctx, param.Alert, param.Prefix, param.Param.GetRouteParamStr(), true) 148 return 149 } 150 151 ctx.HTML(http.StatusOK, fmt.Sprintf(`<script>location.href="%s"</script>`, param.PreviousPath)) 152 ctx.AddHeader(constant.PjaxUrlHeader, param.PreviousPath) 153 return 154 } 155 156 if param.IsIframe { 157 ctx.HTML(http.StatusOK, fmt.Sprintf(`<script> 158 swal('%s', '', 'success'); 159 setTimeout(function(){ 160 $("#%s", window.parent.document).hide(); 161 $('.modal-backdrop.fade.in', window.parent.document).hide(); 162 }, 1000) 163 </script>`, language.Get("success"), param.IframeID)) 164 return 165 } 166 167 buf := h.showTable(ctx, param.Prefix, param.Param, nil) 168 169 ctx.HTML(http.StatusOK, buf.String()) 170 ctx.AddHeader(constant.PjaxUrlHeader, h.routePathWithPrefix("info", param.Prefix)+param.Param.GetRouteParamStr()) 171 }