From 365f7378e9ba9e5e6be6f85f0312c90529b18a6b Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Fri, 18 Nov 2016 11:10:46 -0500 Subject: [PATCH] New category working for edit coupon. Also the member contact does not see the add new category button. --- classes/data/dataCouponCategories.php | 53 +++++++++++++++++++++++++++ models/admin/coupons/list.php | 43 ++++++++++------------ views/admin/coupons/edit.html | 5 ++- 3 files changed, 77 insertions(+), 24 deletions(-) diff --git a/classes/data/dataCouponCategories.php b/classes/data/dataCouponCategories.php index d34b74a..72c0859 100644 --- a/classes/data/dataCouponCategories.php +++ b/classes/data/dataCouponCategories.php @@ -153,4 +153,57 @@ class GlmDataCouponCategories extends GlmDataAbstract return $r; } + /** + * Add a category using supplied parameters + * + * @param string $name Category Name + * + * @return integer ID of new category or false if failed + * + * @access public + */ + + public function addCategory($name) { + + $categoryID = false; + + // Filter new category name + $name = filter_var($name); + if ($name == false || trim($name) == '') { + return false; + } + + // Check if the new category name already exists + $sql = " + SELECT id + FROM ".GLM_MEMBERS_COUPONS_PLUGIN_DB_PREFIX."categories + WHERE name = '$name'"; + $existing = $this->wpdb->get_row($sql, ARRAY_A); + + if ($existing['id']) { + + $categoryID = $existing['id']; + + } else { + + // Try to add the new category + $sql = " + INSERT INTO ".GLM_MEMBERS_COUPONS_PLUGIN_DB_PREFIX."categories + ( name ) + VALUES + ( '".addslashes($name)."' ) + ;"; + $this->wpdb->query($sql); + $categoryID = $this->wpdb->insert_id; + + } + + if (is_admin() && GLM_MEMBERS_PLUGIN_ADMIN_DEBUG_VERBOSE) { + glmMembersAdmin::addNotice("Category added: name = $name, ID = $categoryID", 'DataBlock', "addCategory()"); + } + + return $categoryID; + + } + } diff --git a/models/admin/coupons/list.php b/models/admin/coupons/list.php index 8cc2033..276dc33 100644 --- a/models/admin/coupons/list.php +++ b/models/admin/coupons/list.php @@ -221,24 +221,29 @@ class GlmMembersAdmin_coupons_list extends GlmDataCoupons case 'insert': $coupon = $this->insertEntry(); - $this->couponID = $coupon['fieldData']['id']; - $this->updateCategories(); - $categories = $Categories->getList(false); + if ( $coupon ) { + $this->couponID = $coupon['fieldData']['id']; + $this->updateCategories(); + $categories = $Categories->getList(false); - if ($coupon['status']) { - $haveCoupon = true; - $couponAdded = true; + if ($coupon['status']) { + $haveCoupon = true; + $couponAdded = true; - // Update created timestamp and name slug for URLs - $this->updateTimestamp('created', $this->couponID); - $this->updateSlug($this->couponID); + // Update created timestamp and name slug for URLs + $this->updateTimestamp('created', $this->couponID); + $this->updateSlug($this->couponID); - // Get this again so we have the created date - $coupon = $this->editEntry($this->couponID); + // Get this again so we have the created date + $coupon = $this->editEntry($this->couponID); - $option = 'edit'; - $couponAdded = true; + $option = 'edit'; + $couponAdded = true; + } else { + $option = 'add'; + $couponAddError = true; + } } else { $option = 'add'; $couponAddError = true; @@ -549,19 +554,11 @@ class GlmMembersAdmin_coupons_list extends GlmDataCoupons $newCategory = true; - // Check if a parent is specified - $parent = 0; - if ($_REQUEST['newCatParent'][$key] != '') { - $parent = $_REQUEST['newCatParent'][$key] -0; - } elseif ($_REQUEST['newCatParentName'][$key]) { - $parent = $_REQUEST['newCatParentName'][$key]; - } - // Clean up the category name $category = filter_var($_REQUEST['newCategory'][$key]); // Add it to the category table and get the new category ID - $categoryID = $Categories->addCategory($category, $parent); + $categoryID = $Categories->addCategory($category); // If we got a new category ID, add it to the array of currently selected couponCategory records if ($categoryID) { @@ -587,7 +584,7 @@ class GlmMembersAdmin_coupons_list extends GlmDataCoupons if ($newCategory) { // Get the full category list again - $this->categories = $Categories->getListSortedParentChild(); + $this->categories = $Categories->getList(); } diff --git a/views/admin/coupons/edit.html b/views/admin/coupons/edit.html index 83d96de..7f91906 100644 --- a/views/admin/coupons/edit.html +++ b/views/admin/coupons/edit.html @@ -27,6 +27,7 @@ {if $lockedToMember}
+ {else} @@ -116,7 +117,9 @@ -
Add a new Category
+ {if !$lockedToMember} +
Add a new Category
+ {/if}
-- 2.17.1