/** * Process greetings and cache. * * @param object $contact * Contact object after save. * @param bool $useDefaults * Use default greeting values. */ public 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_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}'"; } //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_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}'"; } // 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_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}'"; } if (!empty($updateQueryString)) { $updateQueryString = implode(',', $updateQueryString); $queryString = "UPDATE civicrm_contact SET {$updateQueryString} WHERE id = {$contact->id}"; CRM_Core_DAO::executeQuery($queryString); } }
/** * Construct array of default greeting values for contact type * * @access public * @static * * @return array - array reference of default greetings. * */ public static function &greetingDefaults() { if (!self::$greetingDefaults) { $defaultGreetings = array(); $contactTypes = array('Individual' => 1, 'Household' => 2, 'Organization' => 3); foreach ($contactTypes as $contactType => $filter) { $filterCondition = " AND (v.filter = 0 OR v.filter = {$filter}) AND v.is_default = 1 "; foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) { $tokenVal = CRM_Core_OptionGroup::values($greeting, NULL, NULL, NULL, $filterCondition, 'label'); $defaultGreetings[$contactType][$greeting] = $tokenVal; } } self::$greetingDefaults = $defaultGreetings; } return self::$greetingDefaults; }
/** * Construct array of default greeting values for contact type * * @access public * @static * * @return array - array reference of default greetings. * */ public static function &greetingDefaults() { if (!self::$greetingDefaults) { $defaultGreetings = array(); $contactTypes = self::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name')); foreach ($contactTypes as $filter => $contactType) { $filterCondition = " AND (v.filter = 0 OR v.filter = {$filter}) AND v.is_default = 1 "; foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) { $tokenVal = CRM_Core_OptionGroup::values($greeting, NULL, NULL, NULL, $filterCondition, 'label'); $defaultGreetings[$contactType][$greeting] = $tokenVal; } } self::$greetingDefaults = $defaultGreetings; } return self::$greetingDefaults; }
/** * Function to process greetings and cache * */ static function processGreetings(&$contact, $useDefaults = FALSE) { if ($useDefaults) { //retrieve default greetings $defaultGreetings = CRM_Core_PseudoConstant::greetingDefaults(); $contactDefaults = $defaultGreetings[$contact->contact_type]; } // 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); } }