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