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