Skip to content

Commit

Permalink
[REF] Code cleanup on location entities for the Contact Summary screen
Browse files Browse the repository at this point in the history
This builds on #22966 to improve loading of location entities.

It addresses 2 problems
1) the code is really confusing - handling the loading in 2 places
2) not all keys are loaded - resulting in enotices at the tpl layer.

Note that address still seems kinda tricky so I haven't worked through
that in this PR.
  • Loading branch information
eileenmcnaughton committed Mar 25, 2022
1 parent 2b2ecd5 commit 2e72655
Showing 1 changed file with 50 additions and 31 deletions.
81 changes: 50 additions & 31 deletions CRM/Contact/Page/View/Summary.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ public function edit() {
/**
* View summary details of a contact.
*
* @throws \API_Exception
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public function view() {
// Add js for tabs, in-place editing, and jstree for tags
Expand Down Expand Up @@ -151,12 +153,12 @@ public function view() {
$params['contact_id'] = $this->_contactId;

CRM_Contact_BAO_Contact::getValues($params, $defaults);
$defaults['im'] = CRM_Core_BAO_IM::getValues(['contact_id' => $params['contact_id']]);
$defaults['email'] = CRM_Core_BAO_Email::getValues(['contact_id' => $params['contact_id']]);
$defaults['openid'] = CRM_Core_BAO_OpenID::getValues(['contact_id' => $params['contact_id']]);
$defaults['phone'] = CRM_Core_BAO_Phone::getValues(['contact_id' => $params['contact_id']]);
$defaults['im'] = $this->getLocationValues($params['contact_id'], 'IM');
$defaults['email'] = $this->getLocationValues($params['contact_id'], 'Email');
$defaults['openid'] = $this->getLocationValues($params['contact_id'], 'OpenID');
$defaults['phone'] = $this->getLocationValues($params['contact_id'], 'Phone');
$defaults['address'] = CRM_Core_BAO_Address::getValues(['contact_id' => $params['contact_id']], TRUE);
CRM_Core_BAO_Website::getValues($params, $defaults);
$defaults['website'] = $this->getLocationValues($params['contact_id'], 'Website');
// Copy employer fields to the current_employer keys.
if (($defaults['contact_type'] === 'Individual') && $defaults['employer_id'] && $defaults['organization_name']) {
$defaults['current_employer'] = $defaults['organization_name'];
Expand All @@ -167,39 +169,15 @@ public function view() {
$mailingBackend = Civi::settings()->get('mailing_backend');
$this->assign('mailingOutboundOption', $mailingBackend['outBound_option']);

// @todo only address is still being special handled here.... Consolidate with other locations.
$communicationType = [
'phone' => [
'type' => 'phoneType',
'id' => 'phone_type',
'daoName' => 'CRM_Core_DAO_Phone',
'fieldName' => 'phone_type_id',
],
'im' => [
'type' => 'IMProvider',
'id' => 'provider',
'daoName' => 'CRM_Core_DAO_IM',
'fieldName' => 'provider_id',
],
'website' => [
'type' => 'websiteType',
'id' => 'website_type',
'daoName' => 'CRM_Core_DAO_Website',
'fieldName' => 'website_type_id',
],
'address' => ['skip' => TRUE, 'customData' => 1],
'email' => ['skip' => TRUE],
'openid' => ['skip' => TRUE],
'address' => ['customData' => 1],
];

foreach ($communicationType as $key => $value) {
if (!empty($defaults[$key])) {
foreach ($defaults[$key] as & $val) {
CRM_Utils_Array::lookupValue($val, 'location_type', CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', ['labelColumn' => 'display_name']), FALSE);
if (empty($value['skip'])) {
$daoName = $value['daoName'];
$pseudoConst = $daoName::buildOptions($value['fieldName'], 'get');
CRM_Utils_Array::lookupValue($val, $value['id'], $pseudoConst, FALSE);
}
}
if (isset($value['customData'])) {
foreach ($defaults[$key] as $blockId => $blockVal) {
Expand Down Expand Up @@ -478,4 +456,45 @@ public function getTabs() {
return $allTabs;
}

/**
* Get the values for the location entity for this contact.
*
* The form layer requires that we put the label values into keys too.
* Unfortunately smarty can't handle {$location_type_id:label} - ie
* the colon - so we need to map the value over in the php layer.
*
* @param int $contact_id
* @param string $entity
*
* @return array
* @throws \API_Exception
*/
protected function getLocationValues(int $contact_id, string $entity): array {
$fieldMap = [
'location_type_id' => 'location_type',
'provider_id' => 'provider',
'phone_type_id' => 'phone_type',
'website_type_id' => 'website_type',
];
$optionFields = array_keys((array) civicrm_api4($entity, 'getFields', [
'where' => [['options', 'IS NOT EMPTY'], ['name', 'IN', array_keys($fieldMap)]],
], 'name'));
$select = ['*', 'custom.*'];
foreach ($optionFields as $optionField) {
$select[] = $optionField . ':label';
}
$locationEntities = (array) civicrm_api4($entity, 'get', [
'select' => $select,
'where' => [['contact_id', '=', $contact_id]],
'orderBy' => $entity === 'Website' ? [] : ['is_primary' => 'DESC'],
], 'id');

foreach ($locationEntities as $index => $locationEntity) {
foreach ($optionFields as $optionField) {
$locationEntities[$index][$fieldMap[$optionField]] = $locationEntity[$optionField . ':label'];
}
}
return $locationEntities;
}

}

0 comments on commit 2e72655

Please sign in to comment.