CRM-13655 - Upgrade/.../FourFour.php - Setup correct schema for civicrm_word_replacem...
[civicrm-core.git] / CRM / Upgrade / Incremental / php / FourFour.php
CommitLineData
a47a2f90 1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
a47a2f90 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. |
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 along with this program; if not, contact CiviCRM LLC |
21 | at info[AT]civicrm[DOT]org. If you have questions about the |
22 | GNU Affero General Public License or the licensing of CiviCRM, |
23 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
24 +--------------------------------------------------------------------+
25*/
26
27/**
28 *
29 * @package CRM
30 * @copyright CiviCRM LLC (c) 2004-2013
31 * $Id$
32 *
33 */
34class CRM_Upgrade_Incremental_php_FourFour {
35 const BATCH_SIZE = 5000;
36
37 function verifyPreDBstate(&$errors) {
38 return TRUE;
39 }
40
41 /**
42 * Compute any messages which should be displayed beforeupgrade
43 *
44 * Note: This function is called iteratively for each upcoming
45 * revision to the database.
46 *
47 * @param $postUpgradeMessage string, alterable
48 * @param $rev string, a version number, e.g. '4.4.alpha1', '4.4.beta3', '4.4.0'
49 * @return void
50 */
51 function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) {
52 }
53
54 /**
55 * Compute any messages which should be displayed after upgrade
56 *
57 * @param $postUpgradeMessage string, alterable
58 * @param $rev string, an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs
59 * @return void
60 */
61 function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
634e1a1a
DL
62 if ($rev == '4.4.alpha1') {
63 $config = CRM_Core_Config::singleton();
64 if (!empty($config->useIDS)) {
d1401e86 65 $postUpgradeMessage .= '<br />' . ts("The setting to skip IDS check has been deprecated. Please use the permission 'skip IDS check' to bypass the IDS system.");
634e1a1a
DL
66 }
67 }
a47a2f90 68 }
69
70 function upgrade_4_4_alpha1($rev) {
634e1a1a 71 // task to process sql
d1401e86 72 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.4.alpha1')), 'task_4_4_x_runSql', $rev);
a47a2f90 73
74 // Consolidate activity contacts CRM-12274.
d1401e86 75 $this->addTask('Consolidate activity contacts', 'activityContacts');
a47a2f90 76
77 return TRUE;
78 }
79
7ee2786a 80 function upgrade_4_4_beta1($rev) {
d1401e86 81 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.4.beta1')), 'task_4_4_x_runSql', $rev);
7ee2786a 82
56d4e0ab 83 // add new 'data' column in civicrm_batch
84 $query = 'ALTER TABLE civicrm_batch ADD data LONGTEXT NULL COMMENT "cache entered data"';
d427e40e 85 CRM_Core_DAO::executeQuery($query, array(), TRUE, NULL, FALSE, FALSE);
56d4e0ab 86
7ee2786a 87 // check if batch entry data exists in civicrm_cache table
f1811020 88 $query = 'SELECT path, data FROM civicrm_cache WHERE group_name = "batch entry"';
7ee2786a 89 $dao = CRM_Core_DAO::executeQuery($query);
90 while ($dao->fetch()) {
91 // get batch id $batchId[2]
92 $batchId = explode('-', $dao->path);
93 $data = unserialize($dao->data);
94
95 // move the data to civicrm_batch table
96 CRM_Core_DAO::setFieldValue('CRM_Batch_DAO_Batch', $batchId[2], 'data', json_encode(array('values' => $data)));
97 }
98
99 // delete entries from civicrm_cache table
f1811020 100 $query = 'DELETE FROM civicrm_cache WHERE group_name = "batch entry"';
7ee2786a 101 CRM_Core_DAO::executeQuery($query);
ba457d64 102
d1401e86 103 $this->addTask('Migrate custom word-replacements', 'wordReplacements');
7ee2786a 104 }
105
6b12405a
RN
106 function upgrade_4_4_1($rev) {
107 // CRM-13327 upgrade handling for the newly added name badges
108 $ogID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'name_badge', 'id', 'name');
109 $nameBadges = array_flip(array_values(CRM_Core_BAO_OptionValue::getOptionValuesAssocArrayFromName('name_badge')));
110 unset($nameBadges['Avery 5395']);
111 if (!empty($nameBadges)) {
112 $dimension = '{"paper-size":"a4","orientation":"portrait","font-name":"times","font-size":6,"font-style":"","NX":2,"NY":4,"metric":"mm","lMargin":6,"tMargin":19,"SpaceX":0,"SpaceY":0,"width":100,"height":65,"lPadding":0,"tPadding":0}';
113 $query = "UPDATE civicrm_option_value
114 SET value = '{$dimension}'
115 WHERE option_group_id = %1 AND name = 'Fattorini Name Badge 100x65'";
116
117 CRM_Core_DAO::executeQuery($query, array(1 => array($ogID, 'Integer')));
118 }
119 else {
120 $dimensions = array(
121 1 => '{"paper-size":"a4","orientation":"landscape","font-name":"times","font-size":6,"font-style":"","NX":2,"NY":1,"metric":"mm","lMargin":25,"tMargin":27,"SpaceX":0,"SpaceY":35,"width":106,"height":150,"lPadding":5,"tPadding":5}',
122 2 => '{"paper-size":"a4","orientation":"portrait","font-name":"times","font-size":6,"font-style":"","NX":2,"NY":4,"metric":"mm","lMargin":6,"tMargin":19,"SpaceX":0,"SpaceY":0,"width":100,"height":65,"lPadding":0,"tPadding":0}',
123 3 => '{"paper-size":"a4","orientation":"portrait","font-name":"times","font-size":6,"font-style":"","NX":2,"NY":2,"metric":"mm","lMargin":10,"tMargin":28,"SpaceX":0,"SpaceY":0,"width":96,"height":121,"lPadding":5,"tPadding":5}',
124 );
125 $insertStatements = array(
126 1 => "($ogID, %1, '{$dimensions[1]}', %1, NULL, 0, NULL, 2, NULL, 0, 0, 1, NULL, NULL)",
127 2 => "($ogID, %2, '{$dimensions[2]}', %2, NULL, 0, NULL, 3, NULL, 0, 0, 1, NULL, NULL)",
128 3 => "($ogID, %3, '{$dimensions[3]}', %3, NULL, 0, NULL, 4, NULL, 0, 0, 1, NULL, NULL)",
129 );
130
131 $queryParams = array(
132 1 => array('A6 Badge Portrait 150x106', 'String'),
133 2 => array('Fattorini Name Badge 100x65', 'String'),
134 3 => array('Hanging Badge 3-3/4" x 4-3"/4', 'String'),
135 );
136
137 foreach ($insertStatements as $values) {
138 $query = 'INSERT INTO civicrm_option_value (`option_group_id`, `label`, `value`, `name`, `grouping`, `filter`, `is_default`, `weight`, `description`, `is_optgroup`, `is_reserved`, `is_active`, `component_id`, `visibility_id`) VALUES' . $values;
139 CRM_Core_DAO::executeQuery($query, $queryParams);
140 }
141 }
142
143 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.4.1')), 'task_4_4_x_runSql', $rev);
144 }
145
a47a2f90 146 /**
147 * Update activity contacts CRM-12274
148 *
149 * @return bool TRUE for success
150 */
151 static function activityContacts(CRM_Queue_TaskContext $ctx) {
152 $upgrade = new CRM_Upgrade_Form();
127ca309 153
e7e657f0 154 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
b95507ab 155 $ovValue[] = $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
156 $ovValue[] = $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
157 $ovValue[] = $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
634e1a1a 158
9744c42c 159 $optionGroupID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'activity_contacts', 'id', 'name');
b95507ab 160 if (!empty($ovValue)) {
161 $ovValues = implode(', ', $ovValue);
162 $query = "
634e1a1a 163UPDATE civicrm_option_value
b95507ab 164SET is_reserved = 1
165WHERE option_group_id = {$optionGroupID} AND value IN ($ovValues)";
166
167 $dao = CRM_Core_DAO::executeQuery($query);
168 }
169
9744c42c 170 if (!$assigneeID) {
b95507ab 171 $assigneeID = 1;
9744c42c 172 $value[] = "({$optionGroupID}, 'Activity Assignees', 1, 'Activity Assignees', 1, 1, 1)";
173 }
174 if (!$sourceID) {
b95507ab 175 $sourceID = 2;
9744c42c 176 $value[] = "({$optionGroupID}, 'Activity Source', 2, 'Activity Source', 2, 1, 1)";
177 }
178 if (!$targetID) {
b95507ab 179 $targetID = 3;
9744c42c 180 $value[] = "({$optionGroupID}, 'Activity Targets', 3, 'Activity Targets', 3, 1, 1)";
181 }
182
183 if (!$assigneeID || !$sourceID || !$targetID ) {
634e1a1a 184 $insert = "
9744c42c 185INSERT INTO civicrm_option_value
186(option_group_id, label, value, name, weight, is_reserved, is_active)
187VALUES
188
189";
190 $values = implode(', ', $value);
191 $query = $insert . $values;
192 $dao = CRM_Core_DAO::executeQuery($query);
193 }
194
195
a47a2f90 196 $query = "
127ca309 197CREATE TABLE IF NOT EXISTS civicrm_activity_contact (
198 id int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Activity contact id',
199 activity_id int(10) unsigned NOT NULL COMMENT 'Foreign key to the activity for this record.',
200 contact_id int(10) unsigned NOT NULL COMMENT 'Foreign key to the contact for this record.',
201 record_type_id int(10) unsigned DEFAULT NULL COMMENT 'The record type id for this row',
202 PRIMARY KEY (id),
203 UNIQUE KEY UI_activity_contact (contact_id,activity_id,record_type_id),
204 KEY FK_civicrm_activity_contact_activity_id (activity_id)
a47a2f90 205) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
206";
207
208 $dao = CRM_Core_DAO::executeQuery($query);
634e1a1a
DL
209
210 $query = "
127ca309 211INSERT INTO civicrm_activity_contact (activity_id, contact_id, record_type_id)
212SELECT activity_id, target_contact_id, {$targetID} as record_type_id
a47a2f90 213FROM civicrm_activity_target";
214
215 $dao = CRM_Core_DAO::executeQuery($query);
216
634e1a1a 217 $query = "
127ca309 218INSERT INTO civicrm_activity_contact (activity_id, contact_id, record_type_id)
219SELECT activity_id, assignee_contact_id, {$assigneeID} as record_type_id
a47a2f90 220FROM civicrm_activity_assignment";
221 $dao = CRM_Core_DAO::executeQuery($query);
634e1a1a 222
a47a2f90 223 $query = "
127ca309 224 INSERT INTO civicrm_activity_contact (activity_id, contact_id, record_type_id)
225SELECT id, source_contact_id, {$sourceID} as record_type_id
634e1a1a 226FROM civicrm_activity
a47a2f90 227WHERE source_contact_id IS NOT NULL";
228
229 $dao = CRM_Core_DAO::executeQuery($query);
230
231 $query = "DROP TABLE civicrm_activity_target";
232 $dao = CRM_Core_DAO::executeQuery($query);
233
234 $query = "DROP TABLE civicrm_activity_assignment";
235 $dao = CRM_Core_DAO::executeQuery($query);
236
634e1a1a 237 $query = "ALTER TABLE civicrm_activity
a47a2f90 238 DROP FOREIGN KEY FK_civicrm_activity_source_contact_id";
634e1a1a 239
a47a2f90 240 $dao = CRM_Core_DAO::executeQuery($query);
241
242 $query = "ALTER TABLE civicrm_activity DROP COLUMN source_contact_id";
243 $dao = CRM_Core_DAO::executeQuery($query);
244
ba457d64
TO
245 return TRUE;
246 }
247
248 /**
249 * Migrate word-replacements from $config to civicrm_word_replacement
250 *
251 * @return bool TRUE for success
252 * @see http://issues.civicrm.org/jira/browse/CRM-13187
253 */
254 static function wordReplacements(CRM_Queue_TaskContext $ctx) {
255 $query = "
7c312eca
TO
256CREATE TABLE IF NOT EXISTS `civicrm_word_replacement` (
257 `id` int unsigned NOT NULL AUTO_INCREMENT COMMENT 'Word replacement ID',
02ffc0f2
TO
258 `find_word` varchar(255) COLLATE utf8_bin COMMENT 'Word which need to be replaced',
259 `replace_word` varchar(255) COLLATE utf8_bin COMMENT 'Word which will replace the word in find',
7c312eca
TO
260 `is_active` tinyint COMMENT 'Is this entry active?',
261 `match_type` enum('wildcardMatch', 'exactMatch') DEFAULT 'wildcardMatch',
262 `domain_id` int unsigned COMMENT 'FK to Domain ID. This is for Domain specific word replacement',
263 PRIMARY KEY ( `id` ),
02ffc0f2 264 UNIQUE INDEX `UI_find`(domain_id, find_word),
7c312eca
TO
265 CONSTRAINT FK_civicrm_word_replacement_domain_id FOREIGN KEY (`domain_id`) REFERENCES `civicrm_domain`(`id`)
266) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ;
267 ";
ba457d64
TO
268 $dao = CRM_Core_DAO::executeQuery($query);
269
daa14c0c 270 self::rebuildWordReplacementTable();
ba457d64 271 return TRUE;
a47a2f90 272 }
273
274 /**
275 * (Queue Task Callback)
276 */
277 static function task_4_4_x_runSql(CRM_Queue_TaskContext $ctx, $rev) {
278 $upgrade = new CRM_Upgrade_Form();
279 $upgrade->processSQL($rev);
280
281 return TRUE;
282 }
283
284 /**
285 * Syntatic sugar for adding a task which (a) is in this class and (b) has
286 * a high priority.
287 *
288 * After passing the $funcName, you can also pass parameters that will go to
289 * the function. Note that all params must be serializable.
290 */
291 protected function addTask($title, $funcName) {
292 $queue = CRM_Queue_Service::singleton()->load(array(
293 'type' => 'Sql',
294 'name' => CRM_Upgrade_Form::QUEUE_NAME,
295 ));
296
297 $args = func_get_args();
298 $title = array_shift($args);
299 $funcName = array_shift($args);
300 $task = new CRM_Queue_Task(
301 array(get_class($this), $funcName),
302 $args,
303 $title
304 );
305 $queue->createItem($task, array('weight' => -1));
306 }
cbe56ea6
TO
307
308 /**
daa14c0c
TO
309 * Get all the word-replacements stored in config-arrays
310 * and convert them to params for the WordReplacement.create API.
311 *
0f65e834
TO
312 * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and
313 * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade
314 * step behaves consistently even as the BAO evolves in future versions.
315 * However, if there's a bug in here prior to 4.4.0, we should apply the
316 * bugfix in both places.
317 *
63b71ea8 318 * @param bool $rebuildEach whether to perform rebuild after each individual API call
daa14c0c
TO
319 * @return array Each item is $params for WordReplacement.create
320 * @see CRM_Core_BAO_WordReplacement::convertConfigArraysToAPIParams
cbe56ea6 321 */
63b71ea8 322 static function getConfigArraysAsAPIParams($rebuildEach) {
cbe56ea6 323 $wordReplacementCreateParams = array();
cbe56ea6 324 // get all domains
daa14c0c
TO
325 $result = civicrm_api3('domain', 'get', array(
326 'return' => array('locale_custom_strings'),
327 ));
cbe56ea6
TO
328 if (!empty($result["values"])) {
329 foreach ($result["values"] as $value) {
330 $params = array();
cbe56ea6 331 $params["domain_id"] = $value["id"];
63b71ea8 332 $params["options"] = array('wp-rebuild' => $rebuildEach);
cbe56ea6
TO
333 // unserialize word match string
334 $localeCustomArray = unserialize($value["locale_custom_strings"]);
335 if (!empty($localeCustomArray)) {
336 $wordMatchArray = array();
7c312eca 337 // Traverse Language array
cbe56ea6 338 foreach ($localeCustomArray as $localCustomData) {
7c312eca
TO
339 // Traverse status array "enabled" "disabled"
340 foreach ($localCustomData as $status => $matchTypes) {
341 $params["is_active"] = ($status == "enabled")?TRUE:FALSE;
342 // Traverse Match Type array "wildcardMatch" "exactMatch"
343 foreach ($matchTypes as $matchType => $words) {
344 $params["match_type"] = $matchType;
345 foreach ($words as $word => $replace) {
346 $params["find_word"] = $word;
347 $params["replace_word"] = $replace;
348 $wordReplacementCreateParams[] = $params;
349 }
350 }
cbe56ea6
TO
351 }
352 }
353 }
354 }
355 }
356 return $wordReplacementCreateParams;
8d57c529 357 }
ba457d64 358
daa14c0c
TO
359 /**
360 * Get all the word-replacements stored in config-arrays
361 * and write them out as records in civicrm_word_replacement.
0f65e834
TO
362 *
363 * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and
364 * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade
365 * step behaves consistently even as the BAO evolves in future versions.
366 * However, if there's a bug in here prior to 4.4.0, we should apply the
367 * bugfix in both places.
daa14c0c
TO
368 */
369 public static function rebuildWordReplacementTable() {
370 civicrm_api3('word_replacement', 'replace', array(
371 'options' => array('match' => array('domain_id', 'find_word')),
372 'values' => self::getConfigArraysAsAPIParams(FALSE),
373 ));
374 CRM_Core_BAO_WordReplacement::rebuild();
ba457d64 375 }
a47a2f90 376}