Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2015-02-02-18-36-16
[civicrm-core.git] / bin / deprecated / UpdateGreeting.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
f5721b07 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
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 )
783f84c9 35 * id=greeting option value
6a488035 36 *
783f84c9 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
6a488035 39 */
4e87860d
EM
40
41/**
42 * Class CRM_UpdateGreeting
43 */
6a488035 44class CRM_UpdateGreeting {
4e87860d 45 /**
4e87860d 46 */
79d7553f 47 public function __construct() {
6a488035
TO
48 $this->initialize();
49
50 $config = CRM_Core_Config::singleton();
51
52 require_once 'CRM/Utils/Request.php';
53 require_once 'CRM/Core/PseudoConstant.php';
54 require_once 'CRM/Contact/BAO/Contact.php';
55
56 // this does not return on failure
57 CRM_Utils_System::authenticateScript(TRUE);
58
59 //log the execution time of script
60 CRM_Core_Error::debug_log_message('UpdateGreeting.php');
61 }
62
79d7553f 63 public function initialize() {
6a488035
TO
64 require_once '../../civicrm.config.php';
65 require_once 'CRM/Core/Config.php';
66 }
67
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,
56fdfc52
TO
72 array('Individual', 'Household', 'Organization')
73 )
74 ) {
6a488035
TO
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,
56fdfc52
TO
80 array('email_greeting', 'postal_greeting', 'addressee')
81 )
82 ) {
6a488035
TO
83 CRM_Core_Error::fatal(ts('Invalid Greeting Type.'));
84 }
85
86 if (in_array($greeting, array(
56fdfc52 87 'email_greeting',
79d7553f 88 'postal_greeting',
56fdfc52
TO
89 )) && $contactType == 'Organization'
90 ) {
6a488035
TO
91 CRM_Core_Error::fatal(ts('You cannot use %1 for contact type %2.', array(1 => $greeting, 2 => $contactType)));
92 }
93
94 $valueID = $id = CRM_Utils_Request::retrieve('id', 'Positive', CRM_Core_DAO::$_nullArray, FALSE, NULL, 'REQUEST');
95
96 // if valueID is not passed use default value
97 if (!$valueID) {
98 require_once 'CRM/Core/OptionGroup.php';
99 $contactTypeFilters = array(1 => 'Individual', 2 => 'Household', 3 => 'Organization');
56fdfc52
TO
100 $filter = CRM_Utils_Array::key($contactType, $contactTypeFilters);
101 $defaulValueID = CRM_Core_OptionGroup::values($greeting, NULL, NULL, NULL,
6a488035
TO
102 " AND is_default = 1 AND ( filter = {$filter} OR filter = 0 )",
103 "value"
104 );
105 $valueID = array_pop($defaulValueID);
106 }
107
108 $filter = array(
109 'contact_type' => $contactType,
110 'greeting_type' => $greeting,
111 );
112
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)));
117 }
118
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);
126 }
127
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(
56fdfc52 132 1,
79d7553f 133 'true',
56fdfc52 134 ))) {
6a488035
TO
135 $processAll = TRUE;
136 }
137 elseif ($force == 2) {
138 $processOnlyIdSet = TRUE;
139 }
140
141 //FIXME : apiQuery should handle these clause.
142 $filterContactFldIds = $filterIds = array();
143 if (!$processAll) {
144 $idFldName = $displayFldName = NULL;
145 if ($greeting == 'email_greeting' || $greeting == 'postal_greeting' || $greeting == 'addressee') {
146 $idFldName = $greeting . '_id';
147 $displayFldName = $greeting . '_display';
148 }
149
150 if ($idFldName) {
151 $sql = "
152SELECT DISTINCT id, $idFldName
783f84c9 153 FROM civicrm_contact
154 WHERE contact_type = %1
155 AND ( {$idFldName} IS NULL OR
6a488035
TO
156 ( {$idFldName} IS NOT NULL AND {$displayFldName} IS NULL ) )
157 ";
158 $dao = CRM_Core_DAO::executeQuery($sql, array(1 => array($contactType, 'String')));
159 while ($dao->fetch()) {
160 $filterContactFldIds[$dao->id] = $dao->$idFldName;
161
162 if (!CRM_Utils_System::isNull($dao->$idFldName)) {
163 $filterIds[$dao->id] = $dao->$idFldName;
164 }
165 }
166 }
167 if (empty($filterContactFldIds)) {
168 $filterContactFldIds[] = 0;
169 }
170 }
171
172 if (empty($filterContactFldIds)) {
173 return;
174 }
175
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
183 );
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) {
188 if (!$processAll &&
189 !array_key_exists($contactID, $filterContactFldIds)
190 ) {
191 continue;
192 }
193
194 if ($processOnlyIdSet) {
195 if (!array_key_exists($contactID, $filterIds)) {
196 continue;
197 }
198 if ($id) {
199 $greetingString = $originalGreetingString;
200 $contactIds[] = $contactID;
201 }
202 else {
203 if ($greetingBuffer = CRM_Utils_Array::value($filterContactFldIds[$contactID], $allGreetings)) {
204 $greetingString = $greetingBuffer;
205 }
206 }
207 $allContactIds[] = $contactID;
208 }
209 else {
210 $greetingString = $originalGreetingString;
211 if ($greetingBuffer = CRM_Utils_Array::value($filterContactFldIds[$contactID], $allGreetings)) {
212 $greetingString = $greetingBuffer;
213 }
214 else {
215 $contactIds[] = $contactID;
216 }
217 }
73d64eb6 218 CRM_Contact_BAO_Contact_Utils::processGreetingTemplate($greetingString, $contactDetails, $contactID, 'CRM_UpdateGreeting');
6a488035
TO
219 $greetingString = CRM_Core_DAO::escapeString($greetingString);
220 $cacheFieldQuery .= " WHEN {$contactID} THEN '{$greetingString}' ";
221
222 $allContactIds[] = $contactID;
223 }
224
225 if (!empty($allContactIds)) {
226 $cacheFieldQuery .= " ELSE {$greeting}_display
227 END;";
228 if (!empty($contactIds)) {
229 // need to update greeting _id field.
230 $queryString = "
783f84c9 231UPDATE civicrm_contact
232 SET {$greeting}_id = {$valueID}
6a488035
TO
233 WHERE id IN (" . implode(',', $contactIds) . ")";
234 CRM_Core_DAO::executeQuery($queryString);
235 }
236
237 // now update cache field
238 CRM_Core_DAO::executeQuery($cacheFieldQuery);
239 }
240 }
96025800 241
6a488035
TO
242}
243
244$obj = new CRM_UpdateGreeting();
245$obj->updateGreeting();
246echo "\n\n Greeting is updated for contact(s). (Done) \n";