Merge pull request #10300 from civicrm/4.7.19-rc
[civicrm-core.git] / CRM / Upgrade / Incremental / php / FourSeven.php
CommitLineData
6cc25669
CW
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 }
b9446a4c 62 if ($rev == '4.7.13') {
4b9e9cda
SL
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>';
64 }
f431d51f
J
65 }
66
6cc25669
CW
67 /**
68 * Compute any messages which should be displayed after upgrade.
69 *
70 * @param string $postUpgradeMessage
71 * alterable.
72 * @param string $rev
73 * an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs.
6cc25669
CW
74 */
75 public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
6dbe2c23
CW
76 if ($rev == '4.7.alpha1') {
77 $config = CRM_Core_Config::singleton();
bf6a5362 78 // FIXME: Performing an upgrade step during postUpgrade message phase is probably bad
6dbe2c23
CW
79 $editor_id = self::updateWysiwyg();
80 $msg = NULL;
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"';
84 switch ($editor_id) {
85 // TinyMCE
86 case 1:
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));
88 break;
89
90 // Drupal/Joomla editor
91 case 3:
92 case 4:
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));
94 break;
95 }
96 if ($msg) {
97 $postUpgradeMessage .= '<p>' . $msg . '</p>';
98 }
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>';
dd55005c 100
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).');
f431d51f
J
102
103 $postUpgradeMessage .= '<p>' . ts('The custom fatal error template setting has been removed.') . '</p>';
6dbe2c23 104 }
a742ee50
TO
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'");
107 //}
11769bd1 108 if ($rev == '4.7.11') {
9867e465 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'.");
11769bd1 110 }
c7c54ade
CW
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))
115 . '</p>';
116 }
c4aa623c 117 if ($rev == '4.7.19') {
7dffc2a7 118 $postUpgradeMessage .= '<br /><br />' . ts('Default version of the following System Workflow Message Templates have been modified: <ul><li>Additional Payment Receipt or Refund Notification</li><li>Contribution Invoice</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).');
eca6c81c
SL
119 $check = CRM_Core_DAO::singleValueQuery("SELECT count(id) FROM civicrm_domain");
120 $smsCheck = CRM_Core_DAO::singleValueQuery("SELECT count(id) FROM civicrm_sms_provider");
121 if ($check > 1 && (bool) $smsCheck) {
122 $postUpgradeMessage .= '<p>civicrm_sms_provider ' . ts('has now had a domain id column added. As there is more than 1 domains in this install you need to manually set the domain id for the providers in this install') . '</p>';
123 }
c4aa623c 124 }
6cc25669
CW
125 }
126
6cc25669
CW
127 /**
128 * Upgrade function.
129 *
130 * @param string $rev
131 */
132 public function upgrade_4_7_alpha1($rev) {
8c748d50 133 $this->addTask('Drop action scheudle mapping foreign key', 'dropActionScheudleMappingForeignKey');
b604d7ec 134 $this->addTask('Migrate \'on behalf of\' information to module_data', 'migrateOnBehalfOfInfo');
bf6a5362 135 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
f806379b 136 $this->addTask(ts('Migrate Settings to %1', array(1 => $rev)), 'migrateSettings', $rev);
b604d7ec 137 $this->addTask('Add Getting Started dashlet', 'addGettingStartedDashlet', $rev);
a40fd1ac
CW
138 }
139
140 /**
141 * Upgrade function.
142 *
143 * @param string $rev
144 */
145 public function upgrade_4_7_alpha4($rev) {
146 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
058b8a5e 147 $this->addTask(ts('Remove %1', array(1 => 'Moneris')), 'removePaymentProcessorType', 'Moneris');
b2222b9f 148 $this->addTask('Update Smart Groups', 'fixContactTypeInSmartGroups');
6cc25669
CW
149 }
150
0094ac08
CW
151 /**
152 * Upgrade function.
153 *
154 * @param string $rev
155 */
156 public function upgrade_4_7_beta2($rev) {
157 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
158 $this->addTask('Delete unused file', 'deleteVersionCheckCacheFile');
159 }
160
fb1f3850
DRJ
161 /**
162 * Upgrade function.
163 *
164 * @param string $rev
165 */
902e557f 166 public function upgrade_4_7_beta6($rev) {
13599400 167 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
fb1f3850 168 $this->addTask('Disable flexible jobs extension', 'disableFlexibleJobsExtension');
20d5377e 169 $this->addTask('Add Index to financial_trxn trxn_id field', 'addIndexFinancialTrxnTrxnID');
fb1f3850
DRJ
170 }
171
8ca47f5c 172 /**
173 * Upgrade function.
174 *
175 * @param string $rev
176 */
177 public function upgrade_4_7_1($rev) {
2af0b023 178 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
8ca47f5c 179 $this->addTask('Add Index to civicrm_contribution creditnote_id field', 'addIndexContributionCreditNoteID');
180 }
181
2179c899 182 /**
183 * Upgrade function.
184 *
185 * @param string $rev
186 */
187 public function upgrade_4_7_2($rev) {
2af0b023 188 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
2179c899 189 $this->addTask('Fix Index on civicrm_financial_item combined entity_id + entity_table', 'addCombinedIndexFinancialItemEntityIDEntityType');
1f395432 190 $this->addTask('enable financial account relationships for chargeback & refund', 'addRefundAndChargeBackAccountsIfNotExist');
3fe26f4c 191 $this->addTask('Add Index to civicrm_contribution.source', 'addIndexContributionSource');
2179c899 192 }
193
fd28b6a0 194 /**
195 * Upgrade function.
196 *
197 * @param string $rev
198 */
199 public function upgrade_4_7_3($rev) {
2af0b023 200 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
fd28b6a0 201 $this->addTask('Add Index to civicrm_contribution.total_amount', 'addIndexContributionAmount');
202 }
203
6eb752fa 204 /**
205 * Upgrade function.
206 *
207 * @param string $rev
208 */
209 public function upgrade_4_7_4($rev) {
2af0b023 210 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
6eb752fa 211 $this->addTask('Add Contact Deleted by Merge Activity Type', 'addDeletedByMergeActivityType');
212 }
213
2cbdd085
J
214 /**
215 * Upgrade function.
216 *
217 * @param string $rev
218 */
6a2d3987 219 public function upgrade_4_7_7($rev) {
2cbdd085 220 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
6a2d3987
J
221 // https://issues.civicrm.org/jira/browse/CRM-18006
222 if (CRM_Core_DAO::checkTableExists('civicrm_install_canary')) {
223 CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_install_canary ENGINE=InnoDB');
224 }
2cbdd085
J
225 }
226
2af0b023
CW
227 /**
228 * Upgrade function.
229 *
230 * @param string $rev
231 */
6a2d3987 232 public function upgrade_4_7_8($rev) {
2af0b023 233 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
6a2d3987 234 $this->addTask('Upgrade mailing foreign key constraints', 'upgradeMailingFKs');
065ffec9
TO
235 }
236
da1ecd73
SL
237 /**
238 * Upgrade function.
239 *
240 * @param string $rev
241 */
e8a4535e 242 public function upgrade_4_7_10($rev) {
da1ecd73 243 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
b5095b43
CW
244 $this->addTask('Upgrade Add Help Pre and Post Fields to price value table', 'addHelpPreAndHelpPostFieldsPriceFieldValue');
245 $this->addTask('Alter index and type for image URL', 'alterIndexAndTypeForImageURL');
da1ecd73
SL
246 }
247
00c27b41
CW
248 /**
249 * Upgrade function.
250 *
251 * @param string $rev
252 */
253 public function upgrade_4_7_11($rev) {
254 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
255 $this->addTask('Dashboard schema updates', 'dashboardSchemaUpdate');
b5095b43 256 $this->addTask('Fill in setting "remote_profile_submissions"', 'migrateRemoteSubmissionsSetting');
00c27b41
CW
257 }
258
7c61320c
SL
259 /**
260 * Upgrade function.
261 *
262 * @param string $rev
263 */
264 public function upgrade_4_7_12($rev) {
265 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
b5095b43 266 $this->addTask('Add Data Type column to civicrm_option_group', 'addDataTypeColumnToOptionGroupTable');
7c61320c 267 }
cb5962bd
SL
268 /**
269 * Upgrade function.
270 *
271 * @param string $rev
272 */
273 public function upgrade_4_7_13($rev) {
274 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
6846f088
CW
275 $this->addTask('CRM-19372 - Add column to allow for payment processors to set what card types are accepted', 'addColumn',
276 'civicrm_payment_processor', 'accepted_credit_cards', "text DEFAULT NULL COMMENT 'array of accepted credit card types'");
cb5962bd
SL
277 }
278
7ad5ae6a
CW
279 /**
280 * Upgrade function.
281 *
282 * @param string $rev
283 */
284 public function upgrade_4_7_14($rev) {
285 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
286 $this->addTask('Add WYSIWYG Editor Presets', 'addWysiwygPresets');
287 }
7c61320c 288
b412f764
CW
289 /**
290 * Upgrade function.
291 *
292 * @param string $rev
293 */
294 public function upgrade_4_7_15($rev) {
6846f088
CW
295 $this->addTask('CRM-19626 - Add min_amount column to civicrm_price_set', 'addColumn',
296 'civicrm_price_set', 'min_amount', "INT(10) UNSIGNED DEFAULT '0' COMMENT 'Minimum Amount required for this set.'");
b412f764
CW
297 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
298 }
299
703875d8
TO
300 /**
301 * Upgrade function.
302 *
303 * @param string $rev
304 */
305 public function upgrade_4_7_16($rev) {
73c5338d
CW
306 $this->addTask('CRM-19723 - Add icon column to civicrm_option_value', 'addColumn',
307 'civicrm_option_value', 'icon', "varchar(255) COMMENT 'crm-i icon class' DEFAULT NULL");
308 $this->addTask('CRM-19769 - Add color column to civicrm_tag', 'addColumn',
309 'civicrm_tag', 'color', "varchar(255) COMMENT 'Hex color value e.g. #ffffff' DEFAULT NULL");
310 $this->addTask('CRM-19779 - Add color column to civicrm_option_value', 'addColumn',
311 'civicrm_option_value', 'color', "varchar(255) COMMENT 'Hex color value e.g. #ffffff' DEFAULT NULL");
703875d8 312 $this->addTask('Add new CiviMail fields', 'addMailingTemplateType');
cd063b10
CW
313 $this->addTask('CRM-19770 - Add is_star column to civicrm_activity', 'addColumn',
314 'civicrm_activity', 'is_star', "tinyint DEFAULT '0' COMMENT 'Activity marked as favorite.'");
73c5338d 315 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
703875d8
TO
316 }
317
36610486 318 /**
319 * Upgrade function.
320 *
321 * @param string $rev
322 */
323 public function upgrade_4_7_18($rev) {
324 $this->addTask('Update Kenyan Provinces', 'updateKenyanProvinces');
325 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
326 }
327
8712faa9
PN
328 /**
329 * Upgrade function.
330 *
331 * @param string $rev
332 */
333 public function upgrade_4_7_19($rev) {
334 $query = "SELECT id FROM civicrm_financial_account WHERE opening_balance <> 0 OR current_period_opening_balance <> 0";
335 $result = CRM_Core_DAO::executeQuery($query);
336 if (!$result->N) {
337 $this->addTask('Drop Column current_period_opening_balance From civicrm_financial_account table.', 'dropColumn', 'civicrm_financial_account', 'current_period_opening_balance');
338 $this->addTask('Drop Column opening_balance From civicrm_financial_account table.', 'dropColumn', 'civicrm_financial_account', 'opening_balance');
339 }
4eae8dda
SL
340 $this->addTask('CRM-19961 - Add domain_id column to civicrm_sms_provider', 'addColumn',
341 'civicrm_sms_provider', 'domain_id', 'int(10) unsigned', "Which Domain is this sms provier for");
342 $this->addTask('CRM-19961 - Populate domain id table and perhaps add foreign key', 'populateSMSProviderDomainId');
8712faa9 343 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
ccfd6962 344 $this->addTask('CRM-16633 - Add "Change Case Subject" activity', 'addChangeCaseSubjectActivityType');
460931ca
JP
345 $this->addTask('Add is_public column to civicrm_custom_group', 'addColumn',
346 'civicrm_custom_group', 'is_public', "boolean DEFAULT '1' COMMENT 'Is this property public?'");
8712faa9
PN
347 }
348
6f0dad97
SL
349 /**
350 * Upgrade function.
351 *
352 * @param string $rev
353 */
354 public function upgrade_4_7_20($rev) {
355 $this->addtask('Fix Schema on civicrm_action_schedule', 'fixSchemaOnCiviCRMActionSchedule');
356 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
357 }
358
2af0b023 359 /*
2cbdbcef 360 * Important! All upgrade functions MUST add a 'runSql' task.
2af0b023
CW
361 * Uncomment and use the following template for a new upgrade version
362 * (change the x in the function name):
363 */
364
365 // /**
366 // * Upgrade function.
367 // *
368 // * @param string $rev
369 // */
370 // public function upgrade_4_7_x($rev) {
371 // $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
372 // // Additional tasks here...
b5095b43
CW
373 // // Note: do not use ts() in the addTask description because it adds unnecessary strings to transifex.
374 // // The above is an exception because 'Upgrade DB to %1: SQL' is generic & reusable.
2af0b023
CW
375 // }
376
6cc25669
CW
377 /**
378 * CRM-16354
379 *
6dbe2c23 380 * @return int
6cc25669 381 */
6dbe2c23 382 public static function updateWysiwyg() {
aaffa79f 383 $editorID = Civi::settings()->get('editor_id');
6dbe2c23
CW
384 // Previously a numeric value indicated one of 4 wysiwyg editors shipped in core, and no value indicated 'Textarea'
385 // Now the options are "Textarea", "CKEditor", and the rest have been dropped from core.
386 $newEditor = $editorID ? "CKEditor" : "Textarea";
08ef4ddd 387 Civi::settings()->set('editor_id', $newEditor);
6cc25669 388
6dbe2c23 389 return $editorID;
6cc25669
CW
390 }
391
f806379b
TO
392 /**
393 * Migrate any last remaining options from `civicrm_domain.config_backend` to `civicrm_setting`.
394 * Cleanup setting schema.
395 *
396 * @param CRM_Queue_TaskContext $ctx
397 * @return bool
398 */
5c9edd99 399 public static function migrateSettings(CRM_Queue_TaskContext $ctx) {
4cc9e637
TO
400 // Tip: If there are problems with adding the new uniqueness index, try inspecting:
401 // SELECT name, domain_id, contact_id, count(*) AS dupes FROM civicrm_setting cs GROUP BY name, domain_id, contact_id HAVING dupes > 1;
402
403 // Nav records are expendable. https://forum.civicrm.org/index.php?topic=36933.0
404 CRM_Core_DAO::executeQuery('DELETE FROM civicrm_setting WHERE contact_id IS NOT NULL AND name = "navigation"');
3ddd2901 405
726e6261
SL
406 CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_setting DROP INDEX index_group_name');
407 CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_setting DROP COLUMN group_name');
408
3ddd2901
SL
409 // Handle Strange activity_tab_filter settings.
410 CRM_Core_DAO::executeQuery('CREATE TABLE civicrm_activity_setting LIKE civicrm_setting');
726e6261 411 CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_activity_setting ADD UNIQUE INDEX index_domain_contact_name (domain_id, contact_id, name)');
3ddd2901 412 CRM_Core_DAO::executeQuery('INSERT INTO civicrm_activity_setting (name, contact_id, domain_id, value)
726e6261 413 SELECT DISTINCT name, contact_id, domain_id, value
3ddd2901
SL
414 FROM civicrm_setting
415 WHERE name = "activity_tab_filter"
416 AND value is not NULL');
ee4d8422 417 CRM_Core_DAO::executeQuery('DELETE FROM civicrm_setting WHERE name = "activity_tab_filter"');
4cc9e637 418
e6a2c901 419 $date = CRM_Utils_Time::getTime('Y-m-d H:i:s');
f806379b 420 CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_setting ADD UNIQUE INDEX index_domain_contact_name (domain_id, contact_id, name)');
e6a2c901
SL
421 CRM_Core_DAO::executeQuery("INSERT INTO civicrm_setting (name, contact_id, domain_id, value, is_domain, created_id, created_date)
422 SELECT name, contact_id, domain_id, value, 0, contact_id,'$date'
3ddd2901 423 FROM civicrm_activity_setting
e6a2c901
SL
424 WHERE name = 'activity_tab_filter'
425 AND value is not NULL"
d09d52ce 426 );
3ddd2901 427 CRM_Core_DAO::executeQuery('DROP TABLE civicrm_activity_setting');
f806379b
TO
428
429 $domainDao = CRM_Core_DAO::executeQuery('SELECT id, config_backend FROM civicrm_domain');
430 while ($domainDao->fetch()) {
431 $settings = CRM_Upgrade_Incremental_php_FourSeven::convertBackendToSettings($domainDao->id, $domainDao->config_backend);
432 CRM_Core_Error::debug_var('convertBackendToSettings', array(
433 'domainId' => $domainDao->id,
434 'backend' => $domainDao->config_backend,
435 'settings' => $settings,
436 ));
437
438 foreach ($settings as $name => $value) {
439 $rowParams = array(
440 1 => array($domainDao->id, 'Positive'),
441 2 => array($name, 'String'),
442 3 => array(serialize($value), 'String'),
443 );
444 $settingId = CRM_Core_DAO::singleValueQuery(
445 'SELECT id FROM civicrm_setting WHERE domain_id = %1 AND name = %2',
446 $rowParams);
447 if (!$settingId) {
448 CRM_Core_DAO::executeQuery(
449 'INSERT INTO civicrm_setting (domain_id, name, value, is_domain) VALUES (%1,%2,%3,1)',
450 $rowParams);
451 }
452 }
453 }
454
9e726168 455 CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_domain DROP COLUMN config_backend');
f806379b
TO
456
457 return TRUE;
458 }
459
460 /**
461 * Take a config_backend blob and produce an equivalent list of settings.
462 *
463 * @param int $domainId
464 * Domain ID.
465 * @param string $config_backend
466 * Serialized blob.
467 * @return array
468 */
469 public static function convertBackendToSettings($domainId, $config_backend) {
470 if (!$config_backend) {
471 return array();
472 }
473
474 $backend = unserialize($config_backend);
475 if (!$backend) {
476 return array();
477 }
478
479 $mappings = \CRM_Core_Config_MagicMerge::getPropertyMap();
480 $settings = array();
481 foreach ($backend as $propertyName => $propertyValue) {
482 if (isset($mappings[$propertyName][0]) && preg_match('/^setting/', $mappings[$propertyName][0])) {
483 // $mapping format: $propertyName => Array(0 => $type, 1 => $setting|NULL).
484 $settingName = isset($mappings[$propertyName][1]) ? $mappings[$propertyName][1] : $propertyName;
485 $settings[$settingName] = $propertyValue;
486 }
487 }
488
489 return $settings;
490 }
491
0a95c936 492 /**
493 * Add Getting Started dashlet to dashboard
494 *
495 * @param \CRM_Queue_TaskContext $ctx
496 *
497 * @return bool
498 */
5c9edd99 499 public static function addGettingStartedDashlet(CRM_Queue_TaskContext $ctx) {
9c4a04f2 500 $sql = "SELECT count(*) FROM civicrm_dashboard WHERE name='getting-started'";
e1674273 501 $res = CRM_Core_DAO::singleValueQuery($sql);
502 $domainId = CRM_Core_Config::domainID();
503 if ($res <= 0) {
504 $sql = "INSERT INTO `civicrm_dashboard`
a8b704c5 505 ( `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 506 CRM_Core_DAO::executeQuery($sql);
507 // Add default position for Getting Started Dashlet ( left column)
508 $sql = "INSERT INTO `civicrm_dashboard_contact` (dashboard_id, contact_id, column_no, is_active)
509SELECT (SELECT MAX(id) FROM `civicrm_dashboard`), contact_id, 0, IF (SUM(is_active) > 0, 1, 0)
429da6a1 510FROM `civicrm_dashboard_contact` JOIN `civicrm_contact` WHERE civicrm_dashboard_contact.contact_id = civicrm_contact.id GROUP BY contact_id";
e1674273 511 CRM_Core_DAO::executeQuery($sql);
512 }
0a95c936 513 return TRUE;
e1674273 514 }
4175ee03 515
6b1e1a2c 516 /**
517 * Migrate on-behalf information to uf_join.module_data as on-behalf columns will be dropped
518 * on DB upgrade
519 *
520 * @param CRM_Queue_TaskContext $ctx
521 *
522 * @return bool
523 * TRUE for success
524 */
525 public static function migrateOnBehalfOfInfo(CRM_Queue_TaskContext $ctx) {
d3e92c88 526 $domain = new CRM_Core_DAO_Domain();
527 $domain->find(TRUE);
6b1e1a2c 528
d3e92c88 529 // fetch onBehalf entry in UFJoin table
6b1e1a2c 530 $ufGroupDAO = new CRM_Core_DAO_UFJoin();
531 $ufGroupDAO->module = 'OnBehalf';
532 $ufGroupDAO->find(TRUE);
533
f635ab77 534 $forOrgColums = array('is_for_organization');
d3e92c88 535 if ($domain->locales) {
536 $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
537 foreach ($locales as $locale) {
538 $forOrgColums[] = "for_organization_{$locale}";
539 }
540 }
541 else {
542 $forOrgColums[] = "for_organization";
543 }
544
545 $query = "
546 SELECT " . implode(", ", $forOrgColums) . ", uj.id as join_id, uj.uf_group_id as uf_group_id
547 FROM civicrm_contribution_page cp
548 INNER JOIN civicrm_uf_join uj ON uj.entity_id = cp.id AND uj.module = 'OnBehalf'";
549 $dao = CRM_Core_DAO::executeQuery($query, array(), TRUE, NULL, FALSE, FALSE);
6b1e1a2c 550
551 if ($dao->N) {
6b1e1a2c 552 while ($dao->fetch()) {
553 $onBehalfParams['on_behalf'] = array('is_for_organization' => $dao->is_for_organization);
554 if ($domain->locales) {
6b1e1a2c 555 foreach ($locales as $locale) {
556 $for_organization = "for_organization_{$locale}";
557 $onBehalfParams['on_behalf'] += array(
558 $locale => array(
559 'for_organization' => $dao->$for_organization,
560 ),
561 );
562 }
563 }
564 else {
565 $onBehalfParams['on_behalf'] += array(
566 'default' => array(
567 'for_organization' => $dao->for_organization,
568 ),
569 );
570 }
571 $ufJoinParam = array(
572 'id' => $dao->join_id,
573 'module' => 'on_behalf',
d3e92c88 574 'uf_group_id' => $dao->uf_group_id,
6b1e1a2c 575 'module_data' => json_encode($onBehalfParams),
576 );
577 CRM_Core_BAO_UFJoin::create($ufJoinParam);
578 }
579 }
580
581 return TRUE;
9e42a501
TO
582 }
583
584 /**
585 * v4.7.11 adds a new setting "remote_profile_submissions". This is
586 * long-standing feature that existing sites may be using; however, it's
587 * a bit prone to abuse. For new sites, the default is to disable it
588 * (since that is more secure). For existing sites, the default is to
589 * enable it (since that is more compatible).
590 *
591 * @param \CRM_Queue_TaskContext $ctx
592 *
593 * @return bool
594 */
5c9edd99 595 public static function migrateRemoteSubmissionsSetting(CRM_Queue_TaskContext $ctx) {
9e42a501
TO
596 $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");
597 while ($domains->fetch()) {
598 CRM_Core_DAO::executeQuery(
599 "INSERT INTO civicrm_setting (`name`, `value`, `domain_id`, `is_domain`, `contact_id`, `component_id`, `created_date`, `created_id`)
600 VALUES (%2, %3, %4, %5, NULL, NULL, %6, NULL)",
601 array(
602 2 => array('remote_profile_submissions', 'String'),
603 3 => array('s:1:"1";', 'String'),
604 4 => array($domains->id, 'Integer'),
605 5 => array(1, 'Integer'),
606 6 => array(date('Y-m-d H:i:s'), 'String'),
607 )
608 );
609 }
610 return TRUE;
6b1e1a2c 611 }
cb804cd9 612
b2222b9f
CW
613 /**
614 * CRM-11782 - Get rid of VALUE_SEPARATOR character in saved search form values
615 *
616 * @param \CRM_Queue_TaskContext $ctx
617 *
618 * @return bool
619 */
5c9edd99 620 public static function fixContactTypeInSmartGroups(CRM_Queue_TaskContext $ctx) {
b2222b9f
CW
621 $sep = CRM_Core_DAO::VALUE_SEPARATOR;
622 $dao = CRM_Core_DAO::executeQuery("SELECT id, form_values FROM civicrm_saved_search WHERE form_values LIKE '%$sep%'");
623 while ($dao->fetch()) {
624 $formValues = unserialize($dao->form_values);
625 if (isset($formValues['contact_type']) && is_array($formValues['contact_type'])) {
626 $newVals = array();
627 foreach ($formValues['contact_type'] as $key => $val) {
628 $newVals[str_replace($sep, '__', $key)] = is_string($val) ? str_replace($sep, '__', $val) : $val;
629 }
630 $formValues['contact_type'] = $newVals;
631 }
632 CRM_Core_DAO::executeQuery("UPDATE civicrm_saved_search SET form_values = %1 WHERE id = {$dao->id}", array(1 => array(serialize($formValues), 'String')));
633 }
634
635 return TRUE;
636 }
637
0094ac08
CW
638 /**
639 * CRM-17637 - Ths file location has been moved; delete the old one
640 *
641 * @param \CRM_Queue_TaskContext $ctx
642 *
643 * @return bool
644 */
5c9edd99 645 public static function deleteVersionCheckCacheFile(CRM_Queue_TaskContext $ctx) {
0094ac08
CW
646 $config = CRM_Core_Config::singleton();
647 $cacheFile = $config->uploadDir . 'version-info-cache.json';
648 if (file_exists($cacheFile)) {
649 unlink($cacheFile);
650 }
651 return TRUE;
652 }
653
fb1f3850
DRJ
654 /**
655 * CRM-17669 and CRM-17686, make scheduled jobs more flexible, disable the 4.6 extension if installed
656 *
657 * @param \CRM_Queue_TaskContext $ctx
658 *
659 * @return bool
660 */
5c9edd99 661 public static function disableFlexibleJobsExtension(CRM_Queue_TaskContext $ctx) {
d357221c
DRJ
662 try {
663 civicrm_api3('Extension', 'disable', array('key' => 'com.klangsoft.flexiblejobs'));
664 }
665 catch (CiviCRM_API3_Exception $e) {
666 // just ignore if the extension isn't installed
667 }
fb1f3850
DRJ
668
669 return TRUE;
670 }
671
20d5377e 672 /**
673 * CRM-17752 add index to civicrm_financial_trxn.trxn_id (deliberately non-unique).
674 *
675 * @param \CRM_Queue_TaskContext $ctx
676 *
677 * @return bool
678 */
5c9edd99 679 public static function addIndexFinancialTrxnTrxnID(CRM_Queue_TaskContext $ctx) {
20d5377e 680 $tables = array('civicrm_financial_trxn' => array('trxn_id'));
681 CRM_Core_BAO_SchemaHandler::createIndexes($tables);
682 return TRUE;
683 }
684
8ca47f5c 685 /**
686 * CRM-17882 Add index to civicrm_contribution.credit_note_id.
687 *
688 * @param \CRM_Queue_TaskContext $ctx
689 *
690 * @return bool
691 */
5c9edd99 692 public static function addIndexContributionCreditNoteID(CRM_Queue_TaskContext $ctx) {
8ca47f5c 693 $tables = array('civicrm_contribution' => array('creditnote_id'));
694 CRM_Core_BAO_SchemaHandler::createIndexes($tables);
695 return TRUE;
696 }
697
2179c899 698 /**
699 * CRM-17775 Add correct index for table civicrm_financial_item.
700 *
701 * Note that the entity ID should always precede the entity_table as
702 * it is more unique. This is better for performance and does not cause fallback
703 * to no index if table it omitted.
704 *
705 * @return bool
706 */
5c9edd99 707 public static function addCombinedIndexFinancialItemEntityIDEntityType() {
2179c899 708 CRM_Core_BAO_SchemaHandler::dropIndexIfExists('civicrm_financial_item', 'UI_id');
709 CRM_Core_BAO_SchemaHandler::dropIndexIfExists('civicrm_financial_item', 'IX_Entity');
710 CRM_Core_BAO_SchemaHandler::createIndexes(array(
711 'civicrm_financial_item' => array(array('entity_id', 'entity_table')),
712 ));
713 return TRUE;
714 }
715
1f395432 716 /**
717 * CRM-17951 Add accounts option values for refund and chargeback.
718 *
719 * Add Chargeback contribution status and Chargeback and Contra account relationships,
720 * checking first if one exists.
721 */
5c9edd99 722 public static function addRefundAndChargeBackAccountsIfNotExist() {
1f395432 723 // First we enable and edit the record for Credit contra - this exists but is disabled for most sites.
724 // Using the ensure function (below) will not enabled a disabled option (by design).
725 CRM_Core_DAO::executeQuery("UPDATE civicrm_option_value v
726 INNER JOIN civicrm_option_group g on v.option_group_id=g.id and g.name='account_relationship'
727 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'
728 WHERE v.name = 'Credit/Contra Account is';");
729
730 CRM_Core_BAO_OptionValue::ensureOptionValueExists(array(
731 'option_group_id' => 'account_relationship',
4a907feb
EM
732 'name' => 'Chargeback Account is',
733 'label' => ts('Chargeback Account is'),
1f395432 734 'is_active' => TRUE,
735 'component_id' => 'CiviContribute',
736 ));
737
738 CRM_Core_BAO_OptionValue::ensureOptionValueExists(array(
739 'option_group_id' => 'contribution_status',
740 'name' => 'Chargeback',
741 'label' => ts('Chargeback'),
742 'is_active' => TRUE,
743 'component_id' => 'CiviContribute',
744 ));
745 return TRUE;
746 }
747
3fe26f4c 748 /**
749 * CRM-17999 Add index to civicrm_contribution.source.
750 *
751 * @param \CRM_Queue_TaskContext $ctx
752 *
753 * @return bool
754 */
5c9edd99 755 public static function addIndexContributionSource(CRM_Queue_TaskContext $ctx) {
3fe26f4c 756 CRM_Core_BAO_SchemaHandler::createIndexes(array('civicrm_contribution' => array('source')));
757 return TRUE;
758 }
759
fd28b6a0 760 /**
761 * CRM-18124 Add index to civicrm_contribution.total_amount.
762 *
763 * Note that I made this a combined index with receive_date because the issue included
764 * both criteria and they seemed likely to be used in conjunction to me in other cases.
765 *
766 * @param \CRM_Queue_TaskContext $ctx
767 *
768 * @return bool
769 */
5c9edd99 770 public static function addIndexContributionAmount(CRM_Queue_TaskContext $ctx) {
fd28b6a0 771 CRM_Core_BAO_SchemaHandler::createIndexes(array(
772 'civicrm_contribution' => array(array('total_amount', 'receive_date')),
773 ));
774 return TRUE;
775 }
776
6eb752fa 777 /**
778 * CRM-18124 Add index to civicrm_contribution.total_amount.
779 *
780 * Note that I made this a combined index with receive_date because the issue included
781 * both criteria and they seemed likely to be used in conjunction to me in other cases.
782 *
783 * @param \CRM_Queue_TaskContext $ctx
784 *
785 * @return bool
786 */
5c9edd99 787 public static function addDeletedByMergeActivityType(CRM_Queue_TaskContext $ctx) {
6eb752fa 788 CRM_Core_BAO_OptionValue::ensureOptionValueExists(array(
789 'option_group_id' => 'activity_type',
790 'name' => 'Contact Deleted by Merge',
791 'label' => ts('Contact Deleted by Merge'),
792 'description' => ts('Contact was merged into another contact'),
793 'is_active' => TRUE,
e14e412b 794 'filter' => 1,
6eb752fa 795 ));
796 return TRUE;
797 }
798
da1ecd73
SL
799 /**
800 * CRM-12252 Add Help Pre and Help Post Fields for Price Field Value Table.
801 *
802 * @param \CRM_Queue_TaskContext $ctx
803 *
804 * @return bool
805 */
5c9edd99 806 public static function addHelpPreAndHelpPostFieldsPriceFieldValue(CRM_Queue_TaskContext $ctx) {
da1ecd73
SL
807 $domain = new CRM_Core_DAO_Domain();
808 $domain->find(TRUE);
809 if ($domain->locales) {
810 $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
811 foreach ($locales as $locale) {
01ee8ba6 812 if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists("civicrm_price_field_value", "help_pre_{$locale}")) {
3dc870a2
SL
813 CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_price_field_value`
814 ADD COLUMN `help_pre_{$locale}` text COLLATE utf8_unicode_ci COMMENT 'Price field option pre help text.'", array(), TRUE, NULL, FALSE, FALSE);
da1ecd73 815 }
01ee8ba6 816 if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists("civicrm_price_field_value", "help_post_{$locale}")) {
3dc870a2
SL
817 CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_price_field_value`
818 ADD COLUMN `help_post_{$locale}` text COLLATE utf8_unicode_ci COMMENT 'Price field option post help text.'", array(), TRUE, NULL, FALSE, FALSE);
da1ecd73
SL
819 }
820 }
01ee8ba6 821 CRM_Core_I18n_Schema::rebuildMultilingualSchema($locales, NULL);
da1ecd73
SL
822 }
823 else {
824 if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists('civicrm_price_field_value', 'help_pre')) {
3dc870a2
SL
825 CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_price_field_value`
826 ADD COLUMN `help_pre` text COLLATE utf8_unicode_ci COMMENT 'Price field option pre help text.'");
da1ecd73
SL
827 }
828 if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists('civicrm_price_field_value', 'help_post')) {
3dc870a2
SL
829 CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_price_field_value`
830 ADD COLUMN `help_post` text COLLATE utf8_unicode_ci COMMENT 'Price field option post help text.'");
da1ecd73
SL
831 }
832 }
833 return TRUE;
834 }
835
8c748d50
SL
836 /**
837 * CRM-18464 Check if Foreign key exists and also drop any index of same name accidentially created.
838 *
839 * @param \CRM_Queue_TaskContext $ctx
840 *
841 * @return bool
842 */
843 public static function dropActionScheudleMappingForeignKey(CRM_Queue_TaskContext $ctx) {
844 CRM_Core_BAO_SchemaHandler::safeRemoveFK('civicrm_action_schedule', 'FK_civicrm_action_schedule_mapping_id');
8c748d50
SL
845 return TRUE;
846 }
847
2cbdd085
J
848 /**
849 * CRM-18345 Don't delete mailing data on email/phone deletion
850 * Implemented here in CRM-18526
851 *
852 * @param \CRM_Queue_TaskContext $ctx
853 *
854 * @return bool
855 */
5c9edd99 856 public static function upgradeMailingFKs(CRM_Queue_TaskContext $ctx) {
2cbdd085
J
857
858 // Safely drop the foreign keys
169475b7
SL
859 CRM_Core_BAO_SchemaHandler::safeRemoveFK('civicrm_mailing_event_queue', 'FK_civicrm_mailing_event_queue_email_id');
860 CRM_Core_BAO_SchemaHandler::safeRemoveFK('civicrm_mailing_event_queue', 'FK_civicrm_mailing_event_queue_phone_id');
861 CRM_Core_BAO_SchemaHandler::safeRemoveFK('civicrm_mailing_recipients', 'FK_civicrm_mailing_recipients_email_id');
862 CRM_Core_BAO_SchemaHandler::safeRemoveFK('civicrm_mailing_recipients', 'FK_civicrm_mailing_recipients_phone_id');
2cbdd085
J
863
864 // Set up the new foreign keys
865 CRM_Core_DAO::executeQuery("SET FOREIGN_KEY_CHECKS = 0;");
866
867 CRM_Core_DAO::executeQuery("
868 ALTER TABLE `civicrm_mailing_event_queue`
869 ADD CONSTRAINT `FK_civicrm_mailing_event_queue_email_id`
870 FOREIGN KEY (`email_id`)
871 REFERENCES `civicrm_email`(`id`)
872 ON DELETE SET NULL
873 ON UPDATE RESTRICT;
874 ");
875
876 CRM_Core_DAO::executeQuery("
877 ALTER TABLE `civicrm_mailing_event_queue`
878 ADD CONSTRAINT `FK_civicrm_mailing_event_queue_phone_id`
879 FOREIGN KEY (`phone_id`)
880 REFERENCES `civicrm_phone`(`id`)
881 ON DELETE SET NULL
882 ON UPDATE RESTRICT;
883 ");
884
885 CRM_Core_DAO::executeQuery("
886 ALTER TABLE `civicrm_mailing_recipients`
887 ADD CONSTRAINT `FK_civicrm_mailing_recipients_email_id`
888 FOREIGN KEY (`email_id`)
889 REFERENCES `civicrm_email`(`id`)
890 ON DELETE SET NULL
891 ON UPDATE RESTRICT;
892 ");
893
894 CRM_Core_DAO::executeQuery("
895 ALTER TABLE `civicrm_mailing_recipients`
896 ADD CONSTRAINT `FK_civicrm_mailing_recipients_phone_id`
897 FOREIGN KEY (`phone_id`)
898 REFERENCES `civicrm_phone`(`id`)
899 ON DELETE SET NULL
900 ON UPDATE RESTRICT;
901 ");
902
903 CRM_Core_DAO::executeQuery("SET FOREIGN_KEY_CHECKS = 1;");
904
905 return TRUE;
906 }
907
00c27b41
CW
908 /**
909 * CRM-17663 - Dashboard schema changes
910 *
911 * @param \CRM_Queue_TaskContext $ctx
912 *
913 * @return bool
914 */
5c9edd99 915 public static function dashboardSchemaUpdate(CRM_Queue_TaskContext $ctx) {
00c27b41
CW
916 if (!CRM_Core_BAO_SchemaHandler::checkIfIndexExists('civicrm_dashboard_contact', 'index_dashboard_id_contact_id')) {
917 // Delete any stray duplicate rows and add unique index to prevent new dupes and enable INSERT/UPDATE combo query
918 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');
919 CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_dashboard_contact ADD UNIQUE INDEX index_dashboard_id_contact_id (dashboard_id, contact_id);');
920 }
87568e34
SL
921 $domain = new CRM_Core_DAO_Domain();
922 $domain->find(TRUE);
242055d3
CW
923 CRM_Core_BAO_SchemaHandler::dropColumn('civicrm_dashboard_contact', 'content');
924 CRM_Core_BAO_SchemaHandler::dropColumn('civicrm_dashboard_contact', 'is_minimized');
925 CRM_Core_BAO_SchemaHandler::dropColumn('civicrm_dashboard_contact', 'is_fullscreen');
926 CRM_Core_BAO_SchemaHandler::dropColumn('civicrm_dashboard_contact', 'created_date');
927 CRM_Core_BAO_SchemaHandler::dropColumn('civicrm_dashboard', 'is_fullscreen');
928 CRM_Core_BAO_SchemaHandler::dropColumn('civicrm_dashboard', 'is_minimized');
929 CRM_Core_BAO_SchemaHandler::dropColumn('civicrm_dashboard', 'column_no');
930 CRM_Core_BAO_SchemaHandler::dropColumn('civicrm_dashboard', 'weight');
931
932 CRM_Core_DAO::executeQuery('UPDATE civicrm_dashboard SET url = REPLACE(url, "&snippet=5", ""), fullscreen_url = REPLACE(fullscreen_url, "&snippet=5", "")');
a8f56d71
CW
933
934 if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists('civicrm_dashboard', 'cache_minutes')) {
8c3f9072
SL
935 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."',
936 array(), TRUE, NULL, FALSE, FALSE);
937 }
938 if ($domain->locales) {
939 $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
940 CRM_Core_I18n_Schema::rebuildMultilingualSchema($locales, NULL);
a8f56d71
CW
941 }
942
943 CRM_Core_DAO::executeQuery('UPDATE civicrm_dashboard SET cache_minutes = 1440 WHERE name = "blog"');
944 CRM_Core_DAO::executeQuery('UPDATE civicrm_dashboard SET cache_minutes = 7200 WHERE name IN ("activity","getting-started")');
242055d3 945 return TRUE;
00c27b41
CW
946 }
947
8e2e96a5 948 /**
949 * CRM-19100 - Alter Index and Type for Image URL
950 * @return bool
951 */
952 public static function alterIndexAndTypeForImageURL() {
98daafd5 953 $length = array();
954 CRM_Core_BAO_SchemaHandler::dropIndexIfExists('civicrm_contact', 'index_image_url');
8e2e96a5 955 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.'");
98daafd5 956
957 $length['civicrm_contact']['image_URL'] = 128;
958 CRM_Core_BAO_SchemaHandler::createIndexes(array('civicrm_contact' => array('image_URL')), 'index', $length);
8e2e96a5 959
960 return TRUE;
961 }
962
bc854509 963 /**
964 * Add mailing template type.
965 *
966 * @return bool
967 */
703875d8
TO
968 public static function addMailingTemplateType() {
969 if (!CRM_Core_DAO::checkFieldExists('civicrm_mailing', 'template_type', FALSE)) {
970 CRM_Core_DAO::executeQuery('
971 ALTER TABLE civicrm_mailing
972 ADD COLUMN `template_type` varchar(64) NOT NULL DEFAULT \'traditional\' COMMENT \'The language/processing system used for email templates.\',
973 ADD COLUMN `template_options` longtext COMMENT \'Advanced options used by the email templating system. (JSON encoded)\'
974 ');
975 }
976 return TRUE;
977 }
978
d0e7e124
SL
979 /**
980 * CRM-18651 Add DataType column to Option Group Table
981 * @return bool
982 */
983 public static function addDataTypeColumnToOptionGroupTable() {
7c61320c 984 if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists('civicrm_option_group', 'data_type')) {
a37e4e6c
SL
985 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.'",
986 array(), TRUE, NULL, FALSE, FALSE);
987 }
988 $domain = new CRM_Core_DAO_Domain();
989 $domain->find(TRUE);
990 if ($domain->locales) {
991 $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
992 CRM_Core_I18n_Schema::rebuildMultilingualSchema($locales, NULL);
d0e7e124 993 }
a37e4e6c 994
d0e7e124
SL
995 CRM_Core_DAO::executeQuery("UPDATE `civicrm_option_group` SET `data_type` = 'Integer'
996 WHERE name IN ('activity_type', 'gender', 'payment_instrument', 'participant_role', 'event_type')");
997 return TRUE;
998 }
999
7ad5ae6a
CW
1000 /**
1001 * CRM-19372 Add field to store accepted credit credit cards for a payment processor.
1002 * @return bool
1003 */
1004 public static function addWysiwygPresets() {
1005 CRM_Core_BAO_OptionGroup::ensureOptionGroupExists(array(
1006 'name' => 'wysiwyg_presets',
1007 'title' => ts('WYSIWYG Editor Presets'),
1008 'is_reserved' => 1,
1009 ));
1010 $values = array(
1011 'default' => array('label' => ts('Default'), 'is_default' => 1),
1012 'civimail' => array('label' => ts('CiviMail'), 'component_id' => 'CiviMail'),
1013 'civievent' => array('label' => ts('CiviEvent'), 'component_id' => 'CiviEvent'),
1014 );
1015 foreach ($values as $name => $value) {
34273b2a 1016 CRM_Core_BAO_OptionValue::ensureOptionValueExists($value + array(
7ad5ae6a
CW
1017 'name' => $name,
1018 'option_group_id' => 'wysiwyg_presets',
1019 ));
1020 }
1021 $fileName = Civi::paths()->getPath('[civicrm.files]/persist/crm-ckeditor-config.js');
99316116 1022 // Ensure the config file contains the allowedContent setting
7ad5ae6a
CW
1023 if (file_exists($fileName)) {
1024 $config = file_get_contents($fileName);
1025 $pos = strrpos($config, '};');
1026 $setting = "\n\tconfig.allowedContent = true;\n";
1027 $config = substr_replace($config, $setting, $pos, 0);
1028 unlink($fileName);
949a87e8
CW
1029 $newFileName = Civi::paths()->getPath('[civicrm.files]/persist/crm-ckeditor-default.js');
1030 file_put_contents($newFileName, $config);
7ad5ae6a 1031 }
7ad5ae6a
CW
1032 return TRUE;
1033 }
1034
36610486 1035 /**
1036 * Update Kenyan Provinces to reflect changes per CRM-20062
1037 *
1038 * @param \CRM_Queue_TaskContext $ctx
1039 */
35e62234 1040 public static function updateKenyanProvinces(CRM_Queue_TaskContext $ctx) {
36610486 1041 $kenyaCountryID = CRM_Core_DAO::singleValueQuery('SELECT max(id) from civicrm_country where iso_code = "KE"');
1042 $oldProvinces = array(
1043 'Nairobi Municipality',
1044 'Coast',
1045 'North-Eastern Kaskazini Mashariki',
1046 'Rift Valley',
1047 'Western Magharibi',
1048 );
1049 self::deprecateStateProvinces($kenyaCountryID, $oldProvinces);
1050 return TRUE;
1051 }
1052
1053 /**
1054 * Deprecate provinces that no longer exist.
1055 *
1056 * @param int $countryID
1057 * @param array $provinces
1058 */
1059 public static function deprecateStateProvinces($countryID, $provinces) {
1060 foreach ($provinces as $province) {
1061 $existingStateID = CRM_Core_DAO::singleValueQuery("
1062 SELECT id FROM civicrm_state_province
1063 WHERE country_id = %1
1064 AND name = %2
1065 ",
1066 array(1 => array($countryID, 'Int'), 2 => array($province, 'String')));
1067
1068 if (!$existingStateID) {
1069 continue;
1070 }
1071 if (!CRM_Core_DAO::singleValueQuery("
1072 SELECT count(*) FROM civicrm_address
1073 WHERE state_province_id = %1
1074 ", array(1 => array($existingStateID, 'Int')))
1075 ) {
1076 CRM_Core_DAO::executeQuery("DELETE FROM civicrm_state_province WHERE id = %1", array(1 => array($existingStateID, 'Int')));
1077 }
1078 else {
1079 $params = array('1' => array(ts("Former - $province"), 'String'));
1080 CRM_Core_DAO::executeQuery("
1081 UPDATE civicrm_state_province SET name = %1 WHERE id = $existingStateID
1082 ", $params);
1083 }
1084 }
1085 }
1086
4eae8dda
SL
1087 /**
1088 * CRM-19961
1089 * Poputate newly added domain id column and add foriegn key onto table.
1090 */
1091 public static function populateSMSProviderDomainId() {
1092 $count = CRM_Core_DAO::singleValueQuery("SELECT count(id) FROM civicrm_domain");
1093 if ($count = 1) {
1094 CRM_Core_DAO::executeQuery("UPDATE civicrm_sms_provider SET domain_id = (SELECT id FROM civicrm_domain)");
1095 }
1096 CRM_Core_DAO::executeQuery("SET FOREIGN_KEY_CHECKS = 0;");
1097 CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_sms_provider`
1098 ADD CONSTRAINT FK_civicrm_sms_provider_domain_id
1099 FOREIGN KEY (`domain_id`) REFERENCES `civicrm_domain`(`id`)
1100 ON DELETE SET NULL");
1101
1102 CRM_Core_DAO::executeQuery("SET FOREIGN_KEY_CHECKS = 1;");
1103 return TRUE;
1104 }
1105
ccfd6962
CW
1106 /**
1107 * CRM-16633 - Add activity type for Change Case Status
1108 *
1109 * @param \CRM_Queue_TaskContext $ctx
1110 *
1111 * @return bool
1112 */
1113 public static function addChangeCaseSubjectActivityType(CRM_Queue_TaskContext $ctx) {
1114 CRM_Core_BAO_OptionValue::ensureOptionValueExists(array(
1115 'option_group_id' => 'activity_type',
1116 'name' => 'Change Case Subject',
1117 'label' => ts('Change Case Subject'),
1118 'is_active' => TRUE,
1119 'component_id' => 'CiviCase',
1120 'icon' => 'fa-pencil-square-o',
1121 ));
1122 return TRUE;
1123 }
1124
6f0dad97
SL
1125 /**
1126 * CRM-19986 fix schema differnces in civicrm_action_schedule
1127 */
1128 public static function fixSchemaOnCiviCRMActionSchedule() {
1129 $config = CRM_Core_Config::singleton();
1130 $dbUf = DB::parseDSN($config->dsn);
1131 $query = "
1132 SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
1133 WHERE TABLE_SCHEMA = %1
1134 AND TABLE_NAME = %2
1135 AND CONSTRAINT_NAME = %3
1136 AND CONSTRAINT_TYPE = 'FOREIGN KEY'
1137 ";
1138 $params = array(
1139 1 => array($dbUf['database'], 'String'),
1140 2 => array('civicrm_action_schedule', 'String'),
1141 3 => array('FK_civicrm_action_schedule_sms_template_id', 'String'),
1142 );
1143 $dao = CRM_Core_DAO::executeQuery($query, $params);
1144 if (!$dao->fetch()) {
1145 CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_action_schedule`
1146 ADD CONSTRAINT FK_civicrm_action_schedule_sms_template_id
1147 FOREIGN KEY (`sms_template_id`) REFERENCES `civicrm_msg_template`(`id`)
1148 ON DELETE SET NULL");
1149 }
1150 CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_action_schedule`
1151 CHANGE `mapping_id` `mapping_id` varchar(64) COLLATE
1152 utf8_unicode_ci DEFAULT NULL COMMENT 'Name/ID of the mapping to use on this table'");
1153 return TRUE;
1154 }
1155
6cc25669 1156}