Merge pull request #3229 from eileenmcnaughton/e-notice
[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 static function checkMessageTemplate(&$template, &$message, $latestVer, $currentVer) {
116 if (version_compare($currentVer, '3.1.alpha1') < 0) {
117 return;
118 }
119
120 $sql = "SELECT orig.workflow_id as workflow_id,
121 orig.msg_title as title
122 FROM civicrm_msg_template diverted JOIN civicrm_msg_template orig ON (
123 diverted.workflow_id = orig.workflow_id AND
124 orig.is_reserved = 1 AND (
125 diverted.msg_subject != orig.msg_subject OR
126 diverted.msg_text != orig.msg_text OR
127 diverted.msg_html != orig.msg_html
128 )
129 )";
130
131 $dao = &CRM_Core_DAO::executeQuery($sql);
132 while ($dao->fetch()) {
133 $workflows[$dao->workflow_id] = $dao->title;
134 }
135
136 if (empty($workflows)) {
137 return;
138 }
139
140 $html = NULL;
141 $pathName = dirname(dirname(__FILE__));
142 $flag = FALSE;
143 foreach ($workflows as $workflow => $title) {
144 $name = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue',
145 $workflow,
146 'name',
147 'id'
148 );
149
150 // check if file exists locally
151 $textFileName = implode(DIRECTORY_SEPARATOR,
152 array(
153 $pathName,
154 "{$latestVer}.msg_template",
155 'message_templates',
156 "{$name}_text.tpl",
157 )
158 );
159
160 $htmlFileName = implode(DIRECTORY_SEPARATOR,
161 array(
162 $pathName,
163 "{$latestVer}.msg_template",
164 'message_templates',
165 "{$name}_html.tpl",
166 )
167 );
168
169 if (file_exists($textFileName) ||
170 file_exists($htmlFileName)
171 ) {
172 $flag = TRUE;
173 $html .= "<li>{$title}</li>";
174 }
175 }
176
177 if ($flag == TRUE) {
178 $html = "<ul>" . $html . "<ul>";
179
180 $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));
181 }
182 }
183
184 /**
185 * Compute any messages which should be displayed after upgrade
186 *
187 * @param $postUpgradeMessage string, alterable
188 * @param $rev string, an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs
189 * @return void
190 */
191 static function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
192 if ($rev == '3.2.alpha1') {
193 $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.");
194 }
195 if ($rev == '3.2.beta3') {
196 $subTypes = CRM_Contact_BAO_ContactType::subTypes();
197
198 if (is_array($subTypes) && !empty($subTypes)) {
199 $config = CRM_Core_Config::singleton();
200 $subTypeTemplates = array();
201
202 if (isset($config->customTemplateDir)) {
203 foreach ($subTypes as $key => $subTypeName) {
204 $customContactSubTypeEdit = $config->customTemplateDir . "CRM/Contact/Form/Edit/" . $subTypeName . ".tpl";
205 $customContactSubTypeView = $config->customTemplateDir . "CRM/Contact/Page/View/" . $subTypeName . ".tpl";
206 if (file_exists($customContactSubTypeEdit) || file_exists($customContactSubTypeView)) {
207 $subTypeTemplates[$subTypeName] = $subTypeName;
208 }
209 }
210 }
211
212 foreach ($subTypes as $key => $subTypeName) {
213 $customContactSubTypeEdit = $config->templateDir . "CRM/Contact/Form/Edit/" . $subTypeName . ".tpl";
214 $customContactSubTypeView = $config->templateDir . "CRM/Contact/Page/View/" . $subTypeName . ".tpl";
215 if (file_exists($customContactSubTypeEdit) || file_exists($customContactSubTypeView)) {
216 $subTypeTemplates[$subTypeName] = $subTypeName;
217 }
218 }
219
220 if (!empty($subTypeTemplates)) {
221 $subTypeTemplates = implode(',', $subTypeTemplates);
222 $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'));
223 }
224 }
225 }
226 if ($rev == '3.2.beta4') {
227 $statuses = array('New', 'Current', 'Grace', 'Expired', 'Pending', 'Cancelled', 'Deceased');
228 $sql = "
229 SELECT count( id ) as statusCount
230 FROM civicrm_membership_status
231 WHERE name IN ( '" . implode("' , '", $statuses) . "' ) ";
232 $count = CRM_Core_DAO::singleValueQuery($sql);
233 if ($count < count($statuses)) {
234 $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).");
235 }
236 }
237 if ($rev == '3.4.alpha1') {
238 $renamedBinScripts = array(
239 'ParticipantProcessor.php',
240 'RespondentProcessor.php',
241 'UpdateGreeting.php',
242 'UpdateMembershipRecord.php',
243 'UpdatePledgeRecord.php ',
244 );
245 $postUpgradeMessage .= '<br />' . ts('The following files have been renamed to have a ".php" extension instead of a ".php.txt" extension') . ': ' . implode(', ', $renamedBinScripts);
246 }
247 }
248
249 /**
250 * Perform an incremental upgrade
251 *
252 * @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)
253 */
254 static function upgrade_2_2_alpha1($rev) {
255 for ($stepID = 1; $stepID <= 4; $stepID++) {
256 $formName = "CRM_Upgrade_TwoTwo_Form_Step{$stepID}";
257 $form = new $formName();
258
259 $error = NULL;
260 if (!$form->verifyPreDBState($error)) {
261 if (!isset($error)) {
262 $error = "pre-condition failed for current upgrade step $stepID, rev $rev";
263 }
264 CRM_Core_Error::fatal($error);
265 }
266
267 if ($stepID == 4) {
268 return;
269 }
270
271 $template = CRM_Core_Smarty::singleton();
272
273 $eventFees = array();
274 $query = "SELECT og.id ogid FROM civicrm_option_group og WHERE og.name LIKE %1";
275 $params = array(1 => array('civicrm_event_page.amount%', 'String'));
276 $dao = CRM_Core_DAO::executeQuery($query, $params);
277 while ($dao->fetch()) {
278 $eventFees[$dao->ogid] = $dao->ogid;
279 }
280 $template->assign('eventFees', $eventFees);
281
282 $form->upgrade();
283
284 if (!$form->verifyPostDBState($error)) {
285 if (!isset($error)) {
286 $error = "post-condition failed for current upgrade step $stepID, rev $rev";
287 }
288 CRM_Core_Error::fatal($error);
289 }
290 }
291 }
292
293 /**
294 * Perform an incremental upgrade
295 *
296 * @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)
297 */
298 static function upgrade_2_1_2($rev) {
299 $formName = "CRM_Upgrade_TwoOne_Form_TwoOneTwo";
300 $form = new $formName($rev);
301
302 $error = NULL;
303 if (!$form->verifyPreDBState($error)) {
304 if (!isset($error)) {
305 $error = "pre-condition failed for current upgrade for $rev";
306 }
307 CRM_Core_Error::fatal($error);
308 }
309
310 $form->upgrade();
311
312 if (!$form->verifyPostDBState($error)) {
313 if (!isset($error)) {
314 $error = "post-condition failed for current upgrade for $rev";
315 }
316 CRM_Core_Error::fatal($error);
317 }
318 }
319
320 /**
321 * This function should check if if need to skip current sql file
322 * Name of this function will change according to the latest release
323 *
324 */
325 static function upgrade_2_2_alpha3($rev) {
326 // skip processing sql file, if fresh install -
327 if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'mail_protocol', 'id', 'name')) {
328 $upgrade = new CRM_Upgrade_Form();
329 $upgrade->processSQL($rev);
330 }
331 return TRUE;
332 }
333
334 /**
335 * Perform an incremental upgrade
336 *
337 * @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)
338 */
339 static function upgrade_2_2_beta1($rev) {
340 if (!CRM_Core_DAO::checkFieldExists('civicrm_pcp_block', 'notify_email')) {
341 $template = CRM_Core_Smarty::singleton();
342 $template->assign('notifyAbsent', TRUE);
343 }
344 $upgrade = new CRM_Upgrade_Form();
345 $upgrade->processSQL($rev);
346 }
347
348 /**
349 * Perform an incremental upgrade
350 *
351 * @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)
352 */
353 static function upgrade_2_2_beta2($rev) {
354 $template = CRM_Core_Smarty::singleton();
355 if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue',
356 'CRM_Contact_Form_Search_Custom_ZipCodeRange', 'id', 'name'
357 )) {
358 $template->assign('customSearchAbsentAll', TRUE);
359 }
360 elseif (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue',
361 'CRM_Contact_Form_Search_Custom_MultipleValues', 'id', 'name'
362 )) {
363 $template->assign('customSearchAbsent', TRUE);
364 }
365 $upgrade = new CRM_Upgrade_Form();
366 $upgrade->processSQL($rev);
367 }
368
369 /**
370 * Perform an incremental upgrade
371 *
372 * @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)
373 */
374 static function upgrade_2_2_beta3($rev) {
375 $template = CRM_Core_Smarty::singleton();
376 if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'custom_data_type', 'id', 'name')) {
377 $template->assign('customDataType', TRUE);
378 }
379
380 $upgrade = new CRM_Upgrade_Form();
381 $upgrade->processSQL($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_0_alpha1($rev) {
390
391 $threeZero = new CRM_Upgrade_ThreeZero_ThreeZero();
392
393 $error = NULL;
394 if (!$threeZero->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 $threeZero->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_3_1_alpha1($rev) {
410
411 $threeOne = new CRM_Upgrade_ThreeOne_ThreeOne();
412
413 $error = NULL;
414 if (!$threeOne->verifyPreDBState($error)) {
415 if (!isset($error)) {
416 $error = 'pre-condition failed for current upgrade for 3.0.alpha2';
417 }
418 CRM_Core_Error::fatal($error);
419 }
420
421 $threeOne->upgrade($rev);
422 }
423
424 /**
425 * Perform an incremental upgrade
426 *
427 * @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)
428 */
429 static function upgrade_2_2_7($rev) {
430 $upgrade = new CRM_Upgrade_Form();
431 $upgrade->processSQL($rev);
432 $sql = "UPDATE civicrm_report_instance
433 SET form_values = REPLACE(form_values,'#',';') ";
434 CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
435
436 // make report component enabled by default
437 $domain = new CRM_Core_DAO_Domain();
438 $domain->selectAdd();
439 $domain->selectAdd('config_backend');
440 $domain->find(TRUE);
441 if ($domain->config_backend) {
442 $defaults = unserialize($domain->config_backend);
443
444 if (is_array($defaults['enableComponents'])) {
445 $compId = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_component WHERE name = 'CiviReport'");
446 if ($compId) {
447 $defaults['enableComponents'][] = 'CiviReport';
448 $defaults['enableComponentIDs'][] = $compId;
449
450 CRM_Core_BAO_ConfigSetting::add($defaults);
451 }
452 }
453 }
454 }
455
456 /**
457 * Perform an incremental upgrade
458 *
459 * @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)
460 */
461 static function upgrade_3_0_2($rev) {
462
463 $template = CRM_Core_Smarty::singleton();
464 //check whether upgraded from 2.1.x or 2.2.x
465 $inboundEmailID = CRM_Core_OptionGroup::getValue('activity_type', 'Inbound Email', 'name');
466
467 if (!empty($inboundEmailID)) {
468 $template->assign('addInboundEmail', FALSE);
469 }
470 else {
471 $template->assign('addInboundEmail', TRUE);
472 }
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_0_4($rev) {
484 //make sure 'Deceased' membership status present in db,CRM-5636
485 $template = CRM_Core_Smarty::singleton();
486
487 $addDeceasedStatus = FALSE;
488 $sql = "SELECT max(id) FROM civicrm_membership_status where name = 'Deceased'";
489 if (!CRM_Core_DAO::singleValueQuery($sql)) {
490 $addDeceasedStatus = TRUE;
491 }
492 $template->assign('addDeceasedStatus', $addDeceasedStatus);
493
494 $upgrade = new CRM_Upgrade_Form();
495 $upgrade->processSQL($rev);
496 }
497
498 /**
499 * Perform an incremental upgrade
500 *
501 * @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)
502 */
503 static function upgrade_3_1_0($rev) {
504 // upgrade all roles who have 'access CiviEvent' permission, to also have
505 // newly added permission 'edit_all_events', CRM-5472
506 $config = CRM_Core_Config::singleton();
507 if (is_callable(array(
508 $config->userSystem, 'replacePermission'))) {
509 $config->userSystem->replacePermission('access CiviEvent', array('access CiviEvent', 'edit all events'));
510 }
511
512 //make sure 'Deceased' membership status present in db,CRM-5636
513 $template = CRM_Core_Smarty::singleton();
514
515 $addDeceasedStatus = FALSE;
516 $sql = "SELECT max(id) FROM civicrm_membership_status where name = 'Deceased'";
517 if (!CRM_Core_DAO::singleValueQuery($sql)) {
518 $addDeceasedStatus = TRUE;
519 }
520 $template->assign('addDeceasedStatus', $addDeceasedStatus);
521
522 $upgrade = new CRM_Upgrade_Form();
523 $upgrade->processSQL($rev);
524 }
525
526 /**
527 * Perform an incremental upgrade
528 *
529 * @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)
530 */
531 static function upgrade_3_1_3($rev) {
532 $threeOne = new CRM_Upgrade_ThreeOne_ThreeOne();
533 $threeOne->upgrade_3_1_3();
534
535 $upgrade = new CRM_Upgrade_Form();
536 $upgrade->processSQL($rev);
537 }
538
539 /**
540 * Perform an incremental upgrade
541 *
542 * @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)
543 */
544 static function upgrade_3_1_4($rev) {
545 $threeOne = new CRM_Upgrade_ThreeOne_ThreeOne();
546 $threeOne->upgrade_3_1_4();
547
548 $upgrade = new CRM_Upgrade_Form();
549 $upgrade->processSQL($rev);
550 }
551 }
552