From 0ee78b54ea5702239b77e5623c4e679ed6eb8aa6 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Thu, 17 Nov 2016 10:05:15 -0500 Subject: [PATCH] Adding models views and data classes. Adding the data class for CouponCategories and dataCoupons. Adding view files for Category settings. Adding models for Setting Categories. --- classes/data/dataCouponCategories.php | 156 +++++++ classes/data/dataCoupons.php | 464 +++++++++++++++++++++ models/admin/coupons/index.php | 9 +- models/admin/settings/couponCategories.php | 200 +++++++++ setup/adminMenus.php | 2 +- setup/validActions.php | 43 +- views/admin/coupons/header.html | 9 + views/admin/coupons/index.html | 20 +- views/admin/settings/couponCategories.html | 191 +++++++++ 9 files changed, 1050 insertions(+), 44 deletions(-) create mode 100644 classes/data/dataCouponCategories.php create mode 100644 classes/data/dataCoupons.php create mode 100644 models/admin/settings/couponCategories.php create mode 100644 views/admin/coupons/header.html create mode 100644 views/admin/settings/couponCategories.html diff --git a/classes/data/dataCouponCategories.php b/classes/data/dataCouponCategories.php new file mode 100644 index 0000000..d34b74a --- /dev/null +++ b/classes/data/dataCouponCategories.php @@ -0,0 +1,156 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @release SVN: $Id: dataCoupons.php,v 1.0 2011/01/25 19:31:47 cscott Exp $ + */ + +/** + * GlmDataCouponsAmenities 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 GlmDataCouponCategories 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 . 'categories'; + + /* + * Table Data Fields + */ + $this->fields = array( + + 'id' => array( + 'field' => 'id', + 'type' => 'integer', + 'view_only' => true, + 'use' => 'a', + ), + + // Name + 'name' => array( + 'field' => 'name', + 'type' => 'text', + 'required' => true, + 'unique' => true, + 'use' => 'a', + ), + + ); + + } + + /** + * 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 amenity + * 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; + } + +} diff --git a/classes/data/dataCoupons.php b/classes/data/dataCoupons.php new file mode 100644 index 0000000..2cf4d77 --- /dev/null +++ b/classes/data/dataCoupons.php @@ -0,0 +1,464 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @release SVN: $Id: dataCoupons.php,v 1.0 2011/01/25 19:31:47 cscott Exp $ + */ + +/** + * GlmDataCoupons 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 GlmDataCoupons 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; + /** + * postAddTimes + * + * @var bool + * @access public + */ + public $postCategories = true; + + /** + * 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 . 'coupons'; + + /* + * Table Data Fields + */ + + $this->fields = array ( + + 'id' => array ( + 'field' => 'id', + 'type' => 'integer', + 'view_only' => true, + 'use' => 'a' + ), + + // Status + 'status' => array ( + 'field' => 'status', + 'type' => 'list', + 'list' => $this->config['status'], + 'required' => true, + 'default' => $this->config['status_numb']['Pending'], + 'use' => 'a' + ), + + // Date/Time Created + 'created' => array ( + 'field' => 'created', + 'type' => 'datetime', + 'use' => 'lgieu' + ), + + // Date/Time Updated + 'updated' => array ( + 'field' => 'updated', + 'type' => 'datetime', + 'use' => 'a' + ), + + // Date/Time Approved + 'approved' => array ( + 'field' => 'approved', + 'type' => 'datetime', + 'use' => 'lge' + ), + + // Date Start Date + 'start_date' => array( + 'field' => 'start_date', + 'type' => 'date', + 'use' => 'a' + ), + + // Date End Date + 'end_date' => array( + 'field' => 'end_date', + 'type' => 'date', + 'use' => 'a' + ), + + // Date Expire Date + 'expire' => array( + 'field' => 'expire', + 'type' => 'date', + 'use' => 'a' + ), + + // Reference Type - Insert new record + 'ref_type_insert' => array ( + 'field' => 'ref_type', + 'type' => 'integer', + 'use' => 'i' + ), + + // Reference Type - Output only + 'ref_type' => array ( + 'field' => 'ref_type', + 'type' => 'list', + 'list' => $this->config['ref_type'], + 'use' => 'a' + ), + + // Ref_name (member name - need to update this to be Reference Type aware) + 'ref_dest' => array ( + 'field' => 'ref_dest', + 'type' => 'pointer', + 'p_table' => GLM_MEMBERS_PLUGIN_DB_PREFIX . 'members', + 'p_field' => 'name', + 'p_orderby' => 'name', + 'p_blank' => true, + 'use' => 'a' + ), + + // Reference Target ID + 'ref_dest_id' => array ( + 'field' => 'ref_dest', + 'as' => 'ref_dest_id', + 'type' => 'integer', + 'use' => 'lged' + ), + + // Coupon Name + 'name' => array ( + 'field' => 'name', + 'type' => 'text', + 'required' => true, + 'use' => 'a' + ), + + // Name Slug for URLs + 'name_slug' => array ( + 'field' => 'name_slug', + 'type' => 'text', + 'required' => true, + 'use' => 'lge' + ), + + // Description + 'descr' => array ( + 'field' => 'descr', + 'type' => 'text', + 'required' => true, + 'use' => 'a' + ), + + // Coupon Image + 'image' => array( + 'field' => 'image', + 'type' => 'image', + 'use' => 'a' + ), + + // Coupon URL + 'url' => array ( + 'field' => 'url', + 'type' => 'text', + 'use' => 'a' + ), + + ); + + } + + /** + * 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) + { + + // Get Member Category data for this entry + if ($this->postCategories) { + $sql = " + SELECT EC.coupon AS coupon_id, C.id, C.name, C.descr, + COALESCE ( + ( + SELECT name + FROM ".GLM_MEMBERS_COUPONS_PLUGIN_DB_PREFIX. "categories + WHERE id = C.parent + ), '' + ) AS parent_name + FROM ".GLM_MEMBERS_COUPONS_PLUGIN_DB_PREFIX. "categories AS C, + ".GLM_MEMBERS_COUPONS_PLUGIN_DB_PREFIX. "coupon_categories AS EC + WHERE C.id = EC.category + AND EC.coupon = ".$r['id']." + ;"; + $r['categories'] = $this->wpdb->get_results($sql, ARRAY_A); + } + + return $r; + } + + /** + * Update coupon slug - should be called after an coupon record is added or updated. + * + * Since slugs must be unique, if there's already a slug matching the slug directly + * derived from the coupon name, then append "-n" where n is a sequential number + * that makes the slug unique. + * + * This function will try numbers from 1 to $maxTries to get a unique slug. + * + * @param integer id ID of coupon that needs the slug updated + * @param integer maxTries Maximum number of tries to create a unique slug + * default 100 + * + * @return string Slug stored in this coupon record + * If this function fails, it will return false. + * @access public + */ + public function updateSlug($id = false, $maxTries = 100) + { + + if ($id == false) { + return false; + } + + // Get the coupon entry + $e = $this->getEntry($id); + if (!$e) { + return false; + } + + // Make a slug out of the coupon name + $slug = sanitize_title($e['name']); + + // If slug is the same as it was then do nothing + if ($e['name_slug'] == $slug) { + return $slug; + } + + // Loop until we have a unique slug - limit loops to 50 just in case + $saveSlug = $slug; + for ($loop = 1 ; $loop <= $maxTries ; $loop++) { + + // New Slug - Check if the new slug is unique + $matchingSlugs = $this->wpdb->get_results( + " + SELECT id, name_slug + FROM ".$this->table." + WHERE name_slug = '$slug' + AND id != $id + ", + ARRAY_A + ); + + // If there's no matching results then we're good to use this one + if (!is_array($matchingSlugs) || count($matchingSlugs) == 0) { + + // Write new slug and return + $sql = " + UPDATE ".$this->table." + SET name_slug = '$slug' + WHERE id = $id + ;"; + $this->wpdb->query($sql); + + return $slug; + + } + + $slug = $saveSlug."-$loop"; + + } // for + + // Unable to create unique slug + $sql = " + UPDATE ".$this->table." + SET name_slug = '' + WHERE id = $id + ;"; + $this->wpdb->query($sql); + + return false; + } + + /** + * Update timestamps for created, updated, approved + * + * @param string $field Field to update + * @param integer $id ID of coupon + * + * @return void + */ + public function updateTimestamp($field = false, $id = false) + { + + if (!in_array($field, array('created', 'updated', 'approved')) || !$id) { + return false; + } + + $sql = " + UPDATE ".$this->table." + SET $field = now() + WHERE id = $id + ;"; + $this->wpdb->query($sql); + + + } + + /** + * Delete an coupon and all data associated with it. + * + * @param integer $id ID of coupon + * + * @return void + */ + public function deleteCoupon($id = false) + { + + // Check ID + $id = ($id - 0); + if ($id <= 0) { + return false; + } + + // Try to get the coupon data + $coupon = $this->getEntry($id); + if (!$coupon) { + return false; + } + + // Delete the coupon + $coupon = $this->deleteEntry($id, true); + + return $coupon; + + } + + /** + * Get ID/Name list + * + * @param string $where + * + * @return array ID/Name pairs + */ + public function getIdName($where = 'true') + { + $savedFields = $this->fields; + $savedCategories = $this->postCategories; + + $this->postCategories = false; + + $this->fields = array( + 'id' => $savedFields['id'], + 'name' => $savedFields['name'] + ); + + $r = $this->getList($where); + + $this->fields = $savedFields; + $this->postCategories = $savedCategories; + + return $r; + + } + +} diff --git a/models/admin/coupons/index.php b/models/admin/coupons/index.php index ab6c37d..6205278 100644 --- a/models/admin/coupons/index.php +++ b/models/admin/coupons/index.php @@ -14,10 +14,9 @@ */ // Load Coupons data abstract -//require_once GLM_MEMBERS_COUPONS_PLUGIN_CLASS_PATH.'/data/dataCoupons.php'; +require_once GLM_MEMBERS_COUPONS_PLUGIN_CLASS_PATH.'/data/dataCoupons.php'; -class GlmMembersAdmin_coupons_index - // extends GlmDataCoupons +class GlmMembersAdmin_coupons_index extends GlmDataCoupons { /** @@ -96,6 +95,7 @@ class GlmMembersAdmin_coupons_index $numbCoupons = 0; $numbPending = 0; $namesList = false; + $pending = false; $haveCategories = false; // Check if there's a logged in user who is locked to their own entity @@ -107,6 +107,7 @@ class GlmMembersAdmin_coupons_index $lockedWhere = 'ref_type = '.$this->config['ref_type_numb']['Member'].' AND ref_dest = '.$memberID; } + /* // Check for required Coupon Categories require_once GLM_MEMBERS_COUPONS_PLUGIN_CLASS_PATH.'/data/dataCategories.php'; $CouponCategories = new GlmDataCouponsCategories($this->wpdb, $this->config); @@ -114,6 +115,7 @@ class GlmMembersAdmin_coupons_index if ($couponCategoriesStats && $couponCategoriesStats > 0) { $haveCategories = true; } + */ // Get full list of names matching this where clause for search box $namesList = $this->getIdName($lockedWhereT); @@ -135,7 +137,6 @@ class GlmMembersAdmin_coupons_index // Get list of Pending Coupons $pending = $this->getIdName($lockedWhereT.' T.status = '.$this->config['status_numb']['Pending']); - // Compile template data $templateData = array( 'lockedToMember' => $lockedToMember, diff --git a/models/admin/settings/couponCategories.php b/models/admin/settings/couponCategories.php new file mode 100644 index 0000000..45e80be --- /dev/null +++ b/models/admin/settings/couponCategories.php @@ -0,0 +1,200 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @version 0.1 + */ + +// Load Coupon Categories data abstract +require_once GLM_MEMBERS_COUPONS_PLUGIN_CLASS_PATH.'/data/dataCouponCategories.php'; + +/* + * This class performs the work for the default action of the "Members" menu + * option, which is to display the members dashboard. + * + */ +class GlmMembersAdmin_settings_couponCategories extends GlmDataCouponCategories +{ + + /** + * WordPress Database Object + * + * @var $wpdb + * @access public + */ + public $wpdb; + /** + * Plugin Configuration Data + * + * @var $config + * @access public + */ + public $config; + + /* + * Constructor + * + * This contructor sets up this model. At this time that only includes + * storing away the WordPress data object. + * + * @return object Class object + * + */ + public function __construct ($wpdb, $config) + { + + // Save WordPress Database object + $this->wpdb = $wpdb; + + // Save plugin configuration object + $this->config = $config; + + // Run constructor for members data class + parent::__construct(false, false); + + } + + /* + * Perform Model Action + * + * This method does the work for this model and returns any resulting data + * + * @return array Status and data array + * + * 'status' + * + * True if successfull and false if there was a fatal failure. + * + * 'menuItemRedirect' + * + * If not false, provides a menu item the controller should + * execute after this one. Normally if this is used, there would also be a + * modelRedirect value supplied as well. + * + * 'modelRedirect' + * + * If not false, provides an action the controller should execute after + * this one. + * + * 'view' + * + * A suggested view name that the controller should use instead of the + * default view for this model or false to indicate that the default view + * should be used. + * + * 'data' + * + * Data that the model is returning for use in merging with the view to + * produce output. + * + */ + public function modelAction ($actionData = false) + { + + $success = true; + $haveCategories = false; + $categories = false; + $error = false; + $enable_members = $this->config['settings']['enable_members']; + + // Check if a category ID is supplied + $id = 0; + if (isset($_REQUEST['id'])) { + $id = $_REQUEST['id']-0; + } + + // If there's an action option + if (isset($_REQUEST['option'])) { + + switch($_REQUEST['option']) { + + case 'addNew': + $this->insertEntry(); + break; + + case 'update': + if ($id > 0) { + $this->updateEntry($id); + } + break; + + case 'delete': + if ($id > 0) { + $this->deleteEntry($id, true); + + // Also delete from member info + $this->wpdb->delete( + GLM_MEMBERS_COUPONS_PLUGIN_DB_PREFIX . 'coupon_categories', + array( + 'category' => $id + ) + ); + } + break; + + } + + } + + // Get a current list of categories + $categories = $this->getList(); + + if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG_VERBOSE) { + glmMembersAdmin::addNotice($categories, 'DataBlock', 'Category Data'); + } + + // If we have list entries - even if it's an empty list + $success = true; + $haveCategories = false; + if ($categories !== false) { + + $success = true; + + // If we have any entries + if (count($categories) > 0) { + $haveCategories = true; + } + } + + // If we had a fatal error, redirect to the error page + if ($error) { + return array( + 'status' => $success, + 'menuItemRedirect' => 'error', + 'modelRedirect' => 'index', + 'view' => 'admin/error/index.html', + 'data' => false + ); + } + + if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG_VERBOSE) { + glmMembersAdmin::addNotice($categories, 'DataBlock', 'Categories Data'); + } + + // Compile template data + $templateData = array( + 'enable_members' => $enable_members, + 'haveCategories' => $haveCategories, + 'categories' => $categories + ); + + // Return status, suggested view, and data to controller + return array( + 'status' => $success, + 'menuItemRedirect' => false, + 'modelRedirect' => false, + 'view' => 'admin/settings/couponCategories.html', + 'data' => $templateData + ); + + } + + +} diff --git a/setup/adminMenus.php b/setup/adminMenus.php index a2a1604..72584ce 100644 --- a/setup/adminMenus.php +++ b/setup/adminMenus.php @@ -54,6 +54,6 @@ add_submenu_page( 'Coupons', 'Coupons', 'glm_members_members', - 'glm-members-admin-menu-coupons', + 'glm-members-admin-menu-coupons-index', function(){$this->controller('coupons');} ); diff --git a/setup/validActions.php b/setup/validActions.php index 396bd6c..3e1a9b5 100644 --- a/setup/validActions.php +++ b/setup/validActions.php @@ -28,42 +28,27 @@ * * This array is integrated into the valid actions array in the main GLM Member * DB plugin when this plugin registers itself. - * - * EXAMPLE - * - * $glmMembersCouponsAddOnValidActions = array( - * 'adminActions' => array( - * 'members' => array( - * 'sample' => GLM_MEMBERS_COUPONS_PLUGIN_SLUG, - * ), - * 'sample' => array( - * 'index' => GLM_MEMBERS_COUPONS_PLUGIN_SLUG, - * 'more' => GLM_MEMBERS_COUPONS_PLUGIN_SLUG, - * ), - * 'info' => array( - * 'index' => GLM_MEMBERS_COUPONS_PLUGIN_SLUG - * ) - * ), - * 'frontActions' => array( - * 'sample' => array( - * 'list' => GLM_MEMBERS_COUPONS_PLUGIN_SLUG, - * 'detail' => GLM_MEMBERS_COUPONS_PLUGIN_SLUG - * ) - * ) - * ); - * - * - * - * */ $glmMembersCouponsAddOnValidActions = array( 'adminActions' => array( 'coupons' => array( - 'coupons' => GLM_MEMBERS_COUPONS_PLUGIN_SLUG - ) + 'index' => GLM_MEMBERS_COUPONS_PLUGIN_SLUG + ), + 'dashboard' => array( + 'coupons' => GLM_MEMBERS_COUPONS_PLUGIN_SLUG, + ), + 'member' => array( + 'coupons' => GLM_MEMBERS_COUPONS_PLUGIN_SLUG, + ), + 'settings' => array( + 'couponCategories' => GLM_MEMBERS_COUPONS_PLUGIN_SLUG, + ), ), 'frontActions' => array( + 'coupons' => array( + 'list' => GLM_MEMBERS_COUPONS_PLUGIN_SLUG, + ), ) ); diff --git a/views/admin/coupons/header.html b/views/admin/coupons/header.html new file mode 100644 index 0000000..3e46df2 --- /dev/null +++ b/views/admin/coupons/header.html @@ -0,0 +1,9 @@ +
+

All Coupons

+ +
+ + diff --git a/views/admin/coupons/index.html b/views/admin/coupons/index.html index eb39bf6..f681229 100644 --- a/views/admin/coupons/index.html +++ b/views/admin/coupons/index.html @@ -1,27 +1,27 @@ {include file='admin/coupons/header.html'} {if apply_filters('glm_members_permit_admin_coupons_index_add_coupon', true)} - Add A New Event + Add A New Coupon {/if} {if apply_filters('glm_members_permit_admin_coupons_index_coupon_config_warning', true)} {if !$haveCategories} - - + + {/if} {/if} -{if $numbEvents == 0} +{if $numbCoupons == 0} @@ -31,17 +31,17 @@
- - Events Search: + + Coupons Search:
You do not have any Events Categories setup.Click here to add Event Categories.You do not have any Coupons Categories setup.Click here to add Coupon Categories.
 
You do not have any coupons listed.
- +
Number of Events Listed: {$numbEvents}
Number of Coupons Listed: {$numbCoupons}
Number Pending: {$numbPending}
{if $pending} -

 
Events Pending Review

+

 
Coupons Pending Review

- + @@ -79,7 +79,7 @@ {/foreach} ]; - $( "#glmEventsList" ).autocomplete({ + $( "#glmCouponsList" ).autocomplete({ source: availableTags, html: true, select: function( coupon, ui ) { diff --git a/views/admin/settings/couponCategories.html b/views/admin/settings/couponCategories.html new file mode 100644 index 0000000..7f4be05 --- /dev/null +++ b/views/admin/settings/couponCategories.html @@ -0,0 +1,191 @@ +{include file='admin/settings/header.html'} + + +
Add a Category
+
+
+ + + +
Event NameCoupon Name
+ + + + + + + + + + + + +
Category Name: + +
Parent Category: + +
Description: + +
+

* Required

+ Cancel + + +
+ + +
+
+

Are you sure you want to delete this category?

+

Yes, delete this category

+

Cancel

+
+
+ + +
+
+ + + + + + + + + + + + + + + + +
Category Name: + +
Parent Category: + +
Description: + +
+

* Required

+ Cancel + +
+
+ +

Categories

+ + + + + + + + + + + +{if $haveCategories} + {assign var="i" value="0"} + {foreach $categories as $t} + {if $i++ is odd by 1} + + {else} + + {/if} + + + + + + {/foreach} +{else} + +{/if} + +
IDCategoryDescription 
{$t.id} + + {$t.name} + + + {$t.descr} + +
Delete
+
(no categories listed)
+ + + +{include file='admin/footer.html'} -- 2.17.1