From dae297a5dcc6a823df760a49bec71a9db7e1a381 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Tue, 28 Mar 2017 13:53:43 -0400 Subject: [PATCH] Update custom fields for saving custom field data Custom field plugin now will create custom fields with three types: text, textarea, and checkbox. All field data stores into text fields. --- classes/customFieldPluginSupport.php | 90 ++++++++ classes/data/dataCustomFields.php | 165 ++++++++++++++ config/plugin.ini | 7 +- index.php | 6 +- models/admin/management/fields.php | 204 ++++++++++++++++++ models/admin/memberinfo/fields.php | 192 +++++++++++++++++ setup/adminHooks.php | 19 ++ setup/adminTabs.php | 17 ++ .../create_database_V0.0.1.sql | 12 +- setup/databaseScripts/dbVersions.php | 19 ++ setup/validActions.php | 8 +- views/admin/management/fields.html | 194 +++++++++++++++++ views/admin/memberinfo/fields.html | 17 ++ 13 files changed, 937 insertions(+), 13 deletions(-) create mode 100644 classes/customFieldPluginSupport.php create mode 100644 classes/data/dataCustomFields.php create mode 100644 models/admin/management/fields.php create mode 100644 models/admin/memberinfo/fields.php create mode 100644 setup/databaseScripts/dbVersions.php create mode 100644 views/admin/management/fields.html create mode 100644 views/admin/memberinfo/fields.html diff --git a/classes/customFieldPluginSupport.php b/classes/customFieldPluginSupport.php new file mode 100644 index 0000000..590deab --- /dev/null +++ b/classes/customFieldPluginSupport.php @@ -0,0 +1,90 @@ + $fieldValue ) { + $fieldData =array( + 'ref_dest' => $memberId, + 'field_id' => $fieldId, + 'field_data' => $fieldValue + ); + if ( !isset($currentCustomFields[$fieldId]) ) { + // Insert Entries + $wpdb->insert( + GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . "custom_field_data", + $fieldData, + $fieldDataFormat + ); + } else { + // Update Entries + $wpdb->update( + GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . "custom_field_data", + $fieldData, + array( + 'ref_dest' => $memberId, + 'field_id' => $fieldId + ), + $fieldDataFormat, + '%d' + ); + } + } +} + +/** + * customFieldsGetMemberInfoFields + * + * Pull the member info custom field data. + * This will be used more for admin side. + * + * @param mixed $memberId Id for the member info record + * + * @access public + * @return void + */ +function customFieldsGetMemberInfoFields( $memberId ) +{ + global $wpdb; + $data = array(); + $sql = " + SELECT * + FROM " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . "custom_field_data + WHERE ref_dest = %d"; + $fieldData = $wpdb->get_results( $wpdb->prepare( $sql, $memberId ), ARRAY_A ); + if ( $fieldData ) { + foreach ( $fieldData as $key => $val ) { + $data[$val['field_id']] = $val['field_data']; + } + return $data; + } else { + return false; + } +} diff --git a/classes/data/dataCustomFields.php b/classes/data/dataCustomFields.php new file mode 100644 index 0000000..c2746ea --- /dev/null +++ b/classes/data/dataCustomFields.php @@ -0,0 +1,165 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @release SVN: $Id: dataFields.php,v 1.0 2011/01/25 19:31:47 cscott Exp $ + */ + +/** + * GlmDataEvent 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 GlmDataFieldsCustomFields 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_FIELDS_PLUGIN_DB_PREFIX . 'custom_fields'; + + /* + * Table Data Fields + */ + + $this->fields = array ( + + 'id' => array ( + 'field' => 'id', + 'type' => 'integer', + 'view_only' => true, + 'use' => 'a' + ), + + // Event ID + 'field_name' => array( + 'field' => 'field_name', + 'type' => 'text', + 'required' => true, + 'use' => 'a' + ), + + // Category ID + 'field_type' => array( + 'field' => 'field_type', + 'type' => 'list', + 'list' => $this->config['custom_field_types'], + 'required' => 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 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; + } + +} diff --git a/config/plugin.ini b/config/plugin.ini index 9222f7e..e6f76cb 100644 --- a/config/plugin.ini +++ b/config/plugin.ini @@ -2,7 +2,10 @@ ; Main Configuration File ; {descrName} ; -; Place any static configuration parameters here. +; Place any static configuration parameters here. ; -[common] +[custom_field_types] +text = 'text' +textarea = 'textarea' +checkbox = 'checkbox' diff --git a/index.php b/index.php index 8fe3212..da5b91b 100644 --- a/index.php +++ b/index.php @@ -31,7 +31,7 @@ * Updates to checkDatabase() in glmPluginSupport.php must be * made together with the DB_VERSION below. ONLY bump the DB * version when there's a change in the database!! Use the - * version nunmber of that release for the DB version. + * version number of that release for the DB version. * * We check the plugin version stored in the WordPress option below * so that we're sure the other add-ons see an up to date @@ -147,7 +147,6 @@ if (is_file(GLM_MEMBERS_FIELDS_PLUGIN_DB_SCRIPTS.'/dbVersions.php')) { $glmMembersFieldsManagementSettings = $wpdb->get_row( "SELECT * FROM ".GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX."management WHERE id = 1", ARRAY_A ); unset($glmMembersFieldsManagementSettings['id']); */ - function glmMembersFieldsRegisterAddOn($addOns) { // Add this add-on to the add-ons array @@ -157,8 +156,7 @@ function glmMembersFieldsRegisterAddOn($addOns) { 'short_name' => GLM_MEMBERS_FIELDS_PLUGIN_SHORT_NAME, 'slug' => GLM_MEMBERS_FIELDS_PLUGIN_SLUG, 'actions' => $GLOBALS['glmMembersFieldsAddOnValidActions'], - 'config' => array( - ), + 'config' => parse_ini_file(GLM_MEMBERS_FIELDS_PLUGIN_PATH.'/config/plugin.ini', true), 'shortcodes' => $GLOBALS['glmMembersFieldsShortcodes'], 'shortcodesDescription' => $GLOBALS['glmMembersFieldsShortcodesDescription'] ); diff --git a/models/admin/management/fields.php b/models/admin/management/fields.php new file mode 100644 index 0000000..4dce64c --- /dev/null +++ b/models/admin/management/fields.php @@ -0,0 +1,204 @@ + + * @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_FIELDS_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_management_fields 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) + { + + $option = false; + $settings_updated = false; + $settings_update_error = false; + $custom_fields = false; + $haveCustomFields = false; + + if (isset($_REQUEST['option'])) { + $option = $_REQUEST['option']; + } + + // Check if a field ID is supplied + $id = 0; + if (isset($_REQUEST['id'])) { + $id = $_REQUEST['id']-0; + } + + 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); + } + break; + + default: + // Make sure option is set if default + $option = 'list'; + + // Determine if current user can edit configurations + if (!current_user_can('glm_members_management')) { + return array( + 'status' => false, + 'menuItemRedirect' => 'error', + 'modelRedirect' => 'index', + 'view' => 'admin/error/index.html', + 'data' => array( + 'reason' => 'User does not have rights to make configuration changes.' + ) + ); + } + + break; + + } + + // Get list of Custom Fields + $custom_fields = $this->getList(); + if ( isset($custom_fields) && $custom_fields && count( $custom_fields ) > 0 ) { + $haveCustomFields = true; + } + + // Compile template data + $template_data = array( + 'option' => $option, + 'settingsUpdated' => $settings_updated, + 'settingsUpdateError' => $settings_update_error, + 'custom_fields' => $custom_fields, + 'field_types' => $this->config['custom_field_types'], + 'haveCustomFields' => $haveCustomFields, + ); + + // Return status, suggested view, and data to controller + return array( + 'status' => true, + 'menuItemRedirect' => false, + 'modelRedirect' => false, + 'view' => 'admin/management/fields.html', + 'data' => $template_data + ); + + + } + +} diff --git a/models/admin/memberinfo/fields.php b/models/admin/memberinfo/fields.php new file mode 100644 index 0000000..0b72856 --- /dev/null +++ b/models/admin/memberinfo/fields.php @@ -0,0 +1,192 @@ + + * @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_FIELDS_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_memberinfo_fields 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) + { + $option = false; + $customFields = false; + $haveCustomFields = false; + $customFieldData = false; + + if (isset($_REQUEST['option'])) { + $option = $_REQUEST['option']; + } + + // Check if a field ID is supplied + $id = 0; + if (isset($_REQUEST['id'])) { + $id = $_REQUEST['id']-0; + } + $memberId = 0; + if (isset($actionData)) { + $memberId = $actionData-0; + } + + switch ($option) { + + default: + // Make sure option is set if default + $option = 'list'; + + // Determine if current user can edit configurations + if (!current_user_can('glm_members_management')) { + return array( + 'status' => false, + 'menuItemRedirect' => 'error', + 'modelRedirect' => 'index', + 'view' => 'admin/error/index.html', + 'data' => array( + 'reason' => 'User does not have rights to make configuration changes.' + ) + ); + } + + break; + + } + + // Get list of Custom Fields + $customFields = $this->getList(); + if ( isset($customFields) && $customFields && count( $customFields ) > 0 ) { + $haveCustomFields = true; + } + + require_once GLM_MEMBERS_FIELDS_PLUGIN_CLASS_PATH . '/customFieldPluginSupport.php'; + $customFieldsData = customFieldsGetMemberInfoFields( $memberId ); + + // Compile template data + $template_data = array( + 'prefix' => 'glm_custom_field', + 'customFields' => $customFields, + 'customFieldsData' => $customFieldsData, + 'fieldTypes' => $this->config['custom_field_types'], + 'haveCustomFields' => $haveCustomFields, + ); + + // Return status, suggested view, and data to controller + return array( + 'status' => true, + 'menuItemRedirect' => false, + 'modelRedirect' => false, + 'view' => 'admin/memberinfo/fields.html', + 'data' => $template_data + ); + + + } + +} diff --git a/setup/adminHooks.php b/setup/adminHooks.php index 6de6790..22bcca9 100644 --- a/setup/adminHooks.php +++ b/setup/adminHooks.php @@ -25,3 +25,22 @@ * * Also note that parameters will be in the context of the main admin controller constructor. */ +add_filter( 'glm-member-db-member-info-custom-nav', function( $memberId ){ + $out = 'Custom Fields'; + return $out; +}); + +add_filter( 'glm-member-db-member-info-custom-tab', function( $memberId ){ + $content = $this->controller( 'memberinfo', 'fields', $memberId ); + return $content; +}); +add_action( + 'glm-member-db-member-info-save-custom-fields', + function( $memberInfoId ){ + require_once GLM_MEMBERS_FIELDS_PLUGIN_CLASS_PATH . '/customFieldPluginSupport.php'; + // this will save the member data + customFieldsSaveMemberInfoFields( $memberInfoId ); + }, + 1, + 1 +); diff --git a/setup/adminTabs.php b/setup/adminTabs.php index 65d5654..0860651 100644 --- a/setup/adminTabs.php +++ b/setup/adminTabs.php @@ -32,4 +32,21 @@ * ); * */ +// If user can manage all members +if (current_user_can('glm_members_members')) { + add_filter('glm-member-db-add-tab-for-management', + function($addOnTabs) { + $newTabs = array( + array( + 'text' => 'Custom Fields', + 'menu' => 'fields', + 'action' => 'fields', + 'option' => 'list' + ), + ); + $addOnTabs = array_merge($addOnTabs, $newTabs); + return $addOnTabs; + } + ); +} diff --git a/setup/databaseScripts/create_database_V0.0.1.sql b/setup/databaseScripts/create_database_V0.0.1.sql index d345d94..1e14758 100644 --- a/setup/databaseScripts/create_database_V0.0.1.sql +++ b/setup/databaseScripts/create_database_V0.0.1.sql @@ -1,5 +1,5 @@ --- Gaslight Media Members Database - Events Add-On --- File Created: 2017-02-07 +-- Gaslight Media Members Database - Fields Add-On +-- File Created: 2017-03-27 -- Database Version: 0.0.1 -- Database Creation Script -- @@ -13,9 +13,10 @@ -- Field Setup Table -CREATE TABLE {prefix}field_setup ( +CREATE TABLE {prefix}custom_fields ( id INT NOT NULL AUTO_INCREMENT, field_name TINYTEXT NOT NULL DEFAULT '', -- Field Name + field_type TINYTEXT NOT NULL DEFAULT '', -- Field Type field_order SMALLINT NOT NULL DEFAULT 0, -- Order for Field PRIMARY KEY (id) ); @@ -23,9 +24,10 @@ CREATE TABLE {prefix}field_setup ( ---- -- Data Table -CREATE TABLE {prefix}field_data ( +CREATE TABLE {prefix}custom_field_data ( id INT NOT NULL AUTO_INCREMENT, - ref_dest INT NOT NULL DEFAULT 0, -- Member Id + field_id INT NOT NULL DEFAULT 0, -- Field Id + ref_dest INT NOT NULL DEFAULT 0, -- Member Info Id field_data TEXT NOT NULL DEFAULT '', -- Data for the field PRIMARY KEY (id) ); diff --git a/setup/databaseScripts/dbVersions.php b/setup/databaseScripts/dbVersions.php new file mode 100644 index 0000000..6c5508f --- /dev/null +++ b/setup/databaseScripts/dbVersions.php @@ -0,0 +1,19 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @release dbVersions.php,v 1.0 2014/10/31 19:31:47 cscott Exp $ + * @link http://dev.gaslightmedia.com/ + */ + +$glmMembersFieldsDbVersions = array( + '0.0.1' => array('version' => '0.0.1', 'tables' => 2, 'date' => '03/27/2017'), +); + diff --git a/setup/validActions.php b/setup/validActions.php index 79d0569..180782d 100644 --- a/setup/validActions.php +++ b/setup/validActions.php @@ -59,9 +59,13 @@ $glmMembersFieldsAddOnValidActions = array( 'adminActions' => array( + 'memberinfo' => array( + 'fields' => GLM_MEMBERS_FIELDS_PLUGIN_SLUG, + ), + 'management' => array( + 'fields' => GLM_MEMBERS_FIELDS_PLUGIN_SLUG, + ), ), 'frontActions' => array( ) ); - -?> \ No newline at end of file diff --git a/views/admin/management/fields.html b/views/admin/management/fields.html new file mode 100644 index 0000000..e958477 --- /dev/null +++ b/views/admin/management/fields.html @@ -0,0 +1,194 @@ +{include file='admin/management/header.html'} + + + + +
Add a Custom Field
+
+
+ + + + + + + + + + + + +
Field Name: + +
Field Type: + +
+

* Required

+ Cancel + +
+
+ + +
+
+

Are you sure you want to delete this field?

+

Yes, delete this field

+

Cancel

+
+
+ + +
+
+ + + + + + + + + + + + +
Field Name: + +
Field Type: + +
+

* Required

+ Cancel + +
+
+ + + + +
+ {if $settingsUpdated}

Settings Updated

{/if} + {if $settingsUpdateError}Settings Update Error{/if} +

Custom Fields

+
+ + + + + + + + + + + {if $haveCustomFields} + {assign var="i" value="0"} + {foreach $custom_fields as $t} + {if $i++ is odd by 1} + + {else} + + {/if} + + + + + + {/foreach} + {else} + + {/if} + +
IDFieldType 
{$t.id} + + + {$t.field_type.name} + +
Delete
+
(no custom fields listed)
+ + + diff --git a/views/admin/memberinfo/fields.html b/views/admin/memberinfo/fields.html new file mode 100644 index 0000000..db2e959 --- /dev/null +++ b/views/admin/memberinfo/fields.html @@ -0,0 +1,17 @@ + + {foreach $customFields as $field} + + + + + {/foreach} +
{$field.field_name} + {if $field.field_type.name == 'text'} + + {elseif $field.field_type.name == 'textarea'} + + {elseif $field.field_type.name == 'checkbox'} + + + {/if} +
-- 2.17.1