From 04450ed05a5e0ed42dd85727b98c24705f3e230a Mon Sep 17 00:00:00 2001 From: omar abu hussein Date: Tue, 2 Aug 2016 17:15:55 +0100 Subject: [PATCH] Fix Address fields not being updated --- includes/utils.inc | 15 ++++++++++ includes/wf_crm_webform_postprocess.inc | 38 +++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/includes/utils.inc b/includes/utils.inc index 81b5598d4..f5c9501f2 100644 --- a/includes/utils.inc +++ b/includes/utils.inc @@ -1593,6 +1593,21 @@ function wf_crm_location_fields() { return array('address', 'email', 'phone', 'website', 'im'); } +/** + * These are the address fields this module supports + * + * @return array + */ +function wf_crm_address_fields() { + return array( + 'street_address', + 'city', + 'state_province_id', + 'country_id', + 'postal_code' + ); +} + /** * Returns a count of children of a webform component * diff --git a/includes/wf_crm_webform_postprocess.inc b/includes/wf_crm_webform_postprocess.inc index f9dc803e9..501ad3a57 100644 --- a/includes/wf_crm_webform_postprocess.inc +++ b/includes/wf_crm_webform_postprocess.inc @@ -2204,7 +2204,6 @@ class wf_crm_webform_postprocess extends wf_crm_webform_base { $index = 1; $entityTypeIdIndex = $entity.'_type_id'; $entity = $entity == 'website' ? 'url' : $entity; // for website only - $submittedLocationValues = self::unsetEmptyValueIndexes($submittedLocationValues, $entity); foreach ($existingLocationValues as $eValue) { $existingLocationTypeId = $entity != 'url' ? $eValue['location_type_id'] : NULL; @@ -2219,6 +2218,24 @@ class wf_crm_webform_postprocess extends wf_crm_webform_base { $reorderedArray[$index][$entity] = $eValue[$entity]; + // address field contain many sub fields and should be handled differently + if ($entity != 'address') { + $submittedLocationValues = self::unsetEmptyValueIndexes($submittedLocationValues, $entity); + $reorderedArray[$index][$entity] = $eValue[$entity]; + } + else { + foreach (wf_crm_address_fields() as $field) { + $reorderedArray[$index][$field] = isset($eValue[$field]) ? $eValue[$field] : ''; + } + // handle supplemental addresses + $subAddressIndex = 1; + $subAddField = 'supplemental_address_' . $subAddressIndex; + while(isset($eValue[$subAddField])) { + $reorderedArray[$index][$subAddField] = $eValue[$subAddField]; + $subAddField = 'supplemental_address_' . ++$subAddressIndex; + } + } + foreach ($submittedLocationValues as $key => $sValue) { $sLocationTypeId = isset($sValue['location_type_id']) ? $sValue['location_type_id'] : NULL; $sEntityTypeId = isset($sValue[$entityTypeIdIndex]) ? $sValue[$entityTypeIdIndex] : NULL; @@ -2229,7 +2246,24 @@ class wf_crm_webform_postprocess extends wf_crm_webform_base { || ($existingLocationTypeId == $sLocationTypeId && $existingEntityTypeId == $sEntityTypeId)) { - $reorderedArray[$index][$entity] = $sValue[$entity]; + // address field contain many sub fields and should be handled differently + if ($entity != 'address') { + $reorderedArray[$index][$entity] = $sValue[$entity]; + } + else { + foreach (wf_crm_address_fields() as $field) { + if (isset($sValue[$field])) { + $reorderedArray[$index][$field] = $sValue[$field]; + } + } + // handle supplemental addresses + $subAddressIndex = 1; + $subAddField = 'supplemental_address_' . $subAddressIndex; + while(isset($sValue[$subAddField])) { + $reorderedArray[$index][$subAddField] = $sValue[$subAddField]; + $subAddField = 'supplemental_address_' . ++$subAddressIndex; + } + } unset($submittedLocationValues[$key]); } }