Merge branch '4.6' into master
[civicrm-core.git] / CRM / Upgrade / Incremental / php / FourSeven.php
CommitLineData
6cc25669
CW
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/**
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 }
f431d51f
J
62 }
63
6cc25669
CW
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.
6cc25669
CW
71 */
72 public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
6dbe2c23
CW
73 if ($rev == '4.7.alpha1') {
74 $config = CRM_Core_Config::singleton();
bf6a5362 75 // FIXME: Performing an upgrade step during postUpgrade message phase is probably bad
6dbe2c23
CW
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>';
dd55005c 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).');
f431d51f
J
99
100 $postUpgradeMessage .= '<p>' . ts('The custom fatal error template setting has been removed.') . '</p>';
6dbe2c23 101 }
6cc25669
CW
102 }
103
6cc25669
CW
104 /**
105 * Upgrade function.
106 *
107 * @param string $rev
108 */
109 public function upgrade_4_7_alpha1($rev) {
18a687d7 110 $this->addTask(ts('Migrate \'on behalf of\' information to module_data'), 'migrateOnBehalfOfInfo');
bf6a5362 111 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
f806379b 112 $this->addTask(ts('Migrate Settings to %1', array(1 => $rev)), 'migrateSettings', $rev);
0a95c936 113 $this->addTask(ts('Add Getting Started dashlet to %1: SQL', array(1 => $rev)), 'addGettingStartedDashlet', $rev);
a40fd1ac
CW
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);
058b8a5e 123 $this->addTask(ts('Remove %1', array(1 => 'Moneris')), 'removePaymentProcessorType', 'Moneris');
b2222b9f 124 $this->addTask('Update Smart Groups', 'fixContactTypeInSmartGroups');
6cc25669
CW
125 }
126
127 /**
128 * CRM-16354
129 *
6dbe2c23 130 * @return int
6cc25669 131 */
6dbe2c23 132 public static function updateWysiwyg() {
6cc25669 133 $editorID = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'editor_id');
6dbe2c23
CW
134 // Previously a numeric value indicated one of 4 wysiwyg editors shipped in core, and no value indicated 'Textarea'
135 // Now the options are "Textarea", "CKEditor", and the rest have been dropped from core.
136 $newEditor = $editorID ? "CKEditor" : "Textarea";
137 CRM_Core_BAO_Setting::setItem($newEditor, CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'editor_id');
6cc25669 138
6dbe2c23 139 return $editorID;
6cc25669
CW
140 }
141
f806379b
TO
142 /**
143 * Migrate any last remaining options from `civicrm_domain.config_backend` to `civicrm_setting`.
144 * Cleanup setting schema.
145 *
146 * @param CRM_Queue_TaskContext $ctx
147 * @return bool
148 */
149 public function migrateSettings(CRM_Queue_TaskContext $ctx) {
150 CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_setting DROP INDEX index_group_name');
151 CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_setting DROP COLUMN group_name');
152 CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_setting ADD UNIQUE INDEX index_domain_contact_name (domain_id, contact_id, name)');
153
154 $domainDao = CRM_Core_DAO::executeQuery('SELECT id, config_backend FROM civicrm_domain');
155 while ($domainDao->fetch()) {
156 $settings = CRM_Upgrade_Incremental_php_FourSeven::convertBackendToSettings($domainDao->id, $domainDao->config_backend);
157 CRM_Core_Error::debug_var('convertBackendToSettings', array(
158 'domainId' => $domainDao->id,
159 'backend' => $domainDao->config_backend,
160 'settings' => $settings,
161 ));
162
163 foreach ($settings as $name => $value) {
164 $rowParams = array(
165 1 => array($domainDao->id, 'Positive'),
166 2 => array($name, 'String'),
167 3 => array(serialize($value), 'String'),
168 );
169 $settingId = CRM_Core_DAO::singleValueQuery(
170 'SELECT id FROM civicrm_setting WHERE domain_id = %1 AND name = %2',
171 $rowParams);
172 if (!$settingId) {
173 CRM_Core_DAO::executeQuery(
174 'INSERT INTO civicrm_setting (domain_id, name, value, is_domain) VALUES (%1,%2,%3,1)',
175 $rowParams);
176 }
177 }
178 }
179
180 // TODO Should drop config_backend, but keeping it during alpha/beta cycle in case we miss something.
181 // CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_domain DROP COLUMN config_backend');
182
183 return TRUE;
184 }
185
186 /**
187 * Take a config_backend blob and produce an equivalent list of settings.
188 *
189 * @param int $domainId
190 * Domain ID.
191 * @param string $config_backend
192 * Serialized blob.
193 * @return array
194 */
195 public static function convertBackendToSettings($domainId, $config_backend) {
196 if (!$config_backend) {
197 return array();
198 }
199
200 $backend = unserialize($config_backend);
201 if (!$backend) {
202 return array();
203 }
204
205 $mappings = \CRM_Core_Config_MagicMerge::getPropertyMap();
206 $settings = array();
207 foreach ($backend as $propertyName => $propertyValue) {
208 if (isset($mappings[$propertyName][0]) && preg_match('/^setting/', $mappings[$propertyName][0])) {
209 // $mapping format: $propertyName => Array(0 => $type, 1 => $setting|NULL).
210 $settingName = isset($mappings[$propertyName][1]) ? $mappings[$propertyName][1] : $propertyName;
211 $settings[$settingName] = $propertyValue;
212 }
213 }
214
215 return $settings;
216 }
217
0a95c936 218 /**
219 * Add Getting Started dashlet to dashboard
220 *
221 * @param \CRM_Queue_TaskContext $ctx
222 *
223 * @return bool
224 */
225 public function addGettingStartedDashlet(CRM_Queue_TaskContext $ctx) {
e1674273 226 $sql = "SELECT count(*) FROM civicrm_dashboard WHERE name='gettingStarted'";
227 $res = CRM_Core_DAO::singleValueQuery($sql);
228 $domainId = CRM_Core_Config::domainID();
229 if ($res <= 0) {
230 $sql = "INSERT INTO `civicrm_dashboard`
a8b704c5 231 ( `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 232 CRM_Core_DAO::executeQuery($sql);
233 // Add default position for Getting Started Dashlet ( left column)
234 $sql = "INSERT INTO `civicrm_dashboard_contact` (dashboard_id, contact_id, column_no, is_active)
235SELECT (SELECT MAX(id) FROM `civicrm_dashboard`), contact_id, 0, IF (SUM(is_active) > 0, 1, 0)
236FROM `civicrm_dashboard_contact` WHERE 1 GROUP BY contact_id";
237 CRM_Core_DAO::executeQuery($sql);
238 }
0a95c936 239 return TRUE;
e1674273 240 }
4175ee03 241
6b1e1a2c 242 /**
243 * Migrate on-behalf information to uf_join.module_data as on-behalf columns will be dropped
244 * on DB upgrade
245 *
246 * @param CRM_Queue_TaskContext $ctx
247 *
248 * @return bool
249 * TRUE for success
250 */
251 public static function migrateOnBehalfOfInfo(CRM_Queue_TaskContext $ctx) {
d3e92c88 252 $domain = new CRM_Core_DAO_Domain();
253 $domain->find(TRUE);
6b1e1a2c 254
d3e92c88 255 // fetch onBehalf entry in UFJoin table
6b1e1a2c 256 $ufGroupDAO = new CRM_Core_DAO_UFJoin();
257 $ufGroupDAO->module = 'OnBehalf';
258 $ufGroupDAO->find(TRUE);
259
d3e92c88 260 $forOrgColums = array();
261 if ($domain->locales) {
262 $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
263 foreach ($locales as $locale) {
264 $forOrgColums[] = "for_organization_{$locale}";
265 }
266 }
267 else {
268 $forOrgColums[] = "for_organization";
269 }
270
271 $query = "
272 SELECT " . implode(", ", $forOrgColums) . ", uj.id as join_id, uj.uf_group_id as uf_group_id
273 FROM civicrm_contribution_page cp
274 INNER JOIN civicrm_uf_join uj ON uj.entity_id = cp.id AND uj.module = 'OnBehalf'";
275 $dao = CRM_Core_DAO::executeQuery($query, array(), TRUE, NULL, FALSE, FALSE);
6b1e1a2c 276
277 if ($dao->N) {
6b1e1a2c 278 while ($dao->fetch()) {
279 $onBehalfParams['on_behalf'] = array('is_for_organization' => $dao->is_for_organization);
280 if ($domain->locales) {
6b1e1a2c 281 foreach ($locales as $locale) {
282 $for_organization = "for_organization_{$locale}";
283 $onBehalfParams['on_behalf'] += array(
284 $locale => array(
285 'for_organization' => $dao->$for_organization,
286 ),
287 );
288 }
289 }
290 else {
291 $onBehalfParams['on_behalf'] += array(
292 'default' => array(
293 'for_organization' => $dao->for_organization,
294 ),
295 );
296 }
297 $ufJoinParam = array(
298 'id' => $dao->join_id,
299 'module' => 'on_behalf',
d3e92c88 300 'uf_group_id' => $dao->uf_group_id,
6b1e1a2c 301 'module_data' => json_encode($onBehalfParams),
302 );
303 CRM_Core_BAO_UFJoin::create($ufJoinParam);
304 }
305 }
306
307 return TRUE;
308 }
cb804cd9 309
b2222b9f
CW
310 /**
311 * CRM-11782 - Get rid of VALUE_SEPARATOR character in saved search form values
312 *
313 * @param \CRM_Queue_TaskContext $ctx
314 *
315 * @return bool
316 */
317 public function fixContactTypeInSmartGroups(CRM_Queue_TaskContext $ctx) {
318 $sep = CRM_Core_DAO::VALUE_SEPARATOR;
319 $dao = CRM_Core_DAO::executeQuery("SELECT id, form_values FROM civicrm_saved_search WHERE form_values LIKE '%$sep%'");
320 while ($dao->fetch()) {
321 $formValues = unserialize($dao->form_values);
322 if (isset($formValues['contact_type']) && is_array($formValues['contact_type'])) {
323 $newVals = array();
324 foreach ($formValues['contact_type'] as $key => $val) {
325 $newVals[str_replace($sep, '__', $key)] = is_string($val) ? str_replace($sep, '__', $val) : $val;
326 }
327 $formValues['contact_type'] = $newVals;
328 }
329 CRM_Core_DAO::executeQuery("UPDATE civicrm_saved_search SET form_values = %1 WHERE id = {$dao->id}", array(1 => array(serialize($formValues), 'String')));
330 }
331
332 return TRUE;
333 }
334
6cc25669 335}