From 1205b91829c05713e649cb31b3c03b4aa03fc749 Mon Sep 17 00:00:00 2001 From: Chuck Scott Date: Thu, 21 Dec 2017 12:36:23 -0500 Subject: [PATCH] Added custom forms recall, and addded costs with totals and enabled priority display in cart summaries. Removed extraneous trigger_error statements used for testing. --- classes/customFieldSupport.php | 6 +- classes/data/dataCustomFields.php | 105 ++++++++++++++-------- classes/data/dataCustomFieldsOption.php | 2 +- setup/commonHooks.php | 11 +-- views/admin/customFields/index.html | 32 +++++-- views/front/customFields/displayForm.html | 3 +- 6 files changed, 96 insertions(+), 63 deletions(-) diff --git a/classes/customFieldSupport.php b/classes/customFieldSupport.php index 60007ee..73c16d5 100644 --- a/classes/customFieldSupport.php +++ b/classes/customFieldSupport.php @@ -1,6 +1,4 @@ 'checkbox', 'use' => 'a' ), - + // required field 'required' => array( 'field' => 'required', @@ -206,44 +206,44 @@ class GlmDataFieldsCustomFields extends GlmDataAbstract // 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 + // 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; } - + // If get stored data flag is set and we have a stored data record ID if ($this->getStoredData && $this->recordId) { - - // If we don't have it yet, instatiate the custom field data class + + // If we don't have it yet, instatiate the custom field data class if (!isset($CustomFieldData)) { require_once GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_CLASS_PATH . '/data/dataCustomFieldsData.php'; $CustomFieldData = new GlmDataFieldsCustomFieldsData($this->wpdb, $this->config); } - - // Get stored data for the current field + + // Get stored data for the current field $customFieldData = $CustomFieldData->getEntry($r['id'], 'field_id', "T.record_id = ".$this->recordId); // Place the save field data into the current result @@ -253,63 +253,73 @@ class GlmDataFieldsCustomFields extends GlmDataAbstract 'integer_data' => $customFieldData['integer_data'], 'float_data' => $customFieldData['float_data'] )); - + + // Add a few data shortcuts $r['stored'] = ''; - + $r['cost'] = '0'; + switch ($r['field_type']) { - + case 'text': case 'textarea': $r['stored'] = $r['text_data']; break; - + case 'checkbox': $r['stored'] = ($r['checkbox_data'] ? 'Yes' : 'No'); break; - + case 'picklist': - + // set default picklist option if (isset($r['options']) && count($options) > 0) { foreach($r['options'] as $optKey=>$optVal) { + $val = false; $name = 'No'; + + // If this option is the selected one if ($optVal['option_value'] == $customFieldData['text_data']) { + $val = true; $name = 'Yes'; + + // Add shortcuts for some useful data $r['stored'] = $optVal['option_value']; - $r['option_text'] = $optVal['option_text']; + $r['cost'] = $optVal['option_cost']; } + $r['options'][$optKey]['option_default']['value'] = $val; $r['options'][$optKey]['option_default']['name'] = $name; + } } - + break; - + case 'integer': $r['stored'] = $r['integer_data']; break; - + case 'float': $r['stored'] = $r['float_data']; break; - + } } - -ini_set(log_errors_max_len,4096); -trigger_error('Form Fields: '.print_r($r, 1), E_USER_NOTICE); - + + // ini_set('log_errors_max_len',4096); + // trigger_error('Form Fields: '.print_r($r, 1), E_USER_NOTICE); + } return $r; } - + /* * Get form fields along with any stored data * - * @param + * @param * @param string $a Action being performed (l, i, g, ...) * * @return object Class object @@ -318,20 +328,37 @@ trigger_error('Form Fields: '.print_r($r, 1), E_USER_NOTICE); public function getFormWithData($fid, $recordId = false) { // Save current status of get stored data flag - $savedGetStoredDataState = $this->getStoredData; - + $savedGetStoredDataState = $this->getStoredData; + // Set stored data flag and record ID $this->getStoredData = true; $this->recordId = $recordId; - + + $result = array( + 'form' => false, + 'hasPriorityField' => false, + 'totalCost' => 0 + ); + // Try to get the form - $form = $this->getList("fid = '$fid'"); - + $result['form'] = $this->getList("fid = '$fid'"); + + // Check if there's any priority display fields or fields with selected cost + foreach ($result['form'] as $field) { + if ($field['priority_display']['value'] || $field['cost'] > 0) { + $result['hasPriorityField'] = true; + } + + if ($field['cost'] > 0) { + $result['totalCost'] += $field['cost']; + } + } + // Restore saved stored data flag $this->getStoredData = $savedGetStoredDataState; - return $form; - + return $result; + } } diff --git a/classes/data/dataCustomFieldsOption.php b/classes/data/dataCustomFieldsOption.php index c2acf53..95dd95a 100644 --- a/classes/data/dataCustomFieldsOption.php +++ b/classes/data/dataCustomFieldsOption.php @@ -148,7 +148,7 @@ class GlmDataFieldsCustomFieldsOption extends GlmDataAbstract // Option Cost 'option_cost' => array( 'field' => 'option_cost', - 'type' => 'money', + 'type' => 'float', 'default' => 0, 'use' => 'a' ), diff --git a/setup/commonHooks.php b/setup/commonHooks.php index 1de61a4..0003c1f 100644 --- a/setup/commonHooks.php +++ b/setup/commonHooks.php @@ -206,15 +206,10 @@ add_filter( 'glm-members-customfields-form-data-recall', function( $content, $fi trigger_error('Custom Fields Filter Called: glm-members-customfields-form-data-recall', E_USER_NOTICE); } - require_once GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_CLASS_PATH.'/data/dataCustomField.php'; - $CustomFields = new GlmCustomFields($this->wpdb, $this->config); - - $form = $CustomFieldSupport->getFormWithData($fid, $recordId, $priorityDisplay); - -ini_set(log_errors_max_len, 5192); -trigger_error('Form Data Recall: '.print_r($form, 1), E_USER_NOTICE); + require_once GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_CLASS_PATH.'/data/dataCustomFields.php'; + $CustomFields = new GlmDataFieldsCustomFields($this->wpdb, $this->config); - // Consider using a shortcode to build the HTML + $content = $CustomFields->getFormWithData($fid, $recordId, $priorityDisplay); return $content; diff --git a/views/admin/customFields/index.html b/views/admin/customFields/index.html index a657777..cc1d6d4 100644 --- a/views/admin/customFields/index.html +++ b/views/admin/customFields/index.html @@ -74,6 +74,10 @@ + + Priority: + + Required: @@ -136,7 +140,14 @@ {$t.required.name} -
Delete
{if $t.field_type=='picklist'} @@ -194,6 +205,7 @@ jQuery(document).ready(function($) { $('#{$fid}_NewFieldName').val(''); $('#{$fid}_NewFieldPrompt').val(''); $('#{$fid}_NewFieldType').prop('selectedIndex',0); + $('#{$fid}_NewFieldPriorityDisplay').removeAttr('checked'); $('#{$fid}_NewFieldRequired').removeAttr('checked'); }); // Submit form @@ -201,14 +213,15 @@ jQuery(document).ready(function($) { // Collect the new field data var formData = { - 'action': 'glm_members_admin_ajax', - 'glm_action': 'customFields', - 'option': 'addNewField', - 'fid': '{$fid}', - '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': 'customFields', + 'option': 'addNewField', + 'fid': '{$fid}', + 'field_name': $('#{$fid}_NewFieldName').val(), + 'field_prompt': $('#{$fid}_NewFieldPrompt').val(), + 'field_type': $('#{$fid}_NewFieldType').val(), + 'priority_display': $('#{$fid}_NewFieldPriorityDisplay').is(':checked'), + 'required': $('#{$fid}_NewFieldRequired').is(':checked') }; // Submit new field data - expect field new ID back @@ -262,6 +275,7 @@ jQuery(document).ready(function($) { var fieldName = $(this).attr('data-fieldName'); var fieldPrompt = $(this).attr('data-fieldPrompt'); var fieldType = $(this).attr('data-fieldType'); + var priority = $(this).attr('data-priority'); var required = $(this).attr('data-required'); $('#editFieldID').val(fieldID); diff --git a/views/front/customFields/displayForm.html b/views/front/customFields/displayForm.html index 12a1b45..37d9939 100644 --- a/views/front/customFields/displayForm.html +++ b/views/front/customFields/displayForm.html @@ -1,5 +1,5 @@ {if $haveForm} - {foreach $formFields as $field} + {foreach $formFields.form as $field} {if $field.field_type == 'text'}
{$field.field_prompt}
@@ -67,4 +67,3 @@ }); {/if} - \ No newline at end of file -- 2.17.1