Merge pull request #4289 from colemanw/CRM-15130
[civicrm-core.git] / bin / deprecated / UpdateGreeting.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
41 /**
42 * Class CRM_UpdateGreeting
43 */
44 class CRM_UpdateGreeting {
45 /**
46 *
47 */
48 function __construct() {
49 $this->initialize();
50
51 $config = CRM_Core_Config::singleton();
52
53 require_once 'CRM/Utils/Request.php';
54 require_once 'CRM/Core/PseudoConstant.php';
55 require_once 'CRM/Contact/BAO/Contact.php';
56
57 // this does not return on failure
58 CRM_Utils_System::authenticateScript(TRUE);
59
60 //log the execution time of script
61 CRM_Core_Error::debug_log_message('UpdateGreeting.php');
62 }
63
64 function initialize() {
65 require_once '../../civicrm.config.php';
66 require_once 'CRM/Core/Config.php';
67 }
68
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')
74 )) {
75 CRM_Core_Error::fatal(ts('Invalid Contact Type.'));
76 }
77
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')
81 )) {
82 CRM_Core_Error::fatal(ts('Invalid Greeting Type.'));
83 }
84
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)));
88 }
89
90 $valueID = $id = CRM_Utils_Request::retrieve('id', 'Positive', CRM_Core_DAO::$_nullArray, FALSE, NULL, 'REQUEST');
91
92 // if valueID is not passed use default value
93 if (!$valueID) {
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 )",
99 "value"
100 );
101 $valueID = array_pop($defaulValueID);
102 }
103
104 $filter = array(
105 'contact_type' => $contactType,
106 'greeting_type' => $greeting,
107 );
108
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)));
113 }
114
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);
122 }
123
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(
128 1, 'true'))) {
129 $processAll = TRUE;
130 }
131 elseif ($force == 2) {
132 $processOnlyIdSet = TRUE;
133 }
134
135 //FIXME : apiQuery should handle these clause.
136 $filterContactFldIds = $filterIds = array();
137 if (!$processAll) {
138 $idFldName = $displayFldName = NULL;
139 if ($greeting == 'email_greeting' || $greeting == 'postal_greeting' || $greeting == 'addressee') {
140 $idFldName = $greeting . '_id';
141 $displayFldName = $greeting . '_display';
142 }
143
144 if ($idFldName) {
145 $sql = "
146 SELECT DISTINCT id, $idFldName
147 FROM civicrm_contact
148 WHERE contact_type = %1
149 AND ( {$idFldName} IS NULL OR
150 ( {$idFldName} IS NOT NULL AND {$displayFldName} IS NULL ) )
151 ";
152 $dao = CRM_Core_DAO::executeQuery($sql, array(1 => array($contactType, 'String')));
153 while ($dao->fetch()) {
154 $filterContactFldIds[$dao->id] = $dao->$idFldName;
155
156 if (!CRM_Utils_System::isNull($dao->$idFldName)) {
157 $filterIds[$dao->id] = $dao->$idFldName;
158 }
159 }
160 }
161 if (empty($filterContactFldIds)) {
162 $filterContactFldIds[] = 0;
163 }
164 }
165
166 if (empty($filterContactFldIds)) {
167 return;
168 }
169
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
177 );
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) {
182 if (!$processAll &&
183 !array_key_exists($contactID, $filterContactFldIds)
184 ) {
185 continue;
186 }
187
188 if ($processOnlyIdSet) {
189 if (!array_key_exists($contactID, $filterIds)) {
190 continue;
191 }
192 if ($id) {
193 $greetingString = $originalGreetingString;
194 $contactIds[] = $contactID;
195 }
196 else {
197 if ($greetingBuffer = CRM_Utils_Array::value($filterContactFldIds[$contactID], $allGreetings)) {
198 $greetingString = $greetingBuffer;
199 }
200 }
201 $allContactIds[] = $contactID;
202 }
203 else {
204 $greetingString = $originalGreetingString;
205 if ($greetingBuffer = CRM_Utils_Array::value($filterContactFldIds[$contactID], $allGreetings)) {
206 $greetingString = $greetingBuffer;
207 }
208 else {
209 $contactIds[] = $contactID;
210 }
211 }
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}' ";
215
216 $allContactIds[] = $contactID;
217 }
218
219 if (!empty($allContactIds)) {
220 $cacheFieldQuery .= " ELSE {$greeting}_display
221 END;";
222 if (!empty($contactIds)) {
223 // need to update greeting _id field.
224 $queryString = "
225 UPDATE civicrm_contact
226 SET {$greeting}_id = {$valueID}
227 WHERE id IN (" . implode(',', $contactIds) . ")";
228 CRM_Core_DAO::executeQuery($queryString);
229 }
230
231 // now update cache field
232 CRM_Core_DAO::executeQuery($cacheFieldQuery);
233 }
234 }
235 }
236
237 $obj = new CRM_UpdateGreeting();
238 $obj->updateGreeting();
239 echo "\n\n Greeting is updated for contact(s). (Done) \n";
240