Merge pull request #6986 from saurabhbatra96/CRM-17120-function-caching-test
[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 *
38 * @param $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 * @return void
44 */
45 public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) {
46 if ($rev == '4.7.alpha1') {
f431d51f
J
47 // CRM-16478 Remove custom fatal error template path option
48 $config = CRM_Core_Config::singleton();
18b3bef6 49 if (!empty($config->fatalErrorTemplate) && $config->fatalErrorTemplate != 'CRM/common/fatal.tpl') {
f431d51f
J
50 $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>';
51 }
f431d51f 52 }
a40fd1ac
CW
53 if ($rev == '4.7.alpha4') {
54 // CRM-17004 Warn of Moneris removal
87a33a95
CW
55 $count = 1;
56 // Query only works in 4.3+
57 if (version_compare($currentVer, "4.3.0") > 0) {
58 $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')");
59 }
60 if ($count && !function_exists('moneris_civicrm_managed')) {
a40fd1ac
CW
61 $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>';
62 }
63 }
f431d51f
J
64 }
65
6cc25669
CW
66 /**
67 * Compute any messages which should be displayed after upgrade.
68 *
69 * @param string $postUpgradeMessage
70 * alterable.
71 * @param string $rev
72 * an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs.
73 * @return void
74 */
75 public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
6dbe2c23
CW
76 if ($rev == '4.7.alpha1') {
77 $config = CRM_Core_Config::singleton();
bf6a5362 78 // FIXME: Performing an upgrade step during postUpgrade message phase is probably bad
6dbe2c23
CW
79 $editor_id = self::updateWysiwyg();
80 $msg = NULL;
81 $ext_href = 'href="' . CRM_Utils_System::url('civicrm/admin/extensions', 'reset=1') . '"';
82 $dsp_href = 'href="' . CRM_Utils_System::url('civicrm/admin/setting/preferences/display', 'reset=1') . '"';
83 $blog_href = 'href="https://civicrm.org/blogs/colemanw/big-changes-wysiwyg-editing-47"';
84 switch ($editor_id) {
85 // TinyMCE
86 case 1:
87 $msg = ts('Your configured editor "TinyMCE" is no longer part of the main CiviCRM download. To continue using it, visit the <a %1>Manage Extensions</a> page to download and install the TinyMCE extension.', array(1 => $ext_href));
88 break;
89
90 // Drupal/Joomla editor
91 case 3:
92 case 4:
93 $msg = ts('CiviCRM no longer integrates with the "%1 Default Editor." Your wysiwyg setting has been reset to the built-in CKEditor. <a %2>Learn more...</a>', array(1 => $config->userFramework, 2 => $blog_href));
94 break;
95 }
96 if ($msg) {
97 $postUpgradeMessage .= '<p>' . $msg . '</p>';
98 }
99 $postUpgradeMessage .= '<p>' . ts('CiviCRM now includes the easy-to-use CKEditor Configurator. To customize the features and display of your wysiwyg editor, visit the <a %1>Display Preferences</a> page. <a %2>Learn more...</a>', array(1 => $dsp_href, 2 => $blog_href)) . '</p>';
dd55005c 100
101 $postUpgradeMessage .= '<br /><br />' . ts('Default version of the following System Workflow Message Templates have been modified: <ul><li>Personal Campaign Pages - Owner Notification</li></ul> If you have modified these templates, please review the new default versions and implement updates as needed to your copies (Administer > Communications > Message Templates > System Workflow Messages).');
f431d51f
J
102
103 $postUpgradeMessage .= '<p>' . ts('The custom fatal error template setting has been removed.') . '</p>';
6dbe2c23 104 }
6cc25669
CW
105 }
106
6cc25669
CW
107 /**
108 * Upgrade function.
109 *
110 * @param string $rev
111 */
112 public function upgrade_4_7_alpha1($rev) {
18a687d7 113 $this->addTask(ts('Migrate \'on behalf of\' information to module_data'), 'migrateOnBehalfOfInfo');
bf6a5362 114 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
f806379b 115 $this->addTask(ts('Migrate Settings to %1', array(1 => $rev)), 'migrateSettings', $rev);
0a95c936 116 $this->addTask(ts('Add Getting Started dashlet to %1: SQL', array(1 => $rev)), 'addGettingStartedDashlet', $rev);
a40fd1ac
CW
117 }
118
119 /**
120 * Upgrade function.
121 *
122 * @param string $rev
123 */
124 public function upgrade_4_7_alpha4($rev) {
125 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
058b8a5e 126 $this->addTask(ts('Remove %1', array(1 => 'Moneris')), 'removePaymentProcessorType', 'Moneris');
6cc25669
CW
127 }
128
129 /**
130 * CRM-16354
131 *
6dbe2c23 132 * @return int
6cc25669 133 */
6dbe2c23 134 public static function updateWysiwyg() {
6cc25669 135 $editorID = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'editor_id');
6dbe2c23
CW
136 // Previously a numeric value indicated one of 4 wysiwyg editors shipped in core, and no value indicated 'Textarea'
137 // Now the options are "Textarea", "CKEditor", and the rest have been dropped from core.
138 $newEditor = $editorID ? "CKEditor" : "Textarea";
139 CRM_Core_BAO_Setting::setItem($newEditor, CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'editor_id');
6cc25669 140
6dbe2c23 141 return $editorID;
6cc25669
CW
142 }
143
f806379b
TO
144 /**
145 * Migrate any last remaining options from `civicrm_domain.config_backend` to `civicrm_setting`.
146 * Cleanup setting schema.
147 *
148 * @param CRM_Queue_TaskContext $ctx
149 * @return bool
150 */
151 public function migrateSettings(CRM_Queue_TaskContext $ctx) {
152 CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_setting DROP INDEX index_group_name');
153 CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_setting DROP COLUMN group_name');
154 CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_setting ADD UNIQUE INDEX index_domain_contact_name (domain_id, contact_id, name)');
155
156 $domainDao = CRM_Core_DAO::executeQuery('SELECT id, config_backend FROM civicrm_domain');
157 while ($domainDao->fetch()) {
158 $settings = CRM_Upgrade_Incremental_php_FourSeven::convertBackendToSettings($domainDao->id, $domainDao->config_backend);
159 CRM_Core_Error::debug_var('convertBackendToSettings', array(
160 'domainId' => $domainDao->id,
161 'backend' => $domainDao->config_backend,
162 'settings' => $settings,
163 ));
164
165 foreach ($settings as $name => $value) {
166 $rowParams = array(
167 1 => array($domainDao->id, 'Positive'),
168 2 => array($name, 'String'),
169 3 => array(serialize($value), 'String'),
170 );
171 $settingId = CRM_Core_DAO::singleValueQuery(
172 'SELECT id FROM civicrm_setting WHERE domain_id = %1 AND name = %2',
173 $rowParams);
174 if (!$settingId) {
175 CRM_Core_DAO::executeQuery(
176 'INSERT INTO civicrm_setting (domain_id, name, value, is_domain) VALUES (%1,%2,%3,1)',
177 $rowParams);
178 }
179 }
180 }
181
182 // TODO Should drop config_backend, but keeping it during alpha/beta cycle in case we miss something.
183 // CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_domain DROP COLUMN config_backend');
184
185 return TRUE;
186 }
187
188 /**
189 * Take a config_backend blob and produce an equivalent list of settings.
190 *
191 * @param int $domainId
192 * Domain ID.
193 * @param string $config_backend
194 * Serialized blob.
195 * @return array
196 */
197 public static function convertBackendToSettings($domainId, $config_backend) {
198 if (!$config_backend) {
199 return array();
200 }
201
202 $backend = unserialize($config_backend);
203 if (!$backend) {
204 return array();
205 }
206
207 $mappings = \CRM_Core_Config_MagicMerge::getPropertyMap();
208 $settings = array();
209 foreach ($backend as $propertyName => $propertyValue) {
210 if (isset($mappings[$propertyName][0]) && preg_match('/^setting/', $mappings[$propertyName][0])) {
211 // $mapping format: $propertyName => Array(0 => $type, 1 => $setting|NULL).
212 $settingName = isset($mappings[$propertyName][1]) ? $mappings[$propertyName][1] : $propertyName;
213 $settings[$settingName] = $propertyValue;
214 }
215 }
216
217 return $settings;
218 }
219
0a95c936 220 /**
221 * Add Getting Started dashlet to dashboard
222 *
223 * @param \CRM_Queue_TaskContext $ctx
224 *
225 * @return bool
226 */
227 public function addGettingStartedDashlet(CRM_Queue_TaskContext $ctx) {
e1674273 228 $sql = "SELECT count(*) FROM civicrm_dashboard WHERE name='gettingStarted'";
229 $res = CRM_Core_DAO::singleValueQuery($sql);
230 $domainId = CRM_Core_Config::domainID();
231 if ($res <= 0) {
232 $sql = "INSERT INTO `civicrm_dashboard`
a8b704c5 233 ( `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 234 CRM_Core_DAO::executeQuery($sql);
235 // Add default position for Getting Started Dashlet ( left column)
236 $sql = "INSERT INTO `civicrm_dashboard_contact` (dashboard_id, contact_id, column_no, is_active)
237SELECT (SELECT MAX(id) FROM `civicrm_dashboard`), contact_id, 0, IF (SUM(is_active) > 0, 1, 0)
238FROM `civicrm_dashboard_contact` WHERE 1 GROUP BY contact_id";
239 CRM_Core_DAO::executeQuery($sql);
240 }
0a95c936 241 return TRUE;
e1674273 242 }
4175ee03 243
6b1e1a2c 244 /**
245 * Migrate on-behalf information to uf_join.module_data as on-behalf columns will be dropped
246 * on DB upgrade
247 *
248 * @param CRM_Queue_TaskContext $ctx
249 *
250 * @return bool
251 * TRUE for success
252 */
253 public static function migrateOnBehalfOfInfo(CRM_Queue_TaskContext $ctx) {
d3e92c88 254 $domain = new CRM_Core_DAO_Domain();
255 $domain->find(TRUE);
6b1e1a2c 256
d3e92c88 257 // fetch onBehalf entry in UFJoin table
6b1e1a2c 258 $ufGroupDAO = new CRM_Core_DAO_UFJoin();
259 $ufGroupDAO->module = 'OnBehalf';
260 $ufGroupDAO->find(TRUE);
261
d3e92c88 262 $forOrgColums = array();
263 if ($domain->locales) {
264 $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
265 foreach ($locales as $locale) {
266 $forOrgColums[] = "for_organization_{$locale}";
267 }
268 }
269 else {
270 $forOrgColums[] = "for_organization";
271 }
272
273 $query = "
274 SELECT " . implode(", ", $forOrgColums) . ", uj.id as join_id, uj.uf_group_id as uf_group_id
275 FROM civicrm_contribution_page cp
276 INNER JOIN civicrm_uf_join uj ON uj.entity_id = cp.id AND uj.module = 'OnBehalf'";
277 $dao = CRM_Core_DAO::executeQuery($query, array(), TRUE, NULL, FALSE, FALSE);
6b1e1a2c 278
279 if ($dao->N) {
6b1e1a2c 280 while ($dao->fetch()) {
281 $onBehalfParams['on_behalf'] = array('is_for_organization' => $dao->is_for_organization);
282 if ($domain->locales) {
6b1e1a2c 283 foreach ($locales as $locale) {
284 $for_organization = "for_organization_{$locale}";
285 $onBehalfParams['on_behalf'] += array(
286 $locale => array(
287 'for_organization' => $dao->$for_organization,
288 ),
289 );
290 }
291 }
292 else {
293 $onBehalfParams['on_behalf'] += array(
294 'default' => array(
295 'for_organization' => $dao->for_organization,
296 ),
297 );
298 }
299 $ufJoinParam = array(
300 'id' => $dao->join_id,
301 'module' => 'on_behalf',
d3e92c88 302 'uf_group_id' => $dao->uf_group_id,
6b1e1a2c 303 'module_data' => json_encode($onBehalfParams),
304 );
305 CRM_Core_BAO_UFJoin::create($ufJoinParam);
306 }
307 }
308
309 return TRUE;
310 }
cb804cd9 311
6cc25669 312}