INFRA-132 - @param type fixes
[civicrm-core.git] / CRM / Upgrade / Incremental / php / FourFive.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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. |
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-2014
31 * $Id$
32 *
33 */
34 class CRM_Upgrade_Incremental_php_FourFive {
35 const BATCH_SIZE = 5000;
36
37 /**
38 * @param $errors
39 *
40 * @return bool
41 */
42 public function verifyPreDBstate(&$errors) {
43 return TRUE;
44 }
45
46 /**
47 * Compute any messages which should be displayed beforeupgrade
48 *
49 * Note: This function is called iteratively for each upcoming
50 * revision to the database.
51 *
52 * @param $preUpgradeMessage
53 * @param string $rev
54 * a version number, e.g. '4.4.alpha1', '4.4.beta3', '4.4.0'.
55 * @param null $currentVer
56 *
57 * @return void
58 */
59 public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) {
60 }
61
62 /**
63 * Compute any messages which should be displayed after upgrade
64 *
65 * @param string $postUpgradeMessage
66 * alterable.
67 * @param string $rev
68 * an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs.
69 * @return void
70 */
71 public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
72 if ($rev == '4.5.alpha1') {
73 $postUpgradeMessage .= '<br /><br />' . ts('Default versions of the following System Workflow Message Templates have been modified to handle new functionality: <ul><li>Contributions - Receipt (off-line)</li><li>Contributions - Receipt (on-line)</li><li>Contributions - Recurring Start and End Notification</li><li>Contributions - Recurring Updates</li><li>Memberships - Receipt (on-line)</li><li>Memberships - Signup and Renewal Receipts (off-line)</li><li>Pledges - Acknowledgement</li></ul> If you have modified these templates, please review the new default versions and implement updates as needed to your copies (Administer > Communications > Message Templates > System Workflow Messages). (<a href="%1">learn more...</a>)', array(1 => 'http://wiki.civicrm.org/confluence/display/CRMDOC/Updating+System+Workflow+Message+Templates+after+Upgrades+-+method+1+-+kdiff'));
74 $postUpgradeMessage .= '<br /><br />' . ts('This release allows you to view and edit multiple-record custom field sets in a table format which will be more usable in some cases. You can try out the format by navigating to Administer > Custom Data & Screens > Custom Fields. Click Settings for a custom field set and change Display Style to "Tab with Tables".');
75 $postUpgradeMessage .= '<br /><br />' . ts('This release changes the way that anonymous event registrations match participants with existing contacts. By default, all event participants will be matched with existing individuals using the Unsupervised rule, even if multiple registrations with the same email address are allowed. However, you can now select a different matching rule to use for each event. Please review your events to make sure you choose the appropriate matching rule and collect sufficient information for it to match contacts.');
76 }
77 if ($rev == '4.5.beta2') {
78 $postUpgradeMessage .= '<br /><br />' . ts('If you use CiviMail for newsletters or other communications, check out the new sample CiviMail templates which use responsive design to optimize display on mobile devices (Administer > Communications > Message Templates ).');
79 }
80 if ($rev == '4.5.1') {
81 $postUpgradeMessage .= '<br /><br />' . ts('WARNING: If you use CiviCase with v4.5.alpha*, v4.5.beta*, or v4.5.0, it is possible that previous upgrades corrupted some CiviCase metadata. If you have not already done so, please identify any custom field sets, smart groups, or reports which refer to CiviCase and ensure that they are properly configured.');
82 }
83 }
84
85 /**
86 * @param $rev
87 *
88 * @return bool
89 */
90 public function upgrade_4_5_alpha1($rev) {
91 // task to process sql
92 $this->addTask(ts('Migrate honoree information to module_data'), 'migrateHonoreeInfo');
93 $this->addTask(ts('Upgrade DB to 4.5.alpha1: SQL'), 'task_4_5_x_runSql', $rev);
94 $this->addTask(ts('Set default for Individual name fields configuration'), 'addNameFieldOptions');
95
96 // CRM-14522 - The below schema checking is done as foreign key name
97 // for pdf_format_id column varies for different databases
98 // if DB is been into upgrade for 3.4.2 version, it would have pdf_format_id name for FK
99 // else FK_civicrm_msg_template_pdf_format_id
100 $config = CRM_Core_Config::singleton();
101 $dbUf = DB::parseDSN($config->dsn);
102 $query = "
103 SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
104 WHERE TABLE_NAME = 'civicrm_msg_template'
105 AND CONSTRAINT_TYPE = 'FOREIGN KEY'
106 AND TABLE_SCHEMA = %1
107 ";
108 $params = array(1 => array($dbUf['database'], 'String'));
109 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, NULL, FALSE, FALSE);
110 if ($dao->fetch()) {
111 if ($dao->CONSTRAINT_NAME == 'FK_civicrm_msg_template_pdf_format_id' ||
112 $dao->CONSTRAINT_NAME == 'pdf_format_id') {
113 $sqlDropFK = "ALTER TABLE `civicrm_msg_template`
114 DROP FOREIGN KEY `{$dao->CONSTRAINT_NAME}`,
115 DROP KEY `{$dao->CONSTRAINT_NAME}`";
116 CRM_Core_DAO::executeQuery($sqlDropFK, CRM_Core_DAO::$_nullArray, TRUE, NULL, FALSE, FALSE);
117 }
118 }
119
120 return TRUE;
121 }
122
123 /**
124 * @param $rev
125 *
126 * @return bool
127 */
128 public function upgrade_4_5_beta9($rev) {
129 $this->addTask(ts('Upgrade DB to 4.5.beta9: SQL'), 'task_4_5_x_runSql', $rev);
130
131 $entityTable = array(
132 'Participant' => 'civicrm_participant_payment',
133 'Contribution' => 'civicrm_contribution',
134 'Membership' => 'civicrm_membership',
135 );
136
137 foreach ($entityTable as $label => $tableName) {
138 list($minId, $maxId) = CRM_Core_DAO::executeQuery("SELECT coalesce(min(id),0), coalesce(max(id),0)
139 FROM {$tableName}")->getDatabaseResult()->fetchRow();
140 for ($startId = $minId; $startId <= $maxId; $startId += self::BATCH_SIZE) {
141 $endId = $startId + self::BATCH_SIZE - 1;
142 $title = ts("Upgrade DB to 4.5.beta9: Fix line items for {$label} (%1 => %2)", array(1 => $startId, 2 => $endId));
143 $this->addTask($title, 'task_4_5_0_fixLineItem', $startId, $endId, $label);
144 }
145 }
146 return TRUE;
147 }
148
149 /**
150 * (Queue Task Callback)
151 *
152 * update the line items
153 *
154 *
155 * @param CRM_Queue_TaskContext $ctx
156 * @param int $startId
157 * the first/lowest entity ID to convert.
158 * @param int $endId
159 * the last/highest entity ID to convert.
160 * @param
161 *
162 * @return bool
163 */
164 public static function task_4_5_0_fixLineItem(CRM_Queue_TaskContext $ctx, $startId, $endId, $entityTable) {
165
166 $sqlParams = array(
167 1 => array($startId, 'Integer'),
168 2 => array($endId, 'Integer'),
169 );
170 switch ($entityTable) {
171 case 'Contribution':
172 // update all the line item entity_table and entity_id with contribution due to bug CRM-15055
173 CRM_Core_DAO::executeQuery("UPDATE civicrm_line_item li
174 INNER JOIN civicrm_contribution cc ON cc.id = li.contribution_id
175 SET entity_id = li.contribution_id, entity_table = 'civicrm_contribution'
176 WHERE li.contribution_id IS NOT NULL AND li.entity_table <> 'civicrm_participant' AND (cc.id BETWEEN %1 AND %2)", $sqlParams);
177
178 // update the civicrm_line_item.contribution_id
179 CRM_Core_DAO::executeQuery("UPDATE civicrm_line_item li
180 INNER JOIN civicrm_contribution cc ON cc.id = li.entity_id
181 SET contribution_id = entity_id
182 WHERE li.contribution_id IS NULL AND li.entity_table = 'civicrm_contribution' AND (cc.id BETWEEN %1 AND %2)", $sqlParams);
183 break;
184
185 case 'Participant':
186 // update the civicrm_line_item.contribution_id
187 CRM_Core_DAO::executeQuery("UPDATE civicrm_line_item li
188 INNER JOIN civicrm_participant_payment pp ON pp.participant_id = li.entity_id
189 SET li.contribution_id = pp.contribution_id
190 WHERE li.entity_table = 'civicrm_participant' AND li.contribution_id IS NULL AND (pp.id BETWEEN %1 AND %2)", $sqlParams);
191 break;
192
193 case 'Membership':
194 $upgrade = new CRM_Upgrade_Form();
195 // update the line item of membership
196 CRM_Core_DAO::executeQuery("UPDATE civicrm_line_item li
197 INNER JOIN civicrm_membership_payment mp ON mp.contribution_id = li.contribution_id
198 INNER JOIN civicrm_membership cm ON mp.membership_id = cm.id
199 INNER JOIN civicrm_price_field_value pv ON pv.id = li.price_field_value_id
200 SET li.entity_table = 'civicrm_membership', li.entity_id = mp.membership_id
201 WHERE li.entity_table = 'civicrm_contribution'
202 AND pv.membership_type_id IS NOT NULL AND cm.membership_type_id = pv.membership_type_id AND (cm.id BETWEEN %1 AND %2)", $sqlParams);
203
204 CRM_Core_DAO::executeQuery("UPDATE civicrm_line_item li
205 INNER JOIN civicrm_membership_payment mp ON mp.contribution_id = li.contribution_id
206 INNER JOIN civicrm_price_field_value pv ON pv.id = li.price_field_value_id
207 SET li.entity_table = 'civicrm_membership', li.entity_id = mp.membership_id
208 WHERE li.entity_table = 'civicrm_contribution'
209 AND pv.membership_type_id IS NOT NULL AND (mp.membership_id BETWEEN %1 AND %2)", $sqlParams);
210
211 CRM_Core_DAO::executeQuery("INSERT INTO civicrm_line_item (entity_table, entity_id, price_field_id, label,
212 qty, unit_price, line_total, price_field_value_id, financial_type_id)
213 SELECT 'civicrm_membership', cm.id, cpf.id price_field_id, cpfv.label, 1 as qty, cpfv.amount, cpfv.amount line_total,
214 cpfv.id price_field_value_id, cpfv.financial_type_id FROM civicrm_membership cm
215 LEFT JOIN civicrm_membership_payment cmp ON cmp.membership_id = cm.id
216 INNER JOIN civicrm_price_field_value cpfv ON cpfv.membership_type_id = cm.membership_type_id
217 INNER JOIN civicrm_price_field cpf ON cpf.id = cpfv.price_field_id
218 INNER JOIN civicrm_price_set cps ON cps.id = cpf.price_set_id
219 WHERE cmp.contribution_id IS NULL AND cps.name = 'default_membership_type_amount' AND (cm.id BETWEEN %1 AND %2)", $sqlParams);
220 break;
221 }
222 return TRUE;
223 }
224
225 /**
226 * Add defaults for the newly introduced name fields configuration in 'contact_edit_options' setting
227 *
228 * @param CRM_Queue_TaskContext $ctx
229 *
230 * @return bool TRUE for success
231 */
232 public static function addNameFieldOptions(CRM_Queue_TaskContext $ctx) {
233 $query = "SELECT `value` FROM `civicrm_setting` WHERE `group_name` = 'CiviCRM Preferences' AND `name` = 'contact_edit_options'";
234 $dao = CRM_Core_DAO::executeQuery($query);
235 $dao->fetch();
236 $oldValue = unserialize($dao->value);
237
238 $newValue = $oldValue . '12\ 114\ 115\ 116\ 117\ 1';
239
240 $query = "UPDATE `civicrm_setting` SET `value` = %1 WHERE `group_name` = 'CiviCRM Preferences' AND `name` = 'contact_edit_options'";
241 $params = array(1 => array(serialize($newValue), 'String'));
242 CRM_Core_DAO::executeQuery($query, $params);
243
244 return TRUE;
245 }
246
247 /**
248 * Migrate honoree information to uf_join.module_data as honoree columns (text and title) will be dropped
249 * on DB upgrade
250 *
251 * @param CRM_Queue_TaskContext $ctx
252 *
253 * @return bool TRUE for success
254 */
255 public static function migrateHonoreeInfo(CRM_Queue_TaskContext $ctx) {
256 $query = "ALTER TABLE `civicrm_uf_join`
257 ADD COLUMN `module_data` longtext COMMENT 'Json serialized array of data used by the ufjoin.module'";
258 CRM_Core_DAO::executeQuery($query);
259
260 $honorTypes = array_keys(CRM_Core_OptionGroup::values('honor_type'));
261 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
262 $ufGroupDAO->name = 'new_individual';
263 $ufGroupDAO->find(TRUE);
264
265 $query = "SELECT * FROM civicrm_contribution_page";
266 $dao = CRM_Core_DAO::executeQuery($query);
267
268 if ($dao->N) {
269 $domain = new CRM_Core_DAO_Domain;
270 $domain->find(TRUE);
271 while ($dao->fetch()) {
272 $honorParams = array('soft_credit' => array('soft_credit_types' => $honorTypes));
273 if ($domain->locales) {
274 $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
275 foreach ($locales as $locale) {
276 $honor_block_title = "honor_block_title_{$locale}";
277 $honor_block_text = "honor_block_text_{$locale}";
278 $honorParams['soft_credit'] += array(
279 $locale => array(
280 'honor_block_title' => $dao->$honor_block_title,
281 'honor_block_text' => $dao->$honor_block_text,
282 ),
283 );
284 }
285 }
286 else {
287 $honorParams['soft_credit'] += array(
288 'default' => array(
289 'honor_block_title' => $dao->honor_block_title,
290 'honor_block_text' => $dao->honor_block_text,
291 ),
292 );
293 }
294 $ufJoinParam = array(
295 'module' => 'soft_credit',
296 'entity_table' => 'civicrm_contribution_page',
297 'is_active' => $dao->honor_block_is_active,
298 'entity_id' => $dao->id,
299 'uf_group_id' => $ufGroupDAO->id,
300 'module_data' => json_encode($honorParams),
301 );
302 CRM_Core_BAO_UFJoin::create($ufJoinParam);
303 }
304 }
305
306 return TRUE;
307 }
308
309 /**
310 * (Queue Task Callback)
311 */
312 public static function task_4_5_x_runSql(CRM_Queue_TaskContext $ctx, $rev) {
313 $upgrade = new CRM_Upgrade_Form();
314 $upgrade->processSQL($rev);
315
316 return TRUE;
317 }
318
319 /**
320 * Syntactic sugar for adding a task which (a) is in this class and (b) has
321 * a high priority.
322 *
323 * After passing the $funcName, you can also pass parameters that will go to
324 * the function. Note that all params must be serializable.
325 */
326 protected function addTask($title, $funcName) {
327 $queue = CRM_Queue_Service::singleton()->load(array(
328 'type' => 'Sql',
329 'name' => CRM_Upgrade_Form::QUEUE_NAME,
330 ));
331
332 $args = func_get_args();
333 $title = array_shift($args);
334 $funcName = array_shift($args);
335 $task = new CRM_Queue_Task(
336 array(get_class($this), $funcName),
337 $args,
338 $title
339 );
340 $queue->createItem($task, array('weight' => -1));
341 }
342 }