From d20c5e6c42de9b2df039b3fb8116d6830b015e77 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 | 81 ++++++++++-------------- tests/phpunit/api/v3/CustomValueTest.php | 19 +++--- 2 files changed, 42 insertions(+), 58 deletions(-) diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index 6ca6fe6607..64a14674ca 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -1765,7 +1765,7 @@ WHERE civicrm_contact.id = " . CRM_Utils_Type::escape($id, 'Integer'); $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 @@ WHERE civicrm_contact.id = " . CRM_Utils_Type::escape($id, 'Integer'); $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 @@ ORDER BY civicrm_email.is_primary DESC"; 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 @@ ORDER BY civicrm_email.is_primary DESC"; $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) { @@ -2766,6 +2766,7 @@ LEFT JOIN civicrm_email ON ( civicrm_contact.id = civicrm_email.contact_id ) // @todo this is still reloading the whole contact -fix to be more selective & use pre-loaded. $contact = new CRM_Contact_BAO_Contact(); $contact->id = $contactID; + $contact->find(TRUE); CRM_Contact_BAO_Contact::processGreetings($contact); } } @@ -2774,11 +2775,14 @@ LEFT JOIN civicrm_email ON ( civicrm_contact.id = civicrm_email.contact_id ) /** * 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 +2800,8 @@ LEFT JOIN civicrm_email ON ( civicrm_contact.id = civicrm_email.contact_id ) 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 +2812,6 @@ LEFT JOIN civicrm_email ON ( civicrm_contact.id = civicrm_email.contact_id ) $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 +2822,6 @@ LEFT JOIN civicrm_email ON ( civicrm_contact.id = civicrm_email.contact_id ) $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 +3485,30 @@ LEFT JOIN civicrm_address ON ( civicrm_address.contact_id = civicrm_contact.id ) } } + /** + * 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 */ diff --git a/tests/phpunit/api/v3/CustomValueTest.php b/tests/phpunit/api/v3/CustomValueTest.php index 4dd4db86a9..3c77535539 100644 --- a/tests/phpunit/api/v3/CustomValueTest.php +++ b/tests/phpunit/api/v3/CustomValueTest.php @@ -491,12 +491,12 @@ class api_v3_CustomValueTest extends CiviUnitTestCase { /** * Test that custom fields in greeting strings are updated. */ - public function testUpdateCustomGreetings() { + public function testUpdateCustomGreetings(): void { // Create a custom group with one field. $customGroupResult = $this->callAPISuccess('CustomGroup', 'create', [ 'sequential' => 1, - 'title' => "test custom group", - 'extends' => "Individual", + 'title' => 'test custom group', + 'extends' => 'Individual', ]); $customFieldResult = $this->callAPISuccess('CustomField', 'create', [ 'custom_group_id' => $customGroupResult['id'], @@ -509,21 +509,20 @@ class api_v3_CustomValueTest extends CiviUnitTestCase { // Create a contact with an email greeting format that includes the new custom field. $contactResult = $this->callAPISuccess('Contact', 'create', [ 'contact_type' => 'Individual', - 'email' => substr(sha1(rand()), 0, 7) . '@yahoo.com', - 'email_greeting_id' => "Customized", + 'email' => 'her@yahoo.com', + 'email_greeting_id' => 'Customized', 'email_greeting_custom' => "Dear {contact.custom_{$customFieldId}}", ]); $cid = $contactResult['id']; // Define testing values. - $uniq = uniqid(); - $testGreetingValue = "Dear $uniq"; + $testGreetingValue = 'Dear custom field'; // Update contact's custom field with CustomValue.create - $customValueResult = $this->callAPISuccess('CustomValue', 'create', [ + $this->callAPISuccess('CustomValue', 'create', [ 'entity_id' => $cid, - "custom_{$customFieldId}" => $uniq, - 'entity_table' => "civicrm_contact", + "custom_{$customFieldId}" => 'custom field', + 'entity_table' => 'civicrm_contact', ]); $contact = $this->callAPISuccessGetSingle('Contact', ['id' => $cid, 'return' => 'email_greeting']); -- 2.25.1