/**
  * Apply the various CRM_Utils_Token helpers.
  *
  * @param TokenRenderEvent $e
  */
 public function onRender(TokenRenderEvent $e)
 {
     $isHtml = $e->message['format'] == 'text/html';
     $useSmarty = !empty($e->context['smarty']);
     $e->string = \CRM_Utils_Token::replaceDomainTokens($e->string, \CRM_Core_BAO_Domain::getDomain(), $isHtml, $e->message['tokens'], $useSmarty);
     if (!empty($e->context['contact'])) {
         $e->string = \CRM_Utils_Token::replaceContactTokens($e->string, $e->context['contact'], $isHtml, $e->message['tokens'], FALSE, $useSmarty);
         // FIXME: This may depend on $contact being merged with hook values.
         $e->string = \CRM_Utils_Token::replaceHookTokens($e->string, $e->context['contact'], $e->context['hookTokenCategories'], $isHtml, $useSmarty);
         \CRM_Utils_Token::replaceGreetingTokens($e->string, NULL, $e->context['contact']['contact_id'], NULL, $useSmarty);
     }
     if ($useSmarty) {
         $smarty = \CRM_Core_Smarty::singleton();
         $e->string = $smarty->fetch("string:" . $e->string);
     }
 }
Esempio n. 2
0
 /**
  * Process a greeting template string to produce the individualised greeting text.
  *
  * This works just like message templates for mailings:
  * the template is processed with the token substitution mechanism,
  * to supply the individual contact data;
  * and it is also processed with Smarty,
  * to allow for conditionals etc. based on the contact data.
  *
  * Note: We don't pass any variables to Smarty --
  * all variable data is inserted into the input string
  * by the token substitution mechanism,
  * before Smarty is invoked.
  *
  * @param string $templateString
  *   The greeting template string with contact tokens + Smarty syntax.
  *
  * @param array $contactDetails
  * @param int $contactID
  * @param string $className
  */
 public static function processGreetingTemplate(&$templateString, $contactDetails, $contactID, $className)
 {
     CRM_Utils_Token::replaceGreetingTokens($templateString, $contactDetails, $contactID, $className, TRUE);
     $smarty = CRM_Core_Smarty::singleton();
     $templateString = $smarty->fetch("string:{$templateString}");
 }
 /**
  * @param $contactId
  * @param $to
  * @param $scheduleID
  * @param $from
  * @param $tokenParams
  *
  * @return bool|null
  * @throws CRM_Core_Exception
  */
 static function sendReminder($contactId, $to, $scheduleID, $from, $tokenParams)
 {
     $email = $to['email'];
     $phoneNumber = $to['phone'];
     $schedule = new CRM_Core_DAO_ActionSchedule();
     $schedule->id = $scheduleID;
     $domain = CRM_Core_BAO_Domain::getDomain();
     $result = NULL;
     $hookTokens = array();
     if ($schedule->find(TRUE)) {
         $body_text = $schedule->body_text;
         $body_html = $schedule->body_html;
         $sms_body_text = $schedule->sms_body_text;
         $body_subject = $schedule->subject;
         if (!$body_text) {
             $body_text = CRM_Utils_String::htmlToText($body_html);
         }
         $params = array(array('contact_id', '=', $contactId, 0, 0));
         list($contact, $_) = CRM_Contact_BAO_Query::apiQuery($params);
         //CRM-4524
         $contact = reset($contact);
         if (!$contact || is_a($contact, 'CRM_Core_Error')) {
             return NULL;
         }
         // merge activity tokens with contact array
         $contact = array_merge($contact, $tokenParams);
         //CRM-5734
         CRM_Utils_Hook::tokenValues($contact, $contactId);
         CRM_Utils_Hook::tokens($hookTokens);
         $categories = array_keys($hookTokens);
         $type = array('body_html' => 'html', 'body_text' => 'text', 'sms_body_text' => 'text');
         foreach ($type as $bodyType => $value) {
             $dummy_mail = new CRM_Mailing_BAO_Mailing();
             if ($bodyType == 'sms_body_text') {
                 $dummy_mail->body_text = ${$bodyType};
             } else {
                 $dummy_mail->{${$bodyType}} = ${$bodyType};
             }
             $tokens = $dummy_mail->getTokens();
             if (${$bodyType}) {
                 CRM_Utils_Token::replaceGreetingTokens(${$bodyType}, NULL, $contact['contact_id']);
                 ${$bodyType} = CRM_Utils_Token::replaceDomainTokens(${$bodyType}, $domain, TRUE, $tokens[$value], TRUE);
                 ${$bodyType} = CRM_Utils_Token::replaceContactTokens(${$bodyType}, $contact, FALSE, $tokens[$value], FALSE, TRUE);
                 ${$bodyType} = CRM_Utils_Token::replaceComponentTokens(${$bodyType}, $contact, $tokens[$value], TRUE, FALSE);
                 ${$bodyType} = CRM_Utils_Token::replaceHookTokens(${$bodyType}, $contact, $categories, TRUE);
             }
         }
         $html = $body_html;
         $text = $body_text;
         $sms_text = $sms_body_text;
         $smarty = CRM_Core_Smarty::singleton();
         foreach (array('text', 'html', 'sms_text') as $elem) {
             ${$elem} = $smarty->fetch("string:{${$elem}}");
         }
         $matches = array();
         preg_match_all('/(?<!\\{|\\\\)\\{(\\w+\\.\\w+)\\}(?!\\})/', $body_subject, $matches, PREG_PATTERN_ORDER);
         $subjectToken = NULL;
         if ($matches[1]) {
             foreach ($matches[1] as $token) {
                 list($type, $name) = preg_split('/\\./', $token, 2);
                 if ($name) {
                     if (!isset($subjectToken[$type])) {
                         $subjectToken[$type] = array();
                     }
                     $subjectToken[$type][] = $name;
                 }
             }
         }
         $messageSubject = CRM_Utils_Token::replaceContactTokens($body_subject, $contact, FALSE, $subjectToken);
         $messageSubject = CRM_Utils_Token::replaceDomainTokens($messageSubject, $domain, TRUE, $subjectToken);
         $messageSubject = CRM_Utils_Token::replaceComponentTokens($messageSubject, $contact, $subjectToken, TRUE);
         $messageSubject = CRM_Utils_Token::replaceHookTokens($messageSubject, $contact, $categories, TRUE);
         $messageSubject = $smarty->fetch("string:{$messageSubject}");
         if ($schedule->mode == 'SMS' or $schedule->mode == 'User_Preference') {
             $session = CRM_Core_Session::singleton();
             $userID = $session->get('userID') ? $session->get('userID') : $contactId;
             $smsParams = array('To' => $phoneNumber, 'provider_id' => $schedule->sms_provider_id, 'activity_subject' => $messageSubject);
             $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', 'SMS', 'name');
             $activityParams = array('source_contact_id' => $userID, 'activity_type_id' => $activityTypeID, 'activity_date_time' => date('YmdHis'), 'subject' => $messageSubject, 'details' => $sms_text, 'status_id' => CRM_Core_OptionGroup::getValue('activity_status', 'Completed', 'name'));
             $activity = CRM_Activity_BAO_Activity::create($activityParams);
             CRM_Activity_BAO_Activity::sendSMSMessage($contactId, $sms_text, $html, $smsParams, $activity->id, $userID);
         }
         if ($schedule->mode == 'Email' or $schedule->mode == 'User_Preference') {
             // set up the parameters for CRM_Utils_Mail::send
             $mailParams = array('groupName' => 'Scheduled Reminder Sender', 'from' => $from, 'toName' => $contact['display_name'], 'toEmail' => $email, 'subject' => $messageSubject, 'entity' => 'action_schedule', 'entity_id' => $scheduleID);
             if (!$html || $contact['preferred_mail_format'] == 'Text' || $contact['preferred_mail_format'] == 'Both') {
                 // render the &amp; entities in text mode, so that the links work
                 $mailParams['text'] = str_replace('&amp;', '&', $text);
             }
             if ($html && ($contact['preferred_mail_format'] == 'HTML' || $contact['preferred_mail_format'] == 'Both')) {
                 $mailParams['html'] = $html;
             }
             $result = CRM_Utils_Mail::send($mailParams);
         }
     }
     $schedule->free();
     return $result;
 }
 /**
  * @param int $contactId
  * @param $email
  * @param int $messageTemplateID
  * @param $from
  *
  * @return bool|NULL
  */
 public static function sendReminder($contactId, $email, $messageTemplateID, $from)
 {
     $messageTemplates = new CRM_Core_DAO_MessageTemplate();
     $messageTemplates->id = $messageTemplateID;
     $domain = CRM_Core_BAO_Domain::getDomain();
     $result = NULL;
     $hookTokens = array();
     if ($messageTemplates->find(TRUE)) {
         $body_text = $messageTemplates->msg_text;
         $body_html = $messageTemplates->msg_html;
         $body_subject = $messageTemplates->msg_subject;
         if (!$body_text) {
             $body_text = CRM_Utils_String::htmlToText($body_html);
         }
         $params = array(array('contact_id', '=', $contactId, 0, 0));
         list($contact, $_) = CRM_Contact_BAO_Query::apiQuery($params);
         //CRM-4524
         $contact = reset($contact);
         if (!$contact || is_a($contact, 'CRM_Core_Error')) {
             return NULL;
         }
         //CRM-5734
         // get tokens to be replaced
         $tokens = array_merge(CRM_Utils_Token::getTokens($body_text), CRM_Utils_Token::getTokens($body_html), CRM_Utils_Token::getTokens($body_subject));
         // get replacement text for these tokens
         $returnProperties = array("preferred_mail_format" => 1);
         if (isset($tokens['contact'])) {
             foreach ($tokens['contact'] as $key => $value) {
                 $returnProperties[$value] = 1;
             }
         }
         list($details) = CRM_Utils_Token::getTokenDetails(array($contactId), $returnProperties, NULL, NULL, FALSE, $tokens, 'CRM_Core_BAO_MessageTemplate');
         $contact = reset($details);
         // call token hook
         $hookTokens = array();
         CRM_Utils_Hook::tokens($hookTokens);
         $categories = array_keys($hookTokens);
         // do replacements in text and html body
         $type = array('html', 'text');
         foreach ($type as $key => $value) {
             $bodyType = "body_{$value}";
             if (${$bodyType}) {
                 CRM_Utils_Token::replaceGreetingTokens(${$bodyType}, NULL, $contact['contact_id']);
                 ${$bodyType} = CRM_Utils_Token::replaceDomainTokens(${$bodyType}, $domain, TRUE, $tokens, TRUE);
                 ${$bodyType} = CRM_Utils_Token::replaceContactTokens(${$bodyType}, $contact, FALSE, $tokens, FALSE, TRUE);
                 ${$bodyType} = CRM_Utils_Token::replaceComponentTokens(${$bodyType}, $contact, $tokens, TRUE);
                 ${$bodyType} = CRM_Utils_Token::replaceHookTokens(${$bodyType}, $contact, $categories, TRUE);
             }
         }
         $html = $body_html;
         $text = $body_text;
         $smarty = CRM_Core_Smarty::singleton();
         foreach (array('text', 'html') as $elem) {
             ${$elem} = $smarty->fetch("string:{${$elem}}");
         }
         // do replacements in message subject
         $messageSubject = CRM_Utils_Token::replaceContactTokens($body_subject, $contact, FALSE, $tokens);
         $messageSubject = CRM_Utils_Token::replaceDomainTokens($messageSubject, $domain, TRUE, $tokens);
         $messageSubject = CRM_Utils_Token::replaceComponentTokens($messageSubject, $contact, $tokens, TRUE);
         $messageSubject = CRM_Utils_Token::replaceHookTokens($messageSubject, $contact, $categories, TRUE);
         $messageSubject = $smarty->fetch("string:{$messageSubject}");
         // set up the parameters for CRM_Utils_Mail::send
         $mailParams = array('groupName' => 'Scheduled Reminder Sender', 'from' => $from, 'toName' => $contact['display_name'], 'toEmail' => $email, 'subject' => $messageSubject);
         if (!$html || $contact['preferred_mail_format'] == 'Text' || $contact['preferred_mail_format'] == 'Both') {
             // render the &amp; entities in text mode, so that the links work
             $mailParams['text'] = str_replace('&amp;', '&', $text);
         }
         if ($html && ($contact['preferred_mail_format'] == 'HTML' || $contact['preferred_mail_format'] == 'Both')) {
             $mailParams['html'] = $html;
         }
         $result = CRM_Utils_Mail::send($mailParams);
     }
     $messageTemplates->free();
     return $result;
 }
