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