Skip to content

Commit

Permalink
Merge pull request #22967 from eileenmcnaughton/ret2
Browse files Browse the repository at this point in the history
[REF] Code cleanup on location entities for the Contact Summary screen
  • Loading branch information
demeritcowboy authored Apr 10, 2022
2 parents 6e3e642 + 70af047 commit d1bae2d
Showing 1 changed file with 53 additions and 38 deletions.
91 changes: 53 additions & 38 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 All @@ -122,7 +124,6 @@ public function view() {
$session->pushUserContext($url);
$this->assignFieldMetadataToTemplate('Contact');

$params = [];
$defaults = [
// Set empty default values for these - they will be overwritten when the contact is
// loaded in CRM_Contact_BAO_Contact::retrieve if there are real values
Expand All @@ -144,19 +145,16 @@ public function view() {
// for Demographics.tpl
'age' => ['y' => '', 'm' => ''],
'birth_date' => '',
// for Website.tpl (the others don't seem to enotice for some reason).
'website' => [],
];

$params['contact_id'] = $this->_contactId;

CRM_Contact_BAO_Contact::getValues(array_merge(['id' => $this->_contactId], $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['address'] = CRM_Core_BAO_Address::getValues(['contact_id' => $params['contact_id']], TRUE);
CRM_Core_BAO_Website::getValues($params, $defaults);
CRM_Contact_BAO_Contact::getValues(['id' => $this->_contactId], $defaults);
$defaults['im'] = $this->getLocationValues($this->_contactId, 'IM');
$defaults['email'] = $this->getLocationValues($this->_contactId, 'Email');
$defaults['openid'] = $this->getLocationValues($this->_contactId, 'OpenID');
$defaults['phone'] = $this->getLocationValues($this->_contactId, 'Phone');
// This microformat magic is still required...
$defaults['address'] = CRM_Core_BAO_Address::getValues(['contact_id' => $this->_contactId], TRUE);
$defaults['website'] = $this->getLocationValues($this->_contactId, 'Website');
// Copy employer fields to the current_employer keys.
if (($defaults['contact_type'] === 'Individual') && !empty($defaults['employer_id']) && !empty($defaults['organization_name'])) {
$defaults['current_employer'] = $defaults['organization_name'];
Expand All @@ -167,39 +165,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 +452,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 d1bae2d

Please sign in to comment.