From d26779ee7e8922350bfc0f3c4db2a1ba50f57944 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sat, 23 Oct 2021 13:58:43 +1300 Subject: [PATCH] [REF] Preliminary cleanup in update greeting --- CRM/Contact/BAO/Contact.php | 80 +++++++++++++++---------------------- 1 file changed, 32 insertions(+), 48 deletions(-) diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index 6ca6fe6607bc..ba04f38c7b76 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -1765,7 +1765,7 @@ public static function getHierContactDetails($contactId, $fields) { $returnProperties['household_name'] = 1; $returnProperties['contact_type'] = 1; $returnProperties['contact_sub_type'] = 1; - list($query) = CRM_Contact_BAO_Query::apiQuery($params, $returnProperties); + [$query] = CRM_Contact_BAO_Query::apiQuery($params, $returnProperties); return $query; } @@ -1789,7 +1789,7 @@ public static function &makeHierReturnProperties($fields, $contactId = NULL) { $multipleFields = ['website' => 'url']; foreach ($fields as $name => $dontCare) { if (strpos($name, '-') !== FALSE) { - list($fieldName, $id, $type) = CRM_Utils_System::explode('-', $name, 3); + [$fieldName, $id, $type] = CRM_Utils_System::explode('-', $name, 3); if (!in_array($fieldName, $multipleFields)) { if ($id == 'Primary') { @@ -2011,7 +2011,7 @@ public static function createProfileContact( CRM_Utils_Hook::pre('create', 'Profile', NULL, $params); } - list($data, $contactDetails) = self::formatProfileContactParams($params, $fields, $contactID, $ufGroupId, $ctype); + [$data, $contactDetails] = self::formatProfileContactParams($params, $fields, $contactID, $ufGroupId, $ctype); // manage is_opt_out if (array_key_exists('is_opt_out', $fields) && array_key_exists('is_opt_out', $params)) { @@ -2193,7 +2193,7 @@ public static function formatProfileContactParams( $primaryPhoneLoc = NULL; $session = CRM_Core_Session::singleton(); foreach ($params as $key => $value) { - list($fieldName, $locTypeId, $typeId) = CRM_Utils_System::explode('-', $key, 3); + [$fieldName, $locTypeId, $typeId] = CRM_Utils_System::explode('-', $key, 3); if ($locTypeId == 'Primary') { if ($contactID) { @@ -2774,11 +2774,14 @@ public static function updateGreetingsOnTokenFieldChange($updatedFields, $contac /** * Process greetings and cache. * - * @param object $contact + * @param \CRM_Contact_DAO_Contact $contact * Contact object after save. */ public static function processGreetings(&$contact) { + $emailGreetingString = self::getTemplateForGreeting('email_greeting', $contact); + $postalGreetingString = self::getTemplateForGreeting('postal_greeting', $contact); + $addresseeString = self::getTemplateForGreeting('addressee', $contact); //@todo this function does a lot of unnecessary loading. // ensureGreetingParamsAreSet now makes sure that the contact is // loaded and using updateGreetingsOnTokenFieldChange @@ -2796,24 +2799,8 @@ public static function processGreetings(&$contact) { CRM_Core_DAO::storeValues($contact, $contactDetails); $contactDetails = [[$contact->id => $contactDetails]]; - $emailGreetingString = $postalGreetingString = $addresseeString = NULL; $updateQueryString = []; - //cache email and postal greeting to greeting display - if ($contact->email_greeting_custom != 'null' && $contact->email_greeting_custom) { - $emailGreetingString = $contact->email_greeting_custom; - } - elseif ($contact->email_greeting_id != 'null' && $contact->email_greeting_id) { - // the filter value for Individual contact type is set to 1 - $filter = [ - 'contact_type' => $contact->contact_type, - 'greeting_type' => 'email_greeting', - ]; - - $emailGreeting = CRM_Core_PseudoConstant::greeting($filter); - $emailGreetingString = $emailGreeting[$contact->email_greeting_id]; - } - if ($emailGreetingString) { CRM_Contact_BAO_Contact_Utils::processGreetingTemplate($emailGreetingString, $contactDetails, @@ -2824,19 +2811,6 @@ public static function processGreetings(&$contact) { $updateQueryString[] = " email_greeting_display = '{$emailGreetingString}'"; } - //postal greetings - if ($contact->postal_greeting_custom !== 'null' && $contact->postal_greeting_custom) { - $postalGreetingString = $contact->postal_greeting_custom; - } - elseif ($contact->postal_greeting_id !== 'null' && $contact->postal_greeting_id) { - $filter = [ - 'contact_type' => $contact->contact_type, - 'greeting_type' => 'postal_greeting', - ]; - $postalGreeting = CRM_Core_PseudoConstant::greeting($filter); - $postalGreetingString = $postalGreeting[$contact->postal_greeting_id]; - } - if ($postalGreetingString) { CRM_Contact_BAO_Contact_Utils::processGreetingTemplate($postalGreetingString, $contactDetails, @@ -2847,20 +2821,6 @@ public static function processGreetings(&$contact) { $updateQueryString[] = " postal_greeting_display = '{$postalGreetingString}'"; } - // addressee - if ($contact->addressee_custom !== 'null' && $contact->addressee_custom) { - $addresseeString = $contact->addressee_custom; - } - elseif ($contact->addressee_id !== 'null' && $contact->addressee_id) { - $filter = [ - 'contact_type' => $contact->contact_type, - 'greeting_type' => 'addressee', - ]; - - $addressee = CRM_Core_PseudoConstant::greeting($filter); - $addresseeString = $addressee[$contact->addressee_id]; - } - if ($addresseeString) { CRM_Contact_BAO_Contact_Utils::processGreetingTemplate($addresseeString, $contactDetails, @@ -3524,6 +3484,30 @@ public static function on_hook_civicrm_post(\Civi\Core\Event\PostEvent $event) { } } + /** + * Get the template string for the given greeting. + * + * @param string $greetingType + * @param CRM_Contact_DAO_Contact $contact + * + * @return string + */ + private static function getTemplateForGreeting(string $greetingType, CRM_Contact_DAO_Contact $contact): string { + $customFieldName = $greetingType . '_custom'; + if (!CRM_Utils_System::isNull($contact->{$customFieldName})) { + return $contact->{$customFieldName}; + } + $idField = $greetingType . '_id'; + if (!is_numeric($contact->{$idField})) { + return ''; + } + $filter = [ + 'contact_type' => $contact->contact_type, + 'greeting_type' => $greetingType, + ]; + return CRM_Core_PseudoConstant::greeting($filter)[$contact->{$idField}]; + } + /** * @inheritDoc */