Now collecting stored data when retrieving form fields with recordID
authorChuck Scott <cscott@gaslightmedia.com>
Tue, 19 Dec 2017 15:01:44 +0000 (10:01 -0500)
committerChuck Scott <cscott@gaslightmedia.com>
Tue, 19 Dec 2017 15:01:44 +0000 (10:01 -0500)
classes/data/dataCustomFields.php
models/front/customFields/formDisplay.php
views/front/customFields/displayForm.html

index 3b0be5f..a00fd8c 100644 (file)
@@ -73,13 +73,20 @@ class GlmDataFieldsCustomFields extends GlmDataAbstract
      */
     public $fields = false;
     /**
-     * MemberInfo DB object
+     * Get Stored Data Flag
      *
-     * @var $MemberInfo
+     * @var $getStoredData
      * @access public
      */
-    public $MemberInfo;
-
+    public $getStoredData = false;
+    /**
+     * Data Record ID
+     *
+     * @var $recordId
+     * @access public
+     */
+    public $recordId = false;
+    
     /**
      * Constructor
      *
@@ -219,9 +226,105 @@ class GlmDataFieldsCustomFields extends GlmDataAbstract
                 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 (!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 
+                $customFieldData = $CustomFieldData->getEntry($r['id'], 'field_id', "T.record_id = ".$this->recordId);
+                
+                // Place the save field data into the current result
+                $r = array_merge($r, array(
+                    'text_data'     => $customFieldData['text_data'],
+                    'checkbox_data' => $customFieldData['checkbox_data'],
+                    'integer_data' => $customFieldData['integer_data'],
+                    'float_data' => $customFieldData['float_data']
+                ));
+                
+                $r['stored'] = '';
+                
+                switch ($r['field_type']) {
+                    
+                    case 'text':
+                    case 'textarea':
+                        $r['stored'] = $customFieldData['text_data'];
+                        break;
+                    
+                    case 'checkbox':
+                        $r['stored'] = ($customFieldData['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 ($optVal['option_value'] == $customFieldData['text_data']) {
+                                    $val = true;
+                                    $name = 'Yes';
+                                }
+                                $r['options'][$optKey]['option_default']['value'] = $val;
+                                $r['options'][$optKey]['option_default']['name'] = $name;
+                            }
+                        }
+                        
+                        $r['stored'] = $optVal['stored'] = $csutomFieldData['text_data'];
+                        
+                        break;
+                    
+                    case 'integer':
+                        $r['stored'] = $customFieldData['integer_data'];
+                        break;
+                        
+                    case 'float':
+                        $r['stored'] = $customFieldData['float_data'];
+                        break;
+                        
+                }
+                
+                
+trigger_error('Stored Data: '.print_r($customFieldData,1), E_USER_NOTICE);                
+                
+            }
         
         }
         return $r;
     }
+    
+    /*
+     * Get form fields along with any stored data
+     *
+     * @param 
+     * @param string $a Action being performed (l, i, g, ...)
+     *
+     * @return object Class object
+     *
+     */
+    public function getFormWithData($fid, $recordId = false)
+    {
+        // Save current status of get stored data flag
+        $savedGetStoredDataState = $this->getStoredData;   
+        
+        // Set stored data flag and record ID
+        $this->getStoredData = true;
+        $this->recordId = $recordId;
+        
+        // Try to get the form
+        $form = $this->getList("fid = '$fid'");
+        
+        // Restore saved stored data flag
+        $this->getStoredData = $savedGetStoredDataState;
+
+        return $form;
+        
+    }
 
 }
index 8c33ed7..df6e1ad 100644 (file)
@@ -102,14 +102,14 @@ class GlmMembersFront_customFields_formDisplay extends GlmDataFieldsCustomFields
      */
     public function modelAction( $actionData = false )
     {
-trigger_error('********* WORKING IN formDisplay.php **********',E_USER_NOTICE);
+
         $formId     = false;
         $formFields = false;
         $haveForm   = false;
         $view       = 'displayForm.html';
         $parentFormId   = $actionData['request']['parent-form-id'];
+        $recordId   = false;
 
-trigger_error('Form Display Submit Data '.print_r($actionData,1),E_USER_NOTICE);        
         if (GLM_MEMBERS_PLUGIN_FRONT_DEBUG) {
             trigger_error('Shortcode Called: glm-members-customfields-form-display', E_USER_NOTICE);
         }
@@ -119,10 +119,19 @@ trigger_error('Form Display Submit Data '.print_r($actionData,1),E_USER_NOTICE);
 
             $formId = filter_var($actionData['request']['fid'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_STRIP_LOW);
 
+            // Check for a sane record ID
+            $recordId = ($actionData['request']['record-id'] - 0);
+            if ($recordId <= 0) {
+                $recordId = false;
+            }
+                
             // Try to retrieve the form
-            $formFields = $this->getList("T.fid = '$formId'");
+            $formFields = $this->getFormWithData($formId, $recordId);
+            
+            // If we received good data
             if ($formFields && count($formFields) > 0) {
 
+                // Have form fields
                 $haveForm = true;
 
                 // Also add default fieldFail flags
@@ -130,23 +139,9 @@ trigger_error('Form Display Submit Data '.print_r($actionData,1),E_USER_NOTICE);
                     $formFields[$fieldKey]['field_fail'] = '';
                 }
                 
-                // If there's a record ID
-                $recordId = ($actionData['request']['record-id'] - 0);
-                if ($recordId > 0) {
-trigger_error("fid = '$formId' AND record_id = $recordId",E_USER_NOTICE);   
-
-                    // Try to get any stored data
-                    $CustomFieldData = new GlmDataFieldsCustomFieldsData($this->wpdb, $this->config);
-                    $customFieldData = $CustomFieldData->getList("fid = '$formId' AND record_id = $recordId");
-trigger_error('Field Data = '.print_r($customFieldData,1), E_USER_NOTICE);                    
-                    
-                    
-                }
-
             }
 
         }
-trigger_error('Field Data = '.print_r($formFields,1), E_USER_NOTICE); 
         
         // Compile template data
         $templateData = array(
index 029a479..1deba44 100644 (file)
@@ -30,7 +30,7 @@
                                 <select id="glmCustomFormField_{$field.id}" type="checkbox" name="glmCustomFormField_{$field.id}">
                                     <option value=""></option>
       {foreach $field.options as $option} 
-                                    <option value="{$option.option_value}">&nbsp;&nbsp;{$option.option_text}
+                                    <option value="{$option.option_value}" {if $option.option_value.option_defaut}selected{/if}>&nbsp;&nbsp;{$option.option_text}
       {/foreach}                              
                                 </select>
                             </div>