3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
30 * Using this script you can update Email Greetings, Postal Greetings and Addressee for a specific contact type
32 * params for this script
33 * ct=Individual or ct=Household or ct=Organization (ct = contact type)
34 * gt=email_greeting or gt=postal_greeting or gt=addressee (gt = greeting )
35 * id=greeting option value
37 * IMPORTANT: You must first create valid option value before using via admin interface.
38 * Check option lists for Email Greetings, Postal Greetings and Addressee
42 * Class CRM_UpdateGreeting
44 class CRM_UpdateGreeting
{
47 public function __construct() {
50 $config = CRM_Core_Config
::singleton();
52 require_once 'CRM/Utils/Request.php';
53 require_once 'CRM/Core/PseudoConstant.php';
54 require_once 'CRM/Contact/BAO/Contact.php';
56 // this does not return on failure
57 CRM_Utils_System
::authenticateScript(TRUE);
59 //log the execution time of script
60 CRM_Core_Error
::debug_log_message('UpdateGreeting.php');
63 public function initialize() {
64 require_once '../../civicrm.config.php';
65 require_once 'CRM/Core/Config.php';
68 public function updateGreeting() {
69 $config = CRM_Core_Config
::singleton();
70 $contactType = CRM_Utils_Request
::retrieve('ct', 'String', CRM_Core_DAO
::$_nullArray, FALSE, NULL, 'REQUEST');
71 if (!in_array($contactType,
72 array('Individual', 'Household', 'Organization')
75 CRM_Core_Error
::fatal(ts('Invalid Contact Type.'));
78 $greeting = CRM_Utils_Request
::retrieve('gt', 'String', CRM_Core_DAO
::$_nullArray, FALSE, NULL, 'REQUEST');
79 if (!in_array($greeting,
80 array('email_greeting', 'postal_greeting', 'addressee')
83 CRM_Core_Error
::fatal(ts('Invalid Greeting Type.'));
86 if (in_array($greeting, array(
89 )) && $contactType == 'Organization'
91 CRM_Core_Error
::fatal(ts('You cannot use %1 for contact type %2.', array(1 => $greeting, 2 => $contactType)));
94 $valueID = $id = CRM_Utils_Request
::retrieve('id', 'Positive', CRM_Core_DAO
::$_nullArray, FALSE, NULL, 'REQUEST');
96 // if valueID is not passed use default value
98 require_once 'CRM/Core/OptionGroup.php';
99 $contactTypeFilters = array(1 => 'Individual', 2 => 'Household', 3 => 'Organization');
100 $filter = CRM_Utils_Array
::key($contactType, $contactTypeFilters);
101 $defaulValueID = CRM_Core_OptionGroup
::values($greeting, NULL, NULL, NULL,
102 " AND is_default = 1 AND ( filter = {$filter} OR filter = 0 )",
105 $valueID = array_pop($defaulValueID);
109 'contact_type' => $contactType,
110 'greeting_type' => $greeting,
113 $allGreetings = CRM_Core_PseudoConstant
::greeting($filter);
114 $originalGreetingString = $greetingString = CRM_Utils_Array
::value($valueID, $allGreetings);
115 if (!$greetingString) {
116 CRM_Core_Error
::fatal(ts('Incorrect greeting value id %1.', array(1 => $valueID)));
119 // build return properties based on tokens
120 require_once 'CRM/Utils/Token.php';
121 $greetingTokens = CRM_Utils_Token
::getTokens($greetingString);
122 $tokens = CRM_Utils_Array
::value('contact', $greetingTokens);
123 $greetingsReturnProperties = array();
124 if (is_array($tokens)) {
125 $greetingsReturnProperties = array_fill_keys(array_values($tokens), 1);
128 //process all contacts only when force pass.
129 $force = CRM_Utils_Request
::retrieve('force', 'String', CRM_Core_DAO
::$_nullArray, FALSE, NULL, 'REQUEST');
130 $processAll = $processOnlyIdSet = FALSE;
131 if (in_array($force, array(
137 elseif ($force == 2) {
138 $processOnlyIdSet = TRUE;
141 //FIXME : apiQuery should handle these clause.
142 $filterContactFldIds = $filterIds = array();
144 $idFldName = $displayFldName = NULL;
145 if ($greeting == 'email_greeting' ||
$greeting == 'postal_greeting' ||
$greeting == 'addressee') {
146 $idFldName = $greeting . '_id';
147 $displayFldName = $greeting . '_display';
152 SELECT DISTINCT id, $idFldName
154 WHERE contact_type = %1
155 AND ( {$idFldName} IS NULL OR
156 ( {$idFldName} IS NOT NULL AND {$displayFldName} IS NULL ) )
158 $dao = CRM_Core_DAO
::executeQuery($sql, array(1 => array($contactType, 'String')));
159 while ($dao->fetch()) {
160 $filterContactFldIds[$dao->id
] = $dao->$idFldName;
162 if (!CRM_Utils_System
::isNull($dao->$idFldName)) {
163 $filterIds[$dao->id
] = $dao->$idFldName;
167 if (empty($filterContactFldIds)) {
168 $filterContactFldIds[] = 0;
172 if (empty($filterContactFldIds)) {
176 // retrieve only required contact information
177 require_once 'CRM/Utils/Token.php';
178 $extraParams[] = array('contact_type', '=', $contactType, 0, 0);
179 // we do token replacement in the replaceGreetingTokens hook
180 list($greetingDetails) = CRM_Utils_Token
::getTokenDetails(array_keys($filterContactFldIds),
181 $greetingsReturnProperties,
182 FALSE, FALSE, $extraParams
184 // perform token replacement and build update SQL
185 $contactIds = array();
186 $cacheFieldQuery = "UPDATE civicrm_contact SET {$greeting}_display = CASE id ";
187 foreach ($greetingDetails as $contactID => $contactDetails) {
189 !array_key_exists($contactID, $filterContactFldIds)
194 if ($processOnlyIdSet) {
195 if (!array_key_exists($contactID, $filterIds)) {
199 $greetingString = $originalGreetingString;
200 $contactIds[] = $contactID;
203 if ($greetingBuffer = CRM_Utils_Array
::value($filterContactFldIds[$contactID], $allGreetings)) {
204 $greetingString = $greetingBuffer;
207 $allContactIds[] = $contactID;
210 $greetingString = $originalGreetingString;
211 if ($greetingBuffer = CRM_Utils_Array
::value($filterContactFldIds[$contactID], $allGreetings)) {
212 $greetingString = $greetingBuffer;
215 $contactIds[] = $contactID;
218 CRM_Contact_BAO_Contact_Utils
::processGreetingTemplate($greetingString, $contactDetails, $contactID, 'CRM_UpdateGreeting');
219 $greetingString = CRM_Core_DAO
::escapeString($greetingString);
220 $cacheFieldQuery .= " WHEN {$contactID} THEN '{$greetingString}' ";
222 $allContactIds[] = $contactID;
225 if (!empty($allContactIds)) {
226 $cacheFieldQuery .= " ELSE {$greeting}_display
228 if (!empty($contactIds)) {
229 // need to update greeting _id field.
231 UPDATE civicrm_contact
232 SET {$greeting}_id = {$valueID}
233 WHERE id IN (" . implode(',', $contactIds) . ")";
234 CRM_Core_DAO
::executeQuery($queryString);
237 // now update cache field
238 CRM_Core_DAO
::executeQuery($cacheFieldQuery);
244 $obj = new CRM_UpdateGreeting();
245 $obj->updateGreeting();
246 echo "\n\n Greeting is updated for contact(s). (Done) \n";