From c5bd9560777413780a1e2baf04c54a9de16a6144 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Mon, 23 Jul 2018 16:46:54 -0400 Subject: [PATCH] WIP grouped custom fields Adding groups into the custom fields. Can add group edit group delete group. Can add fields to groups Can position groups Can position fields within groups. --- classes/data/dataCustomFields.php | 6 + index.php | 2 +- models/admin/ajax/glmCFields.php | 208 ++++++++++++ models/admin/management/fields.php | 92 +++++- ..._V0.0.4.sql => create_database_V0.0.5.sql} | 14 +- setup/databaseScripts/dbVersions.php | 1 + .../update_database_V0.0.5.sql | 35 ++ setup/validActions.php | 1 + views/admin/management/fields.html | 308 ++++++++++++------ 9 files changed, 562 insertions(+), 105 deletions(-) create mode 100644 models/admin/ajax/glmCFields.php rename setup/databaseScripts/{create_database_V0.0.4.sql => create_database_V0.0.5.sql} (78%) create mode 100644 setup/databaseScripts/update_database_V0.0.5.sql diff --git a/classes/data/dataCustomFields.php b/classes/data/dataCustomFields.php index 1136351..d5f32b1 100644 --- a/classes/data/dataCustomFields.php +++ b/classes/data/dataCustomFields.php @@ -122,6 +122,12 @@ class GlmDataFieldsCustomFields extends GlmDataAbstract 'use' => 'a' ), + // Group Id + 'gid' => array( + 'field' => 'gid', + 'type' => 'integer', + 'use' => 'a', + ), // Event ID 'field_name' => array( 'field' => 'field_name', diff --git a/index.php b/index.php index de5126f..9c42f8c 100644 --- a/index.php +++ b/index.php @@ -38,7 +38,7 @@ * version from this plugin. */ define('GLM_MEMBERS_FIELDS_PLUGIN_VERSION', '1.0.4'); -define('GLM_MEMBERS_FIELDS_PLUGIN_DB_VERSION', '0.0.4'); +define('GLM_MEMBERS_FIELDS_PLUGIN_DB_VERSION', '0.0.5'); // This is the minimum version of the GLM Members DB plugin require for this plugin. define('GLM_MEMBERS_FIELDS_PLUGIN_MIN_MEMBERS_REQUIRED_VERSION', '2.8.0'); diff --git a/models/admin/ajax/glmCFields.php b/models/admin/ajax/glmCFields.php new file mode 100644 index 0000000..c7829a1 --- /dev/null +++ b/models/admin/ajax/glmCFields.php @@ -0,0 +1,208 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @version 0.1 + */ + +// Load Management Fields data abstract +require_once GLM_MEMBERS_FIELDS_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_glmCFields 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); + + } + + /* + * 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) + { + + switch($_REQUEST['option']) { + + case 'sortGroupOrder': // Save sort order when done with drag and drop of field in list + + // Fields should be in correct order in the submitted array and keys should be sequential from 0 + foreach ($_REQUEST['sortedIds'] as $sortKey=>$sortVal) { + + // Strip "field_" from field ID number + $fieldId = preg_replace("/[^0-9]/", "", $sortVal); + + // Update the field with the key as the order field + $this->wpdb->update( + GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . 'custom_field_groups', + array( 'group_order' => $sortKey ), + array( 'id' => $fieldId ), + array( '%d' ), + array( '%d' ) + ); + + // No need to reply to this + + } + break; + + case 'sortOrder': // Save sort order when done with drag and drop of field in list + + // Fields should be in correct order in the submitted array and keys should be sequential from 0 + foreach ($_REQUEST['sortedIds'] as $sortKey=>$sortVal) { + + // Strip "field_" from field ID number + $fieldId = preg_replace("/[^0-9]/", "", $sortVal); + + // Update the field with the key as the order field + $this->wpdb->update( + GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . 'custom_fields', + array( 'field_order' => $sortKey ), + array( 'id' => $fieldId ), + array( '%d' ), + array( '%d' ) + ); + + // No need to reply to this + + } + break; + + + default: + + } + + 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/management/fields.php b/models/admin/management/fields.php index 39dfdca..d3fa638 100644 --- a/models/admin/management/fields.php +++ b/models/admin/management/fields.php @@ -147,6 +147,76 @@ class GlmMembersAdmin_management_fields extends GlmDataFieldsCustomFields switch ($option) { + case 'updateNewGroup': + // echo '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; + $gid = filter_var( $_REQUEST['id'], FILTER_VALIDATE_INT ); + if ( $gid ) { + $this->wpdb->update( + GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . 'custom_field_groups', + array( + 'uid' => GLM_MEMBERS_CONTACTS_PLUGIN_SLUG, + 'group_name' => filter_var( $_REQUEST['group_name'] ), + 'publish' => filter_var( $_REQUEST['publish'], FILTER_VALIDATE_BOOLEAN ), + ), + array( 'id' => $gid ), + array( + '%s', + '%s', + '%s', + ), + array( '%d' ) + ); + } + break; + + case 'addNewGroup': + $group_order = 1; + // Add a group + // When adding a group get the last group_order + $max_group_order = $this->wpdb->get_var( + "SELECT max(group_order) + FROM " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . "custom_field_groups + WHERE uid = '" . GLM_MEMBERS_CONTACTS_PLUGIN_SLUG . "'" + ); + if ( $max_group_order ) { + // Set the new group to last place + $group_order = $max_group_order + 1; + } + $this->wpdb->insert( + GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . 'custom_field_groups', + array( + 'uid' => GLM_MEMBERS_CONTACTS_PLUGIN_SLUG, + 'group_name' => filter_var( $_REQUEST['group_name'] ), + 'group_order' => $group_order, + 'publish' => filter_var( $_REQUEST['publish'], FILTER_VALIDATE_BOOLEAN ), + ), + array( + '%s', + '%s', + '%d', + '%s', + ) + ); + break; + + case 'deleteGroup': + // echo '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; + $gid = filter_var( $_REQUEST['id'], FILTER_VALIDATE_INT ); + if ( $gid ) { + // Need to delete the group and the fields in the group + $this->wpdb->delete( + GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . 'custom_field_groups', + array( 'id' => $gid ), + array( '%d' ) + ); + $this->wpdb->delete( + GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . 'custom_fields', + array( 'gid' => $gid ), + array( '%d' ) + ); + } + break; + case 'addNew': $this->insertEntry(); break; @@ -194,6 +264,23 @@ class GlmMembersAdmin_management_fields extends GlmDataFieldsCustomFields // Get list of Custom Fields $custom_fields = $this->getList( $where ); + + // Get all groups + $groups = $this->wpdb->get_results( + "SELECT * + FROM " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . "custom_field_groups + WHERE uid = '" . GLM_MEMBERS_CONTACTS_PLUGIN_SLUG . "' + ORDER BY group_order", + ARRAY_A + ); + + if ( $groups ) { + foreach ( $groups as $key => $group ) { + $groups[$key]['custom_fields'] = $this->getList( "uid = '$uid' AND gid = {$group['id']}", 'field_order' ); + } + // echo '
$groups: ' . print_r( $groups, true ) . '
'; + } + // echo "
REQUEST " . print_r($_REQUEST, true) . "
"; // echo "
GET " . print_r($_GET, true) . "
"; if ( isset($custom_fields) && $custom_fields && count( $custom_fields ) > 0 ) { @@ -205,11 +292,12 @@ class GlmMembersAdmin_management_fields extends GlmDataFieldsCustomFields 'option2' => $option, 'settingsUpdated' => $settings_updated, 'settingsUpdateError' => $settings_update_error, - 'custom_fields' => $custom_fields, + 'custom_fields' => false,//$custom_fields, 'field_types' => $this->config['custom_field_types'], 'haveCustomFields' => $haveCustomFields, 'uid' => $uid, - 'glm_action' => $glm_action + 'glm_action' => $glm_action, + 'groups' => $groups, ); // echo "
Template data:" . print_r($template_data, true) . "
"; diff --git a/setup/databaseScripts/create_database_V0.0.4.sql b/setup/databaseScripts/create_database_V0.0.5.sql similarity index 78% rename from setup/databaseScripts/create_database_V0.0.4.sql rename to setup/databaseScripts/create_database_V0.0.5.sql index 9deb7c2..2c08da2 100644 --- a/setup/databaseScripts/create_database_V0.0.4.sql +++ b/setup/databaseScripts/create_database_V0.0.5.sql @@ -15,7 +15,7 @@ -- Field Setup Table CREATE TABLE {prefix}custom_fields ( id INT NOT NULL AUTO_INCREMENT, - fid TEXT NOT NULL DEFAULT '', + gid TEXT NOT NULL DEFAULT '', -- Field Id (for groups) 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 @@ -51,3 +51,15 @@ CREATE TABLE {prefix}custom_field_options ( PRIMARY KEY (id), INDEX (field_id) ); + +---- + +-- Group Table +CREATE TABLE {prefix}custom_field_groups ( + id INT NOT NULL AUTO_INCREMENT, + uid TINYTEXT NOT NULL DEFAULT '', -- id for the series of custom fields associated with an entity + group_name TINYTEXT NOT NULL, -- Name of Section/Group + group_order SMALLINT NOT NULL DEFAULT 0, -- Group Order/Position + publish BOOLEAN NULL DEFAULT false, -- Bool to publish group to directory + PRIMARY KEY (id) +); diff --git a/setup/databaseScripts/dbVersions.php b/setup/databaseScripts/dbVersions.php index dc0810e..5d2bfa8 100644 --- a/setup/databaseScripts/dbVersions.php +++ b/setup/databaseScripts/dbVersions.php @@ -18,5 +18,6 @@ $glmMembersFieldsDbVersions = array( '0.0.2' => array('version' => '0.0.2', 'tables' => 2, 'date' => '04/14/2017'), '0.0.3' => array('version' => '0.0.3', 'tables' => 2, 'date' => '04/24/2017'), '0.0.4' => array('version' => '0.0.4', 'tables' => 2, 'date' => '10/18/2017'), + '0.0.5' => array('version' => '0.0.5', 'tables' => 4, 'date' => '07/23/2018'), ); diff --git a/setup/databaseScripts/update_database_V0.0.5.sql b/setup/databaseScripts/update_database_V0.0.5.sql new file mode 100644 index 0000000..837a5dd --- /dev/null +++ b/setup/databaseScripts/update_database_V0.0.5.sql @@ -0,0 +1,35 @@ +-- Gaslight Media Members Database +-- File Created: 2017-04-14 +-- Database Version: 0.0.2 +-- Database Update From Previous Version Script +-- +-- To permit each query below to be executed separately, +-- all queries must be separated by a line with four dashes + +-- Add fid to custom_fields +ALTER TABLE {prefix}custom_fields ADD gid TEXT NOT NULL DEFAULT ''; -- Field Id (for groups) + +---- + +-- Picklist Field Options +CREATE TABLE {prefix}custom_field_options ( + id INT NOT NULL AUTO_INCREMENT, + field_id INT NOT NULL DEFAULT 0, -- Pointer to ID of fields in custom_fields table + option_text TINYTEXT NOT NULL DEFAULT '', -- Option's Displayed text + option_value TINYTEXT NOT NULL DEFAULT '', -- Option's value + option_default BOOLEAN NOT NULL DEFAULT false, -- Flag indicating that this option is the default + PRIMARY KEY (id), + INDEX (field_id) +); + +---- + +-- Group Table +CREATE TABLE {prefix}custom_field_groups ( + id INT NOT NULL AUTO_INCREMENT, + uid TINYTEXT NOT NULL DEFAULT '', -- id for the series of custom fields associated with an entity + group_name TINYTEXT NOT NULL, -- Name of Section/Group + group_order SMALLINT NOT NULL DEFAULT 0, -- Group Order/Position + publish BOOLEAN NULL DEFAULT false, -- Bool to publish group to directory + PRIMARY KEY (id) +); diff --git a/setup/validActions.php b/setup/validActions.php index db6208d..63feb8f 100644 --- a/setup/validActions.php +++ b/setup/validActions.php @@ -61,6 +61,7 @@ $glmMembersFieldsAddOnValidActions = array( 'adminActions' => array( 'ajax' => array( 'filterSearch' => GLM_MEMBERS_FIELDS_PLUGIN_SLUG, + 'glmCFields' => GLM_MEMBERS_FIELDS_PLUGIN_SLUG, ), 'entity' => array( 'fields' => GLM_MEMBERS_FIELDS_PLUGIN_SLUG, diff --git a/views/admin/management/fields.html b/views/admin/management/fields.html index 13a6e2e..bc3e8b3 100644 --- a/views/admin/management/fields.html +++ b/views/admin/management/fields.html @@ -1,52 +1,32 @@ - -
Add a Custom Field
-
-
- - - +{* Add Custom Field Group Button and Dialog Box *} +
Add A Custom Field Group
+ +
+ + + + + - - - - - - - - - - + + - + -
Field Name: - -
Field Type: - -
Admin Searchable - - - (text or checkbox only) - Group Name:
Required?Publish to Front End - - + +
-

