3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
28 * Upgrade logic for 4.7
30 class CRM_Upgrade_Incremental_php_FourSeven
extends CRM_Upgrade_Incremental_Base
{
33 * Compute any messages which should be displayed beforeupgrade.
35 * Note: This function is called iteratively for each upcoming
36 * revision to the database.
38 * @param string $preUpgradeMessage
40 * a version number, e.g. '4.4.alpha1', '4.4.beta3', '4.4.0'.
41 * @param null $currentVer
43 public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) {
44 if ($rev == '4.7.alpha1') {
45 // CRM-16478 Remove custom fatal error template path option
46 $config = CRM_Core_Config
::singleton();
47 if (!empty($config->fatalErrorTemplate
) && $config->fatalErrorTemplate
!= 'CRM/common/fatal.tpl') {
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>';
51 if ($rev == '4.7.alpha4') {
52 // CRM-17004 Warn of Moneris removal
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')");
58 if ($count && !function_exists('moneris_civicrm_managed')) {
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>';
62 if ($rev == '4.7.13') {
63 $preUpgradeMessage .= '<p>' . ts('A new permission has been added called %1 This Permission is now used to control access to the Manage Tags screen', array(1 => 'manage tags')) . '</p>';
68 * Compute any messages which should be displayed after upgrade.
70 * @param string $postUpgradeMessage
73 * an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs.
75 public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
76 if ($rev == '4.7.alpha1') {
77 $config = CRM_Core_Config
::singleton();
78 // FIXME: Performing an upgrade step during postUpgrade message phase is probably bad
79 $editor_id = self
::updateWysiwyg();
81 $ext_href = 'href="' . CRM_Utils_System
::url('civicrm/admin/extensions', 'reset=1') . '"';
82 $dsp_href = 'href="' . CRM_Utils_System
::url('civicrm/admin/setting/preferences/display', 'reset=1') . '"';
83 $blog_href = 'href="https://civicrm.org/blogs/colemanw/big-changes-wysiwyg-editing-47"';
87 $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));
90 // Drupal/Joomla editor
93 $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));
97 $postUpgradeMessage .= '<p>' . $msg . '</p>';
99 $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>';
101 $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).');
103 $postUpgradeMessage .= '<p>' . ts('The custom fatal error template setting has been removed.') . '</p>';
105 //if ($rev == '4.7.11') {
106 // $postUpgradeMessage .= '<br /><br />' . ts("WARNING: For increased security, profile submissions embedded in remote sites are no longer allowed to create or edit data by default. If you need to allow users to submit profiles from external sites, you can restore this at Administer > System Settings > Misc (Undelete, PDFs, Limits, Logging, Captcha, etc.) > 'Accept profile submissions from external sites'");
108 if ($rev == '4.7.11') {
109 $postUpgradeMessage .= '<br /><br />' . ts("By default, CiviCRM now disables the ability to import directly from SQL. To use this feature, you must explicitly grant permission 'import SQL datasource'.");
111 if ($rev == '4.7.14') {
112 $ck_href = 'href="' . CRM_Utils_System
::url('civicrm/admin/ckeditor') . '"';
113 $postUpgradeMessage .= '<p>' . ts('CiviMail no longer forces CKEditor to add html/head/body tags to email content because some sites place these in the message header/footer. This was added in 4.7.5 and is now disabled by default.')
114 . '<br />' . ts('You can re-enable it by visitng the <a %1>CKEditor Config</a> screen and setting "fullPage = true" under the Advanced Options of the CiviMail preset.', array(1 => $ck_href))
124 public function upgrade_4_7_alpha1($rev) {
125 $this->addTask('Drop action scheudle mapping foreign key', 'dropActionScheudleMappingForeignKey');
126 $this->addTask('Migrate \'on behalf of\' information to module_data', 'migrateOnBehalfOfInfo');
127 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
128 $this->addTask(ts('Migrate Settings to %1', array(1 => $rev)), 'migrateSettings', $rev);
129 $this->addTask('Add Getting Started dashlet', 'addGettingStartedDashlet', $rev);
137 public function upgrade_4_7_alpha4($rev) {
138 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
139 $this->addTask(ts('Remove %1', array(1 => 'Moneris')), 'removePaymentProcessorType', 'Moneris');
140 $this->addTask('Update Smart Groups', 'fixContactTypeInSmartGroups');
148 public function upgrade_4_7_beta2($rev) {
149 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
150 $this->addTask('Delete unused file', 'deleteVersionCheckCacheFile');
158 public function upgrade_4_7_beta6($rev) {
159 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
160 $this->addTask('Disable flexible jobs extension', 'disableFlexibleJobsExtension');
161 $this->addTask('Add Index to financial_trxn trxn_id field', 'addIndexFinancialTrxnTrxnID');
169 public function upgrade_4_7_1($rev) {
170 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
171 $this->addTask('Add Index to civicrm_contribution creditnote_id field', 'addIndexContributionCreditNoteID');
179 public function upgrade_4_7_2($rev) {
180 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
181 $this->addTask('Fix Index on civicrm_financial_item combined entity_id + entity_table', 'addCombinedIndexFinancialItemEntityIDEntityType');
182 $this->addTask('enable financial account relationships for chargeback & refund', 'addRefundAndChargeBackAccountsIfNotExist');
183 $this->addTask('Add Index to civicrm_contribution.source', 'addIndexContributionSource');
191 public function upgrade_4_7_3($rev) {
192 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
193 $this->addTask('Add Index to civicrm_contribution.total_amount', 'addIndexContributionAmount');
201 public function upgrade_4_7_4($rev) {
202 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
203 $this->addTask('Add Contact Deleted by Merge Activity Type', 'addDeletedByMergeActivityType');
211 public function upgrade_4_7_7($rev) {
212 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
213 // https://issues.civicrm.org/jira/browse/CRM-18006
214 if (CRM_Core_DAO
::checkTableExists('civicrm_install_canary')) {
215 CRM_Core_DAO
::executeQuery('ALTER TABLE civicrm_install_canary ENGINE=InnoDB');
224 public function upgrade_4_7_8($rev) {
225 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
226 $this->addTask('Upgrade mailing foreign key constraints', 'upgradeMailingFKs');
234 public function upgrade_4_7_10($rev) {
235 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
236 $this->addTask('Upgrade Add Help Pre and Post Fields to price value table', 'addHelpPreAndHelpPostFieldsPriceFieldValue');
237 $this->addTask('Alter index and type for image URL', 'alterIndexAndTypeForImageURL');
245 public function upgrade_4_7_11($rev) {
246 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
247 $this->addTask('Dashboard schema updates', 'dashboardSchemaUpdate');
248 $this->addTask('Fill in setting "remote_profile_submissions"', 'migrateRemoteSubmissionsSetting');
256 public function upgrade_4_7_12($rev) {
257 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
258 $this->addTask('Add Data Type column to civicrm_option_group', 'addDataTypeColumnToOptionGroupTable');
265 public function upgrade_4_7_13($rev) {
266 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
267 $this->addTask('CRM-19372 - Add column to allow for payment processors to set what card types are accepted', 'addColumn',
268 'civicrm_payment_processor', 'accepted_credit_cards', "text DEFAULT NULL COMMENT 'array of accepted credit card types'");
276 public function upgrade_4_7_14($rev) {
277 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
278 $this->addTask('Add WYSIWYG Editor Presets', 'addWysiwygPresets');
286 public function upgrade_4_7_15($rev) {
287 $this->addTask('CRM-19626 - Add min_amount column to civicrm_price_set', 'addColumn',
288 'civicrm_price_set', 'min_amount', "INT(10) UNSIGNED DEFAULT '0' COMMENT 'Minimum Amount required for this set.'");
289 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
297 public function upgrade_4_7_16($rev) {
298 $this->addTask('CRM-19723 - Add icon column to civicrm_option_value', 'addColumn',
299 'civicrm_option_value', 'icon', "varchar(255) COMMENT 'crm-i icon class' DEFAULT NULL");
300 $this->addTask('CRM-19769 - Add color column to civicrm_tag', 'addColumn',
301 'civicrm_tag', 'color', "varchar(255) COMMENT 'Hex color value e.g. #ffffff' DEFAULT NULL");
302 $this->addTask('CRM-19779 - Add color column to civicrm_option_value', 'addColumn',
303 'civicrm_option_value', 'color', "varchar(255) COMMENT 'Hex color value e.g. #ffffff' DEFAULT NULL");
304 $this->addTask('Add new CiviMail fields', 'addMailingTemplateType');
305 $this->addTask('CRM-19770 - Add is_star column to civicrm_activity', 'addColumn',
306 'civicrm_activity', 'is_star', "tinyint DEFAULT '0' COMMENT 'Activity marked as favorite.'");
307 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
315 public function upgrade_4_7_18($rev) {
316 $this->addTask('Update Kenyan Provinces', 'updateKenyanProvinces');
317 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
321 * Important! All upgrade functions MUST add a 'runSql' task.
322 * Uncomment and use the following template for a new upgrade version
323 * (change the x in the function name):
327 // * Upgrade function.
329 // * @param string $rev
331 // public function upgrade_4_7_x($rev) {
332 // $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
333 // // Additional tasks here...
334 // // Note: do not use ts() in the addTask description because it adds unnecessary strings to transifex.
335 // // The above is an exception because 'Upgrade DB to %1: SQL' is generic & reusable.
343 public static function updateWysiwyg() {
344 $editorID = Civi
::settings()->get('editor_id');
345 // Previously a numeric value indicated one of 4 wysiwyg editors shipped in core, and no value indicated 'Textarea'
346 // Now the options are "Textarea", "CKEditor", and the rest have been dropped from core.
347 $newEditor = $editorID ?
"CKEditor" : "Textarea";
348 Civi
::settings()->set('editor_id', $newEditor);
354 * Migrate any last remaining options from `civicrm_domain.config_backend` to `civicrm_setting`.
355 * Cleanup setting schema.
357 * @param CRM_Queue_TaskContext $ctx
360 public static function migrateSettings(CRM_Queue_TaskContext
$ctx) {
361 // Tip: If there are problems with adding the new uniqueness index, try inspecting:
362 // SELECT name, domain_id, contact_id, count(*) AS dupes FROM civicrm_setting cs GROUP BY name, domain_id, contact_id HAVING dupes > 1;
364 // Nav records are expendable. https://forum.civicrm.org/index.php?topic=36933.0
365 CRM_Core_DAO
::executeQuery('DELETE FROM civicrm_setting WHERE contact_id IS NOT NULL AND name = "navigation"');
367 CRM_Core_DAO
::executeQuery('ALTER TABLE civicrm_setting DROP INDEX index_group_name');
368 CRM_Core_DAO
::executeQuery('ALTER TABLE civicrm_setting DROP COLUMN group_name');
370 // Handle Strange activity_tab_filter settings.
371 CRM_Core_DAO
::executeQuery('CREATE TABLE civicrm_activity_setting LIKE civicrm_setting');
372 CRM_Core_DAO
::executeQuery('ALTER TABLE civicrm_activity_setting ADD UNIQUE INDEX index_domain_contact_name (domain_id, contact_id, name)');
373 CRM_Core_DAO
::executeQuery('INSERT INTO civicrm_activity_setting (name, contact_id, domain_id, value)
374 SELECT DISTINCT name, contact_id, domain_id, value
376 WHERE name = "activity_tab_filter"
377 AND value is not NULL');
378 CRM_Core_DAO
::executeQuery('DELETE FROM civicrm_setting WHERE name = "activity_tab_filter"');
380 $date = CRM_Utils_Time
::getTime('Y-m-d H:i:s');
381 CRM_Core_DAO
::executeQuery('ALTER TABLE civicrm_setting ADD UNIQUE INDEX index_domain_contact_name (domain_id, contact_id, name)');
382 CRM_Core_DAO
::executeQuery("INSERT INTO civicrm_setting (name, contact_id, domain_id, value, is_domain, created_id, created_date)
383 SELECT name, contact_id, domain_id, value, 0, contact_id,'$date'
384 FROM civicrm_activity_setting
385 WHERE name = 'activity_tab_filter'
386 AND value is not NULL"
388 CRM_Core_DAO
::executeQuery('DROP TABLE civicrm_activity_setting');
390 $domainDao = CRM_Core_DAO
::executeQuery('SELECT id, config_backend FROM civicrm_domain');
391 while ($domainDao->fetch()) {
392 $settings = CRM_Upgrade_Incremental_php_FourSeven
::convertBackendToSettings($domainDao->id
, $domainDao->config_backend
);
393 CRM_Core_Error
::debug_var('convertBackendToSettings', array(
394 'domainId' => $domainDao->id
,
395 'backend' => $domainDao->config_backend
,
396 'settings' => $settings,
399 foreach ($settings as $name => $value) {
401 1 => array($domainDao->id
, 'Positive'),
402 2 => array($name, 'String'),
403 3 => array(serialize($value), 'String'),
405 $settingId = CRM_Core_DAO
::singleValueQuery(
406 'SELECT id FROM civicrm_setting WHERE domain_id = %1 AND name = %2',
409 CRM_Core_DAO
::executeQuery(
410 'INSERT INTO civicrm_setting (domain_id, name, value, is_domain) VALUES (%1,%2,%3,1)',
416 CRM_Core_DAO
::executeQuery('ALTER TABLE civicrm_domain DROP COLUMN config_backend');
422 * Take a config_backend blob and produce an equivalent list of settings.
424 * @param int $domainId
426 * @param string $config_backend
430 public static function convertBackendToSettings($domainId, $config_backend) {
431 if (!$config_backend) {
435 $backend = unserialize($config_backend);
440 $mappings = \CRM_Core_Config_MagicMerge
::getPropertyMap();
442 foreach ($backend as $propertyName => $propertyValue) {
443 if (isset($mappings[$propertyName][0]) && preg_match('/^setting/', $mappings[$propertyName][0])) {
444 // $mapping format: $propertyName => Array(0 => $type, 1 => $setting|NULL).
445 $settingName = isset($mappings[$propertyName][1]) ?
$mappings[$propertyName][1] : $propertyName;
446 $settings[$settingName] = $propertyValue;
454 * Add Getting Started dashlet to dashboard
456 * @param \CRM_Queue_TaskContext $ctx
460 public static function addGettingStartedDashlet(CRM_Queue_TaskContext
$ctx) {
461 $sql = "SELECT count(*) FROM civicrm_dashboard WHERE name='getting-started'";
462 $res = CRM_Core_DAO
::singleValueQuery($sql);
463 $domainId = CRM_Core_Config
::domainID();
465 $sql = "INSERT INTO `civicrm_dashboard`
466 ( `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)";
467 CRM_Core_DAO
::executeQuery($sql);
468 // Add default position for Getting Started Dashlet ( left column)
469 $sql = "INSERT INTO `civicrm_dashboard_contact` (dashboard_id, contact_id, column_no, is_active)
470 SELECT (SELECT MAX(id) FROM `civicrm_dashboard`), contact_id, 0, IF (SUM(is_active) > 0, 1, 0)
471 FROM `civicrm_dashboard_contact` JOIN `civicrm_contact` WHERE civicrm_dashboard_contact.contact_id = civicrm_contact.id GROUP BY contact_id";
472 CRM_Core_DAO
::executeQuery($sql);
478 * Migrate on-behalf information to uf_join.module_data as on-behalf columns will be dropped
481 * @param CRM_Queue_TaskContext $ctx
486 public static function migrateOnBehalfOfInfo(CRM_Queue_TaskContext
$ctx) {
487 $domain = new CRM_Core_DAO_Domain();
490 // fetch onBehalf entry in UFJoin table
491 $ufGroupDAO = new CRM_Core_DAO_UFJoin();
492 $ufGroupDAO->module
= 'OnBehalf';
493 $ufGroupDAO->find(TRUE);
495 $forOrgColums = array('is_for_organization');
496 if ($domain->locales
) {
497 $locales = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
498 foreach ($locales as $locale) {
499 $forOrgColums[] = "for_organization_{$locale}";
503 $forOrgColums[] = "for_organization";
507 SELECT " . implode(", ", $forOrgColums) . ", uj.id as join_id, uj.uf_group_id as uf_group_id
508 FROM civicrm_contribution_page cp
509 INNER JOIN civicrm_uf_join uj ON uj.entity_id = cp.id AND uj.module = 'OnBehalf'";
510 $dao = CRM_Core_DAO
::executeQuery($query, array(), TRUE, NULL, FALSE, FALSE);
513 while ($dao->fetch()) {
514 $onBehalfParams['on_behalf'] = array('is_for_organization' => $dao->is_for_organization
);
515 if ($domain->locales
) {
516 foreach ($locales as $locale) {
517 $for_organization = "for_organization_{$locale}";
518 $onBehalfParams['on_behalf'] +
= array(
520 'for_organization' => $dao->$for_organization,
526 $onBehalfParams['on_behalf'] +
= array(
528 'for_organization' => $dao->for_organization
,
532 $ufJoinParam = array(
533 'id' => $dao->join_id
,
534 'module' => 'on_behalf',
535 'uf_group_id' => $dao->uf_group_id
,
536 'module_data' => json_encode($onBehalfParams),
538 CRM_Core_BAO_UFJoin
::create($ufJoinParam);
546 * v4.7.11 adds a new setting "remote_profile_submissions". This is
547 * long-standing feature that existing sites may be using; however, it's
548 * a bit prone to abuse. For new sites, the default is to disable it
549 * (since that is more secure). For existing sites, the default is to
550 * enable it (since that is more compatible).
552 * @param \CRM_Queue_TaskContext $ctx
556 public static function migrateRemoteSubmissionsSetting(CRM_Queue_TaskContext
$ctx) {
557 $domains = CRM_Core_DAO
::executeQuery("SELECT DISTINCT d.id FROM civicrm_domain d LEFT JOIN civicrm_setting s ON d.id=s.domain_id AND s.name = 'remote_profile_submissions' WHERE s.id IS NULL");
558 while ($domains->fetch()) {
559 CRM_Core_DAO
::executeQuery(
560 "INSERT INTO civicrm_setting (`name`, `value`, `domain_id`, `is_domain`, `contact_id`, `component_id`, `created_date`, `created_id`)
561 VALUES (%2, %3, %4, %5, NULL, NULL, %6, NULL)",
563 2 => array('remote_profile_submissions', 'String'),
564 3 => array('s:1:"1";', 'String'),
565 4 => array($domains->id
, 'Integer'),
566 5 => array(1, 'Integer'),
567 6 => array(date('Y-m-d H:i:s'), 'String'),
575 * CRM-11782 - Get rid of VALUE_SEPARATOR character in saved search form values
577 * @param \CRM_Queue_TaskContext $ctx
581 public static function fixContactTypeInSmartGroups(CRM_Queue_TaskContext
$ctx) {
582 $sep = CRM_Core_DAO
::VALUE_SEPARATOR
;
583 $dao = CRM_Core_DAO
::executeQuery("SELECT id, form_values FROM civicrm_saved_search WHERE form_values LIKE '%$sep%'");
584 while ($dao->fetch()) {
585 $formValues = unserialize($dao->form_values
);
586 if (isset($formValues['contact_type']) && is_array($formValues['contact_type'])) {
588 foreach ($formValues['contact_type'] as $key => $val) {
589 $newVals[str_replace($sep, '__', $key)] = is_string($val) ?
str_replace($sep, '__', $val) : $val;
591 $formValues['contact_type'] = $newVals;
593 CRM_Core_DAO
::executeQuery("UPDATE civicrm_saved_search SET form_values = %1 WHERE id = {$dao->id}", array(1 => array(serialize($formValues), 'String')));
600 * CRM-17637 - Ths file location has been moved; delete the old one
602 * @param \CRM_Queue_TaskContext $ctx
606 public static function deleteVersionCheckCacheFile(CRM_Queue_TaskContext
$ctx) {
607 $config = CRM_Core_Config
::singleton();
608 $cacheFile = $config->uploadDir
. 'version-info-cache.json';
609 if (file_exists($cacheFile)) {
616 * CRM-17669 and CRM-17686, make scheduled jobs more flexible, disable the 4.6 extension if installed
618 * @param \CRM_Queue_TaskContext $ctx
622 public static function disableFlexibleJobsExtension(CRM_Queue_TaskContext
$ctx) {
624 civicrm_api3('Extension', 'disable', array('key' => 'com.klangsoft.flexiblejobs'));
626 catch (CiviCRM_API3_Exception
$e) {
627 // just ignore if the extension isn't installed
634 * CRM-17752 add index to civicrm_financial_trxn.trxn_id (deliberately non-unique).
636 * @param \CRM_Queue_TaskContext $ctx
640 public static function addIndexFinancialTrxnTrxnID(CRM_Queue_TaskContext
$ctx) {
641 $tables = array('civicrm_financial_trxn' => array('trxn_id'));
642 CRM_Core_BAO_SchemaHandler
::createIndexes($tables);
647 * CRM-17882 Add index to civicrm_contribution.credit_note_id.
649 * @param \CRM_Queue_TaskContext $ctx
653 public static function addIndexContributionCreditNoteID(CRM_Queue_TaskContext
$ctx) {
654 $tables = array('civicrm_contribution' => array('creditnote_id'));
655 CRM_Core_BAO_SchemaHandler
::createIndexes($tables);
660 * CRM-17775 Add correct index for table civicrm_financial_item.
662 * Note that the entity ID should always precede the entity_table as
663 * it is more unique. This is better for performance and does not cause fallback
664 * to no index if table it omitted.
668 public static function addCombinedIndexFinancialItemEntityIDEntityType() {
669 CRM_Core_BAO_SchemaHandler
::dropIndexIfExists('civicrm_financial_item', 'UI_id');
670 CRM_Core_BAO_SchemaHandler
::dropIndexIfExists('civicrm_financial_item', 'IX_Entity');
671 CRM_Core_BAO_SchemaHandler
::createIndexes(array(
672 'civicrm_financial_item' => array(array('entity_id', 'entity_table')),
678 * CRM-17951 Add accounts option values for refund and chargeback.
680 * Add Chargeback contribution status and Chargeback and Contra account relationships,
681 * checking first if one exists.
683 public static function addRefundAndChargeBackAccountsIfNotExist() {
684 // First we enable and edit the record for Credit contra - this exists but is disabled for most sites.
685 // Using the ensure function (below) will not enabled a disabled option (by design).
686 CRM_Core_DAO
::executeQuery("UPDATE civicrm_option_value v
687 INNER JOIN civicrm_option_group g on v.option_group_id=g.id and g.name='account_relationship'
688 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'
689 WHERE v.name = 'Credit/Contra Account is';");
691 CRM_Core_BAO_OptionValue
::ensureOptionValueExists(array(
692 'option_group_id' => 'account_relationship',
693 'name' => 'Chargeback Account is',
694 'label' => ts('Chargeback Account is'),
696 'component_id' => 'CiviContribute',
699 CRM_Core_BAO_OptionValue
::ensureOptionValueExists(array(
700 'option_group_id' => 'contribution_status',
701 'name' => 'Chargeback',
702 'label' => ts('Chargeback'),
704 'component_id' => 'CiviContribute',
710 * CRM-17999 Add index to civicrm_contribution.source.
712 * @param \CRM_Queue_TaskContext $ctx
716 public static function addIndexContributionSource(CRM_Queue_TaskContext
$ctx) {
717 CRM_Core_BAO_SchemaHandler
::createIndexes(array('civicrm_contribution' => array('source')));
722 * CRM-18124 Add index to civicrm_contribution.total_amount.
724 * Note that I made this a combined index with receive_date because the issue included
725 * both criteria and they seemed likely to be used in conjunction to me in other cases.
727 * @param \CRM_Queue_TaskContext $ctx
731 public static function addIndexContributionAmount(CRM_Queue_TaskContext
$ctx) {
732 CRM_Core_BAO_SchemaHandler
::createIndexes(array(
733 'civicrm_contribution' => array(array('total_amount', 'receive_date')),
739 * CRM-18124 Add index to civicrm_contribution.total_amount.
741 * Note that I made this a combined index with receive_date because the issue included
742 * both criteria and they seemed likely to be used in conjunction to me in other cases.
744 * @param \CRM_Queue_TaskContext $ctx
748 public static function addDeletedByMergeActivityType(CRM_Queue_TaskContext
$ctx) {
749 CRM_Core_BAO_OptionValue
::ensureOptionValueExists(array(
750 'option_group_id' => 'activity_type',
751 'name' => 'Contact Deleted by Merge',
752 'label' => ts('Contact Deleted by Merge'),
753 'description' => ts('Contact was merged into another contact'),
761 * CRM-12252 Add Help Pre and Help Post Fields for Price Field Value Table.
763 * @param \CRM_Queue_TaskContext $ctx
767 public static function addHelpPreAndHelpPostFieldsPriceFieldValue(CRM_Queue_TaskContext
$ctx) {
768 $domain = new CRM_Core_DAO_Domain();
770 if ($domain->locales
) {
771 $locales = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
772 foreach ($locales as $locale) {
773 if (!CRM_Core_BAO_SchemaHandler
::checkIfFieldExists("civicrm_price_field_value", "help_pre_{$locale}")) {
774 CRM_Core_DAO
::executeQuery("ALTER TABLE `civicrm_price_field_value`
775 ADD COLUMN `help_pre_{$locale}` text COLLATE utf8_unicode_ci COMMENT 'Price field option pre help text.'", array(), TRUE, NULL, FALSE, FALSE);
777 if (!CRM_Core_BAO_SchemaHandler
::checkIfFieldExists("civicrm_price_field_value", "help_post_{$locale}")) {
778 CRM_Core_DAO
::executeQuery("ALTER TABLE `civicrm_price_field_value`
779 ADD COLUMN `help_post_{$locale}` text COLLATE utf8_unicode_ci COMMENT 'Price field option post help text.'", array(), TRUE, NULL, FALSE, FALSE);
782 CRM_Core_I18n_Schema
::rebuildMultilingualSchema($locales, NULL);
785 if (!CRM_Core_BAO_SchemaHandler
::checkIfFieldExists('civicrm_price_field_value', 'help_pre')) {
786 CRM_Core_DAO
::executeQuery("ALTER TABLE `civicrm_price_field_value`
787 ADD COLUMN `help_pre` text COLLATE utf8_unicode_ci COMMENT 'Price field option pre help text.'");
789 if (!CRM_Core_BAO_SchemaHandler
::checkIfFieldExists('civicrm_price_field_value', 'help_post')) {
790 CRM_Core_DAO
::executeQuery("ALTER TABLE `civicrm_price_field_value`
791 ADD COLUMN `help_post` text COLLATE utf8_unicode_ci COMMENT 'Price field option post help text.'");
798 * CRM-18464 Check if Foreign key exists and also drop any index of same name accidentially created.
800 * @param \CRM_Queue_TaskContext $ctx
804 public static function dropActionScheudleMappingForeignKey(CRM_Queue_TaskContext
$ctx) {
805 CRM_Core_BAO_SchemaHandler
::safeRemoveFK('civicrm_action_schedule', 'FK_civicrm_action_schedule_mapping_id');
810 * CRM-18345 Don't delete mailing data on email/phone deletion
811 * Implemented here in CRM-18526
813 * @param \CRM_Queue_TaskContext $ctx
817 public static function upgradeMailingFKs(CRM_Queue_TaskContext
$ctx) {
819 // Safely drop the foreign keys
820 CRM_Core_BAO_SchemaHandler
::safeRemoveFK('civicrm_mailing_event_queue', 'FK_civicrm_mailing_event_queue_email_id');
821 CRM_Core_BAO_SchemaHandler
::safeRemoveFK('civicrm_mailing_event_queue', 'FK_civicrm_mailing_event_queue_phone_id');
822 CRM_Core_BAO_SchemaHandler
::safeRemoveFK('civicrm_mailing_recipients', 'FK_civicrm_mailing_recipients_email_id');
823 CRM_Core_BAO_SchemaHandler
::safeRemoveFK('civicrm_mailing_recipients', 'FK_civicrm_mailing_recipients_phone_id');
825 // Set up the new foreign keys
826 CRM_Core_DAO
::executeQuery("SET FOREIGN_KEY_CHECKS = 0;");
828 CRM_Core_DAO
::executeQuery("
829 ALTER TABLE `civicrm_mailing_event_queue`
830 ADD CONSTRAINT `FK_civicrm_mailing_event_queue_email_id`
831 FOREIGN KEY (`email_id`)
832 REFERENCES `civicrm_email`(`id`)
837 CRM_Core_DAO
::executeQuery("
838 ALTER TABLE `civicrm_mailing_event_queue`
839 ADD CONSTRAINT `FK_civicrm_mailing_event_queue_phone_id`
840 FOREIGN KEY (`phone_id`)
841 REFERENCES `civicrm_phone`(`id`)
846 CRM_Core_DAO
::executeQuery("
847 ALTER TABLE `civicrm_mailing_recipients`
848 ADD CONSTRAINT `FK_civicrm_mailing_recipients_email_id`
849 FOREIGN KEY (`email_id`)
850 REFERENCES `civicrm_email`(`id`)
855 CRM_Core_DAO
::executeQuery("
856 ALTER TABLE `civicrm_mailing_recipients`
857 ADD CONSTRAINT `FK_civicrm_mailing_recipients_phone_id`
858 FOREIGN KEY (`phone_id`)
859 REFERENCES `civicrm_phone`(`id`)
864 CRM_Core_DAO
::executeQuery("SET FOREIGN_KEY_CHECKS = 1;");
870 * CRM-17663 - Dashboard schema changes
872 * @param \CRM_Queue_TaskContext $ctx
876 public static function dashboardSchemaUpdate(CRM_Queue_TaskContext
$ctx) {
877 if (!CRM_Core_BAO_SchemaHandler
::checkIfIndexExists('civicrm_dashboard_contact', 'index_dashboard_id_contact_id')) {
878 // Delete any stray duplicate rows and add unique index to prevent new dupes and enable INSERT/UPDATE combo query
879 CRM_Core_DAO
::executeQuery('DELETE c1 FROM civicrm_dashboard_contact c1, civicrm_dashboard_contact c2 WHERE c1.contact_id = c2.contact_id AND c1.dashboard_id = c2.dashboard_id AND c1.id > c2.id');
880 CRM_Core_DAO
::executeQuery('ALTER TABLE civicrm_dashboard_contact ADD UNIQUE INDEX index_dashboard_id_contact_id (dashboard_id, contact_id);');
882 $domain = new CRM_Core_DAO_Domain();
884 CRM_Core_BAO_SchemaHandler
::dropColumn('civicrm_dashboard_contact', 'content');
885 CRM_Core_BAO_SchemaHandler
::dropColumn('civicrm_dashboard_contact', 'is_minimized');
886 CRM_Core_BAO_SchemaHandler
::dropColumn('civicrm_dashboard_contact', 'is_fullscreen');
887 CRM_Core_BAO_SchemaHandler
::dropColumn('civicrm_dashboard_contact', 'created_date');
888 CRM_Core_BAO_SchemaHandler
::dropColumn('civicrm_dashboard', 'is_fullscreen');
889 CRM_Core_BAO_SchemaHandler
::dropColumn('civicrm_dashboard', 'is_minimized');
890 CRM_Core_BAO_SchemaHandler
::dropColumn('civicrm_dashboard', 'column_no');
891 CRM_Core_BAO_SchemaHandler
::dropColumn('civicrm_dashboard', 'weight');
893 CRM_Core_DAO
::executeQuery('UPDATE civicrm_dashboard SET url = REPLACE(url, "&snippet=5", ""), fullscreen_url = REPLACE(fullscreen_url, "&snippet=5", "")');
895 if (!CRM_Core_BAO_SchemaHandler
::checkIfFieldExists('civicrm_dashboard', 'cache_minutes')) {
896 CRM_Core_DAO
::executeQuery('ALTER TABLE civicrm_dashboard ADD COLUMN cache_minutes int unsigned NOT NULL DEFAULT 60 COMMENT "Number of minutes to cache dashlet content in browser localStorage."',
897 array(), TRUE, NULL, FALSE, FALSE);
899 if ($domain->locales
) {
900 $locales = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
901 CRM_Core_I18n_Schema
::rebuildMultilingualSchema($locales, NULL);
904 CRM_Core_DAO
::executeQuery('UPDATE civicrm_dashboard SET cache_minutes = 1440 WHERE name = "blog"');
905 CRM_Core_DAO
::executeQuery('UPDATE civicrm_dashboard SET cache_minutes = 7200 WHERE name IN ("activity","getting-started")');
910 * CRM-19100 - Alter Index and Type for Image URL
913 public static function alterIndexAndTypeForImageURL() {
915 CRM_Core_BAO_SchemaHandler
::dropIndexIfExists('civicrm_contact', 'index_image_url');
916 CRM_Core_DAO
::executeQuery("ALTER TABLE `civicrm_contact` CHANGE `image_URL` `image_URL` TEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT 'optional URL for preferred image (photo, logo, etc.) to display for this contact.'");
918 $length['civicrm_contact']['image_URL'] = 128;
919 CRM_Core_BAO_SchemaHandler
::createIndexes(array('civicrm_contact' => array('image_URL')), 'index', $length);
925 * Add mailing template type.
929 public static function addMailingTemplateType() {
930 if (!CRM_Core_DAO
::checkFieldExists('civicrm_mailing', 'template_type', FALSE)) {
931 CRM_Core_DAO
::executeQuery('
932 ALTER TABLE civicrm_mailing
933 ADD COLUMN `template_type` varchar(64) NOT NULL DEFAULT \'traditional\' COMMENT \'The language/processing system used for email templates.\',
934 ADD COLUMN `template_options` longtext COMMENT \'Advanced options used by the email templating system. (JSON encoded)\'
941 * CRM-18651 Add DataType column to Option Group Table
944 public static function addDataTypeColumnToOptionGroupTable() {
945 if (!CRM_Core_BAO_SchemaHandler
::checkIfFieldExists('civicrm_option_group', 'data_type')) {
946 CRM_Core_DAO
::executeQuery("ALTER TABLE `civicrm_option_group` ADD COLUMN `data_type` varchar(128) COLLATE utf8_unicode_ci DEFAULT NULL comment 'Data Type of Option Group.'",
947 array(), TRUE, NULL, FALSE, FALSE);
949 $domain = new CRM_Core_DAO_Domain();
951 if ($domain->locales
) {
952 $locales = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
953 CRM_Core_I18n_Schema
::rebuildMultilingualSchema($locales, NULL);
956 CRM_Core_DAO
::executeQuery("UPDATE `civicrm_option_group` SET `data_type` = 'Integer'
957 WHERE name IN ('activity_type', 'gender', 'payment_instrument', 'participant_role', 'event_type')");
962 * CRM-19372 Add field to store accepted credit credit cards for a payment processor.
965 public static function addWysiwygPresets() {
966 CRM_Core_BAO_OptionGroup
::ensureOptionGroupExists(array(
967 'name' => 'wysiwyg_presets',
968 'title' => ts('WYSIWYG Editor Presets'),
972 'default' => array('label' => ts('Default'), 'is_default' => 1),
973 'civimail' => array('label' => ts('CiviMail'), 'component_id' => 'CiviMail'),
974 'civievent' => array('label' => ts('CiviEvent'), 'component_id' => 'CiviEvent'),
976 foreach ($values as $name => $value) {
977 CRM_Core_BAO_OptionValue
::ensureOptionValueExists($value +
array(
979 'option_group_id' => 'wysiwyg_presets',
982 $fileName = Civi
::paths()->getPath('[civicrm.files]/persist/crm-ckeditor-config.js');
983 // Ensure the config file contains the allowedContent setting
984 if (file_exists($fileName)) {
985 $config = file_get_contents($fileName);
986 $pos = strrpos($config, '};');
987 $setting = "\n\tconfig.allowedContent = true;\n";
988 $config = substr_replace($config, $setting, $pos, 0);
990 $newFileName = Civi
::paths()->getPath('[civicrm.files]/persist/crm-ckeditor-default.js');
991 file_put_contents($newFileName, $config);
997 * Update Kenyan Provinces to reflect changes per CRM-20062
999 * @param \CRM_Queue_TaskContext $ctx
1001 public function updateKenyanProvinces(CRM_Queue_TaskContext
$ctx) {
1002 $kenyaCountryID = CRM_Core_DAO
::singleValueQuery('SELECT max(id) from civicrm_country where iso_code = "KE"');
1003 $oldProvinces = array(
1004 'Nairobi Municipality',
1006 'North-Eastern Kaskazini Mashariki',
1008 'Western Magharibi',
1010 self
::deprecateStateProvinces($kenyaCountryID, $oldProvinces);
1015 * Deprecate provinces that no longer exist.
1017 * @param int $countryID
1018 * @param array $provinces
1020 public static function deprecateStateProvinces($countryID, $provinces) {
1021 foreach ($provinces as $province) {
1022 $existingStateID = CRM_Core_DAO
::singleValueQuery("
1023 SELECT id FROM civicrm_state_province
1024 WHERE country_id = %1
1027 array(1 => array($countryID, 'Int'), 2 => array($province, 'String')));
1029 if (!$existingStateID) {
1032 if (!CRM_Core_DAO
::singleValueQuery("
1033 SELECT count(*) FROM civicrm_address
1034 WHERE state_province_id = %1
1035 ", array(1 => array($existingStateID, 'Int')))
1037 CRM_Core_DAO
::executeQuery("DELETE FROM civicrm_state_province WHERE id = %1", array(1 => array($existingStateID, 'Int')));
1040 $params = array('1' => array(ts("Former - $province"), 'String'));
1041 CRM_Core_DAO
::executeQuery("
1042 UPDATE civicrm_state_province SET name = %1 WHERE id = $existingStateID