Browse Source

Delete template button with action

Vova Tkach 4 years ago
parent
commit
c15dfe9dc3

+ 1 - 1
engine/assets/cp.scripts.js

@@ -7616,7 +7616,7 @@
 				}
 			},
 
-			ActionRestoreThemeFile: function(action_name, file_name, message) {
+			ActionThemeFile: function(action_name, file_name, message) {
 				if(confirm(message)) {
 					$.ajax({
 						type: "POST",

File diff suppressed because it is too large
+ 0 - 0
engine/assets/cp.scripts.js.go


+ 10 - 3
engine/modules/module_templates.go

@@ -101,11 +101,18 @@ func (this *Modules) RegisterModule_Templates() *Module {
 						Name:    "file",
 						Value:   "0",
 						CallBack: func(field *builder.DataFormField) string {
+							buttons := ``
+							if wrap.IsSystemMountedTemplateFile(selected_file) {
+								buttons += `<button type="button" class="btn btn-success" onclick="return fave.ActionThemeFile('templates-restore-file','` + selected_file + `','Are you sure want to restore theme file?');" style="position:absolute;right:0;">Restore</button>`
+							} else {
+								buttons += `<button type="button" class="btn btn-danger" onclick="return fave.ActionThemeFile('templates-delete-file','` + selected_file + `','Are you sure want to delete theme file?');" style="position:absolute;right:0;">Delete</button>`
+							}
+
 							return `<div class="form-group n1">` +
 								`<div class="row">` +
 								`<div class="col-12">` +
 								`<div style="position:relative;">` +
-								`<button type="button" class="btn btn-success" onclick="return fave.ActionRestoreThemeFile('templates-restore-file','` + selected_file + `','Are you sure want to restore theme file?');" style="position:absolute;right:0;">Restore</button>` +
+								buttons +
 								`<select class="form-control ignore-lost-data" id="lbl_file" name="file" onchange="setTimeout(function(){$('#lbl_file').val('` + selected_file + `')},500);document.location='/cp/` + wrap.CurrModule + `/?file='+encodeURI(this.value);">` +
 								list_of_files +
 								`</select>` +
@@ -195,12 +202,12 @@ func (this *Modules) RegisterModule_Templates() *Module {
 				{
 					Kind: builder.DFKSubmit,
 					CallBack: func(field *builder.DataFormField) string {
-						return `<div class="row d-lg-none"><div class="col-12"><div class="pt-3"><button type="button" class="btn btn-danger" onclick="return fave.ActionRestoreThemeFile('templates-restore-file-all','all','WARNING! Are you sure want to restore all theme files?');">Restore</button></div></div></div>`
+						return `<div class="row d-lg-none"><div class="col-12"><div class="pt-3"><button type="button" class="btn btn-danger" onclick="return fave.ActionThemeFile('templates-restore-file-all','all','WARNING! Are you sure want to restore all theme files?');">Restore</button></div></div></div>`
 					},
 				},
 			})
 
-			sidebar += `<button class="btn btn-danger btn-sidebar" onclick="return fave.ActionRestoreThemeFile('templates-restore-file-all','all','WARNING! Are you sure want to restore all theme files?');" id="add-edit-button">Restore</button>`
+			sidebar += `<button class="btn btn-danger btn-sidebar" onclick="return fave.ActionThemeFile('templates-restore-file-all','all','WARNING! Are you sure want to restore all theme files?');" id="add-edit-button">Restore</button>`
 		}
 		return this.getSidebarModules(wrap), content, sidebar
 	})

+ 70 - 0
engine/modules/module_templates_act_delete_file.go

@@ -0,0 +1,70 @@
+package modules
+
+import (
+	"context"
+	"os"
+	"strings"
+
+	"golang-fave/engine/utils"
+	"golang-fave/engine/wrapper"
+)
+
+func (this *Modules) RegisterAction_TemplatesDeleteThemeFile() *Action {
+	return this.newAction(AInfo{
+		Mount:     "templates-delete-file",
+		WantAdmin: true,
+	}, func(wrap *wrapper.Wrapper) {
+		pf_file := utils.Trim(wrap.R.FormValue("file"))
+
+		if pf_file == "" {
+			wrap.MsgError(`Please specify file name`)
+			return
+		}
+
+		if wrap.IsSystemMountedTemplateFile(pf_file) {
+			wrap.MsgError(`You can't delete system mounted template`)
+			return
+		}
+
+		template_file := wrap.DTemplate + string(os.PathSeparator) + pf_file
+		if !utils.IsFileExists(template_file) {
+			wrap.MsgError(`File is not exists`)
+			return
+		}
+
+		if err := os.Remove(template_file); err != nil {
+			wrap.MsgError(err.Error())
+			return
+		}
+
+		// Update pages
+		tmpl_name := pf_file
+		if i := strings.LastIndex(tmpl_name, "."); i > -1 {
+			tmpl_name = tmpl_name[:i]
+		}
+
+		if err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
+			_, err := tx.Exec(
+				ctx,
+				`UPDATE fave_pages SET
+					template = ?
+				WHERE
+					template = ?
+				;`,
+				"page",
+				tmpl_name,
+			)
+			if err != nil {
+				return err
+			}
+			return nil
+		}); err != nil {
+			wrap.MsgError(err.Error())
+			return
+		}
+
+		wrap.ResetCacheBlocks()
+
+		wrap.Write(`window.location='/cp/templates/';`)
+	})
+}

Some files were not shown because too many files changed in this diff