From 5ce0cf6b4d8069a429eeae996cdc8751dd9835a2 Mon Sep 17 00:00:00 2001 From: Chuck Scott Date: Fri, 15 Dec 2017 11:04:14 -0500 Subject: [PATCH] Added AJAX receiver for displaying a form segment (by fid) --- models/admin/ajax/customFields.php | 13 -- models/admin/ajax/customFieldsFront.php | 184 ++++++++++++++++++ models/front/customFields/formDisplay.php | 2 +- .../create_database_V1.0.1.sql | 10 +- setup/validActions.php | 3 +- views/front/customFields/displayForm.html | 5 +- 6 files changed, 194 insertions(+), 23 deletions(-) create mode 100644 models/admin/ajax/customFieldsFront.php diff --git a/models/admin/ajax/customFields.php b/models/admin/ajax/customFields.php index 3ed14b3..53fef32 100644 --- a/models/admin/ajax/customFields.php +++ b/models/admin/ajax/customFields.php @@ -62,19 +62,6 @@ class GlmMembersAdmin_ajax_customFields extends GlmDataFieldsCustomFields 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 diff --git a/models/admin/ajax/customFieldsFront.php b/models/admin/ajax/customFieldsFront.php new file mode 100644 index 0000000..9f82dd0 --- /dev/null +++ b/models/admin/ajax/customFieldsFront.php @@ -0,0 +1,184 @@ + + * @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_customFieldsFront 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 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 'displayForm': + + $actionData = array( + 'request' => array( + 'option' => $_REQUEST['option'], + 'fid' => $_REQUEST['fid'], + 'record-id' => $_REQUEST['recordId'], + 'parent-form-id' => $_REQUEST['parentFormId'], + 'form-data' => $_REQUEST['formData'], + ) + ); + + // Call the GlmMembersFront_customFields_formDisplay to let it do the work + require_once GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_PATH.'/models/front/customFields/formDisplay.php'; + $FormDisplay = new GlmMembersFront_customFields_formDisplay($this->wpdb, $this->config); + $res = $FormDisplay->modelAction($actionData); + + $html = $this->generateHTML($res['data'], $res['view']); + + echo $html; + wp_die(); + + break; + + default: + break; + } + + 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/front/customFields/formDisplay.php b/models/front/customFields/formDisplay.php index 3035dcf..3b87906 100644 --- a/models/front/customFields/formDisplay.php +++ b/models/front/customFields/formDisplay.php @@ -126,7 +126,7 @@ class GlmMembersFront_customFields_formDisplay extends GlmDataFieldsCustomFields // Also add default fieldFail flags foreach ($formFields as $fieldKey => $fieldVal) { - $form[$fieldKey]['field_fail'] = ''; + $formFields[$fieldKey]['field_fail'] = ''; } } diff --git a/setup/databaseScripts/create_database_V1.0.1.sql b/setup/databaseScripts/create_database_V1.0.1.sql index 2b49609..765f2a0 100644 --- a/setup/databaseScripts/create_database_V1.0.1.sql +++ b/setup/databaseScripts/create_database_V1.0.1.sql @@ -30,11 +30,11 @@ CREATE TABLE {prefix}custom_fields ( -- Data Table CREATE TABLE {prefix}custom_field_data ( id INT NOT NULL AUTO_INCREMENT, - field_id INT NOT NULL DEFAULT 0, -- Pointer to ID of custom_fields table for this field - record_id INT NOT NULL DEFAULT 0, -- id for this submission of this form - text_data TEXT NOT NULL DEFAULT '', -- Data for a text field - checkbox_data BOOLEAN NOT NULL DEFAULT false, -- Data for a boolean field - integer_data INTEGER NOT NULL DEFAULT 0, -- Data for an integer field + field_id INT NOT NULL DEFAULT 0, -- Pointer to ID of custom_fields table for this field + record_id INT NOT NULL DEFAULT 0, -- id for this submission of this form + text_data TEXT NOT NULL DEFAULT '', -- Data for a text field + checkbox_data BOOLEAN NOT NULL DEFAULT false, -- Data for a boolean field + integer_data INTEGER NOT NULL DEFAULT 0, -- Data for an integer field float_data FLOAT NOT NULL DEFAULT '0.00', -- Data for a float field PRIMARY KEY (id), INDEX (field_id), diff --git a/setup/validActions.php b/setup/validActions.php index 35724b3..b836cb4 100644 --- a/setup/validActions.php +++ b/setup/validActions.php @@ -60,7 +60,8 @@ $glmMembersCustomFieldsAddOnValidActions = array( 'adminActions' => array( 'ajax' => array( - 'customFields' => GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_SLUG + 'customFields' => GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_SLUG, + 'customFieldsFront' => GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_SLUG ), 'customFields' => array( 'index' => GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_SLUG diff --git a/views/front/customFields/displayForm.html b/views/front/customFields/displayForm.html index 3ccce59..6f2dc44 100644 --- a/views/front/customFields/displayForm.html +++ b/views/front/customFields/displayForm.html @@ -34,12 +34,11 @@ $('#{$parentFormId}').submit(function(e){ // If there's any required fields that aren't filled in - if ( + if ( false {assign var="testSep" value=''} {foreach $formFields as $field} {if $field.required.value && ( $field.field_type == 'text' || $field.field_type == 'text' ) } - {$testSep} $('#glmCustomFormField_{$field.id}').val() == '' - {$testSep='||'} + || $('#glmCustomFormField_{$field.id}').val() == '' {/if} {/foreach} ) { -- 2.17.1