From eb1f2848fee4a52cd3556083b84d6c9dd5935e41 Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 30 Mar 2018 00:17:21 +1300 Subject: [PATCH] Simple function extraction on editing Address (within main contact edit --- CRM/Contact/Form/Edit/Address.php | 111 ++++++++++++++++-------------- 1 file changed, 61 insertions(+), 50 deletions(-) diff --git a/CRM/Contact/Form/Edit/Address.php b/CRM/Contact/Form/Edit/Address.php index 366d02ba44ac..a2dceba22aa5 100644 --- a/CRM/Contact/Form/Edit/Address.php +++ b/CRM/Contact/Form/Edit/Address.php @@ -158,56 +158,7 @@ public static function buildQuickForm(&$form, $addressBlockCount = NULL, $sharin } $form->assign('geoCode', $geoCode); - // Process any address custom data - - $groupTree = CRM_Core_BAO_CustomGroup::getTree('Address', NULL, $entityId); - - if (isset($groupTree) && is_array($groupTree)) { - // use simplified formatted groupTree - $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1, $form); - - // make sure custom fields are added /w element-name in the format - 'address[$blockId][custom-X]' - foreach ($groupTree as $id => $group) { - foreach ($group['fields'] as $fldId => $field) { - $groupTree[$id]['fields'][$fldId]['element_custom_name'] = $field['element_name']; - $groupTree[$id]['fields'][$fldId]['element_name'] = "address[$blockId][{$field['element_name']}]"; - } - } - - $defaults = array(); - CRM_Core_BAO_CustomGroup::setDefaults($groupTree, $defaults); - - // since we change element name for address custom data, we need to format the setdefault values - $addressDefaults = array(); - foreach ($defaults as $key => $val) { - if (empty($val)) { - continue; - } - - // inorder to set correct defaults for checkbox custom data, we need to converted flat key to array - // this works for all types custom data - $keyValues = explode('[', str_replace(']', '', $key)); - $addressDefaults[$keyValues[0]][$keyValues[1]][$keyValues[2]] = $val; - } - - $form->setDefaults($addressDefaults); - - // we setting the prefix to 'dnc_' below, so that we don't overwrite smarty's grouptree var. - // And we can't set it to 'address_' because we want to set it in a slightly different format. - CRM_Core_BAO_CustomGroup::buildQuickForm($form, $groupTree, FALSE, 'dnc_'); - - // during contact editing : if no address is filled - // required custom data must not produce 'required' form rule error - // more handling done in formRule func - CRM_Contact_Form_Edit_Address::storeRequiredCustomDataInfo($form, $groupTree); - - $tplGroupTree = CRM_Core_Smarty::singleton()->get_template_vars('address_groupTree'); - $tplGroupTree = empty($tplGroupTree) ? array() : $tplGroupTree; - - $form->assign('address_groupTree', $tplGroupTree + array($blockId => $groupTree)); - // unset the temp smarty var that got created - $form->assign('dnc_groupTree', NULL); - } - // address custom data processing ends .. + self::addCustomDataToForm($form, $entityId, $blockId); if ($sharing) { // shared address @@ -434,4 +385,64 @@ public static function storeRequiredCustomDataInfo(&$form, $groupTree) { } } + /** + * Add custom data to the form. + * + * @param CRM_Core_Form $form + * @param int $entityId + * @param int $blockId + */ + protected static function addCustomDataToForm(&$form, $entityId, $blockId) { + $groupTree = CRM_Core_BAO_CustomGroup::getTree('Address', NULL, $entityId); + + if (isset($groupTree) && is_array($groupTree)) { + // use simplified formatted groupTree + $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1, $form); + + // make sure custom fields are added /w element-name in the format - 'address[$blockId][custom-X]' + foreach ($groupTree as $id => $group) { + foreach ($group['fields'] as $fldId => $field) { + $groupTree[$id]['fields'][$fldId]['element_custom_name'] = $field['element_name']; + $groupTree[$id]['fields'][$fldId]['element_name'] = "address[$blockId][{$field['element_name']}]"; + } + } + + $defaults = array(); + CRM_Core_BAO_CustomGroup::setDefaults($groupTree, $defaults); + + // since we change element name for address custom data, we need to format the setdefault values + $addressDefaults = array(); + foreach ($defaults as $key => $val) { + if (empty($val)) { + continue; + } + + // inorder to set correct defaults for checkbox custom data, we need to converted flat key to array + // this works for all types custom data + $keyValues = explode('[', str_replace(']', '', $key)); + $addressDefaults[$keyValues[0]][$keyValues[1]][$keyValues[2]] = $val; + } + + $form->setDefaults($addressDefaults); + + // we setting the prefix to 'dnc_' below, so that we don't overwrite smarty's grouptree var. + // And we can't set it to 'address_' because we want to set it in a slightly different format. + CRM_Core_BAO_CustomGroup::buildQuickForm($form, $groupTree, FALSE, 'dnc_'); + + // during contact editing : if no address is filled + // required custom data must not produce 'required' form rule error + // more handling done in formRule func + CRM_Contact_Form_Edit_Address::storeRequiredCustomDataInfo($form, $groupTree); + + $tplGroupTree = CRM_Core_Smarty::singleton() + ->get_template_vars('address_groupTree'); + $tplGroupTree = empty($tplGroupTree) ? array() : $tplGroupTree; + + $form->assign('address_groupTree', $tplGroupTree + array($blockId => $groupTree)); + // unset the temp smarty var that got created + $form->assign('dnc_groupTree', NULL); + } + // address custom data processing ends .. + } + }