Skip to content

Commit

Permalink
Simple function extraction on editing Address (within main contact edit
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Mar 29, 2018
1 parent 775d19c commit eb1f284
Showing 1 changed file with 61 additions and 50 deletions.
111 changes: 61 additions & 50 deletions CRM/Contact/Form/Edit/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ..
}

}

0 comments on commit eb1f284

Please sign in to comment.