3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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
{
48 function __construct() {
51 $config = CRM_Core_Config
::singleton();
53 require_once 'CRM/Utils/Request.php';
54 require_once 'CRM/Core/PseudoConstant.php';
55 require_once 'CRM/Contact/BAO/Contact.php';
57 // this does not return on failure
58 CRM_Utils_System
::authenticateScript(TRUE);
60 //log the execution time of script
61 CRM_Core_Error
::debug_log_message('UpdateGreeting.php');
64 function initialize() {
65 require_once '../../civicrm.config.php';
66 require_once 'CRM/Core/Config.php';
69 public function updateGreeting() {
70 $config = CRM_Core_Config
::singleton();
71 $contactType = CRM_Utils_Request
::retrieve('ct', 'String', CRM_Core_DAO
::$_nullArray, FALSE, NULL, 'REQUEST');
72 if (!in_array($contactType,
73 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')
82 CRM_Core_Error
::fatal(ts('Invalid Greeting Type.'));
85 if (in_array($greeting, array(
86 'email_greeting', 'postal_greeting')) && $contactType == 'Organization') {
87 CRM_Core_Error
::fatal(ts('You cannot use %1 for contact type %2.', array(1 => $greeting, 2 => $contactType)));
90 $valueID = $id = CRM_Utils_Request
::retrieve('id', 'Positive', CRM_Core_DAO
::$_nullArray, FALSE, NULL, 'REQUEST');
92 // if valueID is not passed use default value
94 require_once 'CRM/Core/OptionGroup.php';
95 $contactTypeFilters = array(1 => 'Individual', 2 => 'Household', 3 => 'Organization');
96 $filter = CRM_Utils_Array
::key($contactType, $contactTypeFilters);
97 $defaulValueID = CRM_Core_OptionGroup
::values($greeting, NULL, NULL, NULL,
98 " AND is_default = 1 AND ( filter = {$filter} OR filter = 0 )",
101 $valueID = array_pop($defaulValueID);
105 'contact_type' => $contactType,
106 'greeting_type' => $greeting,
109 $allGreetings = CRM_Core_PseudoConstant
::greeting($filter);
110 $originalGreetingString = $greetingString = CRM_Utils_Array
::value($valueID, $allGreetings);
111 if (!$greetingString) {
112 CRM_Core_Error
::fatal(ts('Incorrect greeting value id %1.', array(1 => $valueID)));
115 // build return properties based on tokens
116 require_once 'CRM/Utils/Token.php';
117 $greetingTokens = CRM_Utils_Token
::getTokens($greetingString);
118 $tokens = CRM_Utils_Array
::value('contact', $greetingTokens);
119 $greetingsReturnProperties = array();
120 if (is_array($tokens)) {
121 $greetingsReturnProperties = array_fill_keys(array_values($tokens), 1);
124 //process all contacts only when force pass.
125 $force = CRM_Utils_Request
::retrieve('force', 'String', CRM_Core_DAO
::$_nullArray, FALSE, NULL, 'REQUEST');
126 $processAll = $processOnlyIdSet = FALSE;
127 if (in_array($force, array(
131 elseif ($force == 2) {
132 $processOnlyIdSet = TRUE;
135 //FIXME : apiQuery should handle these clause.
136 $filterContactFldIds = $filterIds = array();
138 $idFldName = $displayFldName = NULL;
139 if ($greeting == 'email_greeting' ||
$greeting == 'postal_greeting' ||
$greeting == 'addressee') {
140 $idFldName = $greeting . '_id';
141 $displayFldName = $greeting . '_display';
146 SELECT DISTINCT id, $idFldName
148 WHERE contact_type = %1
149 AND ( {$idFldName} IS NULL OR
150 ( {$idFldName} IS NOT NULL AND {$displayFldName} IS NULL ) )
152 $dao = CRM_Core_DAO
::executeQuery($sql, array(1 => array($contactType, 'String')));
153 while ($dao->fetch()) {
154 $filterContactFldIds[$dao->id
] = $dao->$idFldName;
156 if (!CRM_Utils_System
::isNull($dao->$idFldName)) {
157 $filterIds[$dao->id
] = $dao->$idFldName;
161 if (empty($filterContactFldIds)) {
162 $filterContactFldIds[] = 0;
166 if (empty($filterContactFldIds)) {
170 // retrieve only required contact information
171 require_once 'CRM/Utils/Token.php';
172 $extraParams[] = array('contact_type', '=', $contactType, 0, 0);
173 // we do token replacement in the replaceGreetingTokens hook
174 list($greetingDetails) = CRM_Utils_Token
::getTokenDetails(array_keys($filterContactFldIds),
175 $greetingsReturnProperties,
176 FALSE, FALSE, $extraParams
178 // perform token replacement and build update SQL
179 $contactIds = array();
180 $cacheFieldQuery = "UPDATE civicrm_contact SET {$greeting}_display = CASE id ";
181 foreach ($greetingDetails as $contactID => $contactDetails) {
183 !array_key_exists($contactID, $filterContactFldIds)
188 if ($processOnlyIdSet) {
189 if (!array_key_exists($contactID, $filterIds)) {
193 $greetingString = $originalGreetingString;
194 $contactIds[] = $contactID;
197 if ($greetingBuffer = CRM_Utils_Array
::value($filterContactFldIds[$contactID], $allGreetings)) {
198 $greetingString = $greetingBuffer;
201 $allContactIds[] = $contactID;
204 $greetingString = $originalGreetingString;
205 if ($greetingBuffer = CRM_Utils_Array
::value($filterContactFldIds[$contactID], $allGreetings)) {
206 $greetingString = $greetingBuffer;
209 $contactIds[] = $contactID;
212 CRM_Contact_BAO_Contact_Utils
::processGreetingTemplate($greetingString, $contactDetails, $contactID, 'CRM_UpdateGreeting');
213 $greetingString = CRM_Core_DAO
::escapeString($greetingString);
214 $cacheFieldQuery .= " WHEN {$contactID} THEN '{$greetingString}' ";
216 $allContactIds[] = $contactID;
219 if (!empty($allContactIds)) {
220 $cacheFieldQuery .= " ELSE {$greeting}_display
222 if (!empty($contactIds)) {
223 // need to update greeting _id field.
225 UPDATE civicrm_contact
226 SET {$greeting}_id = {$valueID}
227 WHERE id IN (" . implode(',', $contactIds) . ")";
228 CRM_Core_DAO
::executeQuery($queryString);
231 // now update cache field
232 CRM_Core_DAO
::executeQuery($cacheFieldQuery);
237 $obj = new CRM_UpdateGreeting();
238 $obj->updateGreeting();
239 echo "\n\n Greeting is updated for contact(s). (Done) \n";