Merge pull request #7827 from circleinteractive/master
[civicrm-core.git] / CRM / Upgrade / Incremental / php / FourSeven.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 */
30 class 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 }
63
64 /**
65 * Compute any messages which should be displayed after upgrade.
66 *
67 * @param string $postUpgradeMessage
68 * alterable.
69 * @param string $rev
70 * an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs.
71 */
72 public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
73 if ($rev == '4.7.alpha1') {
74 $config = CRM_Core_Config::singleton();
75 // FIXME: Performing an upgrade step during postUpgrade message phase is probably bad
76 $editor_id = self::updateWysiwyg();
77 $msg = NULL;
78 $ext_href = 'href="' . CRM_Utils_System::url('civicrm/admin/extensions', 'reset=1') . '"';
79 $dsp_href = 'href="' . CRM_Utils_System::url('civicrm/admin/setting/preferences/display', 'reset=1') . '"';
80 $blog_href = 'href="https://civicrm.org/blogs/colemanw/big-changes-wysiwyg-editing-47"';
81 switch ($editor_id) {
82 // TinyMCE
83 case 1:
84 $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));
85 break;
86
87 // Drupal/Joomla editor
88 case 3:
89 case 4:
90 $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));
91 break;
92 }
93 if ($msg) {
94 $postUpgradeMessage .= '<p>' . $msg . '</p>';
95 }
96 $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>';
97
98 $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).');
99
100 $postUpgradeMessage .= '<p>' . ts('The custom fatal error template setting has been removed.') . '</p>';
101 }
102 }
103
104 /**
105 * Upgrade function.
106 *
107 * @param string $rev
108 */
109 public function upgrade_4_7_alpha1($rev) {
110 $this->addTask('Migrate \'on behalf of\' information to module_data', 'migrateOnBehalfOfInfo');
111 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
112 $this->addTask(ts('Migrate Settings to %1', array(1 => $rev)), 'migrateSettings', $rev);
113 $this->addTask('Add Getting Started dashlet', 'addGettingStartedDashlet', $rev);
114 }
115
116 /**
117 * Upgrade function.
118 *
119 * @param string $rev
120 */
121 public function upgrade_4_7_alpha4($rev) {
122 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
123 $this->addTask(ts('Remove %1', array(1 => 'Moneris')), 'removePaymentProcessorType', 'Moneris');
124 $this->addTask('Update Smart Groups', 'fixContactTypeInSmartGroups');
125 }
126
127 /**
128 * Upgrade function.
129 *
130 * @param string $rev
131 */
132 public function upgrade_4_7_beta2($rev) {
133 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
134 $this->addTask('Delete unused file', 'deleteVersionCheckCacheFile');
135 }
136
137 /**
138 * Upgrade function.
139 *
140 * @param string $rev
141 */
142 public function upgrade_4_7_beta6($rev) {
143 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
144 $this->addTask('Disable flexible jobs extension', 'disableFlexibleJobsExtension');
145 $this->addTask('Add Index to financial_trxn trxn_id field', 'addIndexFinancialTrxnTrxnID');
146 }
147
148 /**
149 * Upgrade function.
150 *
151 * @param string $rev
152 */
153 public function upgrade_4_7_1($rev) {
154 $this->addTask('Add Index to civicrm_contribution creditnote_id field', 'addIndexContributionCreditNoteID');
155 }
156
157 /**
158 * Upgrade function.
159 *
160 * @param string $rev
161 */
162 public function upgrade_4_7_2($rev) {
163 $this->addTask('Fix Index on civicrm_financial_item combined entity_id + entity_table', 'addCombinedIndexFinancialItemEntityIDEntityType');
164 $this->addTask('enable financial account relationships for chargeback & refund', 'addRefundAndChargeBackAccountsIfNotExist');
165 $this->addTask('Add Index to civicrm_contribution.source', 'addIndexContributionSource');
166 }
167
168 /**
169 * Upgrade function.
170 *
171 * @param string $rev
172 */
173 public function upgrade_4_7_3($rev) {
174 $this->addTask('Add Index to civicrm_contribution.total_amount', 'addIndexContributionAmount');
175 }
176
177 /**
178 * CRM-16354
179 *
180 * @return int
181 */
182 public static function updateWysiwyg() {
183 $editorID = Civi::settings()->get('editor_id');
184 // Previously a numeric value indicated one of 4 wysiwyg editors shipped in core, and no value indicated 'Textarea'
185 // Now the options are "Textarea", "CKEditor", and the rest have been dropped from core.
186 $newEditor = $editorID ? "CKEditor" : "Textarea";
187 Civi::settings()->set('editor_id', $newEditor);
188
189 return $editorID;
190 }
191
192 /**
193 * Migrate any last remaining options from `civicrm_domain.config_backend` to `civicrm_setting`.
194 * Cleanup setting schema.
195 *
196 * @param CRM_Queue_TaskContext $ctx
197 * @return bool
198 */
199 public function migrateSettings(CRM_Queue_TaskContext $ctx) {
200 // Tip: If there are problems with adding the new uniqueness index, try inspecting:
201 // SELECT name, domain_id, contact_id, count(*) AS dupes FROM civicrm_setting cs GROUP BY name, domain_id, contact_id HAVING dupes > 1;
202
203 // Nav records are expendable. https://forum.civicrm.org/index.php?topic=36933.0
204 CRM_Core_DAO::executeQuery('DELETE FROM civicrm_setting WHERE contact_id IS NOT NULL AND name = "navigation"');
205
206 CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_setting DROP INDEX index_group_name');
207 CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_setting DROP COLUMN group_name');
208 CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_setting ADD UNIQUE INDEX index_domain_contact_name (domain_id, contact_id, name)');
209
210 $domainDao = CRM_Core_DAO::executeQuery('SELECT id, config_backend FROM civicrm_domain');
211 while ($domainDao->fetch()) {
212 $settings = CRM_Upgrade_Incremental_php_FourSeven::convertBackendToSettings($domainDao->id, $domainDao->config_backend);
213 CRM_Core_Error::debug_var('convertBackendToSettings', array(
214 'domainId' => $domainDao->id,
215 'backend' => $domainDao->config_backend,
216 'settings' => $settings,
217 ));
218
219 foreach ($settings as $name => $value) {
220 $rowParams = array(
221 1 => array($domainDao->id, 'Positive'),
222 2 => array($name, 'String'),
223 3 => array(serialize($value), 'String'),
224 );
225 $settingId = CRM_Core_DAO::singleValueQuery(
226 'SELECT id FROM civicrm_setting WHERE domain_id = %1 AND name = %2',
227 $rowParams);
228 if (!$settingId) {
229 CRM_Core_DAO::executeQuery(
230 'INSERT INTO civicrm_setting (domain_id, name, value, is_domain) VALUES (%1,%2,%3,1)',
231 $rowParams);
232 }
233 }
234 }
235
236 CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_domain DROP COLUMN config_backend');
237
238 return TRUE;
239 }
240
241 /**
242 * Take a config_backend blob and produce an equivalent list of settings.
243 *
244 * @param int $domainId
245 * Domain ID.
246 * @param string $config_backend
247 * Serialized blob.
248 * @return array
249 */
250 public static function convertBackendToSettings($domainId, $config_backend) {
251 if (!$config_backend) {
252 return array();
253 }
254
255 $backend = unserialize($config_backend);
256 if (!$backend) {
257 return array();
258 }
259
260 $mappings = \CRM_Core_Config_MagicMerge::getPropertyMap();
261 $settings = array();
262 foreach ($backend as $propertyName => $propertyValue) {
263 if (isset($mappings[$propertyName][0]) && preg_match('/^setting/', $mappings[$propertyName][0])) {
264 // $mapping format: $propertyName => Array(0 => $type, 1 => $setting|NULL).
265 $settingName = isset($mappings[$propertyName][1]) ? $mappings[$propertyName][1] : $propertyName;
266 $settings[$settingName] = $propertyValue;
267 }
268 }
269
270 return $settings;
271 }
272
273 /**
274 * Add Getting Started dashlet to dashboard
275 *
276 * @param \CRM_Queue_TaskContext $ctx
277 *
278 * @return bool
279 */
280 public function addGettingStartedDashlet(CRM_Queue_TaskContext $ctx) {
281 $sql = "SELECT count(*) FROM civicrm_dashboard WHERE name='getting-started'";
282 $res = CRM_Core_DAO::singleValueQuery($sql);
283 $domainId = CRM_Core_Config::domainID();
284 if ($res <= 0) {
285 $sql = "INSERT INTO `civicrm_dashboard`
286 ( `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)";
287 CRM_Core_DAO::executeQuery($sql);
288 // Add default position for Getting Started Dashlet ( left column)
289 $sql = "INSERT INTO `civicrm_dashboard_contact` (dashboard_id, contact_id, column_no, is_active)
290 SELECT (SELECT MAX(id) FROM `civicrm_dashboard`), contact_id, 0, IF (SUM(is_active) > 0, 1, 0)
291 FROM `civicrm_dashboard_contact` WHERE 1 GROUP BY contact_id";
292 CRM_Core_DAO::executeQuery($sql);
293 }
294 return TRUE;
295 }
296
297 /**
298 * Migrate on-behalf information to uf_join.module_data as on-behalf columns will be dropped
299 * on DB upgrade
300 *
301 * @param CRM_Queue_TaskContext $ctx
302 *
303 * @return bool
304 * TRUE for success
305 */
306 public static function migrateOnBehalfOfInfo(CRM_Queue_TaskContext $ctx) {
307 $domain = new CRM_Core_DAO_Domain();
308 $domain->find(TRUE);
309
310 // fetch onBehalf entry in UFJoin table
311 $ufGroupDAO = new CRM_Core_DAO_UFJoin();
312 $ufGroupDAO->module = 'OnBehalf';
313 $ufGroupDAO->find(TRUE);
314
315 $forOrgColums = array();
316 if ($domain->locales) {
317 $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
318 foreach ($locales as $locale) {
319 $forOrgColums[] = "for_organization_{$locale}";
320 }
321 }
322 else {
323 $forOrgColums[] = "for_organization";
324 }
325
326 $query = "
327 SELECT " . implode(", ", $forOrgColums) . ", uj.id as join_id, uj.uf_group_id as uf_group_id
328 FROM civicrm_contribution_page cp
329 INNER JOIN civicrm_uf_join uj ON uj.entity_id = cp.id AND uj.module = 'OnBehalf'";
330 $dao = CRM_Core_DAO::executeQuery($query, array(), TRUE, NULL, FALSE, FALSE);
331
332 if ($dao->N) {
333 while ($dao->fetch()) {
334 $onBehalfParams['on_behalf'] = array('is_for_organization' => $dao->is_for_organization);
335 if ($domain->locales) {
336 foreach ($locales as $locale) {
337 $for_organization = "for_organization_{$locale}";
338 $onBehalfParams['on_behalf'] += array(
339 $locale => array(
340 'for_organization' => $dao->$for_organization,
341 ),
342 );
343 }
344 }
345 else {
346 $onBehalfParams['on_behalf'] += array(
347 'default' => array(
348 'for_organization' => $dao->for_organization,
349 ),
350 );
351 }
352 $ufJoinParam = array(
353 'id' => $dao->join_id,
354 'module' => 'on_behalf',
355 'uf_group_id' => $dao->uf_group_id,
356 'module_data' => json_encode($onBehalfParams),
357 );
358 CRM_Core_BAO_UFJoin::create($ufJoinParam);
359 }
360 }
361
362 return TRUE;
363 }
364
365 /**
366 * CRM-11782 - Get rid of VALUE_SEPARATOR character in saved search form values
367 *
368 * @param \CRM_Queue_TaskContext $ctx
369 *
370 * @return bool
371 */
372 public function fixContactTypeInSmartGroups(CRM_Queue_TaskContext $ctx) {
373 $sep = CRM_Core_DAO::VALUE_SEPARATOR;
374 $dao = CRM_Core_DAO::executeQuery("SELECT id, form_values FROM civicrm_saved_search WHERE form_values LIKE '%$sep%'");
375 while ($dao->fetch()) {
376 $formValues = unserialize($dao->form_values);
377 if (isset($formValues['contact_type']) && is_array($formValues['contact_type'])) {
378 $newVals = array();
379 foreach ($formValues['contact_type'] as $key => $val) {
380 $newVals[str_replace($sep, '__', $key)] = is_string($val) ? str_replace($sep, '__', $val) : $val;
381 }
382 $formValues['contact_type'] = $newVals;
383 }
384 CRM_Core_DAO::executeQuery("UPDATE civicrm_saved_search SET form_values = %1 WHERE id = {$dao->id}", array(1 => array(serialize($formValues), 'String')));
385 }
386
387 return TRUE;
388 }
389
390 /**
391 * CRM-17637 - Ths file location has been moved; delete the old one
392 *
393 * @param \CRM_Queue_TaskContext $ctx
394 *
395 * @return bool
396 */
397 public function deleteVersionCheckCacheFile(CRM_Queue_TaskContext $ctx) {
398 $config = CRM_Core_Config::singleton();
399 $cacheFile = $config->uploadDir . 'version-info-cache.json';
400 if (file_exists($cacheFile)) {
401 unlink($cacheFile);
402 }
403 return TRUE;
404 }
405
406 /**
407 * CRM-17669 and CRM-17686, make scheduled jobs more flexible, disable the 4.6 extension if installed
408 *
409 * @param \CRM_Queue_TaskContext $ctx
410 *
411 * @return bool
412 */
413 public function disableFlexibleJobsExtension(CRM_Queue_TaskContext $ctx) {
414 try {
415 civicrm_api3('Extension', 'disable', array('key' => 'com.klangsoft.flexiblejobs'));
416 }
417 catch (CiviCRM_API3_Exception $e) {
418 // just ignore if the extension isn't installed
419 }
420
421 return TRUE;
422 }
423
424 /**
425 * CRM-17752 add index to civicrm_financial_trxn.trxn_id (deliberately non-unique).
426 *
427 * @param \CRM_Queue_TaskContext $ctx
428 *
429 * @return bool
430 */
431 public function addIndexFinancialTrxnTrxnID(CRM_Queue_TaskContext $ctx) {
432 $tables = array('civicrm_financial_trxn' => array('trxn_id'));
433 CRM_Core_BAO_SchemaHandler::createIndexes($tables);
434 return TRUE;
435 }
436
437 /**
438 * CRM-17882 Add index to civicrm_contribution.credit_note_id.
439 *
440 * @param \CRM_Queue_TaskContext $ctx
441 *
442 * @return bool
443 */
444 public function addIndexContributionCreditNoteID(CRM_Queue_TaskContext $ctx) {
445 $tables = array('civicrm_contribution' => array('creditnote_id'));
446 CRM_Core_BAO_SchemaHandler::createIndexes($tables);
447 return TRUE;
448 }
449
450 /**
451 * CRM-17775 Add correct index for table civicrm_financial_item.
452 *
453 * Note that the entity ID should always precede the entity_table as
454 * it is more unique. This is better for performance and does not cause fallback
455 * to no index if table it omitted.
456 *
457 * @return bool
458 */
459 public function addCombinedIndexFinancialItemEntityIDEntityType() {
460 CRM_Core_BAO_SchemaHandler::dropIndexIfExists('civicrm_financial_item', 'UI_id');
461 CRM_Core_BAO_SchemaHandler::dropIndexIfExists('civicrm_financial_item', 'IX_Entity');
462 CRM_Core_BAO_SchemaHandler::createIndexes(array(
463 'civicrm_financial_item' => array(array('entity_id', 'entity_table')),
464 ));
465 return TRUE;
466 }
467
468 /**
469 * CRM-17951 Add accounts option values for refund and chargeback.
470 *
471 * Add Chargeback contribution status and Chargeback and Contra account relationships,
472 * checking first if one exists.
473 */
474 public function addRefundAndChargeBackAccountsIfNotExist() {
475 // First we enable and edit the record for Credit contra - this exists but is disabled for most sites.
476 // Using the ensure function (below) will not enabled a disabled option (by design).
477 CRM_Core_DAO::executeQuery("UPDATE civicrm_option_value v
478 INNER JOIN civicrm_option_group g on v.option_group_id=g.id and g.name='account_relationship'
479 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'
480 WHERE v.name = 'Credit/Contra Account is';");
481
482 CRM_Core_BAO_OptionValue::ensureOptionValueExists(array(
483 'option_group_id' => 'account_relationship',
484 'name' => 'Chargeback Account is',
485 'label' => ts('Chargeback Account is'),
486 'is_active' => TRUE,
487 'component_id' => 'CiviContribute',
488 ));
489
490 CRM_Core_BAO_OptionValue::ensureOptionValueExists(array(
491 'option_group_id' => 'contribution_status',
492 'name' => 'Chargeback',
493 'label' => ts('Chargeback'),
494 'is_active' => TRUE,
495 'component_id' => 'CiviContribute',
496 ));
497 return TRUE;
498 }
499
500 /**
501 * CRM-17999 Add index to civicrm_contribution.source.
502 *
503 * @param \CRM_Queue_TaskContext $ctx
504 *
505 * @return bool
506 */
507 public function addIndexContributionSource(CRM_Queue_TaskContext $ctx) {
508 CRM_Core_BAO_SchemaHandler::createIndexes(array('civicrm_contribution' => array('source')));
509 return TRUE;
510 }
511
512 /**
513 * CRM-18124 Add index to civicrm_contribution.total_amount.
514 *
515 * Note that I made this a combined index with receive_date because the issue included
516 * both criteria and they seemed likely to be used in conjunction to me in other cases.
517 *
518 * @param \CRM_Queue_TaskContext $ctx
519 *
520 * @return bool
521 */
522 public function addIndexContributionAmount(CRM_Queue_TaskContext $ctx) {
523 CRM_Core_BAO_SchemaHandler::createIndexes(array(
524 'civicrm_contribution' => array(array('total_amount', 'receive_date')),
525 ));
526 return TRUE;
527 }
528
529 }