Browse Source

Blog categories

Vova Tkach 6 years ago
parent
commit
708b1e25b9
4 changed files with 79 additions and 14 deletions
  1. 6 0
      assets/cp.styles.css
  2. 0 0
      assets/cp.styles.css.go
  3. 4 3
      engine/builder/data_table.go
  4. 69 11
      modules/module_blog.go

+ 6 - 0
assets/cp.styles.css

@@ -298,6 +298,12 @@ ul.pagination {
 	text-align: right;
 }
 
+/* Admin table: table_blog_cats */
+.data-table.table_blog_cats .col_action {
+	width: 6rem;
+	text-align: right;
+}
+
 /* Admin table: table_users */
 .data-table.table_users .col_active,
 .data-table.table_users .col_admin {

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


+ 4 - 3
engine/builder/data_table.go

@@ -26,14 +26,15 @@ func DataTable(
 	data *[]DataTableRow,
 	action func(values *[]string) string,
 	pagination_url string,
-	custom_sql_count func() int,
+	custom_sql_count func() (int, error),
 	custom_sql_data func(limit_offset int, pear_page int) (*sql.Rows, error),
 ) string {
 	var num int
+	var err error
 	if custom_sql_count != nil {
-		num = custom_sql_count()
+		num, err = custom_sql_count()
 	} else {
-		err := wrap.DB.QueryRow("SELECT COUNT(*) FROM `" + table + "`;").Scan(&num)
+		err = wrap.DB.QueryRow("SELECT COUNT(*) FROM `" + table + "`;").Scan(&num)
 		if err != nil {
 			return ""
 		}

+ 69 - 11
modules/module_blog.go

@@ -1,13 +1,15 @@
 package modules
 
 import (
-	//"html"
+	"database/sql"
+	"html"
+	"strings"
 
 	"golang-fave/assets"
 	"golang-fave/consts"
 	"golang-fave/engine/builder"
 	"golang-fave/engine/wrapper"
-	//"golang-fave/utils"
+	"golang-fave/utils"
 )
 
 func (this *Modules) RegisterModule_Blog() *Module {
@@ -17,7 +19,7 @@ func (this *Modules) RegisterModule_Blog() *Module {
 		Name:   "Blog",
 		Order:  1,
 		System: false,
-		Icon:   assets.SysSvgIconPage,
+		Icon:   assets.SysSvgIconList,
 		Sub: &[]MISub{
 			{Mount: "default", Name: "List of posts", Show: true, Icon: assets.SysSvgIconList},
 			{Mount: "add", Name: "Add new post", Show: true, Icon: assets.SysSvgIconPlus},
@@ -43,25 +45,81 @@ func (this *Modules) RegisterModule_Blog() *Module {
 				wrap,
 				"blog_cats",
 				"id",
-				"DESC",
+				"ASC",
 				&[]builder.DataTableRow{
 					{
-						DBField:     "id",
-						NameInTable: "Id",
+						DBField: "id",
+						// NameInTable: "id",
+					},
+					{
+						DBField: "user",
+						// NameInTable: "user",
 					},
 					{
 						DBField:     "name",
 						NameInTable: "Name",
+						CallBack: func(values *[]string) string {
+							sub := strings.Repeat("— ", utils.StrToInt((*values)[4]))
+							name := `<a href="/cp/` + wrap.CurrModule + `/categories-modify/` + (*values)[0] + `/">` + sub + html.EscapeString((*values)[2]) + `</a>`
+							// alias := html.EscapeString((*values)[3])
+							// return `<div>` + name + `</div><div><small>` + alias + `</small></div>`
+							return `<div>` + name + `</div>`
+						},
+					},
+					{
+						DBField: "alias",
+						// NameInTable: "Alias",
 					},
 					{
-						DBField:     "alias",
-						NameInTable: "Alias",
+						DBField: "depth",
+						// NameInTable: "depth",
 					},
 				},
-				nil,
+				func(values *[]string) string {
+					return builder.DataTableAction(&[]builder.DataTableActionRow{
+						{
+							Icon: assets.SysSvgIconEdit,
+							Href: "/cp/" + wrap.CurrModule + "/categories-modify/" + (*values)[0] + "/",
+							Hint: "Edit",
+						},
+						{
+							Icon: assets.SysSvgIconRemove,
+							Href: "javascript:fave.ActionDataTableDelete(this,'blog-categories-delete','" +
+								(*values)[0] + "','Are you sure want to delete category?');",
+							Hint:    "Delete",
+							Classes: "delete",
+						},
+					})
+				},
 				"/cp/"+wrap.CurrModule+"/"+wrap.CurrSubModule+"/",
-				nil,
-				nil,
+				func() (int, error) {
+					var num int
+					var err error
+					err = wrap.DB.QueryRow("SELECT COUNT(*) FROM `blog_cats`;").Scan(&num)
+					return num, err
+				},
+				func(limit_offset int, pear_page int) (*sql.Rows, error) {
+					return wrap.DB.Query(
+						`SELECT
+							node.id,
+							node.user,
+							node.name,
+							node.alias,
+							(COUNT(parent.id) - 1) AS depth
+						FROM
+							blog_cats AS node,
+							blog_cats AS parent
+						WHERE
+							node.lft BETWEEN parent.lft AND parent.rgt
+						GROUP BY
+							node.id
+						ORDER BY
+							node.lft ASC
+						LIMIT ?, ?;`,
+						limit_offset,
+						pear_page,
+					)
+				},
 			)
 		} else if wrap.CurrSubModule == "add" || wrap.CurrSubModule == "modify" {
 			if wrap.CurrSubModule == "add" {

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