From fa92ca0e9a3860a0b3adb414b6e9502b013967b5 Mon Sep 17 00:00:00 2001 From: Chuck Scott Date: Tue, 10 Apr 2018 16:18:57 -0400 Subject: [PATCH] Fixed loop on emty arrays in customFieldsSupport.php that was generating warnings in error log. --- classes/customFieldSupport.php | 135 +++++++++++++++++---------------- 1 file changed, 70 insertions(+), 65 deletions(-) diff --git a/classes/customFieldSupport.php b/classes/customFieldSupport.php index 73c16d5..54fa4cf 100644 --- a/classes/customFieldSupport.php +++ b/classes/customFieldSupport.php @@ -259,9 +259,11 @@ class GlmCustomFieldSupport extends GlmDataFieldsCustomFields // Build a list of form field IDs to use in checking for any existing submitted data for this fid and record ID combination $fieldIds = ''; $sep = ''; - foreach ($submitResult['formFields'] as $field) { - $fieldIds .= $sep.$field['id']; - $sep = ','; + if (is_array($submitResult['formFields']) && count($submitResult['formFields']) > 0) { + foreach ($submitResult['formFields'] as $field) { + $fieldIds .= $sep.$field['id']; + $sep = ','; + } } // Get any data already stored for these field IDs / record ID combination @@ -273,73 +275,76 @@ class GlmCustomFieldSupport extends GlmDataFieldsCustomFields $this->wpdb->query("START TRANSACTION"); // For each field - foreach ($submitResult['formFields'] as $field) { - - $fType = $field['field_type']; - - // If this is an update of existing data - if ($formUpdate) { - - $text_data = ($fType=='text'||$fType=='textarea'||$fType=='picklist' ? $field['submitted'] : ''); - $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', - array( - 'text_data' => $text_data, - 'checkbox_data' => $checkbox_data, - 'integer_data' => $integer_data, - 'float_data' => $float_data, - ), - array( - 'field_id' => $field['id'], - 'record_id' => $recordId - ), - array( - '%s', - '%d', - '%d', - '%f' - ), - array('%d', '%d') - ); + if (is_array($submitResult['formFields']) && count($submitResult['formFields']) > 0) { + foreach ($submitResult['formFields'] as $field) { + + $fType = $field['field_type']; + + // If this is an update of existing data + if ($formUpdate) { + + $text_data = ($fType=='text'||$fType=='textarea'||$fType=='picklist' ? $field['submitted'] : ''); + $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', + array( + 'text_data' => $text_data, + 'checkbox_data' => $checkbox_data, + 'integer_data' => $integer_data, + 'float_data' => $float_data, + ), + array( + 'field_id' => $field['id'], + 'record_id' => $recordId + ), + array( + '%s', + '%d', + '%d', + '%f' + ), + array('%d', '%d') + ); + + } else { + + $res = $this->wpdb->insert( + GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_DB_PREFIX.'custom_field_data', + array( + 'fid' => $formId, + 'field_id' => $field['id'], + 'record_id' => $recordId, + 'text_data' => ($fType=='text'||$fType=='textarea'||$fType=='picklist' ? $field['submitted'] : ''), + 'checkbox_data' => ($fType=='checkbox' ? $field['submitted'] : false), + 'integer_data' => ($fType=='integer' ? $field['submitted'] : false), + 'float_data' => ($fType=='float' ? $field['submitted'] : false), + ), + array( + '%s', + '%d', + '%d', + '%s', + '%d', + '%d', + '%f' + ) + ); - } else { - - $res = $this->wpdb->insert( - GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_DB_PREFIX.'custom_field_data', - array( - 'fid' => $formId, - 'field_id' => $field['id'], - 'record_id' => $recordId, - 'text_data' => ($fType=='text'||$fType=='textarea'||$fType=='picklist' ? $field['submitted'] : ''), - 'checkbox_data' => ($fType=='checkbox' ? $field['submitted'] : false), - 'integer_data' => ($fType=='integer' ? $field['submitted'] : false), - 'float_data' => ($fType=='float' ? $field['submitted'] : false), - ), - array( - '%s', - '%d', - '%d', - '%s', - '%d', - '%d', - '%f' - ) - ); + } - } + // If there was a failure, roll back all changes + if ($res === false) { + $this->wpdb->query("ROLLBACK"); + return false; + } - // If there was a failure, roll back all changes - if ($res === false) { - $this->wpdb->query("ROLLBACK"); - return false; } - } + } // for each field $this->wpdb->query("COMMIT"); -- 2.17.1