From c52cda7df2726c9875019dd398636f54f61e7f33 Mon Sep 17 00:00:00 2001 From: Chuck Scott Date: Mon, 4 Dec 2017 16:15:57 -0500 Subject: [PATCH] Starting rework to remove legacy code get custom fields working cleanly. Have glm-members-customfields-edit admin filter working except for edit. Marked much of the code as OLD to make sure we delete anything that's not used when it's all working. --- classes/data/dataCustomFields.php | 2 - .../admin/{entity => OLD-entity}/fields.php | 0 .../{management => OLD-management}/fields.php | 0 .../admin/OL\357\200\242D-import/fields.php" | 0 models/admin/ajax/customFields.php | 217 +++++++++++++ models/admin/ajax/filterSearch.php | 119 ------- models/admin/customFields/index.php | 217 +++++++++++++ setup/adminHooks.php | 23 +- setup/commonHooks.php | 9 +- setup/validActions.php | 18 +- .../admin/{entity => OLD-entity}/fields.html | 0 .../admin/{import => OLD-import}/fields.html | 0 .../{import => OLD-import}/fieldsProcess.html | 0 .../fieldsValidate.html | 0 .../fields.html | 170 +++++----- views/admin/ajax/newField.html | 11 + views/admin/customFields/index.html | 306 ++++++++++++++++++ 17 files changed, 873 insertions(+), 219 deletions(-) rename models/admin/{entity => OLD-entity}/fields.php (100%) rename models/admin/{management => OLD-management}/fields.php (100%) rename models/admin/import/fields.php => "models/admin/OL\357\200\242D-import/fields.php" (100%) create mode 100644 models/admin/ajax/customFields.php delete mode 100644 models/admin/ajax/filterSearch.php create mode 100644 models/admin/customFields/index.php rename views/admin/{entity => OLD-entity}/fields.html (100%) rename views/admin/{import => OLD-import}/fields.html (100%) rename views/admin/{import => OLD-import}/fieldsProcess.html (100%) rename views/admin/{import => OLD-import}/fieldsValidate.html (100%) rename views/admin/{management => OLD-management}/fields.html (70%) create mode 100644 views/admin/ajax/newField.html create mode 100644 views/admin/customFields/index.html diff --git a/classes/data/dataCustomFields.php b/classes/data/dataCustomFields.php index 814118d..e5ef93c 100644 --- a/classes/data/dataCustomFields.php +++ b/classes/data/dataCustomFields.php @@ -148,14 +148,12 @@ class GlmDataFieldsCustomFields extends GlmDataAbstract 'required' => array( 'field' => 'required', 'type' => 'checkbox', - 'default' => false, 'use' => 'a' ), // admin_search flag 'admin_search' => array ( 'field' => 'admin_search', 'type' => 'checkbox', - 'default' => false, 'use' => 'a' ), diff --git a/models/admin/entity/fields.php b/models/admin/OLD-entity/fields.php similarity index 100% rename from models/admin/entity/fields.php rename to models/admin/OLD-entity/fields.php diff --git a/models/admin/management/fields.php b/models/admin/OLD-management/fields.php similarity index 100% rename from models/admin/management/fields.php rename to models/admin/OLD-management/fields.php diff --git a/models/admin/import/fields.php "b/models/admin/OL\357\200\242D-import/fields.php" similarity index 100% rename from models/admin/import/fields.php rename to "models/admin/OL\357\200\242D-import/fields.php" diff --git a/models/admin/ajax/customFields.php b/models/admin/ajax/customFields.php new file mode 100644 index 0000000..c735748 --- /dev/null +++ b/models/admin/ajax/customFields.php @@ -0,0 +1,217 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @version 0.1 + */ + +// Load Management Fields data abstract +require_once GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_CLASS_PATH.'/data/dataCustomFields.php'; + + +/* + * This class performs the work of handling images passed to it via + * an AJAX call that goes through the WorPress AJAX Handler. + * + */ +class GlmMembersAdmin_ajax_customFields extends GlmDataFieldsCustomFields +{ + + /** + * 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 data class + parent::__construct(false, false); + + } + /** + * getModelEventsData + * + * Return the array of events. + * + * @param integer $categoryId Id of the category for filtering events (optional) + * @param integer $limit Number of events to return (optional) + * @param integer $memberID Member ID if filtering by member + * + * @access public + * @return array events + */ + + + /* + * Perform Model Action + * + * This modelAction takes an AJAX image upload and stores the image in the + * media/images directory of the plugin. + * + * This model action does not return, it simply does it's work then calls die(); + * + * @param $actionData + * + * @return No return is expected from this model. Content should be output then wp_die(); + */ + public function modelAction ($actionData = false) + { + + trigger_error(print_r($_REQUEST,1),E_USER_NOTICE); + + switch($_REQUEST['option']) { + + case 'addNewField': + + $customField = $this->insertEntry(); + +trigger_error($_REQUEST['required'],E_USER_NOTICE); + + if (!is_array($customField) || !$customField['status']) { + echo "0"; + } else { + + $viewFile = 'admin/ajax/newField.html'; + $newFieldHtml = $this->generateHTML($customField, $viewFile); + echo $newFieldHtml; + } + + wp_die(); + break; + + case 'deleteField': + + $fieldId = false; + + if (isset($_REQUEST['id'])) { + $fieldId = $_REQUEST['id'] - 0; + if ($fieldId <= 0) { + echo false; + wp_die(); + } + } + + // Delete this custom field + $this->deleteEntry($fieldId, true); + echo true; + wp_die(); + + break; + + default: + + wp_die(); + } + + wp_die(); + } + + + /** + * Merge template and data to produce HTML + * + * Checks the theme's view directories and the view directories for + * this plugin for a matching view file. + * + * Note that $viewFile needs to have the proper view directory path + * includes. (i.e. "/views/front/registrations/summary.html") + * + * @param $data array Array of data to merge with the template + * @param $view string Path added to + * + * @access public + * @return void + */ + function generateHTML($data, $viewFile) + { + + // If a view file is specified + if ($viewFile) { + + // Get the specified view file - check theme first + $viewPath = GLM_MEMBERS_PLUGIN_CURRENT_THEME_DIR."/views"; + $viewPath2 = GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_PATH . "views"; // Save default + + // If the view is not found in the theme, fall back to views in the plugin + if (!is_file($viewPath.'/'.$viewFile)) { + + // Next try the plugin/add-on + $viewPath = GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_PATH . "/views"; + + if (!is_file($viewPath.'/'.$viewFile)) { + + if (GLM_MEMBERS_PLUGIN_FRONT_DEBUG) { + trigger_error("Bad or missing view file when generating checkout HTML: $viewPath/$viewFile", E_USER_NOTICE); + } + + } + + } + + } + + // Load Smarty Template support + $smarty = new smartyTemplateSupport(); + + // Add standard parameters + require GLM_MEMBERS_PLUGIN_SETUP_PATH.'/standardTemplateParams.php'; + + // Add data from model to Smarty template + if (is_array($data) && count($data) > 0) { + foreach ($data as $k => $d) { + $smarty->templateAssign($k, $d); + } + } + + // Update the Smarty view path + $smarty->template->setTemplateDir($viewPath); + + // If the view path doesn't match the default, add the default (using theme view) + if ($viewPath2 != $viewPath) { + $smarty->template->addTemplateDir($viewPath2); + } + + // Generate output from model data and view + $out = $smarty->template->fetch($viewFile); + + return $out; + + } + + + +} diff --git a/models/admin/ajax/filterSearch.php b/models/admin/ajax/filterSearch.php deleted file mode 100644 index 18fb75a..0000000 --- a/models/admin/ajax/filterSearch.php +++ /dev/null @@ -1,119 +0,0 @@ - - * @license http://www.gaslightmedia.com Gaslightmedia - * @version 0.1 - */ - -// Load Event Times data abstract -require_once GLM_MEMBERS_PLUGIN_CLASS_PATH .'/data/dataCategories.php'; - - -/* - * This class performs the work of handling images passed to it via - * an AJAX call that goes through the WorPress AJAX Handler. - * - */ -class GlmMembersAdmin_ajax_filterSearch extends GlmDataCategories -{ - - /** - * 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); - - } - /** - * getModelEventsData - * - * Return the array of events. - * - * @param integer $categoryId Id of the category for filtering events (optional) - * @param integer $limit Number of events to return (optional) - * @param integer $memberID Member ID if filtering by member - * - * @access public - * @return array events - */ - - - /* - * Perform Model Action - * - * This modelAction takes an AJAX image upload and stores the image in the - * media/images directory of the plugin. - * - * This model action does not return, it simply does it's work then calls die(); - * - * @param $actionData - * - * Echos JSON string as response and does not return - */ - public function modelAction ($actionData = false) - { - - global $wpdb; -// -// $category_ids = $_REQUEST['cat_ids']; -// $category_names = $_REQUEST['cat_names']; -// -// // get cat ids by name -// foreach($category_names as $cat_name){ -// $sql = 'SELECT id FROM ' . GLM_MEMBERS_PLUGIN_DB_PREFIX . "categories WHERE name = '$cat_name';"; -// $cat_id = $wpdb->get_var($sql); -// -// // get the -// } -// -// -// $categories = $this->getListSortedParentChild(); -// $categories = $categories[$category_id]; - $return = array( -// 'manufacturers' => $category_ids, -// 'brands' => $cat_id - ); - - header('Content-type:application/json;charset=utf-8', true); - echo json_encode($return); - wp_die(); - } -} diff --git a/models/admin/customFields/index.php b/models/admin/customFields/index.php new file mode 100644 index 0000000..8bd56c1 --- /dev/null +++ b/models/admin/customFields/index.php @@ -0,0 +1,217 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @release fields.php,v 1.0 2014/10/31 19:31:47 cscott Exp $ + * @link http://dev.gaslightmedia.com/ + */ + +// Load Management Fields data abstract +require_once GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_CLASS_PATH.'/data/dataCustomFields.php'; + +/** + * GlmMembersAdmin_management_fields + * + * PHP version 5 + * + * @category Model + * @package GLM Member DB + * @author Chuck Scott + * @license http://www.gaslightmedia.com Gaslightmedia + * @release SVN: $Id: packaging.php,v 1.0 2011/01/25 19:31:47 cscott + * Exp $ + */ +class GlmMembersAdmin_customFields_index extends GlmDataFieldsCustomFields +{ + + /** + * WordPress Database Object + * + * @var $wpdb + * @access public + */ + public $wpdb; + /** + * dbh Postgres database connection + * + * @var mixed + * @access public + */ + public $dbh; + /** + * settings used for the schema and tablenames + * + * @var mixed + * @access public + */ + public $settings = array(); + /** + * categories + * + * @var bool + * @access public + */ + public $categories = array(); + public $oldCatMap = array(); + /** + * fields + * + * @var bool + * @access public + */ + public $fields = array(); + public $image_owner; + + /** + * Constructor + * + * This contructor performs the work for this model. This model returns + * an array containing the following. + * + * 'status' + * + * True if successfull and false if there was a fatal failure. + * + * 'view' + * + * A suggested view name that the contoller 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. + * + * @wpdb object WordPress database object + * + * @return array Array containing status, suggested view, and any data + */ + 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); + + } + + /** + * modelAction + * + * @param bool $actionData + * @access public + * @return void + */ + public function modelAction($actionData = false) + { + $viewFile = 'index.html'; + $glm_action = ''; + $option = false; + $settings_updated = false; + $settings_update_error = false; + $custom_fields = false; + $haveCustomFields = false; + $where = ' TRUE '; + + if (isset($_REQUEST['option2'])) { + $option = $_REQUEST['option2']; + } + + if (isset($_GET['glm_action'])) { + $glm_action = $_GET['glm_action']; + } + + // Check if a field ID is supplied + $id = 0; + if (isset($_REQUEST['id'])) { + $id = $_REQUEST['id']-0; + } + + $uid = 0; + + if (isset($actionData['uid'])) { + $uid = $actionData['uid']; + } + + switch ($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 any data entries + $this->wpdb->delete( + GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_DB_PREFIX . 'custom_field_data', + array( 'field_id' => $id ) + ); + } + break; + + default: + + // Make sure option is set if default + $option = 'list'; + + break; + + } + + $where .= " AND uid = '$uid' "; + + // Get list of Custom Fields + $custom_fields = $this->getList( $where ); + + if ( isset($custom_fields) && $custom_fields && count( $custom_fields ) > 0 ) { + $haveCustomFields = true; + } + + // Compile template data + $template_data = array( + 'option2' => $option, + 'settingsUpdated' => $settings_updated, + 'settingsUpdateError' => $settings_update_error, + 'custom_fields' => $custom_fields, + 'field_types' => $this->config['custom_field_types'], + 'haveCustomFields' => $haveCustomFields, + 'uid' => $uid, + 'glm_action' => $glm_action + ); + + // echo "
Template data:" . print_r($template_data, true) . "
"; + + // Return status, suggested view, and data to controller + return array( + 'status' => true, + 'menuItemRedirect' => false, + 'modelRedirect' => false, + 'view' => 'admin/customFields/'.$viewFile, + 'data' => $template_data + ); + + + } + +} diff --git a/setup/adminHooks.php b/setup/adminHooks.php index 803d3b1..8a7ce61 100644 --- a/setup/adminHooks.php +++ b/setup/adminHooks.php @@ -44,21 +44,26 @@ add_filter('glm-member-db-admin-management-hooksHelp', function($content) { ); /** - * Filter will return true if the plugin is active. - */ -add_filter( 'glm-members-customfields-active', function( $active ){ - return true; -}, 10, 1 ); - -/** - * Filter returns html from the management -> fields model. + * Filter to edit custom fields for a specific UID + * + * Requires UID - a unique string that identifies this group of field. + * Recommended to use "{plugin-slug}-customfields-edit-{use}-{id}" + * {use} can be a description of the use or where it used (i.e. "reg-event-edit") + * {id} should be an ID of the record these fields are associated with, for example a registration event ID */ add_filter( 'glm-members-customfields-edit', function( $content, $uid ){ unset( $_REQUEST['glm_action'] ); - $content .= $this->controller( 'management', 'fields', array( 'uid' => $uid ), true ); + $content .= $this->controller( 'customFields', 'index', array( 'uid' => $uid ), true ); return $content; }, 10, 2 ); + + + + + + + /** * Filter returns the html for the form segment */ diff --git a/setup/commonHooks.php b/setup/commonHooks.php index 76c044a..009a0ca 100644 --- a/setup/commonHooks.php +++ b/setup/commonHooks.php @@ -15,7 +15,7 @@ */ /* - * Place Misc Hooks and Filters that should be available to both admin and front processes here. + * Place Misc Hooks and Filters that should be available to both admin and front processes here. * If this file exists, it will be included by the main plugin script. * * Note that filter and hook callback functions must be included in-line as shown below... @@ -27,3 +27,10 @@ * Also note that parameters will be in the context of the main admin controller constructor. */ +/** + * Filter will return true if the plugin is active. + */ +add_filter( 'glm-members-customfields-active', function( $active ){ + return true; +}, 10, 1 ); + diff --git a/setup/validActions.php b/setup/validActions.php index fac0518..f6a0815 100644 --- a/setup/validActions.php +++ b/setup/validActions.php @@ -60,22 +60,16 @@ $glmMembersCustomFieldsAddOnValidActions = array( 'adminActions' => array( 'ajax' => array( - 'filterSearch' => GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_SLUG, - ), - 'entity' => array( - 'fields' => GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_SLUG, - ), - 'management' => array( - 'fields' => GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_SLUG, - ), - 'import' => array( - 'fields' => GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_SLUG, + 'customFields' => GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_SLUG ), + 'customFields' => array( + 'index' => GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_SLUG + ) ), 'frontActions' => array( 'fields' => array( - 'list' => GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_SLUG, - 'detail' => GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_SLUG, +// 'list' => GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_SLUG, +// 'detail' => GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_SLUG, ), ) ); diff --git a/views/admin/entity/fields.html b/views/admin/OLD-entity/fields.html similarity index 100% rename from views/admin/entity/fields.html rename to views/admin/OLD-entity/fields.html diff --git a/views/admin/import/fields.html b/views/admin/OLD-import/fields.html similarity index 100% rename from views/admin/import/fields.html rename to views/admin/OLD-import/fields.html diff --git a/views/admin/import/fieldsProcess.html b/views/admin/OLD-import/fieldsProcess.html similarity index 100% rename from views/admin/import/fieldsProcess.html rename to views/admin/OLD-import/fieldsProcess.html diff --git a/views/admin/import/fieldsValidate.html b/views/admin/OLD-import/fieldsValidate.html similarity index 100% rename from views/admin/import/fieldsValidate.html rename to views/admin/OLD-import/fieldsValidate.html diff --git a/views/admin/management/fields.html b/views/admin/OLD-management/fields.html similarity index 70% rename from views/admin/management/fields.html rename to views/admin/OLD-management/fields.html index fdd0d80..8834912 100644 --- a/views/admin/management/fields.html +++ b/views/admin/OLD-management/fields.html @@ -1,55 +1,39 @@
Add a Custom Field
-
-
- - - - - - - - - - - - - - {if $uid == 'glm-member-db'} - - - - - {/if} - +{* New field form *} +
+
Field Name: - -
Field Type: - -
Admin Searchable - - - (text or checkbox only) -
+ + + + + + + + + - - - -
Field Name: + +
Field Type: + +
Required? - - -
-

* Required

- Cancel - -
+ + + + + + +

* Required

+
Cancel
+
Add new Custom Field
- +{* Delete field button confirmation dialog box *}

Are you sure you want to delete this field?

@@ -58,7 +42,7 @@
- +{* Edit field dialog box *}
@@ -107,7 +91,9 @@
- + +{* Update and error messages *} +
{if $settingsUpdated}

Settings Updated

{/if} @@ -116,7 +102,9 @@
- + +{* Fields Table *} +
@@ -171,41 +159,62 @@ -- 2.17.1
ID