Merge pull request #13640 from pradpnayak/notevalidation
[civicrm-core.git] / CRM / Admin / Form / Setting.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
32 */
33
34 /**
35 * This class generates form components generic to CiviCRM settings.
36 */
37 class CRM_Admin_Form_Setting extends CRM_Core_Form {
38
39 use CRM_Admin_Form_SettingTrait;
40
41 protected $_settings = array();
42
43 protected $includesReadOnlyFields;
44
45 /**
46 * Set default values for the form.
47 *
48 * Default values are retrieved from the database.
49 */
50 public function setDefaultValues() {
51 if (!$this->_defaults) {
52 $this->_defaults = array();
53 $formArray = array('Component', 'Localization');
54 $formMode = FALSE;
55 if (in_array($this->_name, $formArray)) {
56 $formMode = TRUE;
57 }
58
59 $this->setDefaultsForMetadataDefinedFields();
60
61 // @todo these should be retrievable from the above function.
62 $this->_defaults['enableSSL'] = Civi::settings()->get('enableSSL');
63 $this->_defaults['verifySSL'] = Civi::settings()->get('verifySSL');
64 $this->_defaults['environment'] = CRM_Core_Config::environment();
65 $this->_defaults['enableComponents'] = Civi::settings()->get('enable_components');
66 }
67
68 return $this->_defaults;
69 }
70
71 /**
72 * Build the form object.
73 */
74 public function buildQuickForm() {
75 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1'));
76 $this->addButtons(array(
77 array(
78 'type' => 'next',
79 'name' => ts('Save'),
80 'isDefault' => TRUE,
81 ),
82 array(
83 'type' => 'cancel',
84 'name' => ts('Cancel'),
85 ),
86 )
87 );
88
89 $this->addFieldsDefinedInSettingsMetadata();
90
91 if ($this->includesReadOnlyFields) {
92 CRM_Core_Session::setStatus(ts("Some fields are loaded as 'readonly' as they have been set (overridden) in civicrm.settings.php."), '', 'info', array('expires' => 0));
93 }
94 }
95
96 /**
97 * Process the form submission.
98 */
99 public function postProcess() {
100 // store the submitted values in an array
101 $params = $this->controller->exportValues($this->_name);
102
103 self::commonProcess($params);
104 }
105
106 /**
107 * Common Process.
108 *
109 * @todo Document what I do.
110 *
111 * @param array $params
112 * @throws \CRM_Core_Exception
113 */
114 public function commonProcess(&$params) {
115
116 // save components to be enabled
117 if (array_key_exists('enableComponents', $params)) {
118 civicrm_api3('setting', 'create', array(
119 'enable_components' => $params['enableComponents'],
120 ));
121 unset($params['enableComponents']);
122 }
123
124 foreach (array('verifySSL', 'enableSSL') as $name) {
125 if (isset($params[$name])) {
126 Civi::settings()->set($name, $params[$name]);
127 unset($params[$name]);
128 }
129 }
130 try {
131 $settings = $this->getSettingsToSetByMetadata($params);
132 $this->saveMetadataDefinedSettings($params);
133 }
134 catch (CiviCRM_API3_Exception $e) {
135 CRM_Core_Session::setStatus($e->getMessage(), ts('Save Failed'), 'error');
136 }
137
138 $this->filterParamsSetByMetadata($params);
139
140 $params = CRM_Core_BAO_ConfigSetting::filterSkipVars($params);
141 if (!empty($params)) {
142 throw new CRM_Core_Exception('Unrecognized setting. This may be a config field which has not been properly migrated to a setting. (' . implode(', ', array_keys($params)) . ')');
143 }
144
145 CRM_Core_Config::clearDBCache();
146 Civi::cache('session')->clear(); // This doesn't make a lot of sense to me, but it maintains pre-existing behavior.
147 CRM_Utils_System::flushCache();
148 CRM_Core_Resources::singleton()->resetCacheCode();
149
150 CRM_Core_Session::setStatus(" ", ts('Changes Saved'), "success");
151 }
152
153 public function rebuildMenu() {
154 // ensure config is set with new values
155 $config = CRM_Core_Config::singleton(TRUE, TRUE);
156
157 // rebuild menu items
158 CRM_Core_Menu::store();
159
160 // also delete the IDS file so we can write a new correct one on next load
161 $configFile = $config->uploadDir . 'Config.IDS.ini';
162 @unlink($configFile);
163 }
164
165 }