Merge pull request #1758 from dlobo/CRM-13554
[civicrm-core.git] / CRM / Upgrade / Incremental / Legacy.php
CommitLineData
6a488035
TO
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 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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35
36/**
37 * This class is a container for legacy upgrade logic which predates
38 * the current 'CRM/Incremental/php/*' structure.
39 */
40class CRM_Upgrade_Incremental_Legacy {
41
42 /**
43 * Compute any messages which should be displayed before upgrade
44 *
45 * @param $preUpgradeMessage string, alterable
46 */
47 static function setPreUpgradeMessage(&$preUpgradeMessage, $currentVer, $latestVer) {
48 $upgrade = new CRM_Upgrade_Form();
49 $template = CRM_Core_Smarty::singleton();
50
51 if ((version_compare($currentVer, '3.3.alpha1') < 0 &&
52 version_compare($latestVer, '3.3.alpha1') >= 0
53 ) ||
54 (version_compare($currentVer, '3.4.alpha1') < 0 &&
55 version_compare($latestVer, '3.4.alpha1') >= 0
56 )
57 ) {
58 $query = "
59SELECT id
60 FROM civicrm_mailing_job
61 WHERE status NOT IN ( 'Complete', 'Canceled' ) AND is_test = 0 LIMIT 1";
62 $mjId = CRM_Core_DAO::singleValueQuery($query);
63 if ($mjId) {
64 $preUpgradeMessage = ts("There are one or more Scheduled or In Progress mailings in your install. Scheduled mailings will not be sent and In Progress mailings will not finish if you continue with the upgrade. We strongly recommend that all Scheduled and In Progress mailings be completed or cancelled and then upgrade your CiviCRM install.");
65 }
66 }
67
68 //turning some tables to monolingual during 3.4.beta3, CRM-7869
69 $upgradeTo = str_replace('4.0.', '3.4.', $latestVer);
70 $upgradeFrom = str_replace('4.0.', '3.4.', $currentVer);
71
72 // check for changed message templates
73 self::checkMessageTemplate($template, $preUpgradeMessage, $upgradeTo, $upgradeFrom);
74
75 $upgrade = new CRM_Upgrade_Form();
76 if ($upgrade->multilingual &&
77 version_compare($upgradeFrom, '3.4.beta3') == -1 &&
78 version_compare($upgradeTo, '3.4.beta3') >= 0
79 ) {
80 $config = CRM_Core_Config::singleton();
81 $preUpgradeMessage .= '<br />' . ts("As per <a href='%1'>the related blog post</a>, we are making contact names, addresses and mailings monolingual; the values entered for the default locale (%2) will be preserved and values for other locales removed.", array(1 => 'http://civicrm.org/blogs/shot/multilingual-civicrm-3440-making-some-fields-monolingual', 2 => $config->lcMessages));
82 }
83
84 if (version_compare($currentVer, '3.4.6') == -1 &&
85 version_compare($latestVer, '3.4.6') >= 0
86 ) {
87 $googleProcessorExists = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_payment_processor WHERE payment_processor_type = 'Google_Checkout' AND is_active = 1 LIMIT 1;");
88
89 if ($googleProcessorExists) {
90 $preUpgradeMessage .= '<br />' . ts('To continue using Google Checkout Payment Processor with latest version of CiviCRM, requires updating merchant account settings. Please refer "Set API callback URL and other settings" section of <a href="%1" target="_blank"><strong>Google Checkout Configuration</strong></a> doc.', array(1 => 'http://wiki.civicrm.org/confluence/x/zAJTAg'));
91 }
92 }
93 }
94
95 static function checkMessageTemplate(&$template, &$message, $latestVer, $currentVer) {
96 if (version_compare($currentVer, '3.1.alpha1') < 0) {
97 return;
98 }
99
100 $sql = "SELECT orig.workflow_id as workflow_id,
101 orig.msg_title as title
102 FROM civicrm_msg_template diverted JOIN civicrm_msg_template orig ON (
103 diverted.workflow_id = orig.workflow_id AND
104 orig.is_reserved = 1 AND (
105 diverted.msg_subject != orig.msg_subject OR
106 diverted.msg_text != orig.msg_text OR
107 diverted.msg_html != orig.msg_html
108 )
109 )";
110
111 $dao = &CRM_Core_DAO::executeQuery($sql);
112 while ($dao->fetch()) {
113 $workflows[$dao->workflow_id] = $dao->title;
114 }
115
116 if (empty($workflows)) {
117 return;
118 }
119
120 $html = NULL;
121 $pathName = dirname(dirname(__FILE__));
122 $flag = FALSE;
123 foreach ($workflows as $workflow => $title) {
124 $name = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue',
125 $workflow,
126 'name',
127 'id'
128 );
129
130 // check if file exists locally
131 $textFileName = implode(DIRECTORY_SEPARATOR,
132 array(
133 $pathName,
134 "{$latestVer}.msg_template",
135 'message_templates',
136 "{$name}_text.tpl",
137 )
138 );
139
140 $htmlFileName = implode(DIRECTORY_SEPARATOR,
141 array(
142 $pathName,
143 "{$latestVer}.msg_template",
144 'message_templates',
145 "{$name}_html.tpl",
146 )
147 );
148
149 if (file_exists($textFileName) ||
150 file_exists($htmlFileName)
151 ) {
152 $flag = TRUE;
153 $html .= "<li>{$title}</li>";
154 }
155 }
156
157 if ($flag == TRUE) {
158 $html = "<ul>" . $html . "<ul>";
159
160 $message .= '<br />' . ts("The default copies of the message templates listed below will be updated to handle new features. Your installation has customized versions of these message templates, and you will need to apply the updates manually after running this upgrade. <a href='%1' style='color:white; text-decoration:underline; font-weight:bold;' target='_blank'>Click here</a> for detailed instructions. %2", array(1 => 'http://wiki.civicrm.org/confluence/display/CRMDOC40/Message+Templates#MessageTemplates-UpgradesandCustomizedSystemWorkflowTemplates', 2 => $html));
161 }
162 }
163
164 /**
165 * Compute any messages which should be displayed after upgrade
166 *
167 * @param $postUpgradeMessage string, alterable
168 * @param $rev string, an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs
169 * @return void
170 */
171 static function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
172 if ($rev == '3.2.alpha1') {
173 $postUpgradeMessage .= '<br />' . ts("We have reset the COUNTED flag to false for the event participant status 'Pending from incomplete transaction'. This change ensures that people who have a problem during registration can try again.");
174 }
175 if ($rev == '3.2.beta3') {
176 $subTypes = CRM_Contact_BAO_ContactType::subTypes();
177
178 if (is_array($subTypes) && !empty($subTypes)) {
179 $config = CRM_Core_Config::singleton();
180 $subTypeTemplates = array();
181
182 if (isset($config->customTemplateDir)) {
183 foreach ($subTypes as $key => $subTypeName) {
184 $customContactSubTypeEdit = $config->customTemplateDir . "CRM/Contact/Form/Edit/" . $subTypeName . ".tpl";
185 $customContactSubTypeView = $config->customTemplateDir . "CRM/Contact/Page/View/" . $subTypeName . ".tpl";
186 if (file_exists($customContactSubTypeEdit) || file_exists($customContactSubTypeView)) {
187 $subTypeTemplates[$subTypeName] = $subTypeName;
188 }
189 }
190 }
191
192 foreach ($subTypes as $key => $subTypeName) {
193 $customContactSubTypeEdit = $config->templateDir . "CRM/Contact/Form/Edit/" . $subTypeName . ".tpl";
194 $customContactSubTypeView = $config->templateDir . "CRM/Contact/Page/View/" . $subTypeName . ".tpl";
195 if (file_exists($customContactSubTypeEdit) || file_exists($customContactSubTypeView)) {
196 $subTypeTemplates[$subTypeName] = $subTypeName;
197 }
198 }
199
200 if (!empty($subTypeTemplates)) {
201 $subTypeTemplates = implode(',', $subTypeTemplates);
202 $postUpgradeMessage .= '<br />' . ts('You are using custom template for contact subtypes: %1.', array(1 => $subTypeTemplates)) . '<br />' . ts('You need to move these subtype templates to the SubType directory in %1 and %2 respectively.', array(1 => 'CRM/Contact/Form/Edit', 2 => 'CRM/Contact/Page/View'));
203 }
204 }
205 }
206 if ($rev == '3.2.beta4') {
207 $statuses = array('New', 'Current', 'Grace', 'Expired', 'Pending', 'Cancelled', 'Deceased');
208 $sql = "
209SELECT count( id ) as statusCount
210 FROM civicrm_membership_status
211 WHERE name IN ( '" . implode("' , '", $statuses) . "' ) ";
212 $count = CRM_Core_DAO::singleValueQuery($sql);
213 if ($count < count($statuses)) {
214 $postUpgradeMessage .= '<br />' . ts("One or more Membership Status Rules was disabled during the upgrade because it did not match a recognized status name. if custom membership status rules were added to this site - review the disabled statuses and re-enable any that are still needed (Administer > CiviMember > Membership Status Rules).");
215 }
216 }
217 if ($rev == '3.4.alpha1') {
218 $renamedBinScripts = array(
219 'ParticipantProcessor.php',
220 'RespondentProcessor.php',
221 'UpdateGreeting.php',
222 'UpdateMembershipRecord.php',
223 'UpdatePledgeRecord.php ',
224 );
225 $postUpgradeMessage .= '<br />' . ts('The following files have been renamed to have a ".php" extension instead of a ".php.txt" extension') . ': ' . implode(', ', $renamedBinScripts);
226 }
227 }
228
229 /**
230 * Perform an incremental upgrade
231 *
232 * @param $rev string, the revision to which we are upgrading (Note: When processing a series of upgrades, this is the immediate upgrade - not the final)
233 */
234 static function upgrade_2_2_alpha1($rev) {
235 for ($stepID = 1; $stepID <= 4; $stepID++) {
236 $formName = "CRM_Upgrade_TwoTwo_Form_Step{$stepID}";
237 eval("\$form = new $formName( );");
238
239 $error = NULL;
240 if (!$form->verifyPreDBState($error)) {
241 if (!isset($error)) {
242 $error = "pre-condition failed for current upgrade step $stepID, rev $rev";
243 }
244 CRM_Core_Error::fatal($error);
245 }
246
247 if ($stepID == 4) {
248 return;
249 }
250
251 $template = CRM_Core_Smarty::singleton();
252
253 $eventFees = array();
254 $query = "SELECT og.id ogid FROM civicrm_option_group og WHERE og.name LIKE %1";
255 $params = array(1 => array('civicrm_event_page.amount%', 'String'));
256 $dao = CRM_Core_DAO::executeQuery($query, $params);
257 while ($dao->fetch()) {
258 $eventFees[$dao->ogid] = $dao->ogid;
259 }
260 $template->assign('eventFees', $eventFees);
261
262 $form->upgrade();
263
264 if (!$form->verifyPostDBState($error)) {
265 if (!isset($error)) {
266 $error = "post-condition failed for current upgrade step $stepID, rev $rev";
267 }
268 CRM_Core_Error::fatal($error);
269 }
270 }
271 }
272
273 /**
274 * Perform an incremental upgrade
275 *
276 * @param $rev string, the revision to which we are upgrading (Note: When processing a series of upgrades, this is the immediate upgrade - not the final)
277 */
278 static function upgrade_2_1_2($rev) {
279 $formName = "CRM_Upgrade_TwoOne_Form_TwoOneTwo";
280 eval("\$form = new $formName( '$rev' );");
281
282 $error = NULL;
283 if (!$form->verifyPreDBState($error)) {
284 if (!isset($error)) {
285 $error = "pre-condition failed for current upgrade for $rev";
286 }
287 CRM_Core_Error::fatal($error);
288 }
289
290 $form->upgrade();
291
292 if (!$form->verifyPostDBState($error)) {
293 if (!isset($error)) {
294 $error = "post-condition failed for current upgrade for $rev";
295 }
296 CRM_Core_Error::fatal($error);
297 }
298 }
299
300 /**
301 * This function should check if if need to skip current sql file
302 * Name of this function will change according to the latest release
303 *
304 */
305 static function upgrade_2_2_alpha3($rev) {
306 // skip processing sql file, if fresh install -
307 if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'mail_protocol', 'id', 'name')) {
308 $upgrade = new CRM_Upgrade_Form();
309 $upgrade->processSQL($rev);
310 }
311 return TRUE;
312 }
313
314 /**
315 * Perform an incremental upgrade
316 *
317 * @param $rev string, the revision to which we are upgrading (Note: When processing a series of upgrades, this is the immediate upgrade - not the final)
318 */
319 static function upgrade_2_2_beta1($rev) {
320 if (!CRM_Core_DAO::checkFieldExists('civicrm_pcp_block', 'notify_email')) {
321 $template = CRM_Core_Smarty::singleton();
322 $template->assign('notifyAbsent', TRUE);
323 }
324 $upgrade = new CRM_Upgrade_Form();
325 $upgrade->processSQL($rev);
326 }
327
328 /**
329 * Perform an incremental upgrade
330 *
331 * @param $rev string, the revision to which we are upgrading (Note: When processing a series of upgrades, this is the immediate upgrade - not the final)
332 */
333 static function upgrade_2_2_beta2($rev) {
334 $template = CRM_Core_Smarty::singleton();
335 if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue',
336 'CRM_Contact_Form_Search_Custom_ZipCodeRange', 'id', 'name'
337 )) {
338 $template->assign('customSearchAbsentAll', TRUE);
339 }
340 elseif (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue',
341 'CRM_Contact_Form_Search_Custom_MultipleValues', 'id', 'name'
342 )) {
343 $template->assign('customSearchAbsent', TRUE);
344 }
345 $upgrade = new CRM_Upgrade_Form();
346 $upgrade->processSQL($rev);
347 }
348
349 /**
350 * Perform an incremental upgrade
351 *
352 * @param $rev string, the revision to which we are upgrading (Note: When processing a series of upgrades, this is the immediate upgrade - not the final)
353 */
354 static function upgrade_2_2_beta3($rev) {
355 $template = CRM_Core_Smarty::singleton();
356 if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'custom_data_type', 'id', 'name')) {
357 $template->assign('customDataType', TRUE);
358 }
359
360 $upgrade = new CRM_Upgrade_Form();
361 $upgrade->processSQL($rev);
362 }
363
364 /**
365 * Perform an incremental upgrade
366 *
367 * @param $rev string, the revision to which we are upgrading (Note: When processing a series of upgrades, this is the immediate upgrade - not the final)
368 */
369 static function upgrade_3_0_alpha1($rev) {
370
371 $threeZero = new CRM_Upgrade_ThreeZero_ThreeZero();
372
373 $error = NULL;
374 if (!$threeZero->verifyPreDBState($error)) {
375 if (!isset($error)) {
376 $error = 'pre-condition failed for current upgrade for 3.0.alpha2';
377 }
378 CRM_Core_Error::fatal($error);
379 }
380
381 $threeZero->upgrade($rev);
382 }
383
384 /**
385 * Perform an incremental upgrade
386 *
387 * @param $rev string, the revision to which we are upgrading (Note: When processing a series of upgrades, this is the immediate upgrade - not the final)
388 */
389 static function upgrade_3_1_alpha1($rev) {
390
391 $threeOne = new CRM_Upgrade_ThreeOne_ThreeOne();
392
393 $error = NULL;
394 if (!$threeOne->verifyPreDBState($error)) {
395 if (!isset($error)) {
396 $error = 'pre-condition failed for current upgrade for 3.0.alpha2';
397 }
398 CRM_Core_Error::fatal($error);
399 }
400
401 $threeOne->upgrade($rev);
402 }
403
404 /**
405 * Perform an incremental upgrade
406 *
407 * @param $rev string, the revision to which we are upgrading (Note: When processing a series of upgrades, this is the immediate upgrade - not the final)
408 */
409 static function upgrade_2_2_7($rev) {
410 $upgrade = new CRM_Upgrade_Form();
411 $upgrade->processSQL($rev);
412 $sql = "UPDATE civicrm_report_instance
413 SET form_values = REPLACE(form_values,'#',';') ";
414 CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
415
416 // make report component enabled by default
417 $domain = new CRM_Core_DAO_Domain();
418 $domain->selectAdd();
419 $domain->selectAdd('config_backend');
420 $domain->find(TRUE);
421 if ($domain->config_backend) {
422 $defaults = unserialize($domain->config_backend);
423
424 if (is_array($defaults['enableComponents'])) {
425 $compId = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_component WHERE name = 'CiviReport'");
426 if ($compId) {
427 $defaults['enableComponents'][] = 'CiviReport';
428 $defaults['enableComponentIDs'][] = $compId;
429
430 CRM_Core_BAO_ConfigSetting::add($defaults);
431 }
432 }
433 }
434 }
435
436 /**
437 * Perform an incremental upgrade
438 *
439 * @param $rev string, the revision to which we are upgrading (Note: When processing a series of upgrades, this is the immediate upgrade - not the final)
440 */
441 static function upgrade_3_0_2($rev) {
442
443 $template = CRM_Core_Smarty::singleton();
444 //check whether upgraded from 2.1.x or 2.2.x
445 $inboundEmailID = CRM_Core_OptionGroup::getValue('activity_type', 'Inbound Email', 'name');
446
447 if (!empty($inboundEmailID)) {
448 $template->assign('addInboundEmail', FALSE);
449 }
450 else {
451 $template->assign('addInboundEmail', TRUE);
452 }
453
454 $upgrade = new CRM_Upgrade_Form();
455 $upgrade->processSQL($rev);
456 }
457
458 /**
459 * Perform an incremental upgrade
460 *
461 * @param $rev string, the revision to which we are upgrading (Note: When processing a series of upgrades, this is the immediate upgrade - not the final)
462 */
463 static function upgrade_3_0_4($rev) {
464 //make sure 'Deceased' membership status present in db,CRM-5636
465 $template = CRM_Core_Smarty::singleton();
466
467 $addDeceasedStatus = FALSE;
468 $sql = "SELECT max(id) FROM civicrm_membership_status where name = 'Deceased'";
469 if (!CRM_Core_DAO::singleValueQuery($sql)) {
470 $addDeceasedStatus = TRUE;
471 }
472 $template->assign('addDeceasedStatus', $addDeceasedStatus);
473
474 $upgrade = new CRM_Upgrade_Form();
475 $upgrade->processSQL($rev);
476 }
477
478 /**
479 * Perform an incremental upgrade
480 *
481 * @param $rev string, the revision to which we are upgrading (Note: When processing a series of upgrades, this is the immediate upgrade - not the final)
482 */
483 static function upgrade_3_1_0($rev) {
484 // upgrade all roles who have 'access CiviEvent' permission, to also have
485 // newly added permission 'edit_all_events', CRM-5472
486 $config = CRM_Core_Config::singleton();
487 if (is_callable(array(
488 $config->userSystem, 'replacePermission'))) {
489 $config->userSystem->replacePermission('access CiviEvent', array('access CiviEvent', 'edit all events'));
490 }
491
492 //make sure 'Deceased' membership status present in db,CRM-5636
493 $template = CRM_Core_Smarty::singleton();
494
495 $addDeceasedStatus = FALSE;
496 $sql = "SELECT max(id) FROM civicrm_membership_status where name = 'Deceased'";
497 if (!CRM_Core_DAO::singleValueQuery($sql)) {
498 $addDeceasedStatus = TRUE;
499 }
500 $template->assign('addDeceasedStatus', $addDeceasedStatus);
501
502 $upgrade = new CRM_Upgrade_Form();
503 $upgrade->processSQL($rev);
504 }
505
506 /**
507 * Perform an incremental upgrade
508 *
509 * @param $rev string, the revision to which we are upgrading (Note: When processing a series of upgrades, this is the immediate upgrade - not the final)
510 */
511 static function upgrade_3_1_3($rev) {
512 $threeOne = new CRM_Upgrade_ThreeOne_ThreeOne();
513 $threeOne->upgrade_3_1_3();
514
515 $upgrade = new CRM_Upgrade_Form();
516 $upgrade->processSQL($rev);
517 }
518
519 /**
520 * Perform an incremental upgrade
521 *
522 * @param $rev string, the revision to which we are upgrading (Note: When processing a series of upgrades, this is the immediate upgrade - not the final)
523 */
524 static function upgrade_3_1_4($rev) {
525 $threeOne = new CRM_Upgrade_ThreeOne_ThreeOne();
526 $threeOne->upgrade_3_1_4();
527
528 $upgrade = new CRM_Upgrade_Form();
529 $upgrade->processSQL($rev);
530 }
531}
532