Esempio n. 5
0
 public function updateGreeting()
 {
     $config = CRM_Core_Config::singleton();
     $contactType = CRM_Utils_Request::retrieve('ct', 'String', CRM_Core_DAO::$_nullArray, FALSE, NULL, 'REQUEST');
     if (!in_array($contactType, array('Individual', 'Household', 'Organization'))) {
         CRM_Core_Error::fatal(ts('Invalid Contact Type.'));
     }
     $greeting = CRM_Utils_Request::retrieve('gt', 'String', CRM_Core_DAO::$_nullArray, FALSE, NULL, 'REQUEST');
     if (!in_array($greeting, array('email_greeting', 'postal_greeting', 'addressee'))) {
         CRM_Core_Error::fatal(ts('Invalid Greeting Type.'));
     }
     if (in_array($greeting, array('email_greeting', 'postal_greeting')) && $contactType == 'Organization') {
         CRM_Core_Error::fatal(ts('You cannot use %1 for contact type %2.', array(1 => $greeting, 2 => $contactType)));
     }
     $valueID = $id = CRM_Utils_Request::retrieve('id', 'Positive', CRM_Core_DAO::$_nullArray, FALSE, NULL, 'REQUEST');
     // if valueID is not passed use default value
     if (!$valueID) {
         require_once 'CRM/Core/OptionGroup.php';
         $contactTypeFilters = array(1 => 'Individual', 2 => 'Household', 3 => 'Organization');
         $filter = CRM_Utils_Array::key($contactType, $contactTypeFilters);
         $defaulValueID = CRM_Core_OptionGroup::values($greeting, NULL, NULL, NULL, " AND is_default = 1 AND ( filter = {$filter} OR filter = 0 )", "value");
         $valueID = array_pop($defaulValueID);
     }
     $filter = array('contact_type' => $contactType, 'greeting_type' => $greeting);
     $allGreetings = CRM_Core_PseudoConstant::greeting($filter);
     $originalGreetingString = $greetingString = CRM_Utils_Array::value($valueID, $allGreetings);
     if (!$greetingString) {
         CRM_Core_Error::fatal(ts('Incorrect greeting value id %1.', array(1 => $valueID)));
     }
     // build return properties based on tokens
     require_once 'CRM/Utils/Token.php';
     $greetingTokens = CRM_Utils_Token::getTokens($greetingString);
     $tokens = CRM_Utils_Array::value('contact', $greetingTokens);
     $greetingsReturnProperties = array();
     if (is_array($tokens)) {
         $greetingsReturnProperties = array_fill_keys(array_values($tokens), 1);
     }
     //process all contacts only when force pass.
     $force = CRM_Utils_Request::retrieve('force', 'String', CRM_Core_DAO::$_nullArray, FALSE, NULL, 'REQUEST');
     $processAll = $processOnlyIdSet = FALSE;
     if (in_array($force, array(1, 'true'))) {
         $processAll = TRUE;
     } elseif ($force == 2) {
         $processOnlyIdSet = TRUE;
     }
     //FIXME : apiQuery should handle these clause.
     $filterContactFldIds = $filterIds = array();
     if (!$processAll) {
         $idFldName = $displayFldName = NULL;
         if ($greeting == 'email_greeting' || $greeting == 'postal_greeting' || $greeting == 'addressee') {
             $idFldName = $greeting . '_id';
             $displayFldName = $greeting . '_display';
         }
         if ($idFldName) {
             $sql = "\nSELECT DISTINCT id, {$idFldName}\n  FROM civicrm_contact\n WHERE contact_type = %1\n   AND ( {$idFldName} IS NULL OR\n         ( {$idFldName} IS NOT NULL AND {$displayFldName} IS NULL ) )\n   ";
             $dao = CRM_Core_DAO::executeQuery($sql, array(1 => array($contactType, 'String')));
             while ($dao->fetch()) {
                 $filterContactFldIds[$dao->id] = $dao->{$idFldName};
                 if (!CRM_Utils_System::isNull($dao->{$idFldName})) {
                     $filterIds[$dao->id] = $dao->{$idFldName};
                 }
             }
         }
         if (empty($filterContactFldIds)) {
             $filterContactFldIds[] = 0;
         }
     }
     if (empty($filterContactFldIds)) {
         return;
     }
     // retrieve only required contact information
     require_once 'CRM/Utils/Token.php';
     $extraParams[] = array('contact_type', '=', $contactType, 0, 0);
     // we do token replacement in the replaceGreetingTokens hook
     list($greetingDetails) = CRM_Utils_Token::getTokenDetails(array_keys($filterContactFldIds), $greetingsReturnProperties, FALSE, FALSE, $extraParams);
     // perform token replacement and build update SQL
     $contactIds = array();
     $cacheFieldQuery = "UPDATE civicrm_contact SET {$greeting}_display = CASE id ";
     foreach ($greetingDetails as $contactID => $contactDetails) {
         if (!$processAll && !array_key_exists($contactID, $filterContactFldIds)) {
             continue;
         }
         if ($processOnlyIdSet) {
             if (!array_key_exists($contactID, $filterIds)) {
                 continue;
             }
             if ($id) {
                 $greetingString = $originalGreetingString;
                 $contactIds[] = $contactID;
             } else {
                 if ($greetingBuffer = CRM_Utils_Array::value($filterContactFldIds[$contactID], $allGreetings)) {
                     $greetingString = $greetingBuffer;
                 }
             }
             $allContactIds[] = $contactID;
         } else {
             $greetingString = $originalGreetingString;
             if ($greetingBuffer = CRM_Utils_Array::value($filterContactFldIds[$contactID], $allGreetings)) {
                 $greetingString = $greetingBuffer;
             } else {
                 $contactIds[] = $contactID;
             }
         }
         CRM_Utils_Token::replaceGreetingTokens($greetingString, $contactDetails, $contactID, 'CRM_UpdateGreeting');
         $greetingString = CRM_Core_DAO::escapeString($greetingString);
         $cacheFieldQuery .= " WHEN {$contactID} THEN '{$greetingString}' ";
         $allContactIds[] = $contactID;
     }
     if (!empty($allContactIds)) {
         $cacheFieldQuery .= " ELSE {$greeting}_display\n                              END;";
         if (!empty($contactIds)) {
             // need to update greeting _id field.
             $queryString = "\nUPDATE civicrm_contact\n   SET {$greeting}_id = {$valueID}\n WHERE id IN (" . implode(',', $contactIds) . ")";
             CRM_Core_DAO::executeQuery($queryString);
         }
         // now update cache field
         CRM_Core_DAO::executeQuery($cacheFieldQuery);
     }
 }
 static function sendReminder($contactId, $email, $scheduleID, $from, $tokenParams)
 {
     $schedule = new CRM_Core_DAO_ActionSchedule();
     $schedule->id = $scheduleID;
     $domain = CRM_Core_BAO_Domain::getDomain();
     $result = NULL;
     $hookTokens = array();
     if ($schedule->find(TRUE)) {
         $body_text = $schedule->body_text;
         $body_html = $schedule->body_html;
         $body_subject = $schedule->subject;
         if (!$body_text) {
             $body_text = CRM_Utils_String::htmlToText($body_html);
         }
         $params = array(array('contact_id', '=', $contactId, 0, 0));
         list($contact, $_) = CRM_Contact_BAO_Query::apiQuery($params);
         //CRM-4524
         $contact = reset($contact);
         if (!$contact || is_a($contact, 'CRM_Core_Error')) {
             return NULL;
         }
         // merge activity tokens with contact array
         $contact = array_merge($contact, $tokenParams);
         //CRM-5734
         CRM_Utils_Hook::tokenValues($contact, $contactId);
         CRM_Utils_Hook::tokens($hookTokens);
         $categories = array_keys($hookTokens);
         $type = array('html', 'text');
         foreach ($type as $key => $value) {
             $dummy_mail = new CRM_Mailing_BAO_Mailing();
             $bodyType = "body_{$value}";
             $dummy_mail->{$bodyType} = ${$bodyType};
             $tokens = $dummy_mail->getTokens();
             if (${$bodyType}) {
                 CRM_Utils_Token::replaceGreetingTokens(${$bodyType}, NULL, $contact['contact_id']);
                 ${$bodyType} = CRM_Utils_Token::replaceDomainTokens(${$bodyType}, $domain, TRUE, $tokens[$value], TRUE);
                 ${$bodyType} = CRM_Utils_Token::replaceContactTokens(${$bodyType}, $contact, FALSE, $tokens[$value], FALSE, TRUE);
                 ${$bodyType} = CRM_Utils_Token::replaceComponentTokens(${$bodyType}, $contact, $tokens[$value], TRUE, FALSE);
                 ${$bodyType} = CRM_Utils_Token::replaceHookTokens(${$bodyType}, $contact, $categories, TRUE);
             }
         }
         $html = $body_html;
         $text = $body_text;
         $smarty = CRM_Core_Smarty::singleton();
         foreach (array('text', 'html') as $elem) {
             ${$elem} = $smarty->fetch("string:{${$elem}}");
         }
         $matches = array();
         preg_match_all('/(?<!\\{|\\\\)\\{(\\w+\\.\\w+)\\}(?!\\})/', $body_subject, $matches, PREG_PATTERN_ORDER);
         $subjectToken = NULL;
         if ($matches[1]) {
             foreach ($matches[1] as $token) {
                 list($type, $name) = preg_split('/\\./', $token, 2);
                 if ($name) {
                     if (!isset($subjectToken['contact'])) {
                         $subjectToken['contact'] = array();
                     }
                     $subjectToken['contact'][] = $name;
                 }
             }
         }
         $messageSubject = CRM_Utils_Token::replaceContactTokens($body_subject, $contact, FALSE, $subjectToken);
         $messageSubject = CRM_Utils_Token::replaceDomainTokens($messageSubject, $domain, TRUE, $tokens[$value]);
         $messageSubject = CRM_Utils_Token::replaceComponentTokens($messageSubject, $contact, $tokens[$value], TRUE);
         $messageSubject = CRM_Utils_Token::replaceHookTokens($messageSubject, $contact, $categories, TRUE);
         $messageSubject = $smarty->fetch("string:{$messageSubject}");
         // set up the parameters for CRM_Utils_Mail::send
         $mailParams = array('groupName' => 'Scheduled Reminder Sender', 'from' => $from, 'toName' => $contact['display_name'], 'toEmail' => $email, 'subject' => $messageSubject);
         if (!$html || $contact['preferred_mail_format'] == 'Text' || $contact['preferred_mail_format'] == 'Both') {
             // render the &amp; entities in text mode, so that the links work
             $mailParams['text'] = str_replace('&amp;', '&', $text);
         }
         if ($html && ($contact['preferred_mail_format'] == 'HTML' || $contact['preferred_mail_format'] == 'Both')) {
             $mailParams['html'] = $html;
         }
         $result = CRM_Utils_Mail::send($mailParams);
     }
     $schedule->free();
     return $result;
 }
 public function updateGreeting($params)
 {
     $contactType = $params['ct'];
     $greeting = $params['gt'];
     $valueID = $id = CRM_Utils_Array::value('id', $params);
     $force = CRM_Utils_Array::value('force', $params);
     // if valueID is not passed use default value
     if (!$valueID) {
         $valueID = $id = self::defaultGreeting($contactType, $greeting);
     }
     $filter = array('contact_type' => $contactType, 'greeting_type' => $greeting);
     $allGreetings = CRM_Core_PseudoConstant::greeting($filter);
     $originalGreetingString = $greetingString = CRM_Utils_Array::value($valueID, $allGreetings);
     if (!$greetingString) {
         CRM_Core_Error::fatal(ts('Incorrect greeting value id %1, or no default greeting for this contact type and greeting type.', array(1 => $valueID)));
     }
     // build return properties based on tokens
     $greetingTokens = CRM_Utils_Token::getTokens($greetingString);
     $tokens = CRM_Utils_Array::value('contact', $greetingTokens);
     $greetingsReturnProperties = array();
     if (is_array($tokens)) {
         $greetingsReturnProperties = array_fill_keys(array_values($tokens), 1);
     }
     // Process ALL contacts only when force=1 or force=2 is passed. Else only contacts with NULL greeting or addressee value are updated.
     $processAll = $processOnlyIdSet = FALSE;
     if ($force == 1) {
         $processAll = TRUE;
     } elseif ($force == 2) {
         $processOnlyIdSet = TRUE;
     }
     //FIXME : apiQuery should handle these clause.
     $filterContactFldIds = $filterIds = array();
     $idFldName = $displayFldName = NULL;
     if (in_array($greeting, CRM_Contact_BAO_Contact::$_greetingTypes)) {
         $idFldName = $greeting . '_id';
         $displayFldName = $greeting . '_display';
     }
     if ($idFldName) {
         // if $force == 1 then update all contacts else only
         // those with NULL greeting or addressee value CRM-9476
         if ($processAll) {
             $sql = "SELECT DISTINCT id, {$idFldName} FROM civicrm_contact WHERE contact_type = %1 ";
         } else {
             $sql = "SELECT DISTINCT id, {$idFldName} FROM civicrm_contact WHERE contact_type = %1\n                     AND ( {$idFldName} IS NULL OR ( {$idFldName} IS NOT NULL AND {$displayFldName} IS NULL ) ) ";
         }
         $dao = CRM_Core_DAO::executeQuery($sql, array(1 => array($contactType, 'String')));
         while ($dao->fetch()) {
             $filterContactFldIds[$dao->id] = $dao->{$idFldName};
             if (!CRM_Utils_System::isNull($dao->{$idFldName})) {
                 $filterIds[$dao->id] = $dao->{$idFldName};
             }
         }
     }
     if (empty($filterContactFldIds)) {
         $filterContactFldIds[] = 0;
     }
     // retrieve only required contact information
     $extraParams[] = array('contact_type', '=', $contactType, 0, 0);
     // we do token replacement in the replaceGreetingTokens hook
     list($greetingDetails) = CRM_Utils_Token::getTokenDetails(array_keys($filterContactFldIds), $greetingsReturnProperties, FALSE, FALSE, $extraParams);
     // perform token replacement and build update SQL
     $contactIds = array();
     $cacheFieldQuery = "UPDATE civicrm_contact SET {$greeting}_display = CASE id ";
     foreach ($greetingDetails as $contactID => $contactDetails) {
         if (!$processAll && !array_key_exists($contactID, $filterContactFldIds)) {
             continue;
         }
         if ($processOnlyIdSet && !array_key_exists($contactID, $filterIds)) {
             continue;
         }
         if ($id) {
             $greetingString = $originalGreetingString;
             $contactIds[] = $contactID;
         } else {
             if ($greetingBuffer = CRM_Utils_Array::value($filterContactFldIds[$contactID], $allGreetings)) {
                 $greetingString = $greetingBuffer;
             }
         }
         CRM_Utils_Token::replaceGreetingTokens($greetingString, $contactDetails, $contactID, 'CRM_UpdateGreeting');
         $greetingString = CRM_Core_DAO::escapeString($greetingString);
         $cacheFieldQuery .= " WHEN {$contactID} THEN '{$greetingString}' ";
         $allContactIds[] = $contactID;
     }
     if (!empty($allContactIds)) {
         $cacheFieldQuery .= " ELSE {$greeting}_display\n                              END;";
         if (!empty($contactIds)) {
             // need to update greeting _id field.
             // reset greeting _custom
             $resetCustomGreeting = '';
             if ($valueID != 4) {
                 $resetCustomGreeting = ", {$greeting}_custom = NULL ";
             }
             $queryString = "\nUPDATE civicrm_contact\nSET {$greeting}_id = {$valueID}\n    {$resetCustomGreeting}\nWHERE id IN (" . implode(',', $contactIds) . ")";
             CRM_Core_DAO::executeQuery($queryString);
         }
         // now update cache field
         CRM_Core_DAO::executeQuery($cacheFieldQuery);
     }
 }
 /**
  * Function to process greetings and cache
  *
  * @param object  $contact contact object after save
  * @param boolean $useDefaults use default greeting values
  *
  * @return void
  * @access public
  * @static
  */
 static function processGreetings(&$contact, $useDefaults = FALSE)
 {
     if ($useDefaults) {
         //retrieve default greetings
         $defaultGreetings = CRM_Core_PseudoConstant::greetingDefaults();
         $contactDefaults = $defaultGreetings[$contact->contact_type];
     }
     // note that contact object not always has required greeting related
     // fields that are required to calculate greeting and
     // also other fields used in tokens etc,
     // hence we need to retrieve it again.
     if ($contact->_query !== FALSE) {
         $contact->find(TRUE);
     }
     // store object values to an array
     $contactDetails = array();
     CRM_Core_DAO::storeValues($contact, $contactDetails);
     $contactDetails = array(array($contact->id => $contactDetails));
     $emailGreetingString = $postalGreetingString = $addresseeString = NULL;
     $updateQueryString = array();
     //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 = array('contact_type' => $contact->contact_type, 'greeting_type' => 'email_greeting');
         $emailGreeting = CRM_Core_PseudoConstant::greeting($filter);
         $emailGreetingString = $emailGreeting[$contact->email_greeting_id];
         $updateQueryString[] = " email_greeting_custom = NULL ";
     } else {
         if ($useDefaults) {
             reset($contactDefaults['email_greeting']);
             $emailGreetingID = key($contactDefaults['email_greeting']);
             $emailGreetingString = $contactDefaults['email_greeting'][$emailGreetingID];
             $updateQueryString[] = " email_greeting_id = {$emailGreetingID} ";
             $updateQueryString[] = " email_greeting_custom = NULL ";
         } elseif ($contact->email_greeting_custom) {
             $updateQueryString[] = " email_greeting_display = NULL ";
         }
     }
     if ($emailGreetingString) {
         CRM_Utils_Token::replaceGreetingTokens($emailGreetingString, $contactDetails, $contact->id, 'CRM_Contact_BAO_Contact');
         $emailGreetingString = CRM_Core_DAO::escapeString(CRM_Utils_String::stripSpaces($emailGreetingString));
         $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 = array('contact_type' => $contact->contact_type, 'greeting_type' => 'postal_greeting');
         $postalGreeting = CRM_Core_PseudoConstant::greeting($filter);
         $postalGreetingString = $postalGreeting[$contact->postal_greeting_id];
         $updateQueryString[] = " postal_greeting_custom = NULL ";
     } else {
         if ($useDefaults) {
             reset($contactDefaults['postal_greeting']);
             $postalGreetingID = key($contactDefaults['postal_greeting']);
             $postalGreetingString = $contactDefaults['postal_greeting'][$postalGreetingID];
             $updateQueryString[] = " postal_greeting_id = {$postalGreetingID} ";
             $updateQueryString[] = " postal_greeting_custom = NULL ";
         } elseif ($contact->postal_greeting_custom) {
             $updateQueryString[] = " postal_greeting_display = NULL ";
         }
     }
     if ($postalGreetingString) {
         CRM_Utils_Token::replaceGreetingTokens($postalGreetingString, $contactDetails, $contact->id, 'CRM_Contact_BAO_Contact');
         $postalGreetingString = CRM_Core_DAO::escapeString(CRM_Utils_String::stripSpaces($postalGreetingString));
         $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 = array('contact_type' => $contact->contact_type, 'greeting_type' => 'addressee');
         $addressee = CRM_Core_PseudoConstant::greeting($filter);
         $addresseeString = $addressee[$contact->addressee_id];
         $updateQueryString[] = " addressee_custom = NULL ";
     } else {
         if ($useDefaults) {
             reset($contactDefaults['addressee']);
             $addresseeID = key($contactDefaults['addressee']);
             $addresseeString = $contactDefaults['addressee'][$addresseeID];
             $updateQueryString[] = " addressee_id = {$addresseeID} ";
             $updateQueryString[] = " addressee_custom = NULL ";
         } elseif ($contact->addressee_custom) {
             $updateQueryString[] = " addressee_display = NULL ";
         }
     }
     if ($addresseeString) {
         CRM_Utils_Token::replaceGreetingTokens($addresseeString, $contactDetails, $contact->id, 'CRM_Contact_BAO_Contact');
         $addresseeString = CRM_Core_DAO::escapeString(CRM_Utils_String::stripSpaces($addresseeString));
         $updateQueryString[] = " addressee_display = '{$addresseeString}'";
     }
     if (!empty($updateQueryString)) {
         $updateQueryString = implode(',', $updateQueryString);
         $queryString = "UPDATE civicrm_contact SET {$updateQueryString} WHERE id = {$contact->id}";
         CRM_Core_DAO::executeQuery($queryString);
     }
 }
 protected function replaceTokens($input, $contact_id)
 {
     //get contact
     $params = array(array('contact_id', '=', $contact_id, 0, 0));
     list($contact, $_) = CRM_Contact_BAO_Query::apiQuery($params);
     $contact = reset($contact);
     if (!$contact || is_a($contact, 'CRM_Core_Error')) {
         throw new API_Exception('Could not find contact with ID: ' . $params['contact_id']);
     }
     $tokens = CRM_Utils_Token::getTokens($input);
     // get replacement text for these tokens
     $returnProperties = array('sort_name' => 1, 'email' => 1, 'do_not_email' => 1, 'is_deceased' => 1, 'on_hold' => 1, 'display_name' => 1, 'preferred_mail_format' => 1);
     if (isset($tokens['contact'])) {
         foreach ($tokens['contact'] as $key => $value) {
             $returnProperties[$value] = 1;
         }
     }
     list($details) = CRM_Utils_Token::getTokenDetails(array($contact_id), $returnProperties, false, false, null, $tokens);
     $contact = reset($details);
     // call token hook
     $hookTokens = array();
     CRM_Utils_Hook::tokens($hookTokens);
     $categories = array_keys($hookTokens);
     CRM_Utils_Token::replaceGreetingTokens($input, NULL, $contact['contact_id']);
     $input = CRM_Utils_Token::replaceDomainTokens($input, $domain, true, $tokens, true);
     $input = CRM_Utils_Token::replaceContactTokens($input, $contact, false, $tokens, false, true);
     $input = CRM_Utils_Token::replaceComponentTokens($input, $contact, $tokens, true);
     $input = CRM_Utils_Token::replaceHookTokens($input, $contact, $categories, true);
     return $input;
 }