From d89a0d9c15f45c8d1fc188970e56199af9bfce90 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 18 Mar 2022 14:30:15 +1300 Subject: [PATCH] [REF] Code cleanup on location entities for the Contact Summary screen This builds on https://github.com/civicrm/civicrm-core/pull/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. --- CRM/Contact/Page/View/Summary.php | 81 +++++++++++++++++++------------ 1 file changed, 50 insertions(+), 31 deletions(-) diff --git a/CRM/Contact/Page/View/Summary.php b/CRM/Contact/Page/View/Summary.php index e6fe5b3af3fa..b24e6c555474 100644 --- a/CRM/Contact/Page/View/Summary.php +++ b/CRM/Contact/Page/View/Summary.php @@ -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 @@ -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']; @@ -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) { @@ -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; + } + }