* Required

- Cancel + Cancel
- +{* Delete Field Dialog Box *}

Are you sure you want to delete this field?

@@ -55,13 +35,25 @@
- +{* Delete Group Dialog Box *} +
+
+

Are you sure you want to delete this Group?

+

All Fields in this group will be deleted.

+

Yes, delete this Group

+

Cancel

+
+
+ + +{* Edit Field Dialog Box *}
- - - - + + + + + @@ -79,8 +71,6 @@ - - {if $uid == 'glm-member-db'} - {/if}
Field Name:
Admin Searchable @@ -89,7 +79,6 @@ (text or checkbox only)
Required? @@ -101,9 +90,11 @@

* Required

Cancel - +
+ +{* Updates / Errors *}
@@ -113,65 +104,113 @@
- - - - - - - - - - - - - - {if $haveCustomFields} - {assign var="i" value="0"} - {foreach $custom_fields as $t} - {if $i++ is odd by 1} - - {else} - - {/if} - - - - - - - - - {/foreach} - {else} - - {/if} - -
IDFieldTypeUID/EntityRequiredAdmin Searchable 
{$t.id} - - - {$t.field_type.name} - - {$t.uid} - - {$t.required.name} - - {$t.admin_search.name} - -
Delete
-
(no custom fields listed)
- + +{* custom fields table *} +
+ {if $groups} + {foreach $groups as $group} +
+
+ {$group.group_name} + ( {if $group.publish}Published{else}Not Published{/if} ) + Delete + Edit +
+ + + + + + + + + + + + {if $group.custom_fields} + {foreach $group.custom_fields as $field} + + + + + + + + {/foreach} + {/if} + +
Field Name Field Type Required Admin Search +
Add a Custom Field
+
+ {$field.field_name} + {$field.field_type.name} {$field.required.name} {$field.admin_search.name} +
Delete
+ +
Edit
+
+
+ {/foreach} + {/if} +