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