INFRA-132 - CRM/Upgrade - phpcbf (plus fixup)
[civicrm-core.git] / CRM / Upgrade / Incremental / php / FourOne.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License along with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35 class CRM_Upgrade_Incremental_php_FourOne {
36 // This was changed in 4.3 so we define it locally for compatability with older dbs
37 const NAVIGATION_NAME = "Navigation Menu";
38
39 /**
40 * @param $errors
41 *
42 * @return bool
43 */
44 public function verifyPreDBstate(&$errors) {
45 $config = CRM_Core_Config::singleton();
46 if (in_array('CiviCase', $config->enableComponents)) {
47 if (!CRM_Core_DAO::checkTriggerViewPermission(TRUE, FALSE)) {
48 $errors[] = ts('CiviCase now requires CREATE VIEW and DROP VIEW permissions for the database user.');
49 return FALSE;
50 }
51 }
52
53 return TRUE;
54 }
55
56 /**
57 * Compute any messages which should be displayed after upgrade
58 *
59 * @param $postUpgradeMessage
60 * String, alterable.
61 * @param $rev
62 * String, an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs.
63 *
64 * @return void
65 */
66 public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
67 if ($rev == '4.1.alpha1') {
68 $postUpgradeMessage .= '<br />' .
69 ts('WARNING! CiviCRM 4.1 introduces an improved way of handling cron jobs. However the new method is NOT backwards compatible. <strong>Please notify your system administrator that all CiviCRM related cron jobs will cease to work, and will need to be re-configured (this includes sending CiviMail mailings, updating membership statuses, etc.).</strong> Refer to the <a href="%1">online documentation</a> for detailed instructions.', array(1 => 'http://wiki.civicrm.org/confluence/display/CRMDOC41/Managing+Scheduled+Jobs'));
70 $postUpgradeMessage .= '<br />' .
71 ts('The CiviCRM Administration menu structure has been re-organized during this upgrade to make it easier to find things and reduce the number of keystrokes. If you have customized this portion of the navigation menu - you should take a few minutes to review the changes. You may need to reimplement or move your customizations.');
72
73 $postUpgradeMessage .= '<br />' .
74 ts('Yahoo recently discontinued their geocoding and mapping API service. If you previously used Yahoo, you will need to select and configure an alternate service in order to continue using geocoding/mapping tools.');
75
76 $postUpgradeMessage .= '<br />' .
77 ts('We have integrated KCFinder with CKEditor and TinyMCE, which enables user to upload images. Note that all the images uploaded using KCFinder will be public.');
78 }
79 }
80
81 /**
82 * @param $rev
83 */
84 public function upgrade_4_1_alpha1($rev) {
85 $config = CRM_Core_Config::singleton();
86 if (in_array('CiviCase', $config->enableComponents)) {
87 if (!CRM_Case_BAO_Case::createCaseViews()) {
88 $template = CRM_Core_Smarty::singleton();
89 $afterUpgradeMessage = '';
90 if ($afterUpgradeMessage = $template->get_template_vars('afterUpgradeMessage')) {
91 $afterUpgradeMessage .= "<br/><br/>";
92 }
93 $afterUpgradeMessage .=
94 '<div class="crm-upgrade-case-views-error" style="background-color: #E43D2B; padding: 10px;">' .
95 ts("There was a problem creating CiviCase database views. Please create the following views manually before using CiviCase:");
96 $afterUpgradeMessage .=
97 '<div class="crm-upgrade-case-views-query"><div>' .
98 CRM_Case_BAO_Case::createCaseViewsQuery('upcoming') . '</div><div>' .
99 CRM_Case_BAO_Case::createCaseViewsQuery('recent') . '</div>' .
100 '</div></div>';
101 $template->assign('afterUpgradeMessage', $afterUpgradeMessage);
102 }
103 }
104
105 $upgrade = new CRM_Upgrade_Form();
106 $upgrade->processSQL($rev);
107
108 $this->transferPreferencesToSettings();
109 $this->createNewSettings();
110
111 // now modify the config so that the directories are now stored in the settings table
112 // CRM-8780
113 $params = array();
114 CRM_Core_BAO_ConfigSetting::add($params);
115
116 // also reset navigation
117 CRM_Core_BAO_Navigation::resetNavigation();
118 }
119
120 public function transferPreferencesToSettings() {
121 // first transfer system preferences
122 $domainColumnNames = array(
123 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME => array(
124 'contact_view_options',
125 'contact_edit_options',
126 'advanced_search_options',
127 'user_dashboard_options',
128 'address_options',
129 'address_format',
130 'mailing_format',
131 'display_name_format',
132 'sort_name_format',
133 'editor_id',
134 'contact_autocomplete_options',
135 ),
136 CRM_Core_BAO_Setting::ADDRESS_STANDARDIZATION_PREFERENCES_NAME => array(
137 'address_standardization_provider',
138 'address_standardization_userid',
139 'address_standardization_url',
140 ),
141 CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME => array(
142 'mailing_backend',
143 ),
144 );
145
146 $userColumnNames = array(
147 self::NAVIGATION_NAME => array(
148 'navigation',
149 ),
150 );
151
152 $sql = "
153 SELECT *
154 FROM civicrm_preferences
155 WHERE domain_id = %1
156 ";
157 $params = array(1 => array(CRM_Core_Config::domainID(), 'Integer'));
158 $dao = CRM_Core_DAO::executeQuery($sql, $params);
159
160 $domainID = CRM_Core_Config::domainID();
161 $createdDate = date('YmdHis');
162
163 while ($dao->fetch()) {
164 if ($dao->is_domain) {
165 $values = array();
166 foreach ($domainColumnNames as $groupName => $settingNames) {
167 foreach ($settingNames as $settingName) {
168 if (empty($dao->$settingName)) {
169 $value = NULL;
170 }
171 else {
172 if ($settingName == 'mailing_backend') {
173 $value = $dao->$settingName;
174 }
175 else {
176 $value = serialize($dao->$settingName);
177 }
178 }
179
180 if ($value) {
181 $value = addslashes($value);
182 }
183 $value = $value ? "'{$value}'" : 'null';
184 $values[] = " ('{$groupName}','{$settingName}', {$value}, {$domainID}, null, 1, '{$createdDate}', null )";
185 }
186 }
187 }
188 else {
189 // this is a user setting
190 foreach ($userColumnNames as $groupName => $settingNames) {
191 foreach ($settingNames as $settingName) {
192 $value = empty($dao->$settingName) ? NULL : serialize($dao->$settingName);
193
194 if ($value) {
195 $value = addslashes($value);
196 }
197 $value = $value ? "'{$value}'" : 'null';
198 $values[] = " ('{$groupName}', '{$settingName}', {$value}, {$domainID}, {$dao->contact_id}, 0, '{$createdDate}', null )";
199 }
200 }
201 }
202 }
203
204 $sql = "
205 INSERT INTO civicrm_setting( group_name, name, value, domain_id, contact_id, is_domain, created_date, created_id )
206 VALUES
207 ";
208
209 $sql .= implode(",\n", $values);
210 CRM_Core_DAO::executeQuery($sql);
211
212 // now drop the civicrm_preferences table
213 $sql = "DROP TABLE civicrm_preferences";
214 CRM_Core_DAO::executeQuery($sql);
215 }
216
217 public function createNewSettings() {
218 $domainColumns = array(
219 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME => array(
220 array('contact_ajax_check_similar', 1),
221 array('activity_assignee_notification', 1),
222 ),
223 CRM_Core_BAO_Setting::CAMPAIGN_PREFERENCES_NAME => array(
224 array('tag_unconfirmed', 'Unconfirmed'),
225 array('petition_contacts', 'Petition Contacts'),
226 ),
227 CRM_Core_BAO_Setting::EVENT_PREFERENCES_NAME => array(
228 array('enable_cart', 0),
229 ),
230 CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME => array(
231 array('profile_double_optin', 1),
232 array('profile_add_to_group_double_optin', 0),
233 array('track_civimail_replies', 0),
234 array('civimail_workflow', 0),
235 array('civimail_server_wide_lock', 0),
236 ),
237 CRM_Core_BAO_Setting::MEMBER_PREFERENCES_NAME => array(
238 array('default_renewal_contribution_page', NULL),
239 ),
240 CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME => array(
241 array('is_enabled', 0),
242 array('uniq_email_per_site', 0),
243 array('domain_group_id', 0),
244 array('event_price_set_domain_id', 0),
245 ),
246 CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME => array(
247 array('uploadDir', NULL),
248 array('imageUploadDir', NULL),
249 array('customFileUploadDir', NULL),
250 array('customTemplateDir', NULL),
251 array('customPHPPathDir', NULL),
252 array('extensionsDir', NULL),
253 ),
254 CRM_Core_BAO_Setting::URL_PREFERENCES_NAME => array(
255 array('userFrameworkResourceURL', NULL),
256 array('imageUploadURL', NULL),
257 array('customCSSURL', NULL),
258 ),
259 );
260
261 $domainID = CRM_Core_Config::domainID();
262 $createdDate = date('YmdHis');
263
264 $dbSettings = array();
265 self::retrieveDirectoryAndURLPaths($dbSettings);
266
267 foreach ($domainColumns as $groupName => $settings) {
268 foreach ($settings as $setting) {
269
270 if (isset($dbSettings[$groupName][$setting[0]]) &&
271 !empty($dbSettings[$groupName][$setting[0]])
272 ) {
273 $setting[1] = $dbSettings[$groupName][$setting[0]];
274 }
275
276 $value = $setting[1] === NULL ? NULL : serialize($setting[1]);
277
278 if ($value) {
279 $value = addslashes($value);
280 }
281
282 $value = $value ? "'{$value}'" : 'null';
283 $values[] = "( '{$groupName}', '{$setting[0]}', {$value}, {$domainID}, null, 0, '{$createdDate}', null )";
284 }
285 }
286 $sql = "
287 INSERT INTO civicrm_setting( group_name, name, value, domain_id, contact_id, is_domain, created_date, created_id )
288 VALUES
289 ";
290 $sql .= implode(",\n", $values);
291 CRM_Core_DAO::executeQuery($sql);
292 }
293
294 /**
295 * @param array $params
296 */
297 public static function retrieveDirectoryAndURLPaths(&$params) {
298
299 $sql = "
300 SELECT v.name as valueName, v.value, g.name as optionName
301 FROM civicrm_option_value v,
302 civicrm_option_group g
303 WHERE ( g.name = 'directory_preferences'
304 OR g.name = 'url_preferences' )
305 AND v.option_group_id = g.id
306 AND v.is_active = 1
307 ";
308 $dao = CRM_Core_DAO::executeQuery($sql);
309 while ($dao->fetch()) {
310 if (!$dao->value) {
311 continue;
312 }
313
314 $groupName = CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME;
315 if ($dao->optionName == 'url_preferences') {
316 $groupName = CRM_Core_BAO_Setting::URL_PREFERENCES_NAME;
317 }
318 $params[$groupName][$dao->valueName] = $dao->value;
319 }
320 }
321
322 /**
323 * @param $rev
324 */
325 public function upgrade_4_1_alpha2($rev) {
326 $dao = new CRM_Core_DAO_Setting();
327 $dao->group_name = 'Directory Preferences';
328 $dao->name = 'customTemplateDir';
329 if (!($dao->find(TRUE))) {
330 $dao->domain_id = CRM_Core_Config::domainID();
331 $dao->created_date = date('YmdHis');
332 $dao->is_domain = 0;
333 $dao->save();
334 }
335 $dao->free();
336
337 // Do the regular upgrade
338 $upgrade = new CRM_Upgrade_Form;
339 $upgrade->processSQL($rev);
340 }
341
342 /**
343 * @param $rev
344 */
345 public function upgrade_4_1_beta1($rev) {
346 //CRM-9311
347 $groupNames = array('directory_preferences', 'url_preferences');
348 foreach ($groupNames as $groupName) {
349 CRM_Core_OptionGroup::deleteAssoc($groupName);
350 }
351
352 $domainCols = array(
353 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME => array(
354 'contact_ajax_check_similar',
355 'activity_assignee_notification',
356 ),
357 CRM_Core_BAO_Setting::CAMPAIGN_PREFERENCES_NAME => array(
358 'tag_unconfirmed',
359 'petition_contacts',
360 ),
361 CRM_Core_BAO_Setting::EVENT_PREFERENCES_NAME => array(
362 'enable_cart',
363 ),
364 CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME => array(
365 'profile_double_optin',
366 'profile_add_to_group_double_optin',
367 'track_civimail_replies',
368 'civimail_workflow',
369 'civimail_server_wide_lock',
370 ),
371 CRM_Core_BAO_Setting::MEMBER_PREFERENCES_NAME => array(
372 'default_renewal_contribution_page',
373 ),
374 CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME => array(
375 'is_enabled',
376 'uniq_email_per_site',
377 'domain_group_id',
378 'event_price_set_domain_id',
379 ),
380 CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME => array(
381 'uploadDir',
382 'imageUploadDir',
383 'customFileUploadDir',
384 'customTemplateDir',
385 'customPHPPathDir',
386 'extensionsDir',
387 ),
388 CRM_Core_BAO_Setting::URL_PREFERENCES_NAME => array(
389 'userFrameworkResourceURL',
390 'imageUploadURL',
391 'customCSSURL',
392 ),
393 );
394
395 $arrGroupNames = array_keys($domainCols);
396 $groupNames = implode("','", $arrGroupNames);
397 $arrNames = array();
398 foreach ($domainCols as $groupName => $names) {
399 $arrNames[] = implode("','", $names);
400 }
401 $name = implode("','", $arrNames);
402
403 $sql = "
404 update civicrm_setting set is_domain = 1 where is_domain = 0 and group_name in ( '{$groupNames}' ) and name in ('{$name}')";
405
406 CRM_Core_DAO::executeQuery($sql);
407
408 $upgrade = new CRM_Upgrade_Form();
409 $upgrade->assign('addWightForActivity', !(CRM_Core_DAO::checkFieldExists('civicrm_activity', 'weight')));
410 $upgrade->processSQL($rev);
411 }
412
413 /**
414 * @param $rev
415 */
416 public function upgrade_4_1_1($rev) {
417 $upgrade = new CRM_Upgrade_Form();
418 $upgrade->assign('addDedupeEmail', !(CRM_Core_DAO::checkFieldExists('civicrm_mailing', 'dedupe_email')));
419
420 $sql = "SELECT id FROM civicrm_worldregion LIMIT 1";
421 $upgrade->assign('worldRegionEmpty', !CRM_Core_DAO::singleValueQuery($sql));
422
423 $upgrade->processSQL($rev);
424 }
425
426 /**
427 * @return string
428 */
429 public function getTemplateMessage() {
430 return "Blah";
431 }
432 }