Browse Source

Shop, product detach action

Vova Tkach 5 years ago
parent
commit
4ff9f3bc2c
1 changed files with 45 additions and 0 deletions
  1. 45 0
      modules/module_shop_act_detach.go

+ 45 - 0
modules/module_shop_act_detach.go

@@ -0,0 +1,45 @@
+package modules
+
+import (
+	"golang-fave/engine/wrapper"
+	"golang-fave/utils"
+)
+
+func (this *Modules) RegisterAction_ShopDetach() *Action {
+	return this.newAction(AInfo{
+		WantDB:    true,
+		Mount:     "shop-detach",
+		WantAdmin: true,
+	}, func(wrap *wrapper.Wrapper) {
+		pf_id := wrap.R.FormValue("id")
+
+		if !utils.IsNumeric(pf_id) {
+			wrap.MsgError(`Inner system error`)
+			return
+		}
+
+		if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+			if _, err := tx.Exec(`
+				UPDATE shop_products SET
+					parent_id = NULL
+				WHERE
+					id = ?;
+				`,
+				utils.StrToInt(pf_id),
+			); err != nil {
+				return err
+			}
+			return nil
+		}); err != nil {
+			wrap.MsgError(err.Error())
+			return
+		}
+
+		wrap.RecreateProductXmlFile()
+
+		wrap.ResetCacheBlocks()
+
+		// Reload current page
+		wrap.Write(`window.location.reload(false);`)
+	})
+}