CRM-13066, minor cleanup
[civicrm-core.git] / CRM / Upgrade / Incremental / php / FourFour.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
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 */
34 class 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) {
62 if ($rev == '4.4.alpha1') {
63 $config = CRM_Core_Config::singleton();
64 if (!empty($config->useIDS)) {
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");
66 }
67 }
68 }
69
70 function upgrade_4_4_alpha1($rev) {
71 // task to process sql
72 $this->addTask(ts('Upgrade DB to 4.4.alpha1: SQL'), 'task_4_4_x_runSql', $rev);
73
74 // Consolidate activity contacts CRM-12274.
75 $this->addTask(ts('Consolidate activity contacts'), 'activityContacts');
76
77 return TRUE;
78 }
79
80 function upgrade_4_4_beta1($rev) {
81 $this->addTask(ts('Upgrade DB to 4.4.beta1: SQL'), 'task_4_4_x_runSql', $rev);
82
83 // check if batch entry data exists in civicrm_cache table
84 $query = 'SELECT path, data FROM civicrm_cache WHERE group_name = "batch entry"';
85 $dao = CRM_Core_DAO::executeQuery($query);
86 while ($dao->fetch()) {
87 // get batch id $batchId[2]
88 $batchId = explode('-', $dao->path);
89 $data = unserialize($dao->data);
90
91 // move the data to civicrm_batch table
92 CRM_Core_DAO::setFieldValue('CRM_Batch_DAO_Batch', $batchId[2], 'data', json_encode(array('values' => $data)));
93 }
94
95 // delete entries from civicrm_cache table
96 $query = 'DELETE FROM civicrm_cache WHERE group_name = "batch entry"';
97 CRM_Core_DAO::executeQuery($query);
98 }
99
100 /**
101 * Update activity contacts CRM-12274
102 *
103 * @return bool TRUE for success
104 */
105 static function activityContacts(CRM_Queue_TaskContext $ctx) {
106 $upgrade = new CRM_Upgrade_Form();
107
108 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
109 $ovValue[] = $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
110 $ovValue[] = $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
111 $ovValue[] = $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
112
113 $optionGroupID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'activity_contacts', 'id', 'name');
114 if (!empty($ovValue)) {
115 $ovValues = implode(', ', $ovValue);
116 $query = "
117 UPDATE civicrm_option_value
118 SET is_reserved = 1
119 WHERE option_group_id = {$optionGroupID} AND value IN ($ovValues)";
120
121 $dao = CRM_Core_DAO::executeQuery($query);
122 }
123
124 if (!$assigneeID) {
125 $assigneeID = 1;
126 $value[] = "({$optionGroupID}, 'Activity Assignees', 1, 'Activity Assignees', 1, 1, 1)";
127 }
128 if (!$sourceID) {
129 $sourceID = 2;
130 $value[] = "({$optionGroupID}, 'Activity Source', 2, 'Activity Source', 2, 1, 1)";
131 }
132 if (!$targetID) {
133 $targetID = 3;
134 $value[] = "({$optionGroupID}, 'Activity Targets', 3, 'Activity Targets', 3, 1, 1)";
135 }
136
137 if (!$assigneeID || !$sourceID || !$targetID ) {
138 $insert = "
139 INSERT INTO civicrm_option_value
140 (option_group_id, label, value, name, weight, is_reserved, is_active)
141 VALUES
142
143 ";
144 $values = implode(', ', $value);
145 $query = $insert . $values;
146 $dao = CRM_Core_DAO::executeQuery($query);
147 }
148
149
150 $query = "
151 CREATE TABLE IF NOT EXISTS civicrm_activity_contact (
152 id int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Activity contact id',
153 activity_id int(10) unsigned NOT NULL COMMENT 'Foreign key to the activity for this record.',
154 contact_id int(10) unsigned NOT NULL COMMENT 'Foreign key to the contact for this record.',
155 record_type_id int(10) unsigned DEFAULT NULL COMMENT 'The record type id for this row',
156 PRIMARY KEY (id),
157 UNIQUE KEY UI_activity_contact (contact_id,activity_id,record_type_id),
158 KEY FK_civicrm_activity_contact_activity_id (activity_id)
159 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
160 ";
161
162 $dao = CRM_Core_DAO::executeQuery($query);
163
164 $query = "
165 INSERT INTO civicrm_activity_contact (activity_id, contact_id, record_type_id)
166 SELECT activity_id, target_contact_id, {$targetID} as record_type_id
167 FROM civicrm_activity_target";
168
169 $dao = CRM_Core_DAO::executeQuery($query);
170
171 $query = "
172 INSERT INTO civicrm_activity_contact (activity_id, contact_id, record_type_id)
173 SELECT activity_id, assignee_contact_id, {$assigneeID} as record_type_id
174 FROM civicrm_activity_assignment";
175 $dao = CRM_Core_DAO::executeQuery($query);
176
177 $query = "
178 INSERT INTO civicrm_activity_contact (activity_id, contact_id, record_type_id)
179 SELECT id, source_contact_id, {$sourceID} as record_type_id
180 FROM civicrm_activity
181 WHERE source_contact_id IS NOT NULL";
182
183 $dao = CRM_Core_DAO::executeQuery($query);
184
185 $query = "DROP TABLE civicrm_activity_target";
186 $dao = CRM_Core_DAO::executeQuery($query);
187
188 $query = "DROP TABLE civicrm_activity_assignment";
189 $dao = CRM_Core_DAO::executeQuery($query);
190
191 $query = "ALTER TABLE civicrm_activity
192 DROP FOREIGN KEY FK_civicrm_activity_source_contact_id";
193
194 $dao = CRM_Core_DAO::executeQuery($query);
195
196 $query = "ALTER TABLE civicrm_activity DROP COLUMN source_contact_id";
197 $dao = CRM_Core_DAO::executeQuery($query);
198
199 return TRUE;
200 }
201
202 /**
203 * (Queue Task Callback)
204 */
205 static function task_4_4_x_runSql(CRM_Queue_TaskContext $ctx, $rev) {
206 $upgrade = new CRM_Upgrade_Form();
207 $upgrade->processSQL($rev);
208
209 return TRUE;
210 }
211
212 /**
213 * Syntatic sugar for adding a task which (a) is in this class and (b) has
214 * a high priority.
215 *
216 * After passing the $funcName, you can also pass parameters that will go to
217 * the function. Note that all params must be serializable.
218 */
219 protected function addTask($title, $funcName) {
220 $queue = CRM_Queue_Service::singleton()->load(array(
221 'type' => 'Sql',
222 'name' => CRM_Upgrade_Form::QUEUE_NAME,
223 ));
224
225 $args = func_get_args();
226 $title = array_shift($args);
227 $funcName = array_shift($args);
228 $task = new CRM_Queue_Task(
229 array(get_class($this), $funcName),
230 $args,
231 $title
232 );
233 $queue->createItem($task, array('weight' => -1));
234 }
235 }