Browse Source

Shop product images reorder action

Vova Tkach 5 years ago
parent
commit
d3a6bdd5fd
2 changed files with 53 additions and 1 deletions
  1. 1 1
      modules/module_shop.go
  2. 52 0
      modules/module_shop_act_images_reorder.go

+ 1 - 1
modules/module_shop.go

@@ -1055,7 +1055,7 @@ func (this *Modules) RegisterModule_Shop() *Module {
 							`</div>` +
 							`</div>` +
 							`</div>` +
 							`</div>` +
 							`</div>` +
 							`</div>` +
-							`<script>WaitForFave(function(){Sortable.create(document.getElementById('list-images'),{animation:0,onEnd:function(evt){var orderData={};$('#list-images div.attached-img').each(function(i,v){orderData[$(v).data('id')]=i;});fave.ShopProductsImageReorder('shop-images-reorder',orderData);},});});</script>`
+							`<script>WaitForFave(function(){Sortable.create(document.getElementById('list-images'),{animation:0,onEnd:function(evt){var orderData=[];$('#list-images div.attached-img').each(function(i,v){orderData.push({Id:$(v).data('id'),Order:i});});fave.ShopProductsImageReorder('shop-images-reorder',{Items:orderData});},});});</script>`
 					},
 					},
 				},
 				},
 				{
 				{

+ 52 - 0
modules/module_shop_act_images_reorder.go

@@ -0,0 +1,52 @@
+package modules
+
+import (
+	"encoding/json"
+
+	"golang-fave/engine/wrapper"
+)
+
+type OrderItem struct {
+	Id    int
+	Order int
+}
+
+type Orders struct {
+	Items []OrderItem
+}
+
+func (this *Modules) RegisterAction_ShopImagesReorder() *Action {
+	return this.newAction(AInfo{
+		WantDB:    true,
+		Mount:     "shop-images-reorder",
+		WantAdmin: true,
+	}, func(wrap *wrapper.Wrapper) {
+		pf_data := wrap.R.FormValue("data")
+
+		var orders Orders
+		if err := json.Unmarshal([]byte(pf_data), &orders); err != nil {
+			wrap.MsgError(err.Error())
+			return
+		}
+
+		if len(orders.Items) > 0 {
+			if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+				for _, value := range orders.Items {
+					if _, err := tx.Exec("UPDATE shop_product_images SET ord = ? WHERE id = ?;", value.Order, value.Id); err != nil {
+						return err
+					}
+				}
+				return nil
+			}); err != nil {
+				wrap.MsgError(err.Error())
+				return
+			}
+		}
+
+		wrap.RecreateProductImgFiles()
+
+		wrap.RecreateProductXmlFile()
+
+		wrap.ResetCacheBlocks()
+	})
+}