Merge pull request #6446 from colemanw/CRM-13160
[civicrm-core.git] / CRM / Admin / Form / Setting / UpdateConfigBackend.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
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 and the CiviCRM Licensing Exception. |
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 and the CiviCRM Licensing Exception along |
21 | 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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class generates form components for Error Handling and Debugging
38 *
39 */
40class CRM_Admin_Form_Setting_UpdateConfigBackend extends CRM_Admin_Form_Setting {
41 protected $_oldBaseDir;
42 protected $_oldBaseURL;
43 protected $_oldSiteName;
44
45 /**
eceb18cc 46 * Build the form object.
6a488035 47 *
355ba699 48 * @return void
6a488035
TO
49 */
50 public function buildQuickForm() {
51 CRM_Utils_System::setTitle(ts('Settings - Cleanup Caches and Update Paths'));
52
e51744f5
DL
53 list(
54 $this->_oldBaseURL,
6a488035
TO
55 $this->_oldBaseDir,
56 $this->_oldSiteName
353ffa53 57 ) = CRM_Core_BAO_ConfigSetting::getConfigSettings();
6a488035
TO
58
59 $this->assign('oldBaseURL', $this->_oldBaseURL);
60 $this->assign('oldBaseDir', $this->_oldBaseDir);
61 $this->assign('oldSiteName', $this->_oldSiteName);
62
e51744f5
DL
63 $this->addElement(
64 'submit', $this->getButtonName('next', 'cleanup'), 'Cleanup Caches',
97e557d7 65 array('class' => 'crm-form-submit', 'id' => 'cleanup-cache')
e51744f5 66 );
6a488035
TO
67
68 $this->add('text', 'newBaseURL', ts('New Base URL'), NULL, TRUE);
69 $this->add('text', 'newBaseDir', ts('New Base Directory'), NULL, TRUE);
70 if ($this->_oldSiteName) {
71 $this->add('text', 'newSiteName', ts('New Site Name'), NULL, TRUE);
72 }
73 $this->addFormRule(array('CRM_Admin_Form_Setting_UpdateConfigBackend', 'formRule'));
74
75 parent::buildQuickForm();
76 }
77
00be9182 78 public function setDefaultValues() {
6a488035
TO
79 if (!$this->_defaults) {
80 parent::setDefaultValues();
81
82 $config = CRM_Core_Config::singleton();
e51744f5
DL
83 list(
84 $this->_defaults['newBaseURL'],
6a488035
TO
85 $this->_defaults['newBaseDir'],
86 $this->_defaults['newSiteName']
353ffa53 87 ) = CRM_Core_BAO_ConfigSetting::getBestGuessSettings();
6a488035
TO
88 }
89
90 return $this->_defaults;
91 }
92
e0ef6999
EM
93 /**
94 * @param $fields
95 *
96 * @return array
97 */
00be9182 98 public static function formRule($fields) {
6a488035
TO
99 $tmpDir = trim($fields['newBaseDir']);
100
101 $errors = array();
22e263ad 102 if (!is_writable($tmpDir)) {
6a488035
TO
103 $errors['newBaseDir'] = ts('%1 directory does not exist or cannot be written by webserver',
104 array(1 => $tmpDir)
105 );
106 }
107 return $errors;
108 }
109
00be9182 110 public function postProcess() {
a7488080 111 if (!empty($_POST['_qf_UpdateConfigBackend_next_cleanup'])) {
6a488035
TO
112
113 $config = CRM_Core_Config::singleton();
114
115 // cleanup templates_c directory
116 $config->cleanup(1, FALSE);
117
514b92c9 118 // clear all caches
6a488035 119 CRM_Core_Config::clearDBCache();
514b92c9
CW
120 CRM_Utils_System::flushCache();
121
6a488035
TO
122 parent::rebuildMenu();
123
3e089a38
BS
124 CRM_Core_BAO_WordReplacement::rebuild();
125
6a488035
TO
126 CRM_Core_Session::setStatus(ts('Cache has been cleared and menu has been rebuilt successfully.'), ts("Success"), "success");
127 return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/setting/updateConfigBackend', 'reset=1'));
128 }
129
130 // redirect to admin page after saving
131 $session = CRM_Core_Session::singleton();
132 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin'));
133
134 $params = $this->controller->exportValues($this->_name);
135
136 //CRM-5679
137 foreach ($params as $name => & $val) {
5c8cb77f 138 if ($val && in_array($name, array('newBaseDir', 'newSiteName'))) {
6a488035
TO
139 $val = CRM_Utils_File::addTrailingSlash($val);
140 }
141 }
d75f2f47
EM
142
143 //CRM-15365 - Fix BaseURL to avoid wrong trailing slash on Windows installs
5c8cb77f
CW
144 foreach ($params as $name => & $val) {
145 if ($val && in_array($name, array('newBaseURL'))) {
02fc859b 146 $val = CRM_Utils_File::addTrailingSlash($val, "/");
37b395e8
DC
147 }
148 }
6a488035
TO
149
150 $from = array($this->_oldBaseURL, $this->_oldBaseDir);
353ffa53
TO
151 $to = array(
152 trim($params['newBaseURL']),
6a488035
TO
153 trim($params['newBaseDir']),
154 );
155 if ($this->_oldSiteName &&
156 $params['newSiteName']
157 ) {
158 $from[] = $this->_oldSiteName;
159 $to[] = $params['newSiteName'];
160 }
161
162 $newValues = str_replace($from,
163 $to,
164 $this->_defaults
165 );
166
167 parent::commonProcess($newValues);
168
169 parent::rebuildMenu();
170 }
96025800 171
6a488035 172}