New category working for edit coupon.
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 18 Nov 2016 16:10:46 +0000 (11:10 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 18 Nov 2016 16:10:46 +0000 (11:10 -0500)
Also the member contact does not see the add new category button.

classes/data/dataCouponCategories.php
models/admin/coupons/list.php
views/admin/coupons/edit.html

index d34b74a..72c0859 100644 (file)
@@ -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;
+
+    }
+
 }
index 8cc2033..276dc33 100644 (file)
@@ -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();
 
                 }
 
index 83d96de..7f91906 100644 (file)
@@ -27,6 +27,7 @@
     {if $lockedToMember}
     <form action="{$thisUrl}?page=glm-members-admin-menu-coupons-list" method="post" enctype="multipart/form-data">
         <input type="hidden" name="glm_action" value="list">
+        <input type="hidden" name="status" value="20">
     {else}
     <form action="{$thisUrl}?page={$thisPage}" method="post" enctype="multipart/form-data">
         <input type="hidden" name="glm_action" value="list">
 
                 <!--  Add new category dialog -->
 
-                <div id="newCategoryButton" class="button button-secondary glm-right">Add a new Category</div>
+                {if !$lockedToMember}
+                    <div id="newCategoryButton" class="button button-secondary glm-right">Add a new Category</div>
+                {/if}
                 <div id="newCategoryDialog" class="glm-dialog-box" title="Enter a New Category">
                         <table class="glm-admin-table">
                             <tr>