Merge pull request #3317 from eileenmcnaughton/CRM-14197-postprocesfn
[civicrm-core.git] / CRM / Upgrade / Incremental / Legacy.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 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-2014
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 */
40 class CRM_Upgrade_Incremental_Legacy {
41
42 /**
43 * Compute any messages which should be displayed before upgrade
44 *
45 * @param $preUpgradeMessage string, alterable
46 * @param $currentVer
47 * @param $latestVer
48 */
49 static function setPreUpgradeMessage(&$preUpgradeMessage, $currentVer, $latestVer) {
50 $upgrade = new CRM_Upgrade_Form();
51 $template = CRM_Core_Smarty::singleton();
52
53 if ((version_compare($currentVer, '3.3.alpha1') < 0 &&
54 version_compare($latestVer, '3.3.alpha1') >= 0
55 ) ||
56 (version_compare($currentVer, '3.4.alpha1') < 0 &&
57 version_compare($latestVer, '3.4.alpha1') >= 0
58 )
59 ) {
60 $query = "
61 SELECT id
62 FROM civicrm_mailing_job
63 WHERE status NOT IN ( 'Complete', 'Canceled' ) AND is_test = 0 LIMIT 1";
64 $mjId = CRM_Core_DAO::singleValueQuery($query);
65 if ($mjId) {
66 $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.");
67 }
68 }
69
70 //turning some tables to monolingual during 3.4.beta3, CRM-7869
71 $upgradeTo = str_replace('4.0.', '3.4.', $latestVer);
72 $upgradeFrom = str_replace('4.0.', '3.4.', $currentVer);
73
74 // check for changed message templates
75 self::checkMessageTemplate($template, $preUpgradeMessage, $upgradeTo, $upgradeFrom);
76
77 $upgrade = new CRM_Upgrade_Form();
78 if ($upgrade->multilingual &&
79 version_compare($upgradeFrom, '3.4.beta3') == -1 &&
80 version_compare($upgradeTo, '3.4.beta3') >= 0
81 ) {
82 $config = CRM_Core_Config::singleton();
83 $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));
84 }
85
86 if (version_compare($currentVer, '3.4.6') == -1 &&
87 version_compare($latestVer, '3.4.6') >= 0
88 ) {
89 $googleProcessorExists = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_payment_processor WHERE payment_processor_type = 'Google_Checkout' AND is_active = 1 LIMIT 1;");
90
91 if ($googleProcessorExists) {
92 $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'));
93 }
94 }
95
96 // http://issues.civicrm.org/jira/browse/CRM-13572
97 // Depending on how the code was upgraded, some sites may still have copies of old
98 // source files left behind. This is often a forgivable offense, but it's quite
99 // dangerous for CIVI-SA-2013-001.
100 global $civicrm_root;
101 $ofcFile = "$civicrm_root/packages/OpenFlashChart/php-ofc-library/ofc_upload_image.php";
102 if (file_exists($ofcFile)) {
103 if (@unlink($ofcFile)) {
104 $preUpgradeMessage .= '<br />' . ts('This system included an outdated, insecure script (%1). The file was automatically deleted.', array(
105 1 => $ofcFile
106 ));
107 } else {
108 $preUpgradeMessage .= '<br />' . ts('This system includes an outdated, insecure script (%1). Please delete it.', array(
109 1 => $ofcFile
110 ));
111 }
112 }
113 }
114
115 /**
116 * @param $template
117 * @param $message
118 * @param $latestVer
119 * @param $currentVer
120 */
121 static function checkMessageTemplate(&$template, &$message, $latestVer, $currentVer) {
122 if (version_compare($currentVer, '3.1.alpha1') < 0) {
123 return;
124 }
125
126 $sql = "SELECT orig.workflow_id as workflow_id,
127 orig.msg_title as title
128 FROM civicrm_msg_template diverted JOIN civicrm_msg_template orig ON (
129 diverted.workflow_id = orig.workflow_id AND
130 orig.is_reserved = 1 AND (
131 diverted.msg_subject != orig.msg_subject OR
132 diverted.msg_text != orig.msg_text OR
133 diverted.msg_html != orig.msg_html
134 )
135 )";
136
137 $dao = &CRM_Core_DAO::executeQuery($sql);
138 while ($dao->fetch()) {
139 $workflows[$dao->workflow_id] = $dao->title;
140 }
141
142 if (empty($workflows)) {
143 return;
144 }
145
146 $html = NULL;
147 $pathName = dirname(dirname(__FILE__));
148 $flag = FALSE;
149 foreach ($workflows as $workflow => $title) {
150 $name = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue',
151 $workflow,
152 'name',
153 'id'
154 );
155
156 // check if file exists locally
157 $textFileName = implode(DIRECTORY_SEPARATOR,
158 array(
159 $pathName,
160 "{$latestVer}.msg_template",
161 'message_templates',
162 "{$name}_text.tpl",
163 )
164 );
165
166 $htmlFileName = implode(DIRECTORY_SEPARATOR,
167 array(
168 $pathName,
169 "{$latestVer}.msg_template",
170 'message_templates',
171 "{$name}_html.tpl",
172 )
173 );
174
175 if (file_exists($textFileName) ||
176 file_exists($htmlFileName)
177 ) {
178 $flag = TRUE;
179 $html .= "<li>{$title}</li>";
180 }
181 }
182
183 if ($flag == TRUE) {
184 $html = "<ul>" . $html . "<ul>";
185
186 $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/CRMDOC/Message+Templates#MessageTemplates-UpgradesandCustomizedSystemWorkflowTemplates', 2 => $html));
187 }
188 }
189
190 /**
191 * Compute any messages which should be displayed after upgrade
192 *
193 * @param $postUpgradeMessage string, alterable
194 * @param $rev string, an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs
195 * @return void
196 */
197 static function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
198 if ($rev == '3.2.alpha1') {
199 $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.");
200 }
201 if ($rev == '3.2.beta3') {
202 $subTypes = CRM_Contact_BAO_ContactType::subTypes();
203
204 if (is_array($subTypes) && !empty($subTypes)) {
205 $config = CRM_Core_Config::singleton();
206 $subTypeTemplates = array();
207
208 if (isset($config->customTemplateDir)) {
209 foreach ($subTypes as $key => $subTypeName) {
210 $customContactSubTypeEdit = $config->customTemplateDir . "CRM/Contact/Form/Edit/" . $subTypeName . ".tpl";
211 $customContactSubTypeView = $config->customTemplateDir . "CRM/Contact/Page/View/" . $subTypeName . ".tpl";
212 if (file_exists($customContactSubTypeEdit) || file_exists($customContactSubTypeView)) {
213 $subTypeTemplates[$subTypeName] = $subTypeName;
214 }
215 }
216 }
217
218 foreach ($subTypes as $key => $subTypeName) {
219 $customContactSubTypeEdit = $config->templateDir . "CRM/Contact/Form/Edit/" . $subTypeName . ".tpl";
220 $customContactSubTypeView = $config->templateDir . "CRM/Contact/Page/View/" . $subTypeName . ".tpl";
221 if (file_exists($customContactSubTypeEdit) || file_exists($customContactSubTypeView)) {
222 $subTypeTemplates[$subTypeName] = $subTypeName;
223 }
224 }
225
226 if (!empty($subTypeTemplates)) {
227 $subTypeTemplates = implode(',', $subTypeTemplates);
228 $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'));
229 }
230 }
231 }
232 if ($rev == '3.2.beta4') {
233 $statuses = array('New', 'Current', 'Grace', 'Expired', 'Pending', 'Cancelled', 'Deceased');
234 $sql = "
235 SELECT count( id ) as statusCount
236 FROM civicrm_membership_status
237 WHERE name IN ( '" . implode("' , '", $statuses) . "' ) ";
238 $count = CRM_Core_DAO::singleValueQuery($sql);
239 if ($count < count($statuses)) {
240 $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).");
241 }
242 }
243 if ($rev == '3.4.alpha1') {
244 $renamedBinScripts = array(
245 'ParticipantProcessor.php',
246 'RespondentProcessor.php',
247 'UpdateGreeting.php',
248 'UpdateMembershipRecord.php',
249 'UpdatePledgeRecord.php ',
250 );
251 $postUpgradeMessage .= '<br />' . ts('The following files have been renamed to have a ".php" extension instead of a ".php.txt" extension') . ': ' . implode(', ', $renamedBinScripts);
252 }
253 }
254
255 /**
256 * Perform an incremental upgrade
257 *
258 * @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)
259 */
260 static function upgrade_2_2_alpha1($rev) {
261 for ($stepID = 1; $stepID <= 4; $stepID++) {
262 $formName = "CRM_Upgrade_TwoTwo_Form_Step{$stepID}";
263 $form = new $formName();
264
265 $error = NULL;
266 if (!$form->verifyPreDBState($error)) {
267 if (!isset($error)) {
268 $error = "pre-condition failed for current upgrade step $stepID, rev $rev";
269 }
270 CRM_Core_Error::fatal($error);
271 }
272
273 if ($stepID == 4) {
274 return;
275 }
276
277 $template = CRM_Core_Smarty::singleton();
278
279 $eventFees = array();
280 $query = "SELECT og.id ogid FROM civicrm_option_group og WHERE og.name LIKE %1";
281 $params = array(1 => array('civicrm_event_page.amount%', 'String'));
282 $dao = CRM_Core_DAO::executeQuery($query, $params);
283 while ($dao->fetch()) {
284 $eventFees[$dao->ogid] = $dao->ogid;
285 }
286 $template->assign('eventFees', $eventFees);
287
288 $form->upgrade();
289
290 if (!$form->verifyPostDBState($error)) {
291 if (!isset($error)) {
292 $error = "post-condition failed for current upgrade step $stepID, rev $rev";
293 }
294 CRM_Core_Error::fatal($error);
295 }
296 }
297 }
298
299 /**
300 * Perform an incremental upgrade
301 *
302 * @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)
303 */
304 static function upgrade_2_1_2($rev) {
305 $formName = "CRM_Upgrade_TwoOne_Form_TwoOneTwo";
306 $form = new $formName($rev);
307
308 $error = NULL;
309 if (!$form->verifyPreDBState($error)) {
310 if (!isset($error)) {
311 $error = "pre-condition failed for current upgrade for $rev";
312 }
313 CRM_Core_Error::fatal($error);
314 }
315
316 $form->upgrade();
317
318 if (!$form->verifyPostDBState($error)) {
319 if (!isset($error)) {
320 $error = "post-condition failed for current upgrade for $rev";
321 }
322 CRM_Core_Error::fatal($error);
323 }
324 }
325
326 /**
327 * This function should check if if need to skip current sql file
328 * Name of this function will change according to the latest release
329 *
330 */
331 static function upgrade_2_2_alpha3($rev) {
332 // skip processing sql file, if fresh install -
333 if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'mail_protocol', 'id', 'name')) {
334 $upgrade = new CRM_Upgrade_Form();
335 $upgrade->processSQL($rev);
336 }
337 return TRUE;
338 }
339
340 /**
341 * Perform an incremental upgrade
342 *
343 * @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)
344 */
345 static function upgrade_2_2_beta1($rev) {
346 if (!CRM_Core_DAO::checkFieldExists('civicrm_pcp_block', 'notify_email')) {
347 $template = CRM_Core_Smarty::singleton();
348 $template->assign('notifyAbsent', TRUE);
349 }
350 $upgrade = new CRM_Upgrade_Form();
351 $upgrade->processSQL($rev);
352 }
353
354 /**
355 * Perform an incremental upgrade
356 *
357 * @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)
358 */
359 static function upgrade_2_2_beta2($rev) {
360 $template = CRM_Core_Smarty::singleton();
361 if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue',
362 'CRM_Contact_Form_Search_Custom_ZipCodeRange', 'id', 'name'
363 )) {
364 $template->assign('customSearchAbsentAll', TRUE);
365 }
366 elseif (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue',
367 'CRM_Contact_Form_Search_Custom_MultipleValues', 'id', 'name'
368 )) {
369 $template->assign('customSearchAbsent', TRUE);
370 }
371 $upgrade = new CRM_Upgrade_Form();
372 $upgrade->processSQL($rev);
373 }
374
375 /**
376 * Perform an incremental upgrade
377 *
378 * @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)
379 */
380 static function upgrade_2_2_beta3($rev) {
381 $template = CRM_Core_Smarty::singleton();
382 if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'custom_data_type', 'id', 'name')) {
383 $template->assign('customDataType', TRUE);
384 }
385
386 $upgrade = new CRM_Upgrade_Form();
387 $upgrade->processSQL($rev);
388 }
389
390 /**
391 * Perform an incremental upgrade
392 *
393 * @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)
394 */
395 static function upgrade_3_0_alpha1($rev) {
396
397 $threeZero = new CRM_Upgrade_ThreeZero_ThreeZero();
398
399 $error = NULL;
400 if (!$threeZero->verifyPreDBState($error)) {
401 if (!isset($error)) {
402 $error = 'pre-condition failed for current upgrade for 3.0.alpha2';
403 }
404 CRM_Core_Error::fatal($error);
405 }
406
407 $threeZero->upgrade($rev);
408 }
409
410 /**
411 * Perform an incremental upgrade
412 *
413 * @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)
414 */
415 static function upgrade_3_1_alpha1($rev) {
416
417 $threeOne = new CRM_Upgrade_ThreeOne_ThreeOne();
418
419 $error = NULL;
420 if (!$threeOne->verifyPreDBState($error)) {
421 if (!isset($error)) {
422 $error = 'pre-condition failed for current upgrade for 3.0.alpha2';
423 }
424 CRM_Core_Error::fatal($error);
425 }
426
427 $threeOne->upgrade($rev);
428 }
429
430 /**
431 * Perform an incremental upgrade
432 *
433 * @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)
434 */
435 static function upgrade_2_2_7($rev) {
436 $upgrade = new CRM_Upgrade_Form();
437 $upgrade->processSQL($rev);
438 $sql = "UPDATE civicrm_report_instance
439 SET form_values = REPLACE(form_values,'#',';') ";
440 CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
441
442 // make report component enabled by default
443 $domain = new CRM_Core_DAO_Domain();
444 $domain->selectAdd();
445 $domain->selectAdd('config_backend');
446 $domain->find(TRUE);
447 if ($domain->config_backend) {
448 $defaults = unserialize($domain->config_backend);
449
450 if (is_array($defaults['enableComponents'])) {
451 $compId = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_component WHERE name = 'CiviReport'");
452 if ($compId) {
453 $defaults['enableComponents'][] = 'CiviReport';
454 $defaults['enableComponentIDs'][] = $compId;
455
456 CRM_Core_BAO_ConfigSetting::add($defaults);
457 }
458 }
459 }
460 }
461
462 /**
463 * Perform an incremental upgrade
464 *
465 * @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)
466 */
467 static function upgrade_3_0_2($rev) {
468
469 $template = CRM_Core_Smarty::singleton();
470 //check whether upgraded from 2.1.x or 2.2.x
471 $inboundEmailID = CRM_Core_OptionGroup::getValue('activity_type', 'Inbound Email', 'name');
472
473 if (!empty($inboundEmailID)) {
474 $template->assign('addInboundEmail', FALSE);
475 }
476 else {
477 $template->assign('addInboundEmail', TRUE);
478 }
479
480 $upgrade = new CRM_Upgrade_Form();
481 $upgrade->processSQL($rev);
482 }
483
484 /**
485 * Perform an incremental upgrade
486 *
487 * @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)
488 */
489 static function upgrade_3_0_4($rev) {
490 //make sure 'Deceased' membership status present in db,CRM-5636
491 $template = CRM_Core_Smarty::singleton();
492
493 $addDeceasedStatus = FALSE;
494 $sql = "SELECT max(id) FROM civicrm_membership_status where name = 'Deceased'";
495 if (!CRM_Core_DAO::singleValueQuery($sql)) {
496 $addDeceasedStatus = TRUE;
497 }
498 $template->assign('addDeceasedStatus', $addDeceasedStatus);
499
500 $upgrade = new CRM_Upgrade_Form();
501 $upgrade->processSQL($rev);
502 }
503
504 /**
505 * Perform an incremental upgrade
506 *
507 * @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)
508 */
509 static function upgrade_3_1_0($rev) {
510 // upgrade all roles who have 'access CiviEvent' permission, to also have
511 // newly added permission 'edit_all_events', CRM-5472
512 $config = CRM_Core_Config::singleton();
513 if (is_callable(array(
514 $config->userSystem, 'replacePermission'))) {
515 $config->userSystem->replacePermission('access CiviEvent', array('access CiviEvent', 'edit all events'));
516 }
517
518 //make sure 'Deceased' membership status present in db,CRM-5636
519 $template = CRM_Core_Smarty::singleton();
520
521 $addDeceasedStatus = FALSE;
522 $sql = "SELECT max(id) FROM civicrm_membership_status where name = 'Deceased'";
523 if (!CRM_Core_DAO::singleValueQuery($sql)) {
524 $addDeceasedStatus = TRUE;
525 }
526 $template->assign('addDeceasedStatus', $addDeceasedStatus);
527
528 $upgrade = new CRM_Upgrade_Form();
529 $upgrade->processSQL($rev);
530 }
531
532 /**
533 * Perform an incremental upgrade
534 *
535 * @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)
536 */
537 static function upgrade_3_1_3($rev) {
538 $threeOne = new CRM_Upgrade_ThreeOne_ThreeOne();
539 $threeOne->upgrade_3_1_3();
540
541 $upgrade = new CRM_Upgrade_Form();
542 $upgrade->processSQL($rev);
543 }
544
545 /**
546 * Perform an incremental upgrade
547 *
548 * @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)
549 */
550 static function upgrade_3_1_4($rev) {
551 $threeOne = new CRM_Upgrade_ThreeOne_ThreeOne();
552 $threeOne->upgrade_3_1_4();
553
554 $upgrade = new CRM_Upgrade_Form();
555 $upgrade->processSQL($rev);
556 }
557 }
558