ba128c4833e6a0a98b861f3af7969d4f5c5e5392
[civicrm-core.git] / CRM / Upgrade / Incremental / php / FourOne.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 *
29 * @package CRM
30 * @copyright CiviCRM LLC (c) 2004-2015
31 * $Id$
32 *
33 */
34 class CRM_Upgrade_Incremental_php_FourOne {
35 // This was changed in 4.3 so we define it locally for compatability with older dbs
36 const NAVIGATION_NAME = "Navigation Menu";
37
38 /**
39 * @param $errors
40 *
41 * @return bool
42 */
43 public function verifyPreDBstate(&$errors) {
44 $config = CRM_Core_Config::singleton();
45 if (in_array('CiviCase', $config->enableComponents)) {
46 if (!CRM_Core_DAO::checkTriggerViewPermission(TRUE, FALSE)) {
47 $errors[] = ts('CiviCase now requires CREATE VIEW and DROP VIEW permissions for the database user.');
48 return FALSE;
49 }
50 }
51
52 return TRUE;
53 }
54
55 /**
56 * Compute any messages which should be displayed after upgrade.
57 *
58 * @param string $postUpgradeMessage
59 * alterable.
60 * @param string $rev
61 * an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs.
62 *
63 * @return void
64 */
65 public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
66 if ($rev == '4.1.alpha1') {
67 $postUpgradeMessage .= '<br />' .
68 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'));
69 $postUpgradeMessage .= '<br />' .
70 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.');
71
72 $postUpgradeMessage .= '<br />' .
73 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.');
74
75 $postUpgradeMessage .= '<br />' .
76 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.');
77 }
78 }
79
80 /**
81 * @param $rev
82 */
83 public function upgrade_4_1_alpha1($rev) {
84 $config = CRM_Core_Config::singleton();
85 if (in_array('CiviCase', $config->enableComponents)) {
86 if (!CRM_Case_BAO_Case::createCaseViews()) {
87 $template = CRM_Core_Smarty::singleton();
88 $afterUpgradeMessage = '';
89 if ($afterUpgradeMessage = $template->get_template_vars('afterUpgradeMessage')) {
90 $afterUpgradeMessage .= "<br/><br/>";
91 }
92 $afterUpgradeMessage .=
93 '<div class="crm-upgrade-case-views-error" style="background-color: #E43D2B; padding: 10px;">' .
94 ts("There was a problem creating CiviCase database views. Please create the following views manually before using CiviCase:");
95 $afterUpgradeMessage .=
96 '<div class="crm-upgrade-case-views-query"><div>' .
97 CRM_Case_BAO_Case::createCaseViewsQuery('upcoming') . '</div><div>' .
98 CRM_Case_BAO_Case::createCaseViewsQuery('recent') . '</div>' .
99 '</div></div>';
100 $template->assign('afterUpgradeMessage', $afterUpgradeMessage);
101 }
102 }
103
104 $upgrade = new CRM_Upgrade_Form();
105 $upgrade->processSQL($rev);
106
107 $this->transferPreferencesToSettings();
108 $this->createNewSettings();
109
110 // now modify the config so that the directories are now stored in the settings table
111 // CRM-8780
112 $params = array();
113 CRM_Core_BAO_ConfigSetting::add($params);
114
115 // also reset navigation
116 CRM_Core_BAO_Navigation::resetNavigation();
117 }
118
119 public function transferPreferencesToSettings() {
120 // first transfer system preferences
121 $domainColumnNames = array(
122 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME => array(
123 'contact_view_options',
124 'contact_edit_options',
125 'advanced_search_options',
126 'user_dashboard_options',
127 'address_options',
128 'address_format',
129 'mailing_format',
130 'display_name_format',
131 'sort_name_format',
132 'editor_id',
133 'contact_autocomplete_options',
134 ),
135 CRM_Core_BAO_Setting::ADDRESS_STANDARDIZATION_PREFERENCES_NAME => array(
136 'address_standardization_provider',
137 'address_standardization_userid',
138 'address_standardization_url',
139 ),
140 CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME => array(
141 'mailing_backend',
142 ),
143 );
144
145 $userColumnNames = array(
146 self::NAVIGATION_NAME => array(
147 'navigation',
148 ),
149 );
150
151 $sql = "
152 SELECT *
153 FROM civicrm_preferences
154 WHERE domain_id = %1
155 ";
156 $params = array(1 => array(CRM_Core_Config::domainID(), 'Integer'));
157 $dao = CRM_Core_DAO::executeQuery($sql, $params);
158
159 $domainID = CRM_Core_Config::domainID();
160 $createdDate = date('YmdHis');
161
162 while ($dao->fetch()) {
163 if ($dao->is_domain) {
164 $values = array();
165 foreach ($domainColumnNames as $groupName => $settingNames) {
166 foreach ($settingNames as $settingName) {
167 if (empty($dao->$settingName)) {
168 $value = NULL;
169 }
170 else {
171 if ($settingName == 'mailing_backend') {
172 $value = $dao->$settingName;
173 }
174 else {
175 $value = serialize($dao->$settingName);
176 }
177 }
178
179 if ($value) {
180 $value = addslashes($value);
181 }
182 $value = $value ? "'{$value}'" : 'null';
183 $values[] = " ('{$groupName}','{$settingName}', {$value}, {$domainID}, null, 1, '{$createdDate}', null )";
184 }
185 }
186 }
187 else {
188 // this is a user setting
189 foreach ($userColumnNames as $groupName => $settingNames) {
190 foreach ($settingNames as $settingName) {
191 $value = empty($dao->$settingName) ? NULL : serialize($dao->$settingName);
192
193 if ($value) {
194 $value = addslashes($value);
195 }
196 $value = $value ? "'{$value}'" : 'null';
197 $values[] = " ('{$groupName}', '{$settingName}', {$value}, {$domainID}, {$dao->contact_id}, 0, '{$createdDate}', null )";
198 }
199 }
200 }
201 }
202
203 $sql = "
204 INSERT INTO civicrm_setting( group_name, name, value, domain_id, contact_id, is_domain, created_date, created_id )
205 VALUES
206 ";
207
208 $sql .= implode(",\n", $values);
209 CRM_Core_DAO::executeQuery($sql);
210
211 // now drop the civicrm_preferences table
212 $sql = "DROP TABLE civicrm_preferences";
213 CRM_Core_DAO::executeQuery($sql);
214 }
215
216 public function createNewSettings() {
217 $domainColumns = array(
218 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME => array(
219 array('contact_ajax_check_similar', 1),
220 array('activity_assignee_notification', 1),
221 ),
222 CRM_Core_BAO_Setting::CAMPAIGN_PREFERENCES_NAME => array(
223 array('tag_unconfirmed', 'Unconfirmed'),
224 array('petition_contacts', 'Petition Contacts'),
225 ),
226 CRM_Core_BAO_Setting::EVENT_PREFERENCES_NAME => array(
227 array('enable_cart', 0),
228 ),
229 CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME => array(
230 array('profile_double_optin', 1),
231 array('profile_add_to_group_double_optin', 0),
232 array('track_civimail_replies', 0),
233 array('civimail_workflow', 0),
234 array('civimail_server_wide_lock', 0),
235 ),
236 CRM_Core_BAO_Setting::MEMBER_PREFERENCES_NAME => array(
237 array('default_renewal_contribution_page', NULL),
238 ),
239 CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME => array(
240 array('is_enabled', 0),
241 array('uniq_email_per_site', 0),
242 array('domain_group_id', 0),
243 array('event_price_set_domain_id', 0),
244 ),
245 CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME => array(
246 array('uploadDir', NULL),
247 array('imageUploadDir', NULL),
248 array('customFileUploadDir', NULL),
249 array('customTemplateDir', NULL),
250 array('customPHPPathDir', NULL),
251 array('extensionsDir', NULL),
252 ),
253 CRM_Core_BAO_Setting::URL_PREFERENCES_NAME => array(
254 array('userFrameworkResourceURL', NULL),
255 array('imageUploadURL', NULL),
256 array('customCSSURL', NULL),
257 ),
258 );
259
260 $domainID = CRM_Core_Config::domainID();
261 $createdDate = date('YmdHis');
262
263 $dbSettings = array();
264 self::retrieveDirectoryAndURLPaths($dbSettings);
265
266 foreach ($domainColumns as $groupName => $settings) {
267 foreach ($settings as $setting) {
268
269 if (isset($dbSettings[$groupName][$setting[0]]) &&
270 !empty($dbSettings[$groupName][$setting[0]])
271 ) {
272 $setting[1] = $dbSettings[$groupName][$setting[0]];
273 }
274
275 $value = $setting[1] === NULL ? NULL : serialize($setting[1]);
276
277 if ($value) {
278 $value = addslashes($value);
279 }
280
281 $value = $value ? "'{$value}'" : 'null';
282 $values[] = "( '{$groupName}', '{$setting[0]}', {$value}, {$domainID}, null, 0, '{$createdDate}', null )";
283 }
284 }
285 $sql = "
286 INSERT INTO civicrm_setting( group_name, name, value, domain_id, contact_id, is_domain, created_date, created_id )
287 VALUES
288 ";
289 $sql .= implode(",\n", $values);
290 CRM_Core_DAO::executeQuery($sql);
291 }
292
293 /**
294 * @param array $params
295 */
296 public static function retrieveDirectoryAndURLPaths(&$params) {
297
298 $sql = "
299 SELECT v.name as valueName, v.value, g.name as optionName
300 FROM civicrm_option_value v,
301 civicrm_option_group g
302 WHERE ( g.name = 'directory_preferences'
303 OR g.name = 'url_preferences' )
304 AND v.option_group_id = g.id
305 AND v.is_active = 1
306 ";
307 $dao = CRM_Core_DAO::executeQuery($sql);
308 while ($dao->fetch()) {
309 if (!$dao->value) {
310 continue;
311 }
312
313 $groupName = CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME;
314 if ($dao->optionName == 'url_preferences') {
315 $groupName = CRM_Core_BAO_Setting::URL_PREFERENCES_NAME;
316 }
317 $params[$groupName][$dao->valueName] = $dao->value;
318 }
319 }
320
321 /**
322 * @param $rev
323 */
324 public function upgrade_4_1_alpha2($rev) {
325 $dao = new CRM_Core_DAO_Setting();
326 $dao->group_name = 'Directory Preferences';
327 $dao->name = 'customTemplateDir';
328 if (!($dao->find(TRUE))) {
329 $dao->domain_id = CRM_Core_Config::domainID();
330 $dao->created_date = date('YmdHis');
331 $dao->is_domain = 0;
332 $dao->save();
333 }
334 $dao->free();
335
336 // Do the regular upgrade
337 $upgrade = new CRM_Upgrade_Form();
338 $upgrade->processSQL($rev);
339 }
340
341 /**
342 * @param $rev
343 */
344 public function upgrade_4_1_beta1($rev) {
345 //CRM-9311
346 $groupNames = array('directory_preferences', 'url_preferences');
347 foreach ($groupNames as $groupName) {
348 CRM_Core_OptionGroup::deleteAssoc($groupName);
349 }
350
351 $domainCols = array(
352 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME => array(
353 'contact_ajax_check_similar',
354 'activity_assignee_notification',
355 ),
356 CRM_Core_BAO_Setting::CAMPAIGN_PREFERENCES_NAME => array(
357 'tag_unconfirmed',
358 'petition_contacts',
359 ),
360 CRM_Core_BAO_Setting::EVENT_PREFERENCES_NAME => array(
361 'enable_cart',
362 ),
363 CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME => array(
364 'profile_double_optin',
365 'profile_add_to_group_double_optin',
366 'track_civimail_replies',
367 'civimail_workflow',
368 'civimail_server_wide_lock',
369 ),
370 CRM_Core_BAO_Setting::MEMBER_PREFERENCES_NAME => array(
371 'default_renewal_contribution_page',
372 ),
373 CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME => array(
374 'is_enabled',
375 'uniq_email_per_site',
376 'domain_group_id',
377 'event_price_set_domain_id',
378 ),
379 CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME => array(
380 'uploadDir',
381 'imageUploadDir',
382 'customFileUploadDir',
383 'customTemplateDir',
384 'customPHPPathDir',
385 'extensionsDir',
386 ),
387 CRM_Core_BAO_Setting::URL_PREFERENCES_NAME => array(
388 'userFrameworkResourceURL',
389 'imageUploadURL',
390 'customCSSURL',
391 ),
392 );
393
394 $arrGroupNames = array_keys($domainCols);
395 $groupNames = implode("','", $arrGroupNames);
396 $arrNames = array();
397 foreach ($domainCols as $groupName => $names) {
398 $arrNames[] = implode("','", $names);
399 }
400 $name = implode("','", $arrNames);
401
402 $sql = "
403 update civicrm_setting set is_domain = 1 where is_domain = 0 and group_name in ( '{$groupNames}' ) and name in ('{$name}')";
404
405 CRM_Core_DAO::executeQuery($sql);
406
407 $upgrade = new CRM_Upgrade_Form();
408 $upgrade->assign('addWightForActivity', !(CRM_Core_DAO::checkFieldExists('civicrm_activity', 'weight')));
409 $upgrade->processSQL($rev);
410 }
411
412 /**
413 * @param $rev
414 */
415 public function upgrade_4_1_1($rev) {
416 $upgrade = new CRM_Upgrade_Form();
417 $upgrade->assign('addDedupeEmail', !(CRM_Core_DAO::checkFieldExists('civicrm_mailing', 'dedupe_email')));
418
419 $sql = "SELECT id FROM civicrm_worldregion LIMIT 1";
420 $upgrade->assign('worldRegionEmpty', !CRM_Core_DAO::singleValueQuery($sql));
421
422 $upgrade->processSQL($rev);
423 }
424
425 /**
426 * @return string
427 */
428 public function getTemplateMessage() {
429 return "Blah";
430 }
431
432 }