3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
40 class CRM_UpdateGreeting
{
41 function __construct() {
44 $config = CRM_Core_Config
::singleton();
46 require_once 'CRM/Utils/Request.php';
47 require_once 'CRM/Core/PseudoConstant.php';
48 require_once 'CRM/Contact/BAO/Contact.php';
50 // this does not return on failure
51 CRM_Utils_System
::authenticateScript(TRUE);
53 //log the execution time of script
54 CRM_Core_Error
::debug_log_message('UpdateGreeting.php');
57 function initialize() {
58 require_once '../../civicrm.config.php';
59 require_once 'CRM/Core/Config.php';
62 public function updateGreeting() {
63 $config = CRM_Core_Config
::singleton();
64 $contactType = CRM_Utils_Request
::retrieve('ct', 'String', CRM_Core_DAO
::$_nullArray, FALSE, NULL, 'REQUEST');
65 if (!in_array($contactType,
66 array('Individual', 'Household', 'Organization')
68 CRM_Core_Error
::fatal(ts('Invalid Contact Type.'));
71 $greeting = CRM_Utils_Request
::retrieve('gt', 'String', CRM_Core_DAO
::$_nullArray, FALSE, NULL, 'REQUEST');
72 if (!in_array($greeting,
73 array('email_greeting', 'postal_greeting', 'addressee')
75 CRM_Core_Error
::fatal(ts('Invalid Greeting Type.'));
78 if (in_array($greeting, array(
79 'email_greeting', 'postal_greeting')) && $contactType == 'Organization') {
80 CRM_Core_Error
::fatal(ts('You cannot use %1 for contact type %2.', array(1 => $greeting, 2 => $contactType)));
83 $valueID = $id = CRM_Utils_Request
::retrieve('id', 'Positive', CRM_Core_DAO
::$_nullArray, FALSE, NULL, 'REQUEST');
85 // if valueID is not passed use default value
87 require_once 'CRM/Core/OptionGroup.php';
88 $contactTypeFilters = array(1 => 'Individual', 2 => 'Household', 3 => 'Organization');
89 $filter = CRM_Utils_Array
::key($contactType, $contactTypeFilters);
90 $defaulValueID = CRM_Core_OptionGroup
::values($greeting, NULL, NULL, NULL,
91 " AND is_default = 1 AND ( filter = {$filter} OR filter = 0 )",
94 $valueID = array_pop($defaulValueID);
98 'contact_type' => $contactType,
99 'greeting_type' => $greeting,
102 $allGreetings = CRM_Core_PseudoConstant
::greeting($filter);
103 $originalGreetingString = $greetingString = CRM_Utils_Array
::value($valueID, $allGreetings);
104 if (!$greetingString) {
105 CRM_Core_Error
::fatal(ts('Incorrect greeting value id %1.', array(1 => $valueID)));
108 // build return properties based on tokens
109 require_once 'CRM/Utils/Token.php';
110 $greetingTokens = CRM_Utils_Token
::getTokens($greetingString);
111 $tokens = CRM_Utils_Array
::value('contact', $greetingTokens);
112 $greetingsReturnProperties = array();
113 if (is_array($tokens)) {
114 $greetingsReturnProperties = array_fill_keys(array_values($tokens), 1);
117 //process all contacts only when force pass.
118 $force = CRM_Utils_Request
::retrieve('force', 'String', CRM_Core_DAO
::$_nullArray, FALSE, NULL, 'REQUEST');
119 $processAll = $processOnlyIdSet = FALSE;
120 if (in_array($force, array(
124 elseif ($force == 2) {
125 $processOnlyIdSet = TRUE;
128 //FIXME : apiQuery should handle these clause.
129 $filterContactFldIds = $filterIds = array();
131 $idFldName = $displayFldName = NULL;
132 if ($greeting == 'email_greeting' ||
$greeting == 'postal_greeting' ||
$greeting == 'addressee') {
133 $idFldName = $greeting . '_id';
134 $displayFldName = $greeting . '_display';
139 SELECT DISTINCT id, $idFldName
141 WHERE contact_type = %1
142 AND ( {$idFldName} IS NULL OR
143 ( {$idFldName} IS NOT NULL AND {$displayFldName} IS NULL ) )
145 $dao = CRM_Core_DAO
::executeQuery($sql, array(1 => array($contactType, 'String')));
146 while ($dao->fetch()) {
147 $filterContactFldIds[$dao->id
] = $dao->$idFldName;
149 if (!CRM_Utils_System
::isNull($dao->$idFldName)) {
150 $filterIds[$dao->id
] = $dao->$idFldName;
154 if (empty($filterContactFldIds)) {
155 $filterContactFldIds[] = 0;
159 if (empty($filterContactFldIds)) {
163 // retrieve only required contact information
164 require_once 'CRM/Utils/Token.php';
165 $extraParams[] = array('contact_type', '=', $contactType, 0, 0);
166 // we do token replacement in the replaceGreetingTokens hook
167 list($greetingDetails) = CRM_Utils_Token
::getTokenDetails(array_keys($filterContactFldIds),
168 $greetingsReturnProperties,
169 FALSE, FALSE, $extraParams
171 // perform token replacement and build update SQL
172 $contactIds = array();
173 $cacheFieldQuery = "UPDATE civicrm_contact SET {$greeting}_display = CASE id ";
174 foreach ($greetingDetails as $contactID => $contactDetails) {
176 !array_key_exists($contactID, $filterContactFldIds)
181 if ($processOnlyIdSet) {
182 if (!array_key_exists($contactID, $filterIds)) {
186 $greetingString = $originalGreetingString;
187 $contactIds[] = $contactID;
190 if ($greetingBuffer = CRM_Utils_Array
::value($filterContactFldIds[$contactID], $allGreetings)) {
191 $greetingString = $greetingBuffer;
194 $allContactIds[] = $contactID;
197 $greetingString = $originalGreetingString;
198 if ($greetingBuffer = CRM_Utils_Array
::value($filterContactFldIds[$contactID], $allGreetings)) {
199 $greetingString = $greetingBuffer;
202 $contactIds[] = $contactID;
205 CRM_Utils_Token
::replaceGreetingTokens($greetingString, $contactDetails, $contactID, 'CRM_UpdateGreeting');
206 $greetingString = CRM_Core_DAO
::escapeString($greetingString);
207 $cacheFieldQuery .= " WHEN {$contactID} THEN '{$greetingString}' ";
209 $allContactIds[] = $contactID;
212 if (!empty($allContactIds)) {
213 $cacheFieldQuery .= " ELSE {$greeting}_display
215 if (!empty($contactIds)) {
216 // need to update greeting _id field.
218 UPDATE civicrm_contact
219 SET {$greeting}_id = {$valueID}
220 WHERE id IN (" . implode(',', $contactIds) . ")";
221 CRM_Core_DAO
::executeQuery($queryString);
224 // now update cache field
225 CRM_Core_DAO
::executeQuery($cacheFieldQuery);
230 $obj = new CRM_UpdateGreeting();
231 $obj->updateGreeting();
232 echo "\n\n Greeting is updated for contact(s). (Done) \n";