Merge pull request #12715 from mattwire/comments_2
[civicrm-core.git] / CRM / Admin / Form / Setting.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
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
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
6a488035
TO
32 */
33
34/**
ce064e4f 35 * This class generates form components generic to CiviCRM settings.
6a488035
TO
36 */
37class CRM_Admin_Form_Setting extends CRM_Core_Form {
38
6a488035
TO
39 protected $_settings = array();
40
41 /**
c490a46a 42 * Set default values for the form.
6a488035 43 *
ee0ce2ef 44 * Default values are retrieved from the database.
6a488035 45 */
00be9182 46 public function setDefaultValues() {
6a488035
TO
47 if (!$this->_defaults) {
48 $this->_defaults = array();
353ffa53
TO
49 $formArray = array('Component', 'Localization');
50 $formMode = FALSE;
6a488035
TO
51 if (in_array($this->_name, $formArray)) {
52 $formMode = TRUE;
53 }
54
55 CRM_Core_BAO_ConfigSetting::retrieve($this->_defaults);
56
6a488035 57 // we can handle all the ones defined in the metadata here. Others to be converted
9b873358 58 foreach ($this->_settings as $setting => $group) {
6a488035 59 $this->_defaults[$setting] = civicrm_api('setting', 'getvalue', array(
353ffa53
TO
60 'version' => 3,
61 'name' => $setting,
62 'group' => $group,
6a488035
TO
63 )
64 );
65 }
66
d434f797
TO
67 $this->_defaults['contact_autocomplete_options'] = self::getAutocompleteContactSearch();
68 $this->_defaults['contact_reference_options'] = self::getAutocompleteContactReference();
aaffa79f 69 $this->_defaults['enableSSL'] = Civi::settings()->get('enableSSL');
70 $this->_defaults['verifySSL'] = Civi::settings()->get('verifySSL');
f008885c 71 $this->_defaults['environment'] = CRM_Core_Config::environment();
f4fb2d79 72 $this->_defaults['enableComponents'] = Civi::settings()->get('enable_components');
6a488035
TO
73 }
74
75 return $this->_defaults;
76 }
77
78 /**
567b2076 79 * Build the form object.
6a488035
TO
80 */
81 public function buildQuickForm() {
089360bb 82 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1'));
6a488035
TO
83 $this->addButtons(array(
84 array(
85 'type' => 'next',
86 'name' => ts('Save'),
87 'isDefault' => TRUE,
88 ),
89 array(
90 'type' => 'cancel',
91 'name' => ts('Cancel'),
92 ),
93 )
94 );
95
30fcf833 96 $descriptions = array();
089360bb 97 $settingMetaData = $this->getSettingsMetaData();
98 foreach ($settingMetaData as $setting => $props) {
9b873358 99 if (isset($props['quick_form_type'])) {
8a61528e 100 if (isset($props['pseudoconstant'])) {
03c5ceba 101 $options = civicrm_api3('Setting', 'getoptions', array(
102 'field' => $setting,
103 ));
8a61528e
TO
104 }
105 else {
106 $options = NULL;
107 }
f6c21a43
JP
108 //Load input as readonly whose values are overridden in civicrm.settings.php.
109 if (Civi::settings()->getMandatory($setting)) {
110 $props['html_attributes']['readonly'] = TRUE;
111 $setStatus = TRUE;
112 }
8a61528e 113
fa8dc18c 114 $add = 'add' . $props['quick_form_type'];
9b873358 115 if ($add == 'addElement') {
6a488035 116 $this->$add(
fa8dc18c 117 $props['html_type'],
6a488035 118 $setting,
fa8dc18c 119 ts($props['title']),
8a61528e
TO
120 ($options !== NULL) ? $options['values'] : CRM_Utils_Array::value('html_attributes', $props, array()),
121 ($options !== NULL) ? CRM_Utils_Array::value('html_attributes', $props, array()) : NULL
6a488035
TO
122 );
123 }
52b03498 124 elseif ($add == 'addSelect') {
03c5ceba 125 $this->addElement('select', $setting, ts($props['title']), $options['values'], CRM_Utils_Array::value('html_attributes', $props));
52b03498 126 }
d434f797
TO
127 elseif ($add == 'addCheckBox') {
128 $this->addCheckBox($setting, ts($props['title']), $options['values'], NULL, CRM_Utils_Array::value('html_attributes', $props), NULL, NULL, array('&nbsp;&nbsp;'));
129 }
8a61528e
TO
130 elseif ($add == 'addChainSelect') {
131 $this->addChainSelect($setting, array(
132 'label' => ts($props['title']),
133 ));
134 }
42f32100 135 elseif ($add == 'addMonthDay') {
136 $this->add('date', $setting, ts($props['title']), CRM_Core_SelectValues::date(NULL, 'M d'));
137 }
92e4c2a5 138 else {
fa8dc18c 139 $this->$add($setting, ts($props['title']));
6a488035 140 }
30fcf833 141 // Migrate to using an array as easier in smart...
142 $descriptions[$setting] = ts($props['description']);
fa8dc18c 143 $this->assign("{$setting}_description", ts($props['description']));
9b873358 144 if ($setting == 'max_attachments') {
6a488035
TO
145 //temp hack @todo fix to get from metadata
146 $this->addRule('max_attachments', ts('Value should be a positive number'), 'positiveInteger');
147 }
9b873358 148 if ($setting == 'maxFileSize') {
6a488035
TO
149 //temp hack
150 $this->addRule('maxFileSize', ts('Value should be a positive number'), 'positiveInteger');
151 }
152
153 }
154 }
f6c21a43 155 if (!empty($setStatus)) {
ea25fd5d 156 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));
f6c21a43 157 }
089360bb 158 // setting_description should be deprecated - see Mail.tpl for metadata based tpl.
30fcf833 159 $this->assign('setting_descriptions', $descriptions);
089360bb 160 $this->assign('settings_fields', $settingMetaData);
6a488035
TO
161 }
162
ce064e4f 163 /**
164 * Get default entity.
165 *
166 * @return string
167 */
52b03498
TO
168 public function getDefaultEntity() {
169 return 'Setting';
170 }
171
6a488035 172 /**
ee0ce2ef 173 * Process the form submission.
6a488035
TO
174 */
175 public function postProcess() {
176 // store the submitted values in an array
177 $params = $this->controller->exportValues($this->_name);
178
179 self::commonProcess($params);
180 }
181
e0ef6999 182 /**
5c9ff055
EM
183 * Common Process.
184 *
185 * @todo Document what I do.
186 *
c490a46a 187 * @param array $params
e0ef6999 188 */
6a488035
TO
189 public function commonProcess(&$params) {
190
191 // save autocomplete search options
d434f797
TO
192 if (!empty($params['contact_autocomplete_options'])) {
193 Civi::settings()->set('contact_autocomplete_options',
194 CRM_Utils_Array::implodePadded(array_keys($params['contact_autocomplete_options'])));
195 unset($params['contact_autocomplete_options']);
6a488035
TO
196 }
197
198 // save autocomplete contact reference options
d434f797
TO
199 if (!empty($params['contact_reference_options'])) {
200 Civi::settings()->set('contact_reference_options',
201 CRM_Utils_Array::implodePadded(array_keys($params['contact_reference_options'])));
202 unset($params['contact_reference_options']);
6a488035
TO
203 }
204
2efcf0c2 205 // save components to be enabled
9dda9790 206 if (array_key_exists('enableComponents', $params)) {
c8074a93
TO
207 civicrm_api3('setting', 'create', array(
208 'enable_components' => $params['enableComponents'],
209 ));
210 unset($params['enableComponents']);
3124edb3
DS
211 }
212
5e7f101a 213 foreach (array('verifySSL', 'enableSSL') as $name) {
214 if (isset($params[$name])) {
215 Civi::settings()->set($name, $params[$name]);
216 unset($params[$name]);
217 }
6a488035
TO
218 }
219 $settings = array_intersect_key($params, $this->_settings);
220 $result = civicrm_api('setting', 'create', $settings + array('version' => 3));
9b873358 221 foreach ($settings as $setting => $settingGroup) {
6a488035
TO
222 //@todo array_diff this
223 unset($params[$setting]);
224 }
af962699
TO
225 if (!empty($result['error_message'])) {
226 CRM_Core_Session::setStatus($result['error_message'], ts('Save Failed'), 'error');
227 }
228
7e0c769c
TO
229 //CRM_Core_BAO_ConfigSetting::create($params);
230 $params = CRM_Core_BAO_ConfigSetting::filterSkipVars($params);
231 if (!empty($params)) {
232 CRM_Core_Error::fatal('Unrecognized setting. This may be a config field which has not been properly migrated to a setting. (' . implode(', ', array_keys($params)) . ')');
233 }
b08fb110
CW
234
235 CRM_Core_Config::clearDBCache();
0a12cd4a 236 Civi::cache('session')->clear(); // This doesn't make a lot of sense to me, but it maintains pre-existing behavior.
b08fb110
CW
237 CRM_Utils_System::flushCache();
238 CRM_Core_Resources::singleton()->resetCacheCode();
239
4481526f 240 CRM_Core_Session::setStatus(" ", ts('Changes Saved'), "success");
6a488035
TO
241 }
242
243 public function rebuildMenu() {
244 // ensure config is set with new values
245 $config = CRM_Core_Config::singleton(TRUE, TRUE);
246
247 // rebuild menu items
248 CRM_Core_Menu::store();
249
250 // also delete the IDS file so we can write a new correct one on next load
251 $configFile = $config->uploadDir . 'Config.IDS.ini';
252 @unlink($configFile);
253 }
96025800 254
d434f797
TO
255 /**
256 * Ugh, this shouldn't exist.
257 *
258 * Get the selected values of "contact_reference_options" formatted for checkboxes.
259 *
260 * @return array
261 */
262 public static function getAutocompleteContactReference() {
263 $cRlist = array_flip(CRM_Core_OptionGroup::values('contact_reference_options',
264 FALSE, FALSE, TRUE, NULL, 'name'
265 ));
266 $cRlistEnabled = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
267 'contact_reference_options'
268 );
269 $cRSearchFields = array();
270 if (!empty($cRlist) && !empty($cRlistEnabled)) {
271 $cRSearchFields = array_combine($cRlist, $cRlistEnabled);
272 }
273 return array(
274 '1' => 1,
275 ) + $cRSearchFields;
276 }
277
278 /**
279 * Ugh, this shouldn't exist.
280 *
281 * Get the selected values of "contact_autocomplete_options" formatted for checkboxes.
282 *
283 * @return array
284 */
285 public static function getAutocompleteContactSearch() {
286 $list = array_flip(CRM_Core_OptionGroup::values('contact_autocomplete_options',
287 FALSE, FALSE, TRUE, NULL, 'name'
288 ));
289 $listEnabled = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
290 'contact_autocomplete_options'
291 );
292 $autoSearchFields = array();
293 if (!empty($list) && !empty($listEnabled)) {
294 $autoSearchFields = array_combine($list, $listEnabled);
295 }
296 //Set defaults for autocomplete and contact reference options
297 return array(
298 '1' => 1,
299 ) + $autoSearchFields;
300 }
301
089360bb 302 /**
303 * Get the metadata relating to the settings on the form, ordered by the keys in $this->_settings.
304 *
305 * @return array
306 */
307 protected function getSettingsMetaData() {
308 $allSettingMetaData = civicrm_api3('setting', 'getfields', array());
309 $settingMetaData = array_intersect_key($allSettingMetaData['values'], $this->_settings);
310 // This array_merge re-orders to the key order of $this->_settings.
311 $settingMetaData = array_merge($this->_settings, $settingMetaData);
312 return $settingMetaData;
313 }
314
6a488035 315}