Browse Source

Categories improvements

Vova Tkach 5 years ago
parent
commit
dd11d61471

+ 16 - 10
assets/template/header_html_file.go

@@ -117,16 +117,22 @@ var VarHeaderHtmlFile = []byte(`<!doctype html>
 						<div class="collapse navbar-collapse" id="navbarSupportedContent">
 						<div class="collapse navbar-collapse" id="navbarSupportedContent">
 							<ul class="navbar-nav mr-auto">
 							<ul class="navbar-nav mr-auto">
 							{{range $.Data.Shop.Categories 0 1}}
 							{{range $.Data.Shop.Categories 0 1}}
-								<li class="nav-item dropdown">
-									<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">{{.Name}}</a>
-									<div class="dropdown-menu" aria-labelledby="navbarDropdown">
-										<a class="dropdown-item" href="{{.Permalink}}">All products</a>
-										<div class="dropdown-divider"></div>
-										{{range $index, $subcat := $.Data.Shop.Categories .Id 1}}
-											<a class="dropdown-item" href="{{$subcat.Permalink}}">{{$subcat.Name}}</a>
-										{{end}}
-									</div>
-								</li>
+								{{if .HaveChilds}}
+									<li class="nav-item dropdown">
+										<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">{{.Name}}</a>
+										<div class="dropdown-menu" aria-labelledby="navbarDropdown">
+											<a class="dropdown-item" href="{{.Permalink}}">All products</a>
+											<div class="dropdown-divider"></div>
+											{{range $index, $subcat := $.Data.Shop.Categories .Id 1}}
+												<a class="dropdown-item" href="{{$subcat.Permalink}}">{{$subcat.Name}}</a>
+											{{end}}
+										</div>
+									</li>
+								{{else}}
+									<li class="nav-item">
+										<a class="nav-link" href="{{.Permalink}}">{{.Name}}</a>
+									</li>
+								{{end}}
 							{{end}}
 							{{end}}
 							</ul>
 							</ul>
 						</div>
 						</div>

+ 85 - 78
engine/fetdata/blog.go

@@ -281,6 +281,90 @@ func (this *Blog) load() *Blog {
 	return this
 	return this
 }
 }
 
 
+func (this *Blog) preload_cats() {
+	if this.bufferCats == nil {
+		this.bufferCats = map[int]*utils.MySql_blog_category{}
+		if rows, err := this.wrap.DB.Query(`
+			SELECT
+				main.id,
+				main.user,
+				main.name,
+				main.alias,
+				main.lft,
+				main.rgt,
+				main.depth,
+				parent.id AS parent_id
+			FROM
+				(
+					SELECT
+						node.id,
+						node.user,
+						node.name,
+						node.alias,
+						node.lft,
+						node.rgt,
+						(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
+				) AS main
+				LEFT JOIN (
+					SELECT
+						node.id,
+						node.user,
+						node.name,
+						node.alias,
+						node.lft,
+						node.rgt,
+						(COUNT(parent.id) - 0) 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
+				) AS parent ON
+				parent.depth = main.depth AND
+				main.lft > parent.lft AND
+				main.rgt < parent.rgt
+			WHERE
+				main.id > 1
+			ORDER BY
+				main.lft ASC
+			;
+		`); err == nil {
+			defer rows.Close()
+			for rows.Next() {
+				row := utils.MySql_blog_category{}
+				if err := rows.Scan(
+					&row.A_id,
+					&row.A_user,
+					&row.A_name,
+					&row.A_alias,
+					&row.A_lft,
+					&row.A_rgt,
+					&row.A_depth,
+					&row.A_parent,
+				); err == nil {
+					this.bufferCats[row.A_id] = &row
+					if _, ok := this.bufferCats[row.A_parent]; ok {
+						this.bufferCats[row.A_parent].A_childs = true
+					}
+				}
+			}
+		}
+	}
+}
+
 func (this *Blog) Category() *BlogCategory {
 func (this *Blog) Category() *BlogCategory {
 	if this == nil {
 	if this == nil {
 		return nil
 		return nil
@@ -362,84 +446,7 @@ func (this *Blog) PaginationNext() *BlogPagination {
 }
 }
 
 
 func (this *Blog) Categories(parent, depth int) []*BlogCategory {
 func (this *Blog) Categories(parent, depth int) []*BlogCategory {
-	if this.bufferCats == nil {
-		this.bufferCats = map[int]*utils.MySql_blog_category{}
-		if rows, err := this.wrap.DB.Query(`
-			SELECT
-				main.id,
-				main.user,
-				main.name,
-				main.alias,
-				main.lft,
-				main.rgt,
-				main.depth,
-				parent.id AS parent_id
-			FROM
-				(
-					SELECT
-						node.id,
-						node.user,
-						node.name,
-						node.alias,
-						node.lft,
-						node.rgt,
-						(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
-				) AS main
-				LEFT JOIN (
-					SELECT
-						node.id,
-						node.user,
-						node.name,
-						node.alias,
-						node.lft,
-						node.rgt,
-						(COUNT(parent.id) - 0) 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
-				) AS parent ON
-				parent.depth = main.depth AND
-				main.lft > parent.lft AND
-				main.rgt < parent.rgt
-			WHERE
-				main.id > 1
-			ORDER BY
-				main.lft ASC
-			;
-		`); err == nil {
-			defer rows.Close()
-			for rows.Next() {
-				row := utils.MySql_blog_category{}
-				if err := rows.Scan(
-					&row.A_id,
-					&row.A_user,
-					&row.A_name,
-					&row.A_alias,
-					&row.A_lft,
-					&row.A_rgt,
-					&row.A_depth,
-					&row.A_parent,
-				); err == nil {
-					this.bufferCats[row.A_id] = &row
-				}
-			}
-		}
-	}
+	this.preload_cats()
 
 
 	depth_tmp := 0
 	depth_tmp := 0
 	result := []*BlogCategory{}
 	result := []*BlogCategory{}

+ 4 - 2
engine/fetdata/blog_category.go

@@ -97,6 +97,9 @@ func (this *BlogCategory) load(cache *map[int]*utils.MySql_blog_category) *BlogC
 				&row.A_parent,
 				&row.A_parent,
 			); err == nil {
 			); err == nil {
 				this.bufferCats[row.A_id] = &row
 				this.bufferCats[row.A_id] = &row
+				if _, ok := this.bufferCats[row.A_parent]; ok {
+					this.bufferCats[row.A_parent].A_childs = true
+				}
 			}
 			}
 		}
 		}
 	}
 	}
@@ -260,9 +263,8 @@ func (this *BlogCategory) Parent() *BlogCategory {
 }
 }
 
 
 func (this *BlogCategory) HaveChilds() bool {
 func (this *BlogCategory) HaveChilds() bool {
-	// TODO: Add ability
 	if this == nil {
 	if this == nil {
 		return false
 		return false
 	}
 	}
-	return false
+	return this.object.A_childs
 }
 }

+ 85 - 78
engine/fetdata/shop.go

@@ -491,6 +491,90 @@ func (this *Shop) load() *Shop {
 	return this
 	return this
 }
 }
 
 
+func (this *Shop) preload_cats() {
+	if this.bufferCats == nil {
+		this.bufferCats = map[int]*utils.MySql_shop_category{}
+		if rows, err := this.wrap.DB.Query(`
+			SELECT
+				main.id,
+				main.user,
+				main.name,
+				main.alias,
+				main.lft,
+				main.rgt,
+				main.depth,
+				parent.id AS parent_id
+			FROM
+				(
+					SELECT
+						node.id,
+						node.user,
+						node.name,
+						node.alias,
+						node.lft,
+						node.rgt,
+						(COUNT(parent.id) - 1) AS depth
+					FROM
+						shop_cats AS node,
+						shop_cats AS parent
+					WHERE
+						node.lft BETWEEN parent.lft AND parent.rgt
+					GROUP BY
+						node.id
+					ORDER BY
+						node.lft ASC
+				) AS main
+				LEFT JOIN (
+					SELECT
+						node.id,
+						node.user,
+						node.name,
+						node.alias,
+						node.lft,
+						node.rgt,
+						(COUNT(parent.id) - 0) AS depth
+					FROM
+						shop_cats AS node,
+						shop_cats AS parent
+					WHERE
+						node.lft BETWEEN parent.lft AND parent.rgt
+					GROUP BY
+						node.id
+					ORDER BY
+						node.lft ASC
+				) AS parent ON
+				parent.depth = main.depth AND
+				main.lft > parent.lft AND
+				main.rgt < parent.rgt
+			WHERE
+				main.id > 1
+			ORDER BY
+				main.lft ASC
+			;
+		`); err == nil {
+			defer rows.Close()
+			for rows.Next() {
+				row := utils.MySql_shop_category{}
+				if err := rows.Scan(
+					&row.A_id,
+					&row.A_user,
+					&row.A_name,
+					&row.A_alias,
+					&row.A_lft,
+					&row.A_rgt,
+					&row.A_depth,
+					&row.A_parent,
+				); err == nil {
+					this.bufferCats[row.A_id] = &row
+					if _, ok := this.bufferCats[row.A_parent]; ok {
+						this.bufferCats[row.A_parent].A_childs = true
+					}
+				}
+			}
+		}
+	}
+}
+
 func (this *Shop) Category() *ShopCategory {
 func (this *Shop) Category() *ShopCategory {
 	if this == nil {
 	if this == nil {
 		return nil
 		return nil
@@ -572,84 +656,7 @@ func (this *Shop) PaginationNext() *ShopPagination {
 }
 }
 
 
 func (this *Shop) Categories(parent, depth int) []*ShopCategory {
 func (this *Shop) Categories(parent, depth int) []*ShopCategory {
-	if this.bufferCats == nil {
-		this.bufferCats = map[int]*utils.MySql_shop_category{}
-		if rows, err := this.wrap.DB.Query(`
-			SELECT
-				main.id,
-				main.user,
-				main.name,
-				main.alias,
-				main.lft,
-				main.rgt,
-				main.depth,
-				parent.id AS parent_id
-			FROM
-				(
-					SELECT
-						node.id,
-						node.user,
-						node.name,
-						node.alias,
-						node.lft,
-						node.rgt,
-						(COUNT(parent.id) - 1) AS depth
-					FROM
-						shop_cats AS node,
-						shop_cats AS parent
-					WHERE
-						node.lft BETWEEN parent.lft AND parent.rgt
-					GROUP BY
-						node.id
-					ORDER BY
-						node.lft ASC
-				) AS main
-				LEFT JOIN (
-					SELECT
-						node.id,
-						node.user,
-						node.name,
-						node.alias,
-						node.lft,
-						node.rgt,
-						(COUNT(parent.id) - 0) AS depth
-					FROM
-						shop_cats AS node,
-						shop_cats AS parent
-					WHERE
-						node.lft BETWEEN parent.lft AND parent.rgt
-					GROUP BY
-						node.id
-					ORDER BY
-						node.lft ASC
-				) AS parent ON
-				parent.depth = main.depth AND
-				main.lft > parent.lft AND
-				main.rgt < parent.rgt
-			WHERE
-				main.id > 1
-			ORDER BY
-				main.lft ASC
-			;
-		`); err == nil {
-			defer rows.Close()
-			for rows.Next() {
-				row := utils.MySql_shop_category{}
-				if err := rows.Scan(
-					&row.A_id,
-					&row.A_user,
-					&row.A_name,
-					&row.A_alias,
-					&row.A_lft,
-					&row.A_rgt,
-					&row.A_depth,
-					&row.A_parent,
-				); err == nil {
-					this.bufferCats[row.A_id] = &row
-				}
-			}
-		}
-	}
+	this.preload_cats()
 
 
 	depth_tmp := 0
 	depth_tmp := 0
 	result := []*ShopCategory{}
 	result := []*ShopCategory{}

+ 4 - 2
engine/fetdata/shop_category.go

@@ -97,6 +97,9 @@ func (this *ShopCategory) load(cache *map[int]*utils.MySql_shop_category) *ShopC
 				&row.A_parent,
 				&row.A_parent,
 			); err == nil {
 			); err == nil {
 				this.bufferCats[row.A_id] = &row
 				this.bufferCats[row.A_id] = &row
+				if _, ok := this.bufferCats[row.A_parent]; ok {
+					this.bufferCats[row.A_parent].A_childs = true
+				}
 			}
 			}
 		}
 		}
 	}
 	}
@@ -260,9 +263,8 @@ func (this *ShopCategory) Parent() *ShopCategory {
 }
 }
 
 
 func (this *ShopCategory) HaveChilds() bool {
 func (this *ShopCategory) HaveChilds() bool {
-	// TODO: Add ability
 	if this == nil {
 	if this == nil {
 		return false
 		return false
 	}
 	}
-	return false
+	return this.object.A_childs
 }
 }

+ 16 - 10
hosts/localhost/template/header.html

@@ -115,16 +115,22 @@
 						<div class="collapse navbar-collapse" id="navbarSupportedContent">
 						<div class="collapse navbar-collapse" id="navbarSupportedContent">
 							<ul class="navbar-nav mr-auto">
 							<ul class="navbar-nav mr-auto">
 							{{range $.Data.Shop.Categories 0 1}}
 							{{range $.Data.Shop.Categories 0 1}}
-								<li class="nav-item dropdown">
-									<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">{{.Name}}</a>
-									<div class="dropdown-menu" aria-labelledby="navbarDropdown">
-										<a class="dropdown-item" href="{{.Permalink}}">All products</a>
-										<div class="dropdown-divider"></div>
-										{{range $index, $subcat := $.Data.Shop.Categories .Id 1}}
-											<a class="dropdown-item" href="{{$subcat.Permalink}}">{{$subcat.Name}}</a>
-										{{end}}
-									</div>
-								</li>
+								{{if .HaveChilds}}
+									<li class="nav-item dropdown">
+										<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">{{.Name}}</a>
+										<div class="dropdown-menu" aria-labelledby="navbarDropdown">
+											<a class="dropdown-item" href="{{.Permalink}}">All products</a>
+											<div class="dropdown-divider"></div>
+											{{range $index, $subcat := $.Data.Shop.Categories .Id 1}}
+												<a class="dropdown-item" href="{{$subcat.Permalink}}">{{$subcat.Name}}</a>
+											{{end}}
+										</div>
+									</li>
+								{{else}}
+									<li class="nav-item">
+										<a class="nav-link" href="{{.Permalink}}">{{.Name}}</a>
+									</li>
+								{{end}}
 							{{end}}
 							{{end}}
 							</ul>
 							</ul>
 						</div>
 						</div>

+ 1 - 0
utils/mysql_struct_blog_category.go

@@ -9,4 +9,5 @@ type MySql_blog_category struct {
 	A_rgt    int
 	A_rgt    int
 	A_depth  int
 	A_depth  int
 	A_parent int
 	A_parent int
+	A_childs bool
 }
 }

+ 1 - 0
utils/mysql_struct_shop_category.go

@@ -9,4 +9,5 @@ type MySql_shop_category struct {
 	A_rgt    int
 	A_rgt    int
 	A_depth  int
 	A_depth  int
 	A_parent int
 	A_parent int
+	A_childs bool
 }
 }