Merge pull request #8642 from jitendrapurohit/CRM-12252
[civicrm-core.git] / CRM / Upgrade / Incremental / php / FourSeven.php
CommitLineData
6cc25669
CW
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
6cc25669
CW
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/**
bf6a5362 28 * Upgrade logic for 4.7
6cc25669 29 */
bf6a5362 30class CRM_Upgrade_Incremental_php_FourSeven extends CRM_Upgrade_Incremental_Base {
6cc25669 31
f431d51f
J
32 /**
33 * Compute any messages which should be displayed beforeupgrade.
34 *
35 * Note: This function is called iteratively for each upcoming
36 * revision to the database.
37 *
3bdf1f3a 38 * @param string $preUpgradeMessage
f431d51f
J
39 * @param string $rev
40 * a version number, e.g. '4.4.alpha1', '4.4.beta3', '4.4.0'.
41 * @param null $currentVer
f431d51f
J
42 */
43 public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) {
44 if ($rev == '4.7.alpha1') {
f431d51f
J
45 // CRM-16478 Remove custom fatal error template path option
46 $config = CRM_Core_Config::singleton();
18b3bef6 47 if (!empty($config->fatalErrorTemplate) && $config->fatalErrorTemplate != 'CRM/common/fatal.tpl') {
f431d51f
J
48 $preUpgradeMessage .= '<p>' . ts('The custom fatal error template setting will be removed during the upgrade. You are currently using this custom template: %1 . Following the upgrade you will need to use the standard approach to overriding template files, as described in the documentation.', array(1 => $config->fatalErrorTemplate)) . '</p>';
49 }
f431d51f 50 }
a40fd1ac
CW
51 if ($rev == '4.7.alpha4') {
52 // CRM-17004 Warn of Moneris removal
87a33a95
CW
53 $count = 1;
54 // Query only works in 4.3+
55 if (version_compare($currentVer, "4.3.0") > 0) {
56 $count = CRM_Core_DAO::singleValueQuery("SELECT COUNT(id) FROM civicrm_payment_processor WHERE payment_processor_type_id IN (SELECT id FROM civicrm_payment_processor_type WHERE name = 'Moneris')");
57 }
58 if ($count && !function_exists('moneris_civicrm_managed')) {
a40fd1ac
CW
59 $preUpgradeMessage .= '<p>' . ts('The %1 payment processor is no longer bundled with CiviCRM. After upgrading you will need to install the extension to continue using it.', array(1 => 'Moneris')) . '</p>';
60 }
61 }
f431d51f
J
62 }
63
6cc25669
CW
64 /**
65 * Compute any messages which should be displayed after upgrade.
66 *
67 * @param string $postUpgradeMessage
68 * alterable.
69 * @param string $rev
70 * an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs.
6cc25669
CW
71 */
72 public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
6dbe2c23
CW
73 if ($rev == '4.7.alpha1') {
74 $config = CRM_Core_Config::singleton();
bf6a5362 75 // FIXME: Performing an upgrade step during postUpgrade message phase is probably bad
6dbe2c23
CW
76 $editor_id = self::updateWysiwyg();
77 $msg = NULL;
78 $ext_href = 'href="' . CRM_Utils_System::url('civicrm/admin/extensions', 'reset=1') . '"';
79 $dsp_href = 'href="' . CRM_Utils_System::url('civicrm/admin/setting/preferences/display', 'reset=1') . '"';
80 $blog_href = 'href="https://civicrm.org/blogs/colemanw/big-changes-wysiwyg-editing-47"';
81 switch ($editor_id) {
82 // TinyMCE
83 case 1:
84 $msg = ts('Your configured editor "TinyMCE" is no longer part of the main CiviCRM download. To continue using it, visit the <a %1>Manage Extensions</a> page to download and install the TinyMCE extension.', array(1 => $ext_href));
85 break;
86
87 // Drupal/Joomla editor
88 case 3:
89 case 4:
90 $msg = ts('CiviCRM no longer integrates with the "%1 Default Editor." Your wysiwyg setting has been reset to the built-in CKEditor. <a %2>Learn more...</a>', array(1 => $config->userFramework, 2 => $blog_href));
91 break;
92 }
93 if ($msg) {
94 $postUpgradeMessage .= '<p>' . $msg . '</p>';
95 }
96 $postUpgradeMessage .= '<p>' . ts('CiviCRM now includes the easy-to-use CKEditor Configurator. To customize the features and display of your wysiwyg editor, visit the <a %1>Display Preferences</a> page. <a %2>Learn more...</a>', array(1 => $dsp_href, 2 => $blog_href)) . '</p>';
dd55005c 97
98 $postUpgradeMessage .= '<br /><br />' . ts('Default version of the following System Workflow Message Templates have been modified: <ul><li>Personal Campaign Pages - Owner Notification</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).');
f431d51f
J
99
100 $postUpgradeMessage .= '<p>' . ts('The custom fatal error template setting has been removed.') . '</p>';
6dbe2c23 101 }
6cc25669
CW
102 }
103
6cc25669
CW
104 /**
105 * Upgrade function.
106 *
107 * @param string $rev
108 */
109 public function upgrade_4_7_alpha1($rev) {
b604d7ec 110 $this->addTask('Migrate \'on behalf of\' information to module_data', 'migrateOnBehalfOfInfo');
bf6a5362 111 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
f806379b 112 $this->addTask(ts('Migrate Settings to %1', array(1 => $rev)), 'migrateSettings', $rev);
b604d7ec 113 $this->addTask('Add Getting Started dashlet', 'addGettingStartedDashlet', $rev);
a40fd1ac
CW
114 }
115
116 /**
117 * Upgrade function.
118 *
119 * @param string $rev
120 */
121 public function upgrade_4_7_alpha4($rev) {
122 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
058b8a5e 123 $this->addTask(ts('Remove %1', array(1 => 'Moneris')), 'removePaymentProcessorType', 'Moneris');
b2222b9f 124 $this->addTask('Update Smart Groups', 'fixContactTypeInSmartGroups');
6cc25669
CW
125 }
126
0094ac08
CW
127 /**
128 * Upgrade function.
129 *
130 * @param string $rev
131 */
132 public function upgrade_4_7_beta2($rev) {
133 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
134 $this->addTask('Delete unused file', 'deleteVersionCheckCacheFile');
135 }
136
fb1f3850
DRJ
137 /**
138 * Upgrade function.
139 *
140 * @param string $rev
141 */
902e557f 142 public function upgrade_4_7_beta6($rev) {
13599400 143 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
fb1f3850 144 $this->addTask('Disable flexible jobs extension', 'disableFlexibleJobsExtension');
20d5377e 145 $this->addTask('Add Index to financial_trxn trxn_id field', 'addIndexFinancialTrxnTrxnID');
fb1f3850
DRJ
146 }
147
8ca47f5c 148 /**
149 * Upgrade function.
150 *
151 * @param string $rev
152 */
153 public function upgrade_4_7_1($rev) {
2af0b023 154 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
8ca47f5c 155 $this->addTask('Add Index to civicrm_contribution creditnote_id field', 'addIndexContributionCreditNoteID');
156 }
157
2179c899 158 /**
159 * Upgrade function.
160 *
161 * @param string $rev
162 */
163 public function upgrade_4_7_2($rev) {
2af0b023 164 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
2179c899 165 $this->addTask('Fix Index on civicrm_financial_item combined entity_id + entity_table', 'addCombinedIndexFinancialItemEntityIDEntityType');
1f395432 166 $this->addTask('enable financial account relationships for chargeback & refund', 'addRefundAndChargeBackAccountsIfNotExist');
3fe26f4c 167 $this->addTask('Add Index to civicrm_contribution.source', 'addIndexContributionSource');
2179c899 168 }
169
fd28b6a0 170 /**
171 * Upgrade function.
172 *
173 * @param string $rev
174 */
175 public function upgrade_4_7_3($rev) {
2af0b023 176 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
fd28b6a0 177 $this->addTask('Add Index to civicrm_contribution.total_amount', 'addIndexContributionAmount');
178 }
179
6eb752fa 180 /**
181 * Upgrade function.
182 *
183 * @param string $rev
184 */
185 public function upgrade_4_7_4($rev) {
2af0b023 186 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
6eb752fa 187 $this->addTask('Add Contact Deleted by Merge Activity Type', 'addDeletedByMergeActivityType');
188 }
189
2cbdd085
J
190 /**
191 * Upgrade function.
192 *
193 * @param string $rev
194 */
6a2d3987 195 public function upgrade_4_7_7($rev) {
2cbdd085 196 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
6a2d3987
J
197 // https://issues.civicrm.org/jira/browse/CRM-18006
198 if (CRM_Core_DAO::checkTableExists('civicrm_install_canary')) {
199 CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_install_canary ENGINE=InnoDB');
200 }
2cbdd085
J
201 }
202
2af0b023
CW
203 /**
204 * Upgrade function.
205 *
206 * @param string $rev
207 */
6a2d3987 208 public function upgrade_4_7_8($rev) {
2af0b023 209 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
6a2d3987 210 $this->addTask('Upgrade mailing foreign key constraints', 'upgradeMailingFKs');
065ffec9
TO
211 }
212
da1ecd73
SL
213 /**
214 * Upgrade function.
215 *
216 * @param string $rev
217 */
e8a4535e 218 public function upgrade_4_7_10($rev) {
da1ecd73 219 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
e8a4535e 220 $this->addTask(ts('Upgrade Add Help Pre and Post Fields to price value table'), 'addHelpPreAndHelpPostFieldsPriceFieldValue');
da1ecd73
SL
221 }
222
2af0b023
CW
223 /*
224 * Important! All upgrade functions MUST call the 'runSql' task.
225 * Uncomment and use the following template for a new upgrade version
226 * (change the x in the function name):
227 */
228
229 // /**
230 // * Upgrade function.
231 // *
232 // * @param string $rev
233 // */
234 // public function upgrade_4_7_x($rev) {
235 // $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
236 // // Additional tasks here...
237 // }
238
6cc25669
CW
239 /**
240 * CRM-16354
241 *
6dbe2c23 242 * @return int
6cc25669 243 */
6dbe2c23 244 public static function updateWysiwyg() {
aaffa79f 245 $editorID = Civi::settings()->get('editor_id');
6dbe2c23
CW
246 // Previously a numeric value indicated one of 4 wysiwyg editors shipped in core, and no value indicated 'Textarea'
247 // Now the options are "Textarea", "CKEditor", and the rest have been dropped from core.
248 $newEditor = $editorID ? "CKEditor" : "Textarea";
08ef4ddd 249 Civi::settings()->set('editor_id', $newEditor);
6cc25669 250
6dbe2c23 251 return $editorID;
6cc25669
CW
252 }
253
f806379b
TO
254 /**
255 * Migrate any last remaining options from `civicrm_domain.config_backend` to `civicrm_setting`.
256 * Cleanup setting schema.
257 *
258 * @param CRM_Queue_TaskContext $ctx
259 * @return bool
260 */
261 public function migrateSettings(CRM_Queue_TaskContext $ctx) {
4cc9e637
TO
262 // Tip: If there are problems with adding the new uniqueness index, try inspecting:
263 // SELECT name, domain_id, contact_id, count(*) AS dupes FROM civicrm_setting cs GROUP BY name, domain_id, contact_id HAVING dupes > 1;
264
265 // Nav records are expendable. https://forum.civicrm.org/index.php?topic=36933.0
266 CRM_Core_DAO::executeQuery('DELETE FROM civicrm_setting WHERE contact_id IS NOT NULL AND name = "navigation"');
3ddd2901 267
726e6261
SL
268 CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_setting DROP INDEX index_group_name');
269 CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_setting DROP COLUMN group_name');
270
3ddd2901
SL
271 // Handle Strange activity_tab_filter settings.
272 CRM_Core_DAO::executeQuery('CREATE TABLE civicrm_activity_setting LIKE civicrm_setting');
726e6261 273 CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_activity_setting ADD UNIQUE INDEX index_domain_contact_name (domain_id, contact_id, name)');
3ddd2901 274 CRM_Core_DAO::executeQuery('INSERT INTO civicrm_activity_setting (name, contact_id, domain_id, value)
726e6261 275 SELECT DISTINCT name, contact_id, domain_id, value
3ddd2901
SL
276 FROM civicrm_setting
277 WHERE name = "activity_tab_filter"
278 AND value is not NULL');
ee4d8422 279 CRM_Core_DAO::executeQuery('DELETE FROM civicrm_setting WHERE name = "activity_tab_filter"');
4cc9e637 280
e6a2c901 281 $date = CRM_Utils_Time::getTime('Y-m-d H:i:s');
f806379b 282 CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_setting ADD UNIQUE INDEX index_domain_contact_name (domain_id, contact_id, name)');
e6a2c901
SL
283 CRM_Core_DAO::executeQuery("INSERT INTO civicrm_setting (name, contact_id, domain_id, value, is_domain, created_id, created_date)
284 SELECT name, contact_id, domain_id, value, 0, contact_id,'$date'
3ddd2901 285 FROM civicrm_activity_setting
e6a2c901
SL
286 WHERE name = 'activity_tab_filter'
287 AND value is not NULL"
d09d52ce 288 );
3ddd2901 289 CRM_Core_DAO::executeQuery('DROP TABLE civicrm_activity_setting');
f806379b
TO
290
291 $domainDao = CRM_Core_DAO::executeQuery('SELECT id, config_backend FROM civicrm_domain');
292 while ($domainDao->fetch()) {
293 $settings = CRM_Upgrade_Incremental_php_FourSeven::convertBackendToSettings($domainDao->id, $domainDao->config_backend);
294 CRM_Core_Error::debug_var('convertBackendToSettings', array(
295 'domainId' => $domainDao->id,
296 'backend' => $domainDao->config_backend,
297 'settings' => $settings,
298 ));
299
300 foreach ($settings as $name => $value) {
301 $rowParams = array(
302 1 => array($domainDao->id, 'Positive'),
303 2 => array($name, 'String'),
304 3 => array(serialize($value), 'String'),
305 );
306 $settingId = CRM_Core_DAO::singleValueQuery(
307 'SELECT id FROM civicrm_setting WHERE domain_id = %1 AND name = %2',
308 $rowParams);
309 if (!$settingId) {
310 CRM_Core_DAO::executeQuery(
311 'INSERT INTO civicrm_setting (domain_id, name, value, is_domain) VALUES (%1,%2,%3,1)',
312 $rowParams);
313 }
314 }
315 }
316
9e726168 317 CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_domain DROP COLUMN config_backend');
f806379b
TO
318
319 return TRUE;
320 }
321
322 /**
323 * Take a config_backend blob and produce an equivalent list of settings.
324 *
325 * @param int $domainId
326 * Domain ID.
327 * @param string $config_backend
328 * Serialized blob.
329 * @return array
330 */
331 public static function convertBackendToSettings($domainId, $config_backend) {
332 if (!$config_backend) {
333 return array();
334 }
335
336 $backend = unserialize($config_backend);
337 if (!$backend) {
338 return array();
339 }
340
341 $mappings = \CRM_Core_Config_MagicMerge::getPropertyMap();
342 $settings = array();
343 foreach ($backend as $propertyName => $propertyValue) {
344 if (isset($mappings[$propertyName][0]) && preg_match('/^setting/', $mappings[$propertyName][0])) {
345 // $mapping format: $propertyName => Array(0 => $type, 1 => $setting|NULL).
346 $settingName = isset($mappings[$propertyName][1]) ? $mappings[$propertyName][1] : $propertyName;
347 $settings[$settingName] = $propertyValue;
348 }
349 }
350
351 return $settings;
352 }
353
0a95c936 354 /**
355 * Add Getting Started dashlet to dashboard
356 *
357 * @param \CRM_Queue_TaskContext $ctx
358 *
359 * @return bool
360 */
361 public function addGettingStartedDashlet(CRM_Queue_TaskContext $ctx) {
9c4a04f2 362 $sql = "SELECT count(*) FROM civicrm_dashboard WHERE name='getting-started'";
e1674273 363 $res = CRM_Core_DAO::singleValueQuery($sql);
364 $domainId = CRM_Core_Config::domainID();
365 if ($res <= 0) {
366 $sql = "INSERT INTO `civicrm_dashboard`
a8b704c5 367 ( `domain_id`, `name`, `label`, `url`, `permission`, `permission_operator`, `column_no`, `is_minimized`, `is_active`, `weight`, `fullscreen_url`, `is_fullscreen`, `is_reserved`) VALUES ( {$domainId}, 'getting-started', 'Getting Started', 'civicrm/dashlet/getting-started?reset=1&snippet=5', 'access CiviCRM', NULL, 0, 0, 1, 0, 'civicrm/dashlet/getting-started?reset=1&snippet=5&context=dashletFullscreen', 1, 1)";
e1674273 368 CRM_Core_DAO::executeQuery($sql);
369 // Add default position for Getting Started Dashlet ( left column)
370 $sql = "INSERT INTO `civicrm_dashboard_contact` (dashboard_id, contact_id, column_no, is_active)
371SELECT (SELECT MAX(id) FROM `civicrm_dashboard`), contact_id, 0, IF (SUM(is_active) > 0, 1, 0)
429da6a1 372FROM `civicrm_dashboard_contact` JOIN `civicrm_contact` WHERE civicrm_dashboard_contact.contact_id = civicrm_contact.id GROUP BY contact_id";
e1674273 373 CRM_Core_DAO::executeQuery($sql);
374 }
0a95c936 375 return TRUE;
e1674273 376 }
4175ee03 377
6b1e1a2c 378 /**
379 * Migrate on-behalf information to uf_join.module_data as on-behalf columns will be dropped
380 * on DB upgrade
381 *
382 * @param CRM_Queue_TaskContext $ctx
383 *
384 * @return bool
385 * TRUE for success
386 */
387 public static function migrateOnBehalfOfInfo(CRM_Queue_TaskContext $ctx) {
d3e92c88 388 $domain = new CRM_Core_DAO_Domain();
389 $domain->find(TRUE);
6b1e1a2c 390
d3e92c88 391 // fetch onBehalf entry in UFJoin table
6b1e1a2c 392 $ufGroupDAO = new CRM_Core_DAO_UFJoin();
393 $ufGroupDAO->module = 'OnBehalf';
394 $ufGroupDAO->find(TRUE);
395
d3e92c88 396 $forOrgColums = array();
397 if ($domain->locales) {
398 $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
399 foreach ($locales as $locale) {
400 $forOrgColums[] = "for_organization_{$locale}";
401 }
402 }
403 else {
404 $forOrgColums[] = "for_organization";
405 }
406
407 $query = "
408 SELECT " . implode(", ", $forOrgColums) . ", uj.id as join_id, uj.uf_group_id as uf_group_id
409 FROM civicrm_contribution_page cp
410 INNER JOIN civicrm_uf_join uj ON uj.entity_id = cp.id AND uj.module = 'OnBehalf'";
411 $dao = CRM_Core_DAO::executeQuery($query, array(), TRUE, NULL, FALSE, FALSE);
6b1e1a2c 412
413 if ($dao->N) {
6b1e1a2c 414 while ($dao->fetch()) {
415 $onBehalfParams['on_behalf'] = array('is_for_organization' => $dao->is_for_organization);
416 if ($domain->locales) {
6b1e1a2c 417 foreach ($locales as $locale) {
418 $for_organization = "for_organization_{$locale}";
419 $onBehalfParams['on_behalf'] += array(
420 $locale => array(
421 'for_organization' => $dao->$for_organization,
422 ),
423 );
424 }
425 }
426 else {
427 $onBehalfParams['on_behalf'] += array(
428 'default' => array(
429 'for_organization' => $dao->for_organization,
430 ),
431 );
432 }
433 $ufJoinParam = array(
434 'id' => $dao->join_id,
435 'module' => 'on_behalf',
d3e92c88 436 'uf_group_id' => $dao->uf_group_id,
6b1e1a2c 437 'module_data' => json_encode($onBehalfParams),
438 );
439 CRM_Core_BAO_UFJoin::create($ufJoinParam);
440 }
441 }
442
443 return TRUE;
444 }
cb804cd9 445
b2222b9f
CW
446 /**
447 * CRM-11782 - Get rid of VALUE_SEPARATOR character in saved search form values
448 *
449 * @param \CRM_Queue_TaskContext $ctx
450 *
451 * @return bool
452 */
453 public function fixContactTypeInSmartGroups(CRM_Queue_TaskContext $ctx) {
454 $sep = CRM_Core_DAO::VALUE_SEPARATOR;
455 $dao = CRM_Core_DAO::executeQuery("SELECT id, form_values FROM civicrm_saved_search WHERE form_values LIKE '%$sep%'");
456 while ($dao->fetch()) {
457 $formValues = unserialize($dao->form_values);
458 if (isset($formValues['contact_type']) && is_array($formValues['contact_type'])) {
459 $newVals = array();
460 foreach ($formValues['contact_type'] as $key => $val) {
461 $newVals[str_replace($sep, '__', $key)] = is_string($val) ? str_replace($sep, '__', $val) : $val;
462 }
463 $formValues['contact_type'] = $newVals;
464 }
465 CRM_Core_DAO::executeQuery("UPDATE civicrm_saved_search SET form_values = %1 WHERE id = {$dao->id}", array(1 => array(serialize($formValues), 'String')));
466 }
467
468 return TRUE;
469 }
470
0094ac08
CW
471 /**
472 * CRM-17637 - Ths file location has been moved; delete the old one
473 *
474 * @param \CRM_Queue_TaskContext $ctx
475 *
476 * @return bool
477 */
478 public function deleteVersionCheckCacheFile(CRM_Queue_TaskContext $ctx) {
479 $config = CRM_Core_Config::singleton();
480 $cacheFile = $config->uploadDir . 'version-info-cache.json';
481 if (file_exists($cacheFile)) {
482 unlink($cacheFile);
483 }
484 return TRUE;
485 }
486
fb1f3850
DRJ
487 /**
488 * CRM-17669 and CRM-17686, make scheduled jobs more flexible, disable the 4.6 extension if installed
489 *
490 * @param \CRM_Queue_TaskContext $ctx
491 *
492 * @return bool
493 */
494 public function disableFlexibleJobsExtension(CRM_Queue_TaskContext $ctx) {
d357221c
DRJ
495 try {
496 civicrm_api3('Extension', 'disable', array('key' => 'com.klangsoft.flexiblejobs'));
497 }
498 catch (CiviCRM_API3_Exception $e) {
499 // just ignore if the extension isn't installed
500 }
fb1f3850
DRJ
501
502 return TRUE;
503 }
504
20d5377e 505 /**
506 * CRM-17752 add index to civicrm_financial_trxn.trxn_id (deliberately non-unique).
507 *
508 * @param \CRM_Queue_TaskContext $ctx
509 *
510 * @return bool
511 */
512 public function addIndexFinancialTrxnTrxnID(CRM_Queue_TaskContext $ctx) {
513 $tables = array('civicrm_financial_trxn' => array('trxn_id'));
514 CRM_Core_BAO_SchemaHandler::createIndexes($tables);
515 return TRUE;
516 }
517
8ca47f5c 518 /**
519 * CRM-17882 Add index to civicrm_contribution.credit_note_id.
520 *
521 * @param \CRM_Queue_TaskContext $ctx
522 *
523 * @return bool
524 */
525 public function addIndexContributionCreditNoteID(CRM_Queue_TaskContext $ctx) {
526 $tables = array('civicrm_contribution' => array('creditnote_id'));
527 CRM_Core_BAO_SchemaHandler::createIndexes($tables);
528 return TRUE;
529 }
530
2179c899 531 /**
532 * CRM-17775 Add correct index for table civicrm_financial_item.
533 *
534 * Note that the entity ID should always precede the entity_table as
535 * it is more unique. This is better for performance and does not cause fallback
536 * to no index if table it omitted.
537 *
538 * @return bool
539 */
540 public function addCombinedIndexFinancialItemEntityIDEntityType() {
541 CRM_Core_BAO_SchemaHandler::dropIndexIfExists('civicrm_financial_item', 'UI_id');
542 CRM_Core_BAO_SchemaHandler::dropIndexIfExists('civicrm_financial_item', 'IX_Entity');
543 CRM_Core_BAO_SchemaHandler::createIndexes(array(
544 'civicrm_financial_item' => array(array('entity_id', 'entity_table')),
545 ));
546 return TRUE;
547 }
548
1f395432 549 /**
550 * CRM-17951 Add accounts option values for refund and chargeback.
551 *
552 * Add Chargeback contribution status and Chargeback and Contra account relationships,
553 * checking first if one exists.
554 */
555 public function addRefundAndChargeBackAccountsIfNotExist() {
556 // First we enable and edit the record for Credit contra - this exists but is disabled for most sites.
557 // Using the ensure function (below) will not enabled a disabled option (by design).
558 CRM_Core_DAO::executeQuery("UPDATE civicrm_option_value v
559 INNER JOIN civicrm_option_group g on v.option_group_id=g.id and g.name='account_relationship'
560 SET v.is_active=1, v.label='Credit/Contra Revenue Account is', v.name='Credit/Contra Revenue Account is', v.description='Credit/Contra Revenue Account is'
561 WHERE v.name = 'Credit/Contra Account is';");
562
563 CRM_Core_BAO_OptionValue::ensureOptionValueExists(array(
564 'option_group_id' => 'account_relationship',
4a907feb
EM
565 'name' => 'Chargeback Account is',
566 'label' => ts('Chargeback Account is'),
1f395432 567 'is_active' => TRUE,
568 'component_id' => 'CiviContribute',
569 ));
570
571 CRM_Core_BAO_OptionValue::ensureOptionValueExists(array(
572 'option_group_id' => 'contribution_status',
573 'name' => 'Chargeback',
574 'label' => ts('Chargeback'),
575 'is_active' => TRUE,
576 'component_id' => 'CiviContribute',
577 ));
578 return TRUE;
579 }
580
3fe26f4c 581 /**
582 * CRM-17999 Add index to civicrm_contribution.source.
583 *
584 * @param \CRM_Queue_TaskContext $ctx
585 *
586 * @return bool
587 */
588 public function addIndexContributionSource(CRM_Queue_TaskContext $ctx) {
589 CRM_Core_BAO_SchemaHandler::createIndexes(array('civicrm_contribution' => array('source')));
590 return TRUE;
591 }
592
fd28b6a0 593 /**
594 * CRM-18124 Add index to civicrm_contribution.total_amount.
595 *
596 * Note that I made this a combined index with receive_date because the issue included
597 * both criteria and they seemed likely to be used in conjunction to me in other cases.
598 *
599 * @param \CRM_Queue_TaskContext $ctx
600 *
601 * @return bool
602 */
603 public function addIndexContributionAmount(CRM_Queue_TaskContext $ctx) {
604 CRM_Core_BAO_SchemaHandler::createIndexes(array(
605 'civicrm_contribution' => array(array('total_amount', 'receive_date')),
606 ));
607 return TRUE;
608 }
609
6eb752fa 610 /**
611 * CRM-18124 Add index to civicrm_contribution.total_amount.
612 *
613 * Note that I made this a combined index with receive_date because the issue included
614 * both criteria and they seemed likely to be used in conjunction to me in other cases.
615 *
616 * @param \CRM_Queue_TaskContext $ctx
617 *
618 * @return bool
619 */
620 public function addDeletedByMergeActivityType(CRM_Queue_TaskContext $ctx) {
621 CRM_Core_BAO_OptionValue::ensureOptionValueExists(array(
622 'option_group_id' => 'activity_type',
623 'name' => 'Contact Deleted by Merge',
624 'label' => ts('Contact Deleted by Merge'),
625 'description' => ts('Contact was merged into another contact'),
626 'is_active' => TRUE,
e14e412b 627 'filter' => 1,
6eb752fa 628 ));
629 return TRUE;
630 }
631
da1ecd73
SL
632 /**
633 * CRM-12252 Add Help Pre and Help Post Fields for Price Field Value Table.
634 *
635 * @param \CRM_Queue_TaskContext $ctx
636 *
637 * @return bool
638 */
639 public function addHelpPreAndHelpPostFieldsPriceFieldValue(CRM_Queue_TaskContext $ctx) {
640 $domain = new CRM_Core_DAO_Domain();
641 $domain->find(TRUE);
642 if ($domain->locales) {
643 $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
644 foreach ($locales as $locale) {
01ee8ba6 645 if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists("civicrm_price_field_value", "help_pre_{$locale}")) {
3dc870a2
SL
646 CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_price_field_value`
647 ADD COLUMN `help_pre_{$locale}` text COLLATE utf8_unicode_ci COMMENT 'Price field option pre help text.'", array(), TRUE, NULL, FALSE, FALSE);
da1ecd73 648 }
01ee8ba6 649 if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists("civicrm_price_field_value", "help_post_{$locale}")) {
3dc870a2
SL
650 CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_price_field_value`
651 ADD COLUMN `help_post_{$locale}` text COLLATE utf8_unicode_ci COMMENT 'Price field option post help text.'", array(), TRUE, NULL, FALSE, FALSE);
da1ecd73
SL
652 }
653 }
01ee8ba6 654 CRM_Core_I18n_Schema::rebuildMultilingualSchema($locales, NULL);
da1ecd73
SL
655 }
656 else {
657 if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists('civicrm_price_field_value', 'help_pre')) {
3dc870a2
SL
658 CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_price_field_value`
659 ADD COLUMN `help_pre` text COLLATE utf8_unicode_ci COMMENT 'Price field option pre help text.'");
da1ecd73
SL
660 }
661 if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists('civicrm_price_field_value', 'help_post')) {
3dc870a2
SL
662 CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_price_field_value`
663 ADD COLUMN `help_post` text COLLATE utf8_unicode_ci COMMENT 'Price field option post help text.'");
da1ecd73
SL
664 }
665 }
666 return TRUE;
667 }
668
2cbdd085
J
669 /**
670 * Remove a foreign key from a table if it exists
671 *
672 * @param $table_name
673 * @param $constraint_name
674 */
675 public function safeRemoveFK($table_name, $constraint_name) {
676
677 $config = CRM_Core_Config::singleton();
678 $dbUf = DB::parseDSN($config->dsn);
679 $query = "
680 SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
681 WHERE TABLE_SCHEMA = %1
682 AND TABLE_NAME = %2
683 AND CONSTRAINT_NAME = %3
684 AND CONSTRAINT_TYPE = 'FOREIGN KEY'
685 ";
686 $params = array(
687 1 => array($dbUf['database'], 'String'),
688 2 => array($table_name, 'String'),
689 3 => array($constraint_name, 'String'),
690 );
691 $dao = CRM_Core_DAO::executeQuery($query, $params);
692
693 if ($dao->fetch()) {
694 CRM_Core_DAO::executeQuery("ALTER TABLE {$table_name} DROP FOREIGN KEY {$constraint_name}", array());
695 }
696
697 }
698
699 /**
700 * CRM-18345 Don't delete mailing data on email/phone deletion
701 * Implemented here in CRM-18526
702 *
703 * @param \CRM_Queue_TaskContext $ctx
704 *
705 * @return bool
706 */
707 public function upgradeMailingFKs(CRM_Queue_TaskContext $ctx) {
708
709 // Safely drop the foreign keys
a430a278
J
710 self::safeRemoveFK('civicrm_mailing_event_queue', 'FK_civicrm_mailing_event_queue_email_id');
711 self::safeRemoveFK('civicrm_mailing_event_queue', 'FK_civicrm_mailing_event_queue_phone_id');
712 self::safeRemoveFK('civicrm_mailing_recipients', 'FK_civicrm_mailing_recipients_email_id');
713 self::safeRemoveFK('civicrm_mailing_recipients', 'FK_civicrm_mailing_recipients_phone_id');
2cbdd085
J
714
715 // Set up the new foreign keys
716 CRM_Core_DAO::executeQuery("SET FOREIGN_KEY_CHECKS = 0;");
717
718 CRM_Core_DAO::executeQuery("
719 ALTER TABLE `civicrm_mailing_event_queue`
720 ADD CONSTRAINT `FK_civicrm_mailing_event_queue_email_id`
721 FOREIGN KEY (`email_id`)
722 REFERENCES `civicrm_email`(`id`)
723 ON DELETE SET NULL
724 ON UPDATE RESTRICT;
725 ");
726
727 CRM_Core_DAO::executeQuery("
728 ALTER TABLE `civicrm_mailing_event_queue`
729 ADD CONSTRAINT `FK_civicrm_mailing_event_queue_phone_id`
730 FOREIGN KEY (`phone_id`)
731 REFERENCES `civicrm_phone`(`id`)
732 ON DELETE SET NULL
733 ON UPDATE RESTRICT;
734 ");
735
736 CRM_Core_DAO::executeQuery("
737 ALTER TABLE `civicrm_mailing_recipients`
738 ADD CONSTRAINT `FK_civicrm_mailing_recipients_email_id`
739 FOREIGN KEY (`email_id`)
740 REFERENCES `civicrm_email`(`id`)
741 ON DELETE SET NULL
742 ON UPDATE RESTRICT;
743 ");
744
745 CRM_Core_DAO::executeQuery("
746 ALTER TABLE `civicrm_mailing_recipients`
747 ADD CONSTRAINT `FK_civicrm_mailing_recipients_phone_id`
748 FOREIGN KEY (`phone_id`)
749 REFERENCES `civicrm_phone`(`id`)
750 ON DELETE SET NULL
751 ON UPDATE RESTRICT;
752 ");
753
754 CRM_Core_DAO::executeQuery("SET FOREIGN_KEY_CHECKS = 1;");
755
756 return TRUE;
757 }
758
6cc25669 759}