From: Steve Sutton Date: Thu, 17 Nov 2016 20:26:34 +0000 (-0500) Subject: Adding back in the categories for coupons. X-Git-Tag: v1.0.0^2~26 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=5f728c3cd843286cd6557b80a6fa945c848d43da;p=WP-Plugins%2Fglm-member-db-coupons.git Adding back in the categories for coupons. These will be just like the categories for the events. In that they can add more than one category per coupon. --- diff --git a/classes/data/dataCoupons.php b/classes/data/dataCoupons.php index b7637cf..f56ca16 100644 --- a/classes/data/dataCoupons.php +++ b/classes/data/dataCoupons.php @@ -85,6 +85,7 @@ class GlmDataCoupons extends GlmDataAbstract * @var bool * @access public */ + public $postCategories = true; /** * Constructor @@ -275,6 +276,17 @@ class GlmDataCoupons extends GlmDataAbstract */ public function entryPostProcessing($r, $a) { + + // Get Member Category data for this entry + if ($this->postCategories) { + $sql = " + SELECT CC.coupon AS coupon_id, C.id, C.name + FROM ".GLM_MEMBERS_COUPONS_PLUGIN_DB_PREFIX. "categories AS C, + ".GLM_MEMBERS_COUPONS_PLUGIN_DB_PREFIX. "coupon_categories AS CC + WHERE C.id = CC.category + AND CC.coupon = ".$r['id']; + $r['categories'] = $this->wpdb->get_results($sql, ARRAY_A); + } return $r; } @@ -425,6 +437,9 @@ class GlmDataCoupons extends GlmDataAbstract public function getIdName($where = 'true') { $savedFields = $this->fields; + $savedCategories = $this->postCategories; + + $this->postCategories = false; $this->fields = array( 'id' => $savedFields['id'], @@ -434,6 +449,7 @@ class GlmDataCoupons extends GlmDataAbstract $r = $this->getList($where); $this->fields = $savedFields; + $this->postCategories = $savedCategories; return $r; diff --git a/classes/data/dataCouponsCouponCategories.php b/classes/data/dataCouponsCouponCategories.php new file mode 100644 index 0000000..15d1620 --- /dev/null +++ b/classes/data/dataCouponsCouponCategories.php @@ -0,0 +1,352 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @release SVN: $Id: dataCoupons.php,v 1.0 2011/01/25 19:31:47 cscott Exp $ + */ + +/** + * GlmDataCoupon class + * + * PHP version 5 + * + * @category Data + * @package GLM Member DB + * @author Chuck Scott + * @license http://www.gaslightmedia.com Gaslightmedia + * @release SVN: $Id: dataMembers.php,v 1.0 2011/01/25 19:31:47 cscott + * Exp $ + */ +class GlmDataCouponsCouponCategories extends GlmDataAbstract +{ + + /** + * WordPress Database Object + * + * @var $wpdb + * @access public + */ + public $wpdb; + /** + * Plugin Configuration Data + * + * @var $config + * @access public + */ + public $config; + /** + * Data Table Name + * + * @var $table + * @access public + */ + public $table; + /** + * Field definitions + * + * 'type' is type of field as defined by the application + * text Regular text field + * pointer Pointer to an entry in another table + * 'filters' is the filter name for a particular filter ID in PHP filter + * functions + * See PHP filter_id() + * + * 'use' is when to use the field + * l = List + * g = Get + * n = New + * i = Insert + * e = Edit + * u = Update + * d = Delete + * a = All + * + * @var $ini + * @access public + */ + public $fields = false; + /** + * MemberInfo DB object + * + * @var $MemberInfo + * @access public + */ + public $MemberInfo; + + /** + * Constructor + * + * @param object $d database connection + * @param array $config Configuration array + * @param bool $limitedEdit Flag to say indicate limited edit requested + * + * @return void + * @access public + */ + public function __construct($wpdb, $config, $limitedEdit = false) + { + + // If this class is not being extended along with existing $wpdb and $config + if (!$this->wpdb) { + + // Save WordPress Database object + $this->wpdb = $wpdb; + + // Save plugin configuration object + $this->config = $config; + + } + + /* + * Table Name + */ + $this->table = GLM_MEMBERS_COUPONS_PLUGIN_DB_PREFIX . 'coupon_categories'; + + /* + * Table Data Fields + */ + + $this->fields = array ( + + 'id' => array ( + 'field' => 'id', + 'type' => 'integer', + 'view_only' => true, + 'use' => 'a' + ), + + // Coupon ID + 'coupon' => array( + 'field' => 'coupon', + 'type' => 'integer', + 'required' => true, + 'use' => 'a' + ), + + // Category ID + 'category' => array( + 'field' => 'category', + 'type' => 'integer', + 'required' => true, + 'use' => 'a' + ), + + // Category Name - for easy reference + 'category_name' => array( + 'field' => 'category', + 'as' => 'category_name', + 'type' => 'pointer', + 'p_table' => GLM_MEMBERS_COUPONS_PLUGIN_DB_PREFIX . 'categories', + 'p_field' => 'name', + 'p_orderby' => 'name', + 'use' => 'gl' + ) + + + ); + + } + + /* + * Entry Post Processing Call-Back Method + * + * Perform post-processing for all result entries. + * + * In this case we're using it to append an array of category + * data to each member result and also sort by member name. + * + * @param array $r Array of field result data for a single entry + * @param string $a Action being performed (l, i, g, ...) + * + * @return object Class object + * + */ + public function entryPostProcessing($r, $a) + { + return $r; + } + + /* + * Get list of categories with parent information + * + * @param integer $couponID Optional coupon ID to match + * Used to find all categories for a particular coupon ID + * + * @param integer $category Optional category ID to match + * Used to find all coupon records for a particular category + * + * NOTE: Both parameters above can't be used at the same time + * + * @return array List of selected categories + * + * @access public + */ + + public function getListWithParents($couponID = false, $category = false) + { + + $where = ''; + + if ($category) { + $where = "WHERE T.category = $category"; + } elseif ($ventID) { + $where = "WHERE T.coupon = $couponID"; + } + + $sql = " + SELECT T.category, T.coupon, + ( + SELECT name + FROM ".GLM_MEMBERS_COUPONS_PLUGIN_DB_PREFIX."categories + WHERE id = T.category + ) AS category_name, + ( + SELECT parent + FROM ".GLM_MEMBERS_COUPONS_PLUGIN_DB_PREFIX."categories + WHERE id = T.category + ) AS category_parent, + COALESCE ( + ( + SELECT name + FROM ".GLM_MEMBERS_COUPONS_PLUGIN_DB_PREFIX."categories + WHERE id = category_parent + ), + '' + ) AS parent_name + FROM ".GLM_MEMBERS_COUPONS_PLUGIN_DB_PREFIX."coupon_categories T + $where + ;"; + $list = $this->wpdb->get_results($sql, ARRAY_A); + + if (is_admin() && GLM_MEMBERS_PLUGIN_ADMIN_DEBUG_VERBOSE) { + glmMembersAdmin::addNotice($sql, 'DataBlock', "DataAbstract - getListWithParents() query"); + glmMembersAdmin::addNotice($list, 'DataBlock', 'getListWithParents() data'); + } + + return $list; + + } + + /** + * Use supplied category ID list to set selected categories for this + * coupon record. Any others that were set will be cleared. + * + * @param integer $couponID Coupon Record ID + * @param array $selectedCategories Array of selected category IDs (key is also category ID) + * + * @return array List of selected categories + * + * @access public + */ + + public function setCouponCategories($couponID, $selectedCategories) + { + + // Check supplied data + if (!is_int($couponID) || $couponID <=0 || !is_array($selectedCategories) || count($selectedCategories) == 0 ) { + return false; + } + + // Get current list + $current = $this->getList("T.coupon = $couponID"); + + // If we have any currently selected categories + if (is_array($current) && count($current) > 0) { + + // For each category in the list + foreach ($current as $key => $val) { + + $current[$key]['selected'] = false; + + // Delete existing ones from the supplied selection list and mark selected ones in the current list + if (isset($selectedCategories[$val['id']])) { + + unset($selectedCategories[$val['id']]); + + $current[$key]['selected'] = true; + + } + + } + + } + + // For each remaining selected category, add the category + foreach ($selectedCategories as $s) { + + // Add the category + $sql = " + INSERT INTO ".GLM_MEMBERS_COUPONS_PLUGIN_DB_PREFIX."coupon_categories + ( category, coupon ) + VALUES + ( $s, $couponID ) + ;"; + $this->wpdb->query($sql); + + + } + + // For any existing categories listed that aren't marked as selected, remove them + if (is_array($current) && count($current) > 0) { + foreach ($current as $key => $val) { + + if (!$val['selected']) { + + // Delete the entry + // Add the category + $sql = " + DELETE FROM ".GLM_MEMBERS_COUPONS_PLUGIN_DB_PREFIX."coupon_categories + WHERE coupon = $couponID + AND id = ".$val['id']." + ;"; + $this->wpdb->query($sql); + + } + } + } + + // Get new list and return it + $current = $this->getList($couponID); + + if (is_admin() && GLM_MEMBERS_PLUGIN_ADMIN_DEBUG_VERBOSE) { + glmMembersAdmin::addNotice($current, 'DataBlock', 'Currently Selected Coupon Categories'); + } + + return $current; + } + + /** + * Clear all categories for a specific coupon record + * + * @param integer $couponID Coupon Record ID + * + * @return null + * + * @access public + */ + + public function clearCouponCategories($couponID) + { + + $sql = " + DELETE FROM ".GLM_MEMBERS_COUPONS_PLUGIN_DB_PREFIX."coupon_categories + WHERE coupon = $couponID + ;"; + $this->wpdb->query($sql); + + // Returns false to indicate there are no categories selected + return false; + + } + + +} + +?> diff --git a/models/admin/coupons/list.php b/models/admin/coupons/list.php index 8d3ba50..8cc2033 100644 --- a/models/admin/coupons/list.php +++ b/models/admin/coupons/list.php @@ -18,6 +18,7 @@ require_once GLM_MEMBERS_COUPONS_PLUGIN_CLASS_PATH.'/data/dataCoupons.php'; // Load coupon categories data class require_once GLM_MEMBERS_COUPONS_PLUGIN_CLASS_PATH.'/data/dataCouponCategories.php'; +require_once GLM_MEMBERS_COUPONS_PLUGIN_CLASS_PATH.'/data/dataCouponsCouponCategories.php'; class GlmMembersAdmin_coupons_list extends GlmDataCoupons { @@ -91,9 +92,9 @@ class GlmMembersAdmin_coupons_list extends GlmDataCoupons public function modelAction($actionData = false) { - $lockedToMember = false; + $lockedToMember = false; $numbCoupons = 0; - $option = 'list'; + $option = 'list'; $coupons = false; $haveCoupons = false; $coupon = false; @@ -103,36 +104,36 @@ class GlmMembersAdmin_coupons_list extends GlmDataCoupons $couponUpdateError = false; $couponAdded = false; $couponAddError = false; - $view = 'list'; - $fromDate = false; - $toDate = false; - $filterArchived = false; - $filterPending = false; + $view = 'list'; + $fromDate = false; + $toDate = false; + $filterArchived = false; + $filterPending = false; $couponDeleted = false; $couponDeleteError = false; - $locations = false; - $haveLocations = false; - $numbLocations = 0; - $recurrences = false; - $haveRecurrences = false; - $numbRecurrences = 0; - $times = false; - $haveTimes = false; - $firstTime = false; - $lastTime = false; - $memberID = false; - $haveMember = false; - $memberName = false; - $memberData = false; - $numbDisplayed = false; - $lastDisplayed = false; - $paging = true; - $prevStart = false; - $nextStart = false; - $start = 1; - $limit = 20; // Set to the number of listings per page - $namesList = false; - $enable_members = $this->config['settings']['enable_members']; + $locations = false; + $haveLocations = false; + $numbLocations = 0; + $recurrences = false; + $haveRecurrences = false; + $numbRecurrences = 0; + $times = false; + $haveTimes = false; + $firstTime = false; + $lastTime = false; + $memberID = false; + $haveMember = false; + $memberName = false; + $memberData = false; + $numbDisplayed = false; + $lastDisplayed = false; + $paging = true; + $prevStart = false; + $nextStart = false; + $start = 1; + $limit = 20; // Set to the number of listings per page + $namesList = false; + $enable_members = $this->config['settings']['enable_members']; // Check if there's a logged in user who is locked to their own entity $lockedToMember = apply_filters('glm_members_locked_to_member_id', false); @@ -214,19 +215,13 @@ class GlmMembersAdmin_coupons_list extends GlmDataCoupons switch ($option) { case 'add': - $coupon = $this->newEntry(); - $view = 'edit'; - break; case 'insert': - $coupon = $this->insertEntry(); - $this->couponID = $coupon['fieldData']['id']; - $this->updateCategories(); $categories = $Categories->getList(false); @@ -254,7 +249,6 @@ class GlmMembersAdmin_coupons_list extends GlmDataCoupons break; case 'edit': - $coupon = $this->editEntry($this->couponID); if ($coupon['status']) { @@ -265,7 +259,6 @@ class GlmMembersAdmin_coupons_list extends GlmDataCoupons break; case 'update': - // Get the original Coupon Status. Before the update. $old_coupon_status = $this->wpdb->get_var( $this->wpdb->prepare( @@ -277,12 +270,7 @@ class GlmMembersAdmin_coupons_list extends GlmDataCoupons ); $this->updateCategories(); - $categories = $Categories->getListSortedParentChild(false); - - if ( $this->config['settings']['use_coupon_amenities'] ) { - $this->updateAmenities(); - $amenities = $Amenities->getList(false); - } + $categories = $Categories->getList(false); // Try to update this coupon $coupon = $this->updateEntry($this->couponID); @@ -315,7 +303,6 @@ class GlmMembersAdmin_coupons_list extends GlmDataCoupons break; case 'delete': - $coupon = $this->deleteCoupon($this->couponID); if ($coupon) { @@ -326,7 +313,6 @@ class GlmMembersAdmin_coupons_list extends GlmDataCoupons case 'list': default: - $where = 'true'; // Check for Archived filter @@ -361,12 +347,11 @@ class GlmMembersAdmin_coupons_list extends GlmDataCoupons } $where .= " AND id in ( - SELECT DISTINCT(EC.coupon) - FROM ".GLM_MEMBERS_COUPONS_PLUGIN_DB_PREFIX."categories EC, + SELECT DISTINCT(CC.coupon) + FROM ".GLM_MEMBERS_COUPONS_PLUGIN_DB_PREFIX."coupon_categories CC, ".GLM_MEMBERS_COUPONS_PLUGIN_DB_PREFIX."categories C WHERE ( - EC.category in ($cats) - OR (C.parent in ($cats) AND EC.category = C.id) + CC.category in ($cats) ) )"; } @@ -383,7 +368,7 @@ class GlmMembersAdmin_coupons_list extends GlmDataCoupons if (isset($_REQUEST['fromDate']) && trim($_REQUEST['fromDate']) != '') { $fromDate = date('m/d/Y', strtotime($_REQUEST['fromDate'])); $fromMYSQL = date('Y-m-d', strtotime($fromDate)); - $dateWhere = " end_time >= '$fromMYSQL' "; + $dateWhere = " end_date >= '$fromMYSQL' "; } // If we have a to Date @@ -394,23 +379,18 @@ class GlmMembersAdmin_coupons_list extends GlmDataCoupons // If we have a from date then we need Parens and AND if ($dateWhere != '') { - $dateWhere = "( ".$dateWhere." AND start_time <= '$toMYSQL' )"; + $dateWhere = "( ".$dateWhere." AND start_date <= '$toMYSQL' )"; // Otherwise we don't } else { - $dateWhere = " start_time <= '$toMYSQL' "; + $dateWhere = " start_date <= '$toMYSQL' "; } } // If we have from and to dates, do search for those inclusive if ($dateWhere != '') { $where .= " - AND id in ( - SELECT DISTINCT(coupon) - FROM ".GLM_MEMBERS_COUPONS_PLUGIN_DB_PREFIX."times - WHERE $dateWhere - AND active - ) + AND $dateWhere "; } @@ -487,23 +467,23 @@ class GlmMembersAdmin_coupons_list extends GlmDataCoupons 'enable_members' => $enable_members, 'lockedToMember' => $lockedToMember, 'option' => $option, - 'coupons' => $coupons, - 'haveCoupons' => $haveCoupons, - 'coupon' => $coupon, - 'haveCoupon' => $haveCoupon, - 'couponID' => $this->couponID, - 'couponUpdated' => $couponUpdated, - 'couponUpdateError' => $couponUpdateError, - 'couponAdded' => $couponAdded, - 'couponAddError' => $couponAddError, - 'numbCoupons' => $numbCoupons, + 'coupons' => $coupons, + 'haveCoupons' => $haveCoupons, + 'coupon' => $coupon, + 'haveCoupon' => $haveCoupon, + 'couponID' => $this->couponID, + 'couponUpdated' => $couponUpdated, + 'couponUpdateError' => $couponUpdateError, + 'couponAdded' => $couponAdded, + 'couponAddError' => $couponAddError, + 'numbCoupons' => $numbCoupons, 'categories' => $categories, 'fromDate' => $fromDate, 'toDate' => $toDate, 'filterArchived' => $filterArchived, 'filterPending' => $filterPending, - 'couponDeleted' => $couponDeleted, - 'couponDeleteError' => $couponDeleteError, + 'couponDeleted' => $couponDeleted, + 'couponDeleteError' => $couponDeleteError, 'locations' => $locations, 'haveLocations' => $haveLocations, 'numbLocations' => $numbLocations, @@ -541,7 +521,7 @@ class GlmMembersAdmin_coupons_list extends GlmDataCoupons { // Instatiate the dataCategories class - $Categories = new GlmDataCouponsCategories($this->wpdb, $this->config); + $Categories = new GlmDataCouponCategories($this->wpdb, $this->config); // Instatiate Coupon/Categories data class $CouponCategories = new GlmDataCouponsCouponCategories($this->wpdb, $this->config); diff --git a/setup/adminMenus.php b/setup/adminMenus.php index 72584ce..b643093 100644 --- a/setup/adminMenus.php +++ b/setup/adminMenus.php @@ -50,10 +50,18 @@ * */ add_submenu_page( - 'glm-members-admin-menu-members', - 'Coupons', - 'Coupons', - 'glm_members_members', - 'glm-members-admin-menu-coupons-index', + 'glm-members-admin-menu-members', // Parent slug + 'Coupons', // Page Title + 'Coupons', // Menu Title + 'glm_members_members', // Capability required + 'glm-members-admin-menu-coupons-index', // Menu slug function(){$this->controller('coupons');} ); +add_submenu_page( + 'glm-members-admin-menu-members', // Parent slug + 'Coupon List', // Page title + '    List', // Menu Title + 'glm_members_members', // Capability required + 'glm-members-admin-menu-coupons-list', // Menu slug + function() {$this->controller('coupons', 'list');} +); diff --git a/views/admin/coupons/edit.html b/views/admin/coupons/edit.html index bf863e0..83d96de 100644 --- a/views/admin/coupons/edit.html +++ b/views/admin/coupons/edit.html @@ -6,6 +6,7 @@ {if apply_filters('glm_members_permit_admin_member_coupon', true)} + {if $haveMember} Return to Coupons List {else} @@ -75,6 +76,33 @@ {/if} + {if $enable_members} + {if $coupon.fieldData.ref_dest.list|@count > 1} + + {$terms.term_member_cap}: + + {if $haveMember} + + {$memberData.name} + {else} + + {/if} + + + {else} + + {/if} + {else} + {if $coupon.fieldData.ref_dest.value} + + {else} + + {/if} + {/if} Coupon Name: @@ -82,6 +110,56 @@ {if $coupon.fieldFail.name}

{$coupon.fieldFail.name}

{/if}
+ + Categories + + + + +
Add a new Category
+
+ + + + + +
Category Name: + +
+
+

* Required

+ Cancel + +
+ + + +    Select a category to add to box below.
+
+ + {if isset($coupon.fieldData.categories) && $coupon.fieldData.categories} + {foreach $coupon.fieldData.categories as $c} +
+ {$c.name} + X + +
+ {/foreach} + {/if} +
+ + Starting Date: @@ -242,7 +320,6 @@ // Get the ID, name, and parent of the category var catValue = $('#categorySelect').val(); var catName = $('#categorySelect').find(':selected').text(); - var catParent = $('#categorySelect').find(':selected').attr('data-parent'); // Check if the category has already been added var found = false; @@ -255,9 +332,6 @@ // Check if there's a parent parentName = ''; - if (catParent != '') { - parentName = catParent + ': '; - } // If not found, Add the category if (!found) { @@ -304,7 +378,6 @@ // Get new category information var newCatName = $('#newCatName').val(); var newCatParent = $('#newCatParent').val(); - var catParent = $('#newCatParent').find(':selected').attr('data-parent'); var newCatParentName = $('#newCatParentName').val(); // If there's no name, tell the user we need one. @@ -318,8 +391,6 @@ parentName = ''; if (newCatParentName && newCatParentName != '') { parentName = newCatParentName + ': '; - } else if (catParent && catParent != '') { - parentName = catParent + ': '; } // Add the new category to the active categories list diff --git a/views/admin/coupons/list.html b/views/admin/coupons/list.html index b270301..d489868 100644 --- a/views/admin/coupons/list.html +++ b/views/admin/coupons/list.html @@ -64,9 +64,9 @@ Coupon Name {$terms.term_member_cap} Status - First Date - Last Date - + Start Date + End Date + Expire Date @@ -99,13 +99,13 @@ {$e.status.name} - {$e.firstDate} + {$e.start_date.date} - {$e.lastDate} + {$e.end_date.date} - View Coupon + {$e.expire.date} {/foreach}