Browse Source

Shop category can return parent, theme breadcrumbs

Vova Tkach 5 years ago
parent
commit
aeed4811f0

+ 30 - 0
assets/template/header_html_file.go

@@ -141,10 +141,40 @@ var VarHeaderHtmlFile = []byte(`<!doctype html>
 									<li class="breadcrumb-item"><a href="/shop/">Shop</a></li>
 								{{end}}
 								{{if eq $.Data.Module "shop-category"}}
+									{{if $.Data.Shop.Category.Parent.Parent.Parent.Parent.Parent}}
+										<li class="breadcrumb-item"><a href="{{$.Data.Shop.Category.Parent.Parent.Parent.Parent.Parent.Permalink}}">{{$.Data.Shop.Category.Parent.Parent.Parent.Parent.Parent.Name}}</a></li>
+									{{end}}
+									{{if $.Data.Shop.Category.Parent.Parent.Parent.Parent}}
+										<li class="breadcrumb-item"><a href="{{$.Data.Shop.Category.Parent.Parent.Parent.Parent.Permalink}}">{{$.Data.Shop.Category.Parent.Parent.Parent.Parent.Name}}</a></li>
+									{{end}}
+									{{if $.Data.Shop.Category.Parent.Parent.Parent}}
+										<li class="breadcrumb-item"><a href="{{$.Data.Shop.Category.Parent.Parent.Parent.Permalink}}">{{$.Data.Shop.Category.Parent.Parent.Parent.Name}}</a></li>
+									{{end}}
+									{{if $.Data.Shop.Category.Parent.Parent}}
+										<li class="breadcrumb-item"><a href="{{$.Data.Shop.Category.Parent.Parent.Permalink}}">{{$.Data.Shop.Category.Parent.Parent.Name}}</a></li>
+									{{end}}
+									{{if $.Data.Shop.Category.Parent}}
+										<li class="breadcrumb-item"><a href="{{$.Data.Shop.Category.Parent.Permalink}}">{{$.Data.Shop.Category.Parent.Name}}</a></li>
+									{{end}}
 									<li class="breadcrumb-item">{{$.Data.Shop.Category.Name}}</li>
 								{{end}}
 								{{if eq $.Data.Module "shop-product"}}
 									{{if $.Data.Shop.Product.Category.Id}}
+										{{if $.Data.Shop.Product.Category.Parent.Parent.Parent.Parent.Parent}}
+											<li class="breadcrumb-item"><a href="{{$.Data.Shop.Product.Category.Parent.Parent.Parent.Parent.Parent.Permalink}}">{{$.Data.Shop.Product.Category.Parent.Parent.Parent.Parent.Parent.Name}}</a></li>
+										{{end}}
+										{{if $.Data.Shop.Product.Category.Parent.Parent.Parent.Parent}}
+											<li class="breadcrumb-item"><a href="{{$.Data.Shop.Product.Category.Parent.Parent.Parent.Parent.Permalink}}">{{$.Data.Shop.Product.Category.Parent.Parent.Parent.Parent.Name}}</a></li>
+										{{end}}
+										{{if $.Data.Shop.Product.Category.Parent.Parent.Parent}}
+											<li class="breadcrumb-item"><a href="{{$.Data.Shop.Product.Category.Parent.Parent.Parent.Permalink}}">{{$.Data.Shop.Product.Category.Parent.Parent.Parent.Name}}</a></li>
+										{{end}}
+										{{if $.Data.Shop.Product.Category.Parent.Parent}}
+											<li class="breadcrumb-item"><a href="{{$.Data.Shop.Product.Category.Parent.Parent.Permalink}}">{{$.Data.Shop.Product.Category.Parent.Parent.Name}}</a></li>
+										{{end}}
+										{{if $.Data.Shop.Product.Category.Parent}}
+											<li class="breadcrumb-item"><a href="{{$.Data.Shop.Product.Category.Parent.Permalink}}">{{$.Data.Shop.Product.Category.Parent.Name}}</a></li>
+										{{end}}
 										<li class="breadcrumb-item"><a href="{{$.Data.Shop.Product.Category.Permalink}}">{{$.Data.Shop.Product.Category.Name}}</a></li>
 									{{end}}
 									<li class="breadcrumb-item active">{{$.Data.Shop.Product.Name}}</li>

+ 1 - 1
engine/fetdata/fetdata.go

@@ -43,7 +43,7 @@ func New(wrap *wrapper.Wrapper, drow interface{}, is404 bool) *FERData {
 	} else if wrap.CurrModule == "shop" {
 		if len(wrap.UrlArgs) == 3 && wrap.UrlArgs[0] == "shop" && wrap.UrlArgs[1] == "category" && wrap.UrlArgs[2] != "" {
 			if o, ok := drow.(*utils.MySql_shop_category); ok {
-				d_Shop = &Shop{wrap: wrap, category: (&ShopCategory{wrap: wrap, object: o}).load()}
+				d_Shop = &Shop{wrap: wrap, category: (&ShopCategory{wrap: wrap, object: o}).load(nil)}
 				d_Shop.load()
 			}
 		} else if len(wrap.UrlArgs) == 2 && wrap.UrlArgs[0] == "shop" && wrap.UrlArgs[1] != "" {

+ 10 - 1
engine/fetdata/shop.go

@@ -624,7 +624,16 @@ func (this *Shop) Categories(mlvl int) []*ShopCategory {
 			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 {
+				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 {
 					cats = append(cats, &ShopCategory{object: &row})
 				}
 			}

+ 92 - 4
engine/fetdata/shop_category.go

@@ -10,9 +10,90 @@ type ShopCategory struct {
 	object *utils.MySql_shop_category
 
 	user *User
+
+	bufferCats map[int]*utils.MySql_shop_category
 }
 
-func (this *ShopCategory) load() *ShopCategory {
+func (this *ShopCategory) load(cache *map[int]*utils.MySql_shop_category) *ShopCategory {
+	if this == nil {
+		return this
+	}
+	if cache != nil {
+		this.bufferCats = (*cache)
+		return this
+	}
+	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,
+			depth.depth,
+			MAX(main.parent_id) AS parent_id
+		FROM
+			(
+				SELECT
+					node.id,
+					node.user,
+					node.name,
+					node.alias,
+					node.lft,
+					node.rgt,
+					parent.id AS parent_id
+				FROM
+					shop_cats AS node,
+					shop_cats AS parent
+				WHERE
+					node.lft BETWEEN parent.lft AND parent.rgt AND
+					node.id > 1
+				ORDER BY
+					node.lft ASC
+			) AS main
+			LEFT JOIN (
+				SELECT
+					node.id,
+					(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 depth ON depth.id = main.id
+		WHERE
+			main.id > 1 AND
+			main.id <> main.parent_id
+		GROUP BY
+			main.id
+		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
+			}
+		}
+	}
 	return this
 }
 
@@ -150,9 +231,16 @@ func (this *ShopCategory) Level() int {
 	return this.object.A_depth
 }
 
-func (this *ShopCategory) ParentId() int {
+func (this *ShopCategory) Parent() *ShopCategory {
 	if this == nil {
-		return 0
+		return nil
+	}
+	if this.bufferCats == nil {
+		return nil
+	}
+	if _, ok := this.bufferCats[this.object.A_parent]; !ok {
+		return nil
 	}
-	return this.object.A_parent
+	cat := &ShopCategory{wrap: this.wrap, object: this.bufferCats[this.object.A_parent]}
+	return cat.load(&this.bufferCats)
 }

+ 1 - 1
engine/fetdata/shop_product.go

@@ -179,7 +179,7 @@ func (this *ShopProduct) Category() *ShopCategory {
 	if this.category != nil {
 		return this.category
 	}
-	this.category = (&ShopCategory{wrap: this.wrap}).load()
+	this.category = (&ShopCategory{wrap: this.wrap}).load(nil)
 	this.category.loadById(this.object.A_category)
 	return this.category
 }

+ 30 - 0
hosts/localhost/template/header.html

@@ -139,10 +139,40 @@
 									<li class="breadcrumb-item"><a href="/shop/">Shop</a></li>
 								{{end}}
 								{{if eq $.Data.Module "shop-category"}}
+									{{if $.Data.Shop.Category.Parent.Parent.Parent.Parent.Parent}}
+										<li class="breadcrumb-item"><a href="{{$.Data.Shop.Category.Parent.Parent.Parent.Parent.Parent.Permalink}}">{{$.Data.Shop.Category.Parent.Parent.Parent.Parent.Parent.Name}}</a></li>
+									{{end}}
+									{{if $.Data.Shop.Category.Parent.Parent.Parent.Parent}}
+										<li class="breadcrumb-item"><a href="{{$.Data.Shop.Category.Parent.Parent.Parent.Parent.Permalink}}">{{$.Data.Shop.Category.Parent.Parent.Parent.Parent.Name}}</a></li>
+									{{end}}
+									{{if $.Data.Shop.Category.Parent.Parent.Parent}}
+										<li class="breadcrumb-item"><a href="{{$.Data.Shop.Category.Parent.Parent.Parent.Permalink}}">{{$.Data.Shop.Category.Parent.Parent.Parent.Name}}</a></li>
+									{{end}}
+									{{if $.Data.Shop.Category.Parent.Parent}}
+										<li class="breadcrumb-item"><a href="{{$.Data.Shop.Category.Parent.Parent.Permalink}}">{{$.Data.Shop.Category.Parent.Parent.Name}}</a></li>
+									{{end}}
+									{{if $.Data.Shop.Category.Parent}}
+										<li class="breadcrumb-item"><a href="{{$.Data.Shop.Category.Parent.Permalink}}">{{$.Data.Shop.Category.Parent.Name}}</a></li>
+									{{end}}
 									<li class="breadcrumb-item">{{$.Data.Shop.Category.Name}}</li>
 								{{end}}
 								{{if eq $.Data.Module "shop-product"}}
 									{{if $.Data.Shop.Product.Category.Id}}
+										{{if $.Data.Shop.Product.Category.Parent.Parent.Parent.Parent.Parent}}
+											<li class="breadcrumb-item"><a href="{{$.Data.Shop.Product.Category.Parent.Parent.Parent.Parent.Parent.Permalink}}">{{$.Data.Shop.Product.Category.Parent.Parent.Parent.Parent.Parent.Name}}</a></li>
+										{{end}}
+										{{if $.Data.Shop.Product.Category.Parent.Parent.Parent.Parent}}
+											<li class="breadcrumb-item"><a href="{{$.Data.Shop.Product.Category.Parent.Parent.Parent.Parent.Permalink}}">{{$.Data.Shop.Product.Category.Parent.Parent.Parent.Parent.Name}}</a></li>
+										{{end}}
+										{{if $.Data.Shop.Product.Category.Parent.Parent.Parent}}
+											<li class="breadcrumb-item"><a href="{{$.Data.Shop.Product.Category.Parent.Parent.Parent.Permalink}}">{{$.Data.Shop.Product.Category.Parent.Parent.Parent.Name}}</a></li>
+										{{end}}
+										{{if $.Data.Shop.Product.Category.Parent.Parent}}
+											<li class="breadcrumb-item"><a href="{{$.Data.Shop.Product.Category.Parent.Parent.Permalink}}">{{$.Data.Shop.Product.Category.Parent.Parent.Name}}</a></li>
+										{{end}}
+										{{if $.Data.Shop.Product.Category.Parent}}
+											<li class="breadcrumb-item"><a href="{{$.Data.Shop.Product.Category.Parent.Permalink}}">{{$.Data.Shop.Product.Category.Parent.Name}}</a></li>
+										{{end}}
 										<li class="breadcrumb-item"><a href="{{$.Data.Shop.Product.Category.Permalink}}">{{$.Data.Shop.Product.Category.Name}}</a></li>
 									{{end}}
 									<li class="breadcrumb-item active">{{$.Data.Shop.Product.Name}}</li>