Browse Source

Page can use index.html or page.html as template, migration

Vova Tkach 5 years ago
parent
commit
390d77366f

+ 2 - 1
engine/assets/cp.styles.css

@@ -805,7 +805,8 @@ ul.pagination {
 }
 }
 
 
 .data-table.table-bordered td .parent,
 .data-table.table-bordered td .parent,
-.data-table.table-bordered td .parent a {
+.data-table.table-bordered td .parent a,
+.data-table.table-bordered td .template {
 	color: #adafb2;
 	color: #adafb2;
 }
 }
 
 

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


+ 1 - 1
engine/assets/template/index_html_file.go

@@ -8,7 +8,7 @@ var VarIndexHtmlFile = []byte(`{{template "header.html" .}}
 				<a href="/cp/index/modify/{{$.Data.Page.Id}}/" target="_blank" style="float:right;">Edit</a>
 				<a href="/cp/index/modify/{{$.Data.Page.Id}}/" target="_blank" style="float:right;">Edit</a>
 			{{end}}
 			{{end}}
 		{{end}}
 		{{end}}
-		<h2 class="card-title">{{$.Data.Page.Name}}</h2>
+		<h2 class="card-title">Index: {{$.Data.Page.Name}}</h2>
 		<div class="page-content">
 		<div class="page-content">
 			{{$.Data.Page.Content}}
 			{{$.Data.Page.Content}}
 		</div>
 		</div>

+ 1 - 1
engine/assets/template/page_html_file.go

@@ -8,7 +8,7 @@ var VarPageHtmlFile = []byte(`{{template "header.html" .}}
 				<a href="/cp/index/modify/{{$.Data.Page.Id}}/" target="_blank" style="float:right;">Edit</a>
 				<a href="/cp/index/modify/{{$.Data.Page.Id}}/" target="_blank" style="float:right;">Edit</a>
 			{{end}}
 			{{end}}
 		{{end}}
 		{{end}}
-		<h2 class="card-title">{{$.Data.Page.Name}}</h2>
+		<h2 class="card-title">Page: {{$.Data.Page.Name}}</h2>
 		<div class="page-content">
 		<div class="page-content">
 			{{$.Data.Page.Content}}
 			{{$.Data.Page.Content}}
 		</div>
 		</div>

+ 60 - 13
engine/modules/module_index.go

@@ -12,6 +12,28 @@ import (
 	"golang-fave/engine/wrapper"
 	"golang-fave/engine/wrapper"
 )
 )
 
 
