Browse Source

User templates, display in list, can be selected on add/edit page form

Vova Tkach 4 years ago
parent
commit
6e918fa24c
3 changed files with 73 additions and 3 deletions
  1. 29 0
      engine/modules/module_index.go
  2. 15 3
      engine/modules/module_templates.go
  3. 29 0
      engine/wrapper/wrapper.go

+ 29 - 0
engine/modules/module_index.go

@@ -2,7 +2,9 @@ package modules
 
 
 import (
 import (
 	"html"
 	"html"
+	"io/ioutil"
 	"net/http"
 	"net/http"
+	"strings"
 
 
 	"golang-fave/engine/assets"
 	"golang-fave/engine/assets"
 	"golang-fave/engine/builder"
 	"golang-fave/engine/builder"
@@ -12,6 +14,13 @@ import (
 	"golang-fave/engine/wrapper"
 	"golang-fave/engine/wrapper"
 )
 )
 
 
+func (this *Modules) index_TemplateNameToValue(filename string) string {
+	if i := strings.LastIndex(filename, "."); i > -1 {
+		return filename[:i]
+	}
+	return filename
+}
+
 func (this *Modules) index_GetTemplateSelectOptions(wrap *wrapper.Wrapper, template string) string {
 func (this *Modules) index_GetTemplateSelectOptions(wrap *wrapper.Wrapper, template string) string {
 	result := ``
 	result := ``
 
 
@@ -29,6 +38,26 @@ func (this *Modules) index_GetTemplateSelectOptions(wrap *wrapper.Wrapper, templ
 	}
 	}
 	result += `>page.html</option>`
 	result += `>page.html</option>`
 
 
+	// User templates
+	if files, err := ioutil.ReadDir(wrap.DTemplate); err == nil {
+		for _, file := range files {
+			if len(file.Name()) > 0 && file.Name()[0] == '.' {
+				continue
+			}
+			if len(file.Name()) > 0 && strings.ToLower(file.Name()) == "robots.txt" {
+				continue
+			}
+			if !wrap.IsSystemMountedTemplateFile(file.Name()) {
+				value := this.index_TemplateNameToValue(file.Name())
+				result += `<option title="` + file.Name() + `" value="` + value + `"`
+				if template == value {
+					result += ` selected`
+				}
+				result += `>` + file.Name() + `</option>`
+			}
+		}
+	}
+
 	return result
 	return result
 }
 }
 
 

+ 15 - 3
engine/modules/module_templates.go

@@ -59,14 +59,26 @@ func (this *Modules) RegisterModule_Templates() *Module {
 					selected_file = files[0]
 					selected_file = files[0]
 				}
 				}
 
 
-				list_of_files := ``
+				list_of_system_files := ``
+				list_of_user_files := ``
 				for _, file := range files {
 				for _, file := range files {
 					selected := ""
 					selected := ""
 					if file == selected_file {
 					if file == selected_file {
 						selected = " selected"
 						selected = " selected"
 					}
 					}
-					list_of_files += `<option value="` + html.EscapeString(file) +
-						`"` + selected + `>` + html.EscapeString(file) + `</option>`
+					if wrap.IsSystemMountedTemplateFile(file) {
+						list_of_system_files += `<option value="` + html.EscapeString(file) +
+							`"` + selected + `>` + html.EscapeString(file) + `</option>`
+					} else {
+						list_of_user_files += `<option value="` + html.EscapeString(file) +
+							`"` + selected + `>` + html.EscapeString(file) + `</option>`
+					}
+				}
+
+				list_of_files := list_of_system_files
+				if list_of_user_files != "" {
+					list_of_files += `<option disabled>&mdash;</option>`
+					list_of_files += list_of_user_files
 				}
 				}
 
 
 				fcont := []byte(``)
 				fcont := []byte(``)

+ 29 - 0
engine/wrapper/wrapper.go

@@ -477,3 +477,32 @@ func (this *Wrapper) ShopGetCurrentCurrency() *utils.MySql_shop_currency {
 	}
 	}
 	return nil
 	return nil
 }
 }
+
+func (this *Wrapper) IsSystemMountedTemplateFile(filename string) bool {
+	return utils.InArrayString([]string{
+		"404.html",
+		"blog-category.html",
+		"blog-post.html",
+		"blog.html",
+		"cached-block-1.html",
+		"cached-block-2.html",
+		"cached-block-3.html",
+		"cached-block-4.html",
+		"cached-block-5.html",
+		"email-new-order-admin.html",
+		"email-new-order-user.html",
+		"footer.html",
+		"header.html",
+		"index.html",
+		"maintenance.html",
+		"page.html",
+		"robots.txt",
+		"scripts.js",
+		"shop-category.html",
+		"shop-product.html",
+		"shop.html",
+		"sidebar-left.html",
+		"sidebar-right.html",
+		"styles.css",
+	}, filename)
+}