Skip to content

Commit

Permalink
Improve performance of greeting procssing
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Aug 2, 2022
1 parent 91837d8 commit a795e66
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 52 deletions.
70 changes: 23 additions & 47 deletions CRM/Contact/BAO/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
+--------------------------------------------------------------------+
*/

use Civi\Token\TokenProcessor;

/**
*
* @package CRM
Expand Down Expand Up @@ -2725,64 +2727,38 @@ public static function updateGreetingsOnTokenFieldChange($updatedFields, $contac
* @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
// allows us the possibility of only doing an update if required.
public static function processGreetings(CRM_Contact_DAO_Contact $contact) {

// The contact object has not always required the
// fields that are required to calculate greetings
// so we need to retrieve it again.
if ($contact->_query !== FALSE) {
$contact->find(TRUE);
}
$greetings = array_filter([
'email_greeting_display' => self::getTemplateForGreeting('email_greeting', $contact),
'postal_greeting_display' => self::getTemplateForGreeting('postal_greeting', $contact),
'addressee_display' => self::getTemplateForGreeting('addressee', $contact),
]);

// store object values to an array
// Store object values to an array
$contactDetails = [];
CRM_Core_DAO::storeValues($contact, $contactDetails);
$contactDetails = [[$contact->id => $contactDetails]];

$updateQueryString = [];

if ($emailGreetingString) {
CRM_Contact_BAO_Contact_Utils::processGreetingTemplate($emailGreetingString,
$contactDetails,
$contact->id,
'CRM_Contact_BAO_Contact'
);
$emailGreetingString = CRM_Core_DAO::escapeString(CRM_Utils_String::stripSpaces($emailGreetingString));
$updateQueryString[] = " email_greeting_display = '$emailGreetingString'";
}

if ($postalGreetingString) {
CRM_Contact_BAO_Contact_Utils::processGreetingTemplate($postalGreetingString,
$contactDetails,
$contact->id,
'CRM_Contact_BAO_Contact'
);
$postalGreetingString = CRM_Core_DAO::escapeString(CRM_Utils_String::stripSpaces($postalGreetingString));
$updateQueryString[] = " postal_greeting_display = '$postalGreetingString'";
$tokenProcessor = new TokenProcessor(\Civi::dispatcher(), [
'smarty' => TRUE,
'class' => __CLASS__,
'schema' => ['contactId'],
]);
$tokenProcessor->addRow(['contactId' => $contact->id, 'contact' => $contactDetails]);
foreach ($greetings as $greetingKey => $greetingString) {
$tokenProcessor->addMessage($greetingKey, $greetingString, 'text/plain');
}

if ($addresseeString) {
CRM_Contact_BAO_Contact_Utils::processGreetingTemplate($addresseeString,
$contactDetails,
$contact->id,
'CRM_Contact_BAO_Contact'
);
$addresseeString = CRM_Core_DAO::escapeString(CRM_Utils_String::stripSpaces($addresseeString));
$updateQueryString[] = " addressee_display = '$addresseeString'";
$tokenProcessor->evaluate();
$row = $tokenProcessor->getRow(0);
foreach ($greetings as $greetingKey => $greetingString) {
$parsedGreeting = CRM_Core_DAO::escapeString(CRM_Utils_String::stripSpaces($row->render($greetingKey)));
$updateQueryString[] = " $greetingKey = '$parsedGreeting'";
}

if (!empty($updateQueryString)) {
$updateQueryString = implode(',', $updateQueryString);
$queryString = "UPDATE civicrm_contact SET $updateQueryString WHERE id = {$contact->id}";
CRM_Core_DAO::executeQuery($queryString);
CRM_Core_DAO::executeQuery("UPDATE civicrm_contact SET $updateQueryString WHERE id = {$contact->id}");
}
}

Expand Down
2 changes: 2 additions & 0 deletions CRM/Contact/BAO/Contact/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,8 @@ public static function getTokensRequiredForContactGreetings($contactParams) {
* @param string $templateString
* The greeting template string with contact tokens + Smarty syntax.
*
* @deprecated
*
* @param array $contactDetails
* @param int $contactID
* @param string $className
Expand Down
2 changes: 2 additions & 0 deletions CRM/Utils/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,8 @@ public static function getMembershipTokenDetails($membershipIDs) {
*
* @TODO Remove that inconsistency in usage.
*
* @deprecated
*
* @param string $tokenString
* @param array $contactDetails
* @param int $contactId
Expand Down
5 changes: 0 additions & 5 deletions tests/phpunit/api/v3/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4843,7 +4843,6 @@ public function testCreateCommunicationStylePassed(): void {
* @param int $version
*
* @dataProvider versionThreeAndFour
* @throws \CRM_Core_Exception
*/
public function testContactGreetingsCreate(int $version): void {
$this->_apiversion = $version;
Expand Down Expand Up @@ -4873,8 +4872,6 @@ public function testContactGreetingsCreate(int $version): void {
* @param int $version
*
* @dataProvider versionThreeAndFour
*
* @throws \CRM_Core_Exception
*/
public function testContactGreetingsCreateWithCustomField(int $version): void {
$this->_apiversion = $version;
Expand Down Expand Up @@ -4917,8 +4914,6 @@ public function testContactGreetingsCreateWithCustomField(int $version): void {
*
* @param int $version
*
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
* @dataProvider versionThreeAndFour
*/
public function testGreetingParseSmarty(int $version): void {
Expand Down

0 comments on commit a795e66

Please sign in to comment.