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