Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-06-05-06-52-23
[civicrm-core.git] / bin / deprecated / UpdateGreeting.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28
29 /*
30 * Using this script you can update Email Greetings, Postal Greetings and Addressee for a specific contact type
31 *
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
36 *
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
39 */
40 class CRM_UpdateGreeting {
41 function __construct() {
42 $this->initialize();
43
44 $config = CRM_Core_Config::singleton();
45
46 require_once 'CRM/Utils/Request.php';
47 require_once 'CRM/Core/PseudoConstant.php';
48 require_once 'CRM/Contact/BAO/Contact.php';
49
50 // this does not return on failure
51 CRM_Utils_System::authenticateScript(TRUE);
52
53 //log the execution time of script
54 CRM_Core_Error::debug_log_message('UpdateGreeting.php');
55 }
56
57 function initialize() {
58 require_once '../../civicrm.config.php';
59 require_once 'CRM/Core/Config.php';
60 }
61
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')
67 )) {
68 CRM_Core_Error::fatal(ts('Invalid Contact Type.'));
69 }
70
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')
74 )) {
75 CRM_Core_Error::fatal(ts('Invalid Greeting Type.'));
76 }
77
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)));
81 }
82
83 $valueID = $id = CRM_Utils_Request::retrieve('id', 'Positive', CRM_Core_DAO::$_nullArray, FALSE, NULL, 'REQUEST');
84
85 // if valueID is not passed use default value
86 if (!$valueID) {
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 )",
92 "value"
93 );
94 $valueID = array_pop($defaulValueID);
95 }
96
97 $filter = array(
98 'contact_type' => $contactType,
99 'greeting_type' => $greeting,
100 );
101
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)));
106 }
107
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);
115 }
116
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(
121 1, 'true'))) {
122 $processAll = TRUE;
123 }
124 elseif ($force == 2) {
125 $processOnlyIdSet = TRUE;
126 }
127
128 //FIXME : apiQuery should handle these clause.
129 $filterContactFldIds = $filterIds = array();
130 if (!$processAll) {
131 $idFldName = $displayFldName = NULL;
132 if ($greeting == 'email_greeting' || $greeting == 'postal_greeting' || $greeting == 'addressee') {
133 $idFldName = $greeting . '_id';
134 $displayFldName = $greeting . '_display';
135 }
136
137 if ($idFldName) {
138 $sql = "
139 SELECT DISTINCT id, $idFldName
140 FROM civicrm_contact
141 WHERE contact_type = %1
142 AND ( {$idFldName} IS NULL OR
143 ( {$idFldName} IS NOT NULL AND {$displayFldName} IS NULL ) )
144 ";
145 $dao = CRM_Core_DAO::executeQuery($sql, array(1 => array($contactType, 'String')));
146 while ($dao->fetch()) {
147 $filterContactFldIds[$dao->id] = $dao->$idFldName;
148
149 if (!CRM_Utils_System::isNull($dao->$idFldName)) {
150 $filterIds[$dao->id] = $dao->$idFldName;
151 }
152 }
153 }
154 if (empty($filterContactFldIds)) {
155 $filterContactFldIds[] = 0;
156 }
157 }
158
159 if (empty($filterContactFldIds)) {
160 return;
161 }
162
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
170 );
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) {
175 if (!$processAll &&
176 !array_key_exists($contactID, $filterContactFldIds)
177 ) {
178 continue;
179 }
180
181 if ($processOnlyIdSet) {
182 if (!array_key_exists($contactID, $filterIds)) {
183 continue;
184 }
185 if ($id) {
186 $greetingString = $originalGreetingString;
187 $contactIds[] = $contactID;
188 }
189 else {
190 if ($greetingBuffer = CRM_Utils_Array::value($filterContactFldIds[$contactID], $allGreetings)) {
191 $greetingString = $greetingBuffer;
192 }
193 }
194 $allContactIds[] = $contactID;
195 }
196 else {
197 $greetingString = $originalGreetingString;
198 if ($greetingBuffer = CRM_Utils_Array::value($filterContactFldIds[$contactID], $allGreetings)) {
199 $greetingString = $greetingBuffer;
200 }
201 else {
202 $contactIds[] = $contactID;
203 }
204 }
205 CRM_Utils_Token::replaceGreetingTokens($greetingString, $contactDetails, $contactID, 'CRM_UpdateGreeting');
206 $greetingString = CRM_Core_DAO::escapeString($greetingString);
207 $cacheFieldQuery .= " WHEN {$contactID} THEN '{$greetingString}' ";
208
209 $allContactIds[] = $contactID;
210 }
211
212 if (!empty($allContactIds)) {
213 $cacheFieldQuery .= " ELSE {$greeting}_display
214 END;";
215 if (!empty($contactIds)) {
216 // need to update greeting _id field.
217 $queryString = "
218 UPDATE civicrm_contact
219 SET {$greeting}_id = {$valueID}
220 WHERE id IN (" . implode(',', $contactIds) . ")";
221 CRM_Core_DAO::executeQuery($queryString);
222 }
223
224 // now update cache field
225 CRM_Core_DAO::executeQuery($cacheFieldQuery);
226 }
227 }
228 }
229
230 $obj = new CRM_UpdateGreeting();
231 $obj->updateGreeting();
232 echo "\n\n Greeting is updated for contact(s). (Done) \n";
233