+func (this *Modules) index_GetTemplateSelectOptions(wrap *wrapper.Wrapper, template string) string {
+	result := ``
+
+	selected := ""
+
+	// index.html
+	if template == "index" {
+		selected = " selected"
+	}
+	result += `<option title="index.html" value="index"` + selected + `>index.html</option>`
+	selected = ""
+
+	// page.html
+	if template == "" || template == "page" {
+		selected = " selected"
+	}
+	result += `<option title="page.html" value="page"` + selected + `>page.html</option>`
+	selected = ""
+
+	return result
+}
+
 func (this *Modules) RegisterModule_Index() *Module {
 func (this *Modules) RegisterModule_Index() *Module {
 	return this.newModule(MInfo{
 	return this.newModule(MInfo{
 		Mount: "index",
 		Mount: "index",
@@ -32,6 +54,7 @@ func (this *Modules) RegisterModule_Index() *Module {
 			`SELECT
 			`SELECT
 				fave_pages.id,
 				fave_pages.id,
 				fave_pages.user,
 				fave_pages.user,
+				fave_pages.template,
 				fave_pages.name,
 				fave_pages.name,
 				fave_pages.alias,
 				fave_pages.alias,
 				fave_pages.content,
 				fave_pages.content,
@@ -57,6 +80,7 @@ func (this *Modules) RegisterModule_Index() *Module {
 		).Scan(
 		).Scan(
 			&row.A_id,
 			&row.A_id,
 			&row.A_user,
 			&row.A_user,
+			&row.A_template,
 			&row.A_name,
 			&row.A_name,
 			&row.A_alias,
 			&row.A_alias,
 			&row.A_content,
 			&row.A_content,
@@ -83,14 +107,8 @@ func (this *Modules) RegisterModule_Index() *Module {
 			return
 			return
 		}
 		}
 
 
-		// Which template
-		tname := "index"
-		if wrap.R.URL.Path != "/" {
-			tname = "page"
-		}
-
 		// Render template
 		// Render template
-		wrap.RenderFrontEnd(tname, fetdata.New(wrap, false, row, rou), http.StatusOK)
+		wrap.RenderFrontEnd(row.A_template, fetdata.New(wrap, false, row, rou), http.StatusOK)
 	}, func(wrap *wrapper.Wrapper) (string, string, string) {
 	}, func(wrap *wrapper.Wrapper) (string, string, string) {
 		content := ""
 		content := ""
 		sidebar := ""
 		sidebar := ""
@@ -107,13 +125,17 @@ func (this *Modules) RegisterModule_Index() *Module {
 					{
 					{
 						DBField: "id",
 						DBField: "id",
 					},
 					},
+					{
+						DBField: "template",
+					},
 					{
 					{
 						DBField:     "name",
 						DBField:     "name",
 						NameInTable: "Page / URL",
 						NameInTable: "Page / URL",
 						CallBack: func(values *[]string) string {
 						CallBack: func(values *[]string) string {
-							name := `<a href="/cp/` + wrap.CurrModule + `/modify/` + (*values)[0] + `/">` + html.EscapeString((*values)[1]) + `</a>`
+							name := `<a href="/cp/` + wrap.CurrModule + `/modify/` + (*values)[0] + `/">` + html.EscapeString((*values)[2]) + `</a>`
-							alias := html.EscapeString((*values)[2])
+							alias := html.EscapeString((*values)[3])
-							return `<div>` + name + `</div><div><small>` + alias + `</small></div>`
+							template := html.EscapeString((*values)[1]) + ".html"
+							return `<div>` + name + `</div><div class="template"><small>` + template + `</small></div><div><small>` + alias + `</small></div>`
 						},
 						},
 					},
 					},
 					{
 					{
@@ -125,7 +147,7 @@ func (this *Modules) RegisterModule_Index() *Module {
 						NameInTable: "Date / Time",
 						NameInTable: "Date / Time",
 						Classes:     "d-none d-md-table-cell",
 						Classes:     "d-none d-md-table-cell",
 						CallBack: func(values *[]string) string {
 						CallBack: func(values *[]string) string {
-							t := int64(utils.StrToInt((*values)[3]))
+							t := int64(utils.StrToInt((*values)[4]))
 							return `<div>` + utils.UnixTimestampToFormat(t, "02.01.2006") + `</div>` +
 							return `<div>` + utils.UnixTimestampToFormat(t, "02.01.2006") + `</div>` +
 								`<div><small>` + utils.UnixTimestampToFormat(t, "15:04:05") + `</small></div>`
 								`<div><small>` + utils.UnixTimestampToFormat(t, "15:04:05") + `</small></div>`
 						},
 						},
@@ -135,7 +157,7 @@ func (this *Modules) RegisterModule_Index() *Module {
 						NameInTable: "Active",
 						NameInTable: "Active",
 						Classes:     "d-none d-sm-table-cell",
 						Classes:     "d-none d-sm-table-cell",
 						CallBack: func(values *[]string) string {
 						CallBack: func(values *[]string) string {
-							return builder.CheckBox(utils.StrToInt((*values)[4]))
+							return builder.CheckBox(utils.StrToInt((*values)[5]))
 						},
 						},
 					},
 					},
 				},
 				},
@@ -143,7 +165,7 @@ func (this *Modules) RegisterModule_Index() *Module {
 					return builder.DataTableAction(&[]builder.DataTableActionRow{
 					return builder.DataTableAction(&[]builder.DataTableActionRow{
 						{
 						{
 							Icon:   assets.SysSvgIconView,
 							Icon:   assets.SysSvgIconView,
-							Href:   (*values)[2],
+							Href:   (*values)[3],
 							Hint:   "View",
 							Hint:   "View",
 							Target: "_blank",
 							Target: "_blank",
 						},
 						},
@@ -180,6 +202,7 @@ func (this *Modules) RegisterModule_Index() *Module {
 			data := utils.MySql_page{
 			data := utils.MySql_page{
 				A_id:               0,
 				A_id:               0,
 				A_user:             0,
 				A_user:             0,
+				A_template:         "",
 				A_name:             "",
 				A_name:             "",
 				A_alias:            "",
 				A_alias:            "",
 				A_content:          "",
 				A_content:          "",
@@ -202,6 +225,7 @@ func (this *Modules) RegisterModule_Index() *Module {
 					`SELECT
 					`SELECT
 						id,
 						id,
 						user,
 						user,
+						template,
 						name,
 						name,
 						alias,
 						alias,
 						content,
 						content,
@@ -218,6 +242,7 @@ func (this *Modules) RegisterModule_Index() *Module {
 				).Scan(
 				).Scan(
 					&data.A_id,
 					&data.A_id,
 					&data.A_user,
 					&data.A_user,
+					&data.A_template,
 					&data.A_name,
 					&data.A_name,
 					&data.A_alias,
 					&data.A_alias,
 					&data.A_content,
 					&data.A_content,
@@ -264,6 +289,28 @@ func (this *Modules) RegisterModule_Index() *Module {
 					Hint:    "Example: /about-us/ or /about-us.html",
 					Hint:    "Example: /about-us/ or /about-us.html",
 					Max:     "255",
 					Max:     "255",
 				},
 				},
+				{
+					Kind:    builder.DFKText,
+					Caption: "Page template",
+					Name:    "template",
+					Value:   "0",
+					CallBack: func(field *builder.DataFormField) string {
+						return `<div class="form-group n2">` +
+							`<div class="row">` +
+							`<div class="col-md-3">` +
+							`<label for="lbl_template">Page template</label>` +
+							`</div>` +
+							`<div class="col-md-9">` +
+							`<div>` +
+							`<select class="selectpicker form-control" id="lbl_template" name="template" data-live-search="true">` +
+							this.index_GetTemplateSelectOptions(wrap, data.A_template) +
+							`</select>` +
+							`</div>` +
+							`</div>` +
+							`</div>` +
+							`</div>`
+					},
+				},
 				{
 				{
 					Kind:    builder.DFKTextArea,
 					Kind:    builder.DFKTextArea,
 					Caption: "Page content",
 					Caption: "Page content",

+ 10 - 0
engine/modules/module_index_act_modify.go

@@ -15,6 +15,7 @@ func (this *Modules) RegisterAction_IndexModify() *Action {
 		pf_id := utils.Trim(wrap.R.FormValue("id"))
 		pf_id := utils.Trim(wrap.R.FormValue("id"))
 		pf_name := utils.Trim(wrap.R.FormValue("name"))
 		pf_name := utils.Trim(wrap.R.FormValue("name"))
 		pf_alias := utils.Trim(wrap.R.FormValue("alias"))
 		pf_alias := utils.Trim(wrap.R.FormValue("alias"))
+		pf_template := utils.Trim(wrap.R.FormValue("template"))
 		pf_content := utils.Trim(wrap.R.FormValue("content"))
 		pf_content := utils.Trim(wrap.R.FormValue("content"))
 		pf_meta_title := utils.Trim(wrap.R.FormValue("meta_title"))
 		pf_meta_title := utils.Trim(wrap.R.FormValue("meta_title"))
 		pf_meta_keywords := utils.Trim(wrap.R.FormValue("meta_keywords"))
 		pf_meta_keywords := utils.Trim(wrap.R.FormValue("meta_keywords"))
@@ -44,6 +45,11 @@ func (this *Modules) RegisterAction_IndexModify() *Action {
 			return
 			return
 		}
 		}
 
 
+		if pf_template == "" {
+			wrap.MsgError(`Please specify page template`)
+			return
+		}
+
 		if pf_id == "0" {
 		if pf_id == "0" {
 			// Add new page
 			// Add new page
 			var lastID int64 = 0
 			var lastID int64 = 0
@@ -52,6 +58,7 @@ func (this *Modules) RegisterAction_IndexModify() *Action {
 					ctx,
 					ctx,
 					`INSERT INTO fave_pages SET
 					`INSERT INTO fave_pages SET
 						user = ?,
 						user = ?,
+						template = ?,
 						name = ?,
 						name = ?,
 						alias = ?,
 						alias = ?,
 						content = ?,
 						content = ?,
@@ -62,6 +69,7 @@ func (this *Modules) RegisterAction_IndexModify() *Action {
 						active = ?
 						active = ?
 					;`,
 					;`,
 					wrap.User.A_id,
 					wrap.User.A_id,
+					pf_template,
 					pf_name,
 					pf_name,
 					pf_alias,
 					pf_alias,
 					pf_content,
 					pf_content,
@@ -92,6 +100,7 @@ func (this *Modules) RegisterAction_IndexModify() *Action {
 				_, err := tx.Exec(
 				_, err := tx.Exec(
 					ctx,
 					ctx,
 					`UPDATE fave_pages SET
 					`UPDATE fave_pages SET
+						template = ?,
 						name = ?,
 						name = ?,
 						alias = ?,
 						alias = ?,
 						content = ?,
 						content = ?,
@@ -102,6 +111,7 @@ func (this *Modules) RegisterAction_IndexModify() *Action {
 					WHERE
 					WHERE
 						id = ?
 						id = ?
 					;`,
 					;`,
+					pf_template,
 					pf_name,
 					pf_name,
 					pf_alias,
 					pf_alias,
 					pf_content,
 					pf_content,

File diff suppressed because it is too large
+ 3 - 0
engine/modules/module_index_act_mysql_setup.go


+ 1 - 0
engine/utils/mysql_struct_page.go

@@ -3,6 +3,7 @@ package utils
 type MySql_page struct {
 type MySql_page struct {
 	A_id               int
 	A_id               int
 	A_user             int
 	A_user             int
+	A_template         string
 	A_name             string
 	A_name             string
 	A_alias            string
 	A_alias            string
 	A_content          string
 	A_content          string

+ 18 - 18
hosts/localhost/template/index.html

@@ -1,19 +1,19 @@
-{{template "header.html" .}}
+{{template "header.html" .}}
-<div class="card mb-4">
+<div class="card mb-4">
-	<div class="card-body">
+	<div class="card-body">
-		{{if $.Data.IsUserLoggedIn}}
+		{{if $.Data.IsUserLoggedIn}}
-			{{if $.Data.CurrentUser.IsAdmin}}
+			{{if $.Data.CurrentUser.IsAdmin}}
-				<a href="/cp/index/modify/{{$.Data.Page.Id}}/" target="_blank" style="float:right;">Edit</a>
+				<a href="/cp/index/modify/{{$.Data.Page.Id}}/" target="_blank" style="float:right;">Edit</a>
-			{{end}}
+			{{end}}
-		{{end}}
+		{{end}}
-		<h2 class="card-title">{{$.Data.Page.Name}}</h2>
+		<h2 class="card-title">Index: {{$.Data.Page.Name}}</h2>
-		<div class="page-content">
+		<div class="page-content">
-			{{$.Data.Page.Content}}
+			{{$.Data.Page.Content}}
-		</div>
+		</div>
-	</div>
+	</div>
-	<div class="card-footer text-muted">
+	<div class="card-footer text-muted">
-		<div>Published on {{$.Data.Page.DateTimeFormat "02/01/2006, 15:04:05"}}</div>
+		<div>Published on {{$.Data.Page.DateTimeFormat "02/01/2006, 15:04:05"}}</div>
-		<div>Author: {{$.Data.Page.User.FirstName}} {{$.Data.Page.User.LastName}}</div>
+		<div>Author: {{$.Data.Page.User.FirstName}} {{$.Data.Page.User.LastName}}</div>
-	</div>
+	</div>
-</div>
+</div>
 {{template "footer.html" .}}
 {{template "footer.html" .}}

+ 18 - 18
hosts/localhost/template/page.html

@@ -1,19 +1,19 @@
-{{template "header.html" .}}
+{{template "header.html" .}}
-<div class="card mb-4">
+<div class="card mb-4">
-	<div class="card-body">
+	<div class="card-body">
-		{{if $.Data.IsUserLoggedIn}}
+		{{if $.Data.IsUserLoggedIn}}
-			{{if $.Data.CurrentUser.IsAdmin}}
+			{{if $.Data.CurrentUser.IsAdmin}}
-				<a href="/cp/index/modify/{{$.Data.Page.Id}}/" target="_blank" style="float:right;">Edit</a>
+				<a href="/cp/index/modify/{{$.Data.Page.Id}}/" target="_blank" style="float:right;">Edit</a>
-			{{end}}
+			{{end}}
-		{{end}}
+		{{end}}
-		<h2 class="card-title">{{$.Data.Page.Name}}</h2>
+		<h2 class="card-title">Page: {{$.Data.Page.Name}}</h2>
-		<div class="page-content">
+		<div class="page-content">
-			{{$.Data.Page.Content}}
+			{{$.Data.Page.Content}}
-		</div>
+		</div>
-	</div>
+	</div>
-	<div class="card-footer text-muted">
+	<div class="card-footer text-muted">
-		<div>Published on {{$.Data.Page.DateTimeFormat "02/01/2006, 15:04:05"}}</div>
+		<div>Published on {{$.Data.Page.DateTimeFormat "02/01/2006, 15:04:05"}}</div>
-		<div>Author: {{$.Data.Page.User.FirstName}} {{$.Data.Page.User.LastName}}</div>
+		<div>Author: {{$.Data.Page.User.FirstName}} {{$.Data.Page.User.LastName}}</div>
-	</div>
+	</div>
-</div>
+</div>
 {{template "footer.html" .}}
 {{template "footer.html" .}}

+ 1 - 0
support/migrate/000000001.go

@@ -31,4 +31,5 @@ var Migrations = map[string]func(context.Context, *sqlw.DB, string) error{
 	"000000021": Migrate_000000021,
 	"000000021": Migrate_000000021,
 	"000000022": Migrate_000000022,
 	"000000022": Migrate_000000022,
 	"000000023": Migrate_000000023,
 	"000000023": Migrate_000000023,
+	"000000024": Migrate_000000024,
 }
 }

+ 19 - 0
support/migrate/000000024.go

@@ -0,0 +1,19 @@
+package migrate
+
+import (
+	"context"
+
+	"golang-fave/engine/sqlw"
+)
+
+func Migrate_000000024(ctx context.Context, db *sqlw.DB, host string) error {
+	if _, err := db.Exec(ctx, `ALTER TABLE fave_pages ADD COLUMN template varchar(255) NOT NULL DEFAULT 'page' AFTER user;`); err != nil {
+		return err
+	}
+
+	if _, err := db.Exec(ctx, `UPDATE fave_pages SET template = 'index' WHERE alias = '/' LIMIT 1;`); err != nil {
+		return err
+	}
+
+	return nil
+}

+ 1 - 0
support/schema.sql

@@ -2,6 +2,7 @@
 CREATE TABLE fave_blog_cats (
 CREATE TABLE fave_blog_cats (
 	id int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI',
 	id int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI',
 	user int(11) NOT NULL COMMENT 'User id',
 	user int(11) NOT NULL COMMENT 'User id',
+	template varchar(255) NOT NULL DEFAULT 'page' COMMENT 'Template',
 	name varchar(255) NOT NULL COMMENT 'Category name',
 	name varchar(255) NOT NULL COMMENT 'Category name',
 	alias varchar(255) NOT NULL COMMENT 'Category alias',
 	alias varchar(255) NOT NULL COMMENT 'Category alias',
 	lft int(11) NOT NULL COMMENT 'For nested set model',
 	lft int(11) NOT NULL COMMENT 'For nested set model',

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