From 2f071021b581f7ec87c21132228462de78e76bfd Mon Sep 17 00:00:00 2001 From: Chuck Scott Date: Mon, 18 Dec 2017 13:17:43 -0500 Subject: [PATCH] Picklist fields have been added --- classes/customFieldSupport.php | 6 +- classes/data/dataCustomFields.php | 32 +++- classes/data/dataCustomFieldsOption.php | 185 ++++++++++++++++++++ config/plugin.ini | 2 +- models/admin/ajax/customFieldOptions.php | 201 ++++++++++++++++++++++ models/admin/ajax/customFields.php | 6 +- models/admin/customFields/index.php | 3 +- setup/validActions.php | 1 + views/admin/ajax/newField.html | 16 +- views/admin/ajax/newFieldOption.html | 12 ++ views/admin/customFields/index.html | 131 +++++++++----- views/front/customFields/displayForm.html | 13 ++ 12 files changed, 553 insertions(+), 55 deletions(-) create mode 100644 classes/data/dataCustomFieldsOption.php create mode 100644 models/admin/ajax/customFieldOptions.php create mode 100644 views/admin/ajax/newFieldOption.html diff --git a/classes/customFieldSupport.php b/classes/customFieldSupport.php index e110d31..1495d58 100644 --- a/classes/customFieldSupport.php +++ b/classes/customFieldSupport.php @@ -151,7 +151,8 @@ class GlmCustomFieldSupport extends GlmDataFieldsCustomFields case 'text': case 'textarea': - + case 'picklist': + $formField[$fieldId] = ''; // If the expected form field exists @@ -181,7 +182,7 @@ class GlmCustomFieldSupport extends GlmDataFieldsCustomFields $fieldRes['field_data'] = ($in ? 'Yes' : 'No'); break; - + default: break; @@ -284,6 +285,7 @@ class GlmCustomFieldSupport extends GlmDataFieldsCustomFields $checkbox_data = ($fType=='checkbox' ? $field['submitted'] : false); $integer_data = ($fType=='integer' ? $field['submitted'] : false); $float_data = ($fType=='float' ? $field['submitted'] : false); + $text_data = ($fType=='text'||$fType=='picklist' ? $field['submitted'] : ''); $res = $this->wpdb->update( GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_DB_PREFIX.'custom_field_data', diff --git a/classes/data/dataCustomFields.php b/classes/data/dataCustomFields.php index f106a69..3b0be5f 100644 --- a/classes/data/dataCustomFields.php +++ b/classes/data/dataCustomFields.php @@ -187,12 +187,40 @@ class GlmDataFieldsCustomFields extends GlmDataAbstract * @return object Class object * */ - public function entryPostProcessing($r, $a) + public function entryPostProcessing($r, $action) { // Get field type description from ini file. $r['field_type_descr'] = $this->config['custom_field_type'][$r['field_type']]; - + + // If doing the following actions + if (in_array($action, array('l','g'))) { + + // Try to get additional data for specific field types + switch ($r['field_type']) { + + case 'picklist': + + if (!isset($FieldOptions)) { + require_once GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_CLASS_PATH.'/data/dataCustomFieldsOption.php'; + $FieldOptions = new GlmDataFieldsCustomFieldsOption($this->wpdb, $this->config); + } + + // Try to get any options for this picklist + $options = $FieldOptions->getList('T.field_id = '.$r['id']); + + $r['options'] = false; + if (is_array($options) && count($options) > 0) { + $r['options'] = $options; + } + + break; + + default: + break; + } + + } return $r; } diff --git a/classes/data/dataCustomFieldsOption.php b/classes/data/dataCustomFieldsOption.php new file mode 100644 index 0000000..c2acf53 --- /dev/null +++ b/classes/data/dataCustomFieldsOption.php @@ -0,0 +1,185 @@ + + * @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 GlmDataFieldsCustomFieldsOption 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_CUSTOMFIELDS_PLUGIN_DB_PREFIX . 'custom_field_options'; + + /* + * Table Data Fields + */ + + $this->fields = array ( + + 'id' => array ( + 'field' => 'id', + 'type' => 'integer', + 'view_only' => true, + 'use' => 'a' + ), + + // Field ID + 'field_id' => array ( + 'field' => 'field_id', + 'type' => 'integer', + 'use' => 'a' + ), + + // Option Text - Displayed text for option + 'option_text' => array( + 'field' => 'option_text', + 'type' => 'text', + 'required' => true, + 'use' => 'a' + ), + + // Option Value - Value of this option + 'option_value' => array( + 'field' => 'option_value', + 'type' => 'text', + 'required' => true, + 'use' => 'a' + ), + + // Option Cost + 'option_cost' => array( + 'field' => 'option_cost', + 'type' => 'money', + 'default' => 0, + 'use' => 'a' + ), + + // Option Default + 'option_default' => array( + 'field' => 'option_default', + 'type' => 'checkbox', + '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 b4f66f4..0a3c535 100644 --- a/config/plugin.ini +++ b/config/plugin.ini @@ -12,6 +12,6 @@ custom_field_type['text'] = 'Single Line Text Field' custom_field_type['textarea'] = 'Multi-Line Text Area' custom_field_type['checkbox'] = 'Checkbox' -; custom_field_type['picklist'] = 'Picklist of Options' +custom_field_type['picklist'] = 'Picklist of Options' ; custom_field_type['integer'] = 'Integer Number' ; custom_field_type['float'] = 'Floating Point number' diff --git a/models/admin/ajax/customFieldOptions.php b/models/admin/ajax/customFieldOptions.php new file mode 100644 index 0000000..3f5fff5 --- /dev/null +++ b/models/admin/ajax/customFieldOptions.php @@ -0,0 +1,201 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @version 0.1 + */ + +// Load Custom Fields Picklist Options Data Class +require_once GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_CLASS_PATH.'/data/dataCustomFieldsOption.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_customFieldOptions extends GlmDataFieldsCustomFieldsOption +{ + + /** + * 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 'addNewOption': + + $fieldOption = $this->insertEntry(); + + if (!is_array($fieldOption) || !$fieldOption['status']) { + echo "0"; + } else { + + // Produce HTML for the new field + $viewFile = 'admin/ajax/newFieldOption.html'; + $fieldOption['fieldData']['fid'] = $_REQUEST['fid']; + + $newFieldHtml = $this->generateHTML($fieldOption, $viewFile); + echo $newFieldHtml; + + } + + break; + + case 'deleteOption': + + $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; + + 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/ajax/customFields.php b/models/admin/ajax/customFields.php index 53fef32..3d65f98 100644 --- a/models/admin/ajax/customFields.php +++ b/models/admin/ajax/customFields.php @@ -1,5 +1,4 @@ insertEntry(); @@ -99,7 +98,6 @@ class GlmMembersAdmin_ajax_customFields extends GlmDataFieldsCustomFields echo $newFieldHtml; } - wp_die(); break; case 'deleteField': @@ -117,13 +115,11 @@ class GlmMembersAdmin_ajax_customFields extends GlmDataFieldsCustomFields // Delete this custom field $this->deleteEntry($fieldId, true); echo true; - wp_die(); break; default: - wp_die(); } wp_die(); diff --git a/models/admin/customFields/index.php b/models/admin/customFields/index.php index 3985ba0..34a569f 100644 --- a/models/admin/customFields/index.php +++ b/models/admin/customFields/index.php @@ -194,7 +194,8 @@ class GlmMembersAdmin_customFields_index extends GlmDataFieldsCustomFields if ( isset($custom_fields) && $custom_fields && count( $custom_fields ) > 0 ) { $haveCustomFields = true; } - + + // Compile template data $template_data = array( 'option2' => $option, diff --git a/setup/validActions.php b/setup/validActions.php index b836cb4..3fdf509 100644 --- a/setup/validActions.php +++ b/setup/validActions.php @@ -61,6 +61,7 @@ $glmMembersCustomFieldsAddOnValidActions = array( 'adminActions' => array( 'ajax' => array( 'customFields' => GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_SLUG, + 'customFieldOptions' => GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_SLUG, 'customFieldsFront' => GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_SLUG ), 'customFields' => array( diff --git a/views/admin/ajax/newField.html b/views/admin/ajax/newField.html index 13dfedc..7f1aaea 100644 --- a/views/admin/ajax/newField.html +++ b/views/admin/ajax/newField.html @@ -3,13 +3,23 @@ {$fieldData.field_name} {$fieldData.field_type_descr} {$fieldData.required.name} - +
Delete
{if $fieldData.field_type=='picklist'} -
Add Option
+
Add Option
{/if} - Prompt: {$fieldData.field_prompt} \ No newline at end of file + Prompt: {$fieldData.field_prompt} + {if $fieldData.field_type=='picklist'} + + Option Value + Option Text + Cost + Default +   + + {/if} +   \ No newline at end of file diff --git a/views/admin/ajax/newFieldOption.html b/views/admin/ajax/newFieldOption.html new file mode 100644 index 0000000..031eb1d --- /dev/null +++ b/views/admin/ajax/newFieldOption.html @@ -0,0 +1,12 @@ +{* A line describing the new picklist option. *} + + + {$fieldData.option_value} + {$fieldData.option_text} + {$fieldData.option_cost} + {$fieldData.option_default.name} + +
Delete
+ + + diff --git a/views/admin/customFields/index.html b/views/admin/customFields/index.html index 0589337..bf85f70 100644 --- a/views/admin/customFields/index.html +++ b/views/admin/customFields/index.html @@ -81,7 +81,7 @@ - + @@ -89,7 +89,7 @@ - + @@ -108,7 +108,7 @@ - @@ -124,16 +124,39 @@ - - + + {if $t.field_type=='picklist'} + + + + + + + + {if $t.options} + {foreach $t.options as $option} + + + + + + + + {/foreach} + {/if} + {/if} + {/foreach} {/if} @@ -189,17 +212,20 @@ jQuery(document).ready(function($) { if (fieldHtml == '') { flashElement('{$fid}_NewFieldDialogError'); } else { - + + // Add new field to form $('#{$fid}_FieldsListBody').append(fieldHtml); $("#{$fid}_NewFieldDialog").dialog("close"); - // Need to rebind edit field buttons + // Need to rebind some buttons $('body').off('click', '.{$fid}_EditFieldButton', {$fid}_editFieldButton); $('body').on('click', '.{$fid}_EditFieldButton', {$fid}_editFieldButton); - - // Need to rebind delete field buttons $('body').off('click', '.{$fid}_DeleteFieldButton', {$fid}_deleteFieldButton); $('body').on('click', '.{$fid}_DeleteFieldButton', {$fid}_deleteFieldButton); + $('body').off('click', '.{$fid}_AddOptionButton', {$fid}_AddOptionButton); + $('body').on('click', '.{$fid}_AddOptionButton', {$fid}_AddOptionButton); + $('body').off('click', '#{$fid}_NewOptionCancel', {$fid}_NewOptionCancel); + $('body').on('click', '#{$fid}_NewOptionCancel', {$fid}_NewOptionCancel); } }); @@ -220,7 +246,7 @@ jQuery(document).ready(function($) { }); // Display form $('body').on('click', '.{$fid}_EditFieldButton', {$fid}_editFieldButton); - function {$fid}_editFieldButton() { + function {$fid}_editFieldButton(e) { var fieldID = $(this).attr('data-fieldId'); var fieldName = $(this).attr('data-fieldName'); var fieldPrompt = $(this).attr('data-fieldPrompt'); @@ -302,9 +328,10 @@ jQuery(document).ready(function($) { dialogClass: "glm-dialog-no-close" }); // Display form and reset all input - $('#{$fid}-add-option-button').click( function() { - var fieldId = $(this).attr('data-fieldId'); -alert(fieldId); + $('body').on('click', '.{$fid}_AddOptionButton', {$fid}_AddOptionButton); + function {$fid}_AddOptionButton(e) { + var {$fid}_fieldId = $(this).attr('data-fieldId'); + $("#{$fid}_NewOptionDialog").dialog("open"); $('#{$fid}_NewOptionName').val(''); $('#{$fid}_NewOptionText').val(''); @@ -312,23 +339,23 @@ alert(fieldId); $('#{$fid}_NewOptionCost').val(''); $('#{$fid}_NewOptionSubmit').data('data-fieldId', $(this).attr('data-fieldId')); - }); + }; // Submit form - $('#{$fid}_NewOptionSubmit').click( function() { -console.log($(this).attr('data-fieldId')); + $('#{$fid}_NewOptionSubmit').click( function(e) { + // Collect the new field data var formData = { - 'action': 'glm_members_admin_ajax', - 'glm_action': 'customFields', - 'option': 'addNewPicklistOption', - 'field_id': $(this).attr('data-fieldId'), - 'field_name': $('#{$fid}_NewFieldName').val(), - 'field_prompt': $('#{$fid}_NewFieldPrompt').val(), - 'field_type': $('#{$fid}_NewFieldType').val(), - 'required': $('#{$fid}_NewFieldRequired').is(':checked') + 'action': 'glm_members_admin_ajax', + 'glm_action': 'customFieldOptions', + 'option': 'addNewOption', + 'field_id': $('#{$fid}_NewOptionSubmit').data('data-fieldId'), + 'option_value': $('#{$fid}_NewOptionValue').val(), + 'option_text': $('#{$fid}_NewOptionText').val(), + 'option_cost': $('#{$fid}_NewOptionCost').val(), + 'option_default': $('#{$fid}_NewOptionDefault').is(':checked'), + 'fid': '{$fid}' }; -alert(formData.field_id); -return; + // Submit new field data - expect field new ID back $.ajax({ type: 'POST', @@ -337,30 +364,52 @@ return; encode: true, dataType: 'text' }) - .done( function(fieldHtml) { - if (fieldHtml == '') { + .done( function(optionHtml) { + if (optionHtml == '') { flashElement('{$fid}_NewFieldDialogError'); } else { - - $('#{$fid}_FieldsListBody').append(fieldHtml); - $("#{$fid}_NewFieldDialog").dialog("close"); + var {$fid}_fieldId = $('#{$fid}_NewOptionSubmit').data('data-fieldId'); + $('#EndOfFieldOptions_'+{$fid}_fieldId).before(optionHtml); + $("#{$fid}_NewOptionDialog").dialog("close"); - // Need to rebind edit field buttons - $('body').off('click', '.{$fid}_EditFieldButton', {$fid}_editFieldButton); - $('body').on('click', '.{$fid}_EditFieldButton', {$fid}_editFieldButton); - - // Need to rebind delete field buttons - $('body').off('click', '.{$fid}_DeleteFieldButton', {$fid}_deleteFieldButton); - $('body').on('click', '.{$fid}_DeleteFieldButton', {$fid}_deleteFieldButton); + // Need to rebind delete option buttons + $('body').off('click', '.DeleteOptionButton_{$fid}', DeleteOptionButton_{$fid}); + $('body').on('click', '.DeleteOptionButton_{$fid}', DeleteOptionButton_{$fid}); } }); }); // Cancel - $('#{$fid}_NewOptionCancel').click( function() { + $('body').on('click', '#{$fid}_NewOptionCancel', {$fid}_NewOptionCancel); + function {$fid}_NewOptionCancel(e) { $("#{$fid}_NewOptionDialog").dialog("close"); - }); + }; + // Delete + $('body').on('click', '.DeleteOptionButton_{$fid}', DeleteOptionButton_{$fid}); + function DeleteOptionButton_{$fid}(e) { + + var {$fid}_optionDeleteId = $(this).attr('data-fieldOptionId'); + // Delete the selected option and remove it from the display + var formData = { + 'action': 'glm_members_admin_ajax', + 'glm_action': 'customFieldOptions', + 'option': 'deleteOption', + 'id': {$fid}_optionDeleteId, + 'fid': '{$fid}' + }; + $.ajax({ + type: 'POST', + url: '{$ajaxUrl}', + data: formData, + encode: true, + dataType: 'text' + }) + .done( function(deleted) { + $('#FieldOptionRow_' + {$fid}_optionDeleteId).remove(); + }); + + }; // Flash an element for a short time function flashElement(id) { diff --git a/views/front/customFields/displayForm.html b/views/front/customFields/displayForm.html index 6f2dc44..029a479 100644 --- a/views/front/customFields/displayForm.html +++ b/views/front/customFields/displayForm.html @@ -23,6 +23,19 @@ {/if} + {if $field.field_type == 'picklist'} +
+
{$field.field_prompt}
+
+ +
+
+ {/if} {/foreach} {/if} -- 2.17.1
Option Name (for reference):
Displayed Text:
Selected by Dedault
Cost:Field Name Type Required +
Add a Custom Field
 
{$t.required.name} +
Delete
{if $t.field_type=='picklist'} -
Add Option
+
Add Option
{/if}
Prompt: {$t.field_prompt}
Prompt: {$t.field_prompt}
Option ValueOption TextCostDefault 
{$option.option_value}{$option.option_text}{$option.option_cost}{$option.option_default.name} +
Delete
+