Merge pull request #10492 from seamuslee001/CRM-20716
[civicrm-core.git] / CRM / Upgrade / Incremental / php / FourSeven.php
... / ...
CommitLineData
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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/**
28 * Upgrade logic for 4.7
29 */
30class CRM_Upgrade_Incremental_php_FourSeven extends CRM_Upgrade_Incremental_Base {
31
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 *
38 * @param string $preUpgradeMessage
39 * @param string $rev
40 * a version number, e.g. '4.4.alpha1', '4.4.beta3', '4.4.0'.
41 * @param null $currentVer
42 */
43 public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) {
44 if ($rev == '4.7.alpha1') {
45 // CRM-16478 Remove custom fatal error template path option
46 $config = CRM_Core_Config::singleton();
47 if (!empty($config->fatalErrorTemplate) && $config->fatalErrorTemplate != 'CRM/common/fatal.tpl') {
48 $preUpgradeMessage .= '<p>' . ts('The custom fatal error template setting will be removed during the upgrade. You are currently using this custom template: %1 . Following the upgrade you will need to use the standard approach to overriding template files, as described in the documentation.', array(1 => $config->fatalErrorTemplate)) . '</p>';
49 }
50 }
51 if ($rev == '4.7.alpha4') {
52 // CRM-17004 Warn of Moneris removal
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')) {
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 }
62 if ($rev == '4.7.13') {
63 $preUpgradeMessage .= '<p>' . ts('A new permission has been added called %1 This Permission is now used to control access to the Manage Tags screen', array(1 => 'manage tags')) . '</p>';
64 }
65 }
66
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.
74 */
75 public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
76 if ($rev == '4.7.alpha1') {
77 $config = CRM_Core_Config::singleton();
78 // FIXME: Performing an upgrade step during postUpgrade message phase is probably bad
79 $editor_id = self::updateWysiwyg();
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>';
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).');
102
103 $postUpgradeMessage .= '<p>' . ts('The custom fatal error template setting has been removed.') . '</p>';
104 }
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 //}
108 if ($rev == '4.7.11') {
109 $postUpgradeMessage .= '<br /><br />' . ts("By default, CiviCRM now disables the ability to import directly from SQL. To use this feature, you must explicitly grant permission 'import SQL datasource'.");
110 }
111 if ($rev == '4.7.14') {
112 $ck_href = 'href="' . CRM_Utils_System::url('civicrm/admin/ckeditor') . '"';
113 $postUpgradeMessage .= '<p>' . ts('CiviMail no longer forces CKEditor to add html/head/body tags to email content because some sites place these in the message header/footer. This was added in 4.7.5 and is now disabled by default.')
114 . '<br />' . ts('You can re-enable it by visitng the <a %1>CKEditor Config</a> screen and setting "fullPage = true" under the Advanced Options of the CiviMail preset.', array(1 => $ck_href))
115 . '</p>';
116 }
117 if ($rev == '4.7.19') {
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).');
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 }
124 }
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 }
128 }
129
130 /**
131 * Upgrade function.
132 *
133 * @param string $rev
134 */
135 public function upgrade_4_7_alpha1($rev) {
136 $this->addTask('Drop action scheudle mapping foreign key', 'dropActionScheudleMappingForeignKey');
137 $this->addTask('Migrate \'on behalf of\' information to module_data', 'migrateOnBehalfOfInfo');
138 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
139 $this->addTask(ts('Migrate Settings to %1', array(1 => $rev)), 'migrateSettings', $rev);
140 $this->addTask('Add Getting Started dashlet', 'addGettingStartedDashlet', $rev);
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);
150 $this->addTask(ts('Remove %1', array(1 => 'Moneris')), 'removePaymentProcessorType', 'Moneris');
151 $this->addTask('Update Smart Groups', 'fixContactTypeInSmartGroups');
152 }
153
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
164 /**
165 * Upgrade function.
166 *
167 * @param string $rev
168 */
169 public function upgrade_4_7_beta6($rev) {
170 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
171 $this->addTask('Disable flexible jobs extension', 'disableFlexibleJobsExtension');
172 $this->addTask('Add Index to financial_trxn trxn_id field', 'addIndexFinancialTrxnTrxnID');
173 }
174
175 /**
176 * Upgrade function.
177 *
178 * @param string $rev
179 */
180 public function upgrade_4_7_1($rev) {
181 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
182 $this->addTask('Add Index to civicrm_contribution creditnote_id field', 'addIndexContributionCreditNoteID');
183 }
184
185 /**
186 * Upgrade function.
187 *
188 * @param string $rev
189 */
190 public function upgrade_4_7_2($rev) {
191 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
192 $this->addTask('Fix Index on civicrm_financial_item combined entity_id + entity_table', 'addCombinedIndexFinancialItemEntityIDEntityType');
193 $this->addTask('enable financial account relationships for chargeback & refund', 'addRefundAndChargeBackAccountsIfNotExist');
194 $this->addTask('Add Index to civicrm_contribution.source', 'addIndexContributionSource');
195 }
196
197 /**
198 * Upgrade function.
199 *
200 * @param string $rev
201 */
202 public function upgrade_4_7_3($rev) {
203 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
204 $this->addTask('Add Index to civicrm_contribution.total_amount', 'addIndexContributionAmount');
205 }
206
207 /**
208 * Upgrade function.
209 *
210 * @param string $rev
211 */
212 public function upgrade_4_7_4($rev) {
213 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
214 $this->addTask('Add Contact Deleted by Merge Activity Type', 'addDeletedByMergeActivityType');
215 }
216
217 /**
218 * Upgrade function.
219 *
220 * @param string $rev
221 */
222 public function upgrade_4_7_7($rev) {
223 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
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 }
228 }
229
230 /**
231 * Upgrade function.
232 *
233 * @param string $rev
234 */
235 public function upgrade_4_7_8($rev) {
236 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
237 $this->addTask('Upgrade mailing foreign key constraints', 'upgradeMailingFKs');
238 }
239
240 /**
241 * Upgrade function.
242 *
243 * @param string $rev
244 */
245 public function upgrade_4_7_10($rev) {
246 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
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');
249 }
250
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');
259 $this->addTask('Fill in setting "remote_profile_submissions"', 'migrateRemoteSubmissionsSetting');
260 }
261
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);
269 $this->addTask('Add Data Type column to civicrm_option_group', 'addDataTypeColumnToOptionGroupTable');
270 }
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);
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'");
280 }
281
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 }
291
292 /**
293 * Upgrade function.
294 *
295 * @param string $rev
296 */
297 public function upgrade_4_7_15($rev) {
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.'");
300 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
301 }
302
303 /**
304 * Upgrade function.
305 *
306 * @param string $rev
307 */
308 public function upgrade_4_7_16($rev) {
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");
315 $this->addTask('Add new CiviMail fields', 'addMailingTemplateType');
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.'");
318 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
319 }
320
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
331 /**
332 * Upgrade function.
333 *
334 * @param string $rev
335 */
336 public function upgrade_4_7_19($rev) {
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 }
344 }
345 $this->addTask('CRM-19961 - Add domain_id column to civicrm_sms_provider', 'addColumn',
346 'civicrm_sms_provider', 'domain_id', 'int(10) unsigned', "Which Domain is this sms provier for");
347 $this->addTask('CRM-19961 - Populate domain id table and perhaps add foreign key', 'populateSMSProviderDomainId');
348 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
349 $this->addTask('CRM-16633 - Add "Change Case Subject" activity', 'addChangeCaseSubjectActivityType');
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?'");
352 }
353
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);
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.'");
364 }
365
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
377 /*
378 * Important! All upgrade functions MUST add a 'runSql' task.
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...
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.
393 // }
394
395 /**
396 * CRM-16354
397 *
398 * @return int
399 */
400 public static function updateWysiwyg() {
401 $editorID = Civi::settings()->get('editor_id');
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";
405 Civi::settings()->set('editor_id', $newEditor);
406
407 return $editorID;
408 }
409
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 */
417 public static function migrateSettings(CRM_Queue_TaskContext $ctx) {
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"');
423
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
427 // Handle Strange activity_tab_filter settings.
428 CRM_Core_DAO::executeQuery('CREATE TABLE civicrm_activity_setting LIKE civicrm_setting');
429 CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_activity_setting ADD UNIQUE INDEX index_domain_contact_name (domain_id, contact_id, name)');
430 CRM_Core_DAO::executeQuery('INSERT INTO civicrm_activity_setting (name, contact_id, domain_id, value)
431 SELECT DISTINCT name, contact_id, domain_id, value
432 FROM civicrm_setting
433 WHERE name = "activity_tab_filter"
434 AND value is not NULL');
435 CRM_Core_DAO::executeQuery('DELETE FROM civicrm_setting WHERE name = "activity_tab_filter"');
436
437 $date = CRM_Utils_Time::getTime('Y-m-d H:i:s');
438 CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_setting ADD UNIQUE INDEX index_domain_contact_name (domain_id, contact_id, name)');
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'
441 FROM civicrm_activity_setting
442 WHERE name = 'activity_tab_filter'
443 AND value is not NULL"
444 );
445 CRM_Core_DAO::executeQuery('DROP TABLE civicrm_activity_setting');
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
473 CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_domain DROP COLUMN config_backend');
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
510 /**
511 * Add Getting Started dashlet to dashboard
512 *
513 * @param \CRM_Queue_TaskContext $ctx
514 *
515 * @return bool
516 */
517 public static function addGettingStartedDashlet(CRM_Queue_TaskContext $ctx) {
518 $sql = "SELECT count(*) FROM civicrm_dashboard WHERE name='getting-started'";
519 $res = CRM_Core_DAO::singleValueQuery($sql);
520 $domainId = CRM_Core_Config::domainID();
521 if ($res <= 0) {
522 $sql = "INSERT INTO `civicrm_dashboard`
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)";
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)
528FROM `civicrm_dashboard_contact` JOIN `civicrm_contact` WHERE civicrm_dashboard_contact.contact_id = civicrm_contact.id GROUP BY contact_id";
529 CRM_Core_DAO::executeQuery($sql);
530 }
531 return TRUE;
532 }
533
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) {
544 $domain = new CRM_Core_DAO_Domain();
545 $domain->find(TRUE);
546
547 // fetch onBehalf entry in UFJoin table
548 $ufGroupDAO = new CRM_Core_DAO_UFJoin();
549 $ufGroupDAO->module = 'OnBehalf';
550 $ufGroupDAO->find(TRUE);
551
552 $forOrgColums = array('is_for_organization');
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);
568
569 if ($dao->N) {
570 while ($dao->fetch()) {
571 $onBehalfParams['on_behalf'] = array('is_for_organization' => $dao->is_for_organization);
572 if ($domain->locales) {
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',
592 'uf_group_id' => $dao->uf_group_id,
593 'module_data' => json_encode($onBehalfParams),
594 );
595 CRM_Core_BAO_UFJoin::create($ufJoinParam);
596 }
597 }
598
599 return TRUE;
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 */
613 public static function migrateRemoteSubmissionsSetting(CRM_Queue_TaskContext $ctx) {
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;
629 }
630
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 */
638 public static function fixContactTypeInSmartGroups(CRM_Queue_TaskContext $ctx) {
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
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 */
663 public static function deleteVersionCheckCacheFile(CRM_Queue_TaskContext $ctx) {
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
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 */
679 public static function disableFlexibleJobsExtension(CRM_Queue_TaskContext $ctx) {
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 }
686
687 return TRUE;
688 }
689
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 */
697 public static function addIndexFinancialTrxnTrxnID(CRM_Queue_TaskContext $ctx) {
698 $tables = array('civicrm_financial_trxn' => array('trxn_id'));
699 CRM_Core_BAO_SchemaHandler::createIndexes($tables);
700 return TRUE;
701 }
702
703 /**
704 * CRM-17882 Add index to civicrm_contribution.credit_note_id.
705 *
706 * @param \CRM_Queue_TaskContext $ctx
707 *
708 * @return bool
709 */
710 public static function addIndexContributionCreditNoteID(CRM_Queue_TaskContext $ctx) {
711 $tables = array('civicrm_contribution' => array('creditnote_id'));
712 CRM_Core_BAO_SchemaHandler::createIndexes($tables);
713 return TRUE;
714 }
715
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 */
725 public static function addCombinedIndexFinancialItemEntityIDEntityType() {
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
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 */
740 public static function addRefundAndChargeBackAccountsIfNotExist() {
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',
750 'name' => 'Chargeback Account is',
751 'label' => ts('Chargeback Account is'),
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
766 /**
767 * CRM-17999 Add index to civicrm_contribution.source.
768 *
769 * @param \CRM_Queue_TaskContext $ctx
770 *
771 * @return bool
772 */
773 public static function addIndexContributionSource(CRM_Queue_TaskContext $ctx) {
774 CRM_Core_BAO_SchemaHandler::createIndexes(array('civicrm_contribution' => array('source')));
775 return TRUE;
776 }
777
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 */
788 public static function addIndexContributionAmount(CRM_Queue_TaskContext $ctx) {
789 CRM_Core_BAO_SchemaHandler::createIndexes(array(
790 'civicrm_contribution' => array(array('total_amount', 'receive_date')),
791 ));
792 return TRUE;
793 }
794
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 */
805 public static function addDeletedByMergeActivityType(CRM_Queue_TaskContext $ctx) {
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,
812 'filter' => 1,
813 ));
814 return TRUE;
815 }
816
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 */
824 public static function addHelpPreAndHelpPostFieldsPriceFieldValue(CRM_Queue_TaskContext $ctx) {
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) {
830 if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists("civicrm_price_field_value", "help_pre_{$locale}")) {
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);
833 }
834 if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists("civicrm_price_field_value", "help_post_{$locale}")) {
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);
837 }
838 }
839 CRM_Core_I18n_Schema::rebuildMultilingualSchema($locales, NULL);
840 }
841 else {
842 if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists('civicrm_price_field_value', 'help_pre')) {
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.'");
845 }
846 if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists('civicrm_price_field_value', 'help_post')) {
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.'");
849 }
850 }
851 return TRUE;
852 }
853
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');
863 return TRUE;
864 }
865
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 */
874 public static function upgradeMailingFKs(CRM_Queue_TaskContext $ctx) {
875
876 // Safely drop the foreign keys
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');
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
926 /**
927 * CRM-17663 - Dashboard schema changes
928 *
929 * @param \CRM_Queue_TaskContext $ctx
930 *
931 * @return bool
932 */
933 public static function dashboardSchemaUpdate(CRM_Queue_TaskContext $ctx) {
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 }
939 $domain = new CRM_Core_DAO_Domain();
940 $domain->find(TRUE);
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", "")');
951
952 if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists('civicrm_dashboard', 'cache_minutes')) {
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);
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")');
963 return TRUE;
964 }
965
966 /**
967 * CRM-19100 - Alter Index and Type for Image URL
968 * @return bool
969 */
970 public static function alterIndexAndTypeForImageURL() {
971 $length = array();
972 CRM_Core_BAO_SchemaHandler::dropIndexIfExists('civicrm_contact', 'index_image_url');
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.'");
974
975 $length['civicrm_contact']['image_URL'] = 128;
976 CRM_Core_BAO_SchemaHandler::createIndexes(array('civicrm_contact' => array('image_URL')), 'index', $length);
977
978 return TRUE;
979 }
980
981 /**
982 * Add mailing template type.
983 *
984 * @return bool
985 */
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
997 /**
998 * CRM-18651 Add DataType column to Option Group Table
999 * @return bool
1000 */
1001 public static function addDataTypeColumnToOptionGroupTable() {
1002 if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists('civicrm_option_group', 'data_type')) {
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);
1011 }
1012
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
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) {
1034 CRM_Core_BAO_OptionValue::ensureOptionValueExists($value + array(
1035 'name' => $name,
1036 'option_group_id' => 'wysiwyg_presets',
1037 ));
1038 }
1039 $fileName = Civi::paths()->getPath('[civicrm.files]/persist/crm-ckeditor-config.js');
1040 // Ensure the config file contains the allowedContent setting
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);
1047 $newFileName = Civi::paths()->getPath('[civicrm.files]/persist/crm-ckeditor-default.js');
1048 file_put_contents($newFileName, $config);
1049 }
1050 return TRUE;
1051 }
1052
1053 /**
1054 * Update Kenyan Provinces to reflect changes per CRM-20062
1055 *
1056 * @param \CRM_Queue_TaskContext $ctx
1057 */
1058 public static function updateKenyanProvinces(CRM_Queue_TaskContext $ctx) {
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
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");
1111 if ($count == 1) {
1112 CRM_Core_DAO::executeQuery("UPDATE civicrm_sms_provider SET domain_id = (SELECT id FROM civicrm_domain)");
1113 }
1114 if (!parent::checkFKExists('civicrm_sms_provider', 'FK_civicrm_sms_provider_domain_id')) {
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");
1120
1121 CRM_Core_DAO::executeQuery("SET FOREIGN_KEY_CHECKS = 1;");
1122 }
1123 return TRUE;
1124 }
1125
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
1145 /**
1146 * CRM-19986 fix schema differnces in civicrm_action_schedule
1147 */
1148 public static function fixSchemaOnCiviCRMActionSchedule() {
1149 if (!parent::checkFKExists('civicrm_action_schedule', 'FK_civicrm_action_schedule_sms_template_id')) {
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
1161}