Skip to content

Commit

Permalink
[REF] Preliminary cleanup in update greeting
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Nov 11, 2021
1 parent 9d73b56 commit d26779e
Showing 1 changed file with 32 additions and 48 deletions.
80 changes: 32 additions & 48 deletions CRM/Contact/BAO/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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') {
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit d26779e

Please sign in to comment.