CRM-18231 Added settings on developer page
[civicrm-core.git] / CRM / Admin / Form / Setting.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
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 protected $_settings = array();
40
41 /**
42 * Set default values for the form.
43 *
44 * Default values are retrieved from the database.
45 */
46 public function setDefaultValues() {
47 if (!$this->_defaults) {
48 $this->_defaults = array();
49 $formArray = array('Component', 'Localization');
50 $formMode = FALSE;
51 if (in_array($this->_name, $formArray)) {
52 $formMode = TRUE;
53 }
54
55 CRM_Core_BAO_ConfigSetting::retrieve($this->_defaults);
56
57 // we can handle all the ones defined in the metadata here. Others to be converted
58 foreach ($this->_settings as $setting => $group) {
59 $this->_defaults[$setting] = civicrm_api('setting', 'getvalue', array(
60 'version' => 3,
61 'name' => $setting,
62 'group' => $group,
63 )
64 );
65 }
66
67 $this->_defaults['contact_autocomplete_options'] = self::getAutocompleteContactSearch();
68 $this->_defaults['contact_reference_options'] = self::getAutocompleteContactReference();
69 $this->_defaults['enableSSL'] = Civi::settings()->get('enableSSL');
70 $this->_defaults['verifySSL'] = Civi::settings()->get('verifySSL');
71 $this->_defaults['environment'] = CRM_Core_Config::environment();
72 $this->_defaults['enableComponents'] = Civi::settings()->get('enable_components');
73 }
74
75 return $this->_defaults;
76 }
77
78 /**
79 * Build the form object.
80 */
81 public function buildQuickForm() {
82 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1'));
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
96 $descriptions = array();
97 $settingMetaData = $this->getSettingsMetaData();
98 foreach ($settingMetaData as $setting => $props) {
99 if (isset($props['quick_form_type'])) {
100 if (isset($props['pseudoconstant'])) {
101 if (array_key_exists('optionGroupName', $props['pseudoconstant'])) {
102 $optionValues = civicrm_api3('OptionValue', 'get', array(
103 'return' => array("label", "value"),
104 'option_group_id' => $setting,
105 ));
106 if ($optionValues['count'] > 0) {
107 foreach ($optionValues['values'] as $key => $values) {
108 $vals[$values['value']] = $values['label'];
109 }
110 $options['values'] = $vals;
111 }
112 }
113 else {
114 $options = civicrm_api3('Setting', 'getoptions', array(
115 'field' => $setting,
116 ));
117 }
118 }
119 else {
120 $options = NULL;
121 }
122
123 $add = 'add' . $props['quick_form_type'];
124 if ($add == 'addElement') {
125 $this->$add(
126 $props['html_type'],
127 $setting,
128 ts($props['title']),
129 ($options !== NULL) ? $options['values'] : CRM_Utils_Array::value('html_attributes', $props, array()),
130 ($options !== NULL) ? CRM_Utils_Array::value('html_attributes', $props, array()) : NULL
131 );
132 }
133 elseif ($add == 'addSelect') {
134 $element = $this->addElement('select', $setting, ts($props['title']), $options['values'], CRM_Utils_Array::value('html_attributes', $props));
135 if (defined('CIVICRM_ENVIRONMENT')) {
136 $element->freeze();
137 CRM_Core_Session::setStatus(ts('The environment settings have been disabled because it has been overridden in the settings file.'), ts('Environment settings'), 'info');
138 }
139 }
140 elseif ($add == 'addCheckBox') {
141 $this->addCheckBox($setting, ts($props['title']), $options['values'], NULL, CRM_Utils_Array::value('html_attributes', $props), NULL, NULL, array('&nbsp;&nbsp;'));
142 }
143 elseif ($add == 'addChainSelect') {
144 $this->addChainSelect($setting, array(
145 'label' => ts($props['title']),
146 ));
147 }
148 elseif ($add == 'addMonthDay') {
149 $this->add('date', $setting, ts($props['title']), CRM_Core_SelectValues::date(NULL, 'M d'));
150 }
151 else {
152 $this->$add($setting, ts($props['title']));
153 }
154 // Migrate to using an array as easier in smart...
155 $descriptions[$setting] = ts($props['description']);
156 $this->assign("{$setting}_description", ts($props['description']));
157 if ($setting == 'max_attachments') {
158 //temp hack @todo fix to get from metadata
159 $this->addRule('max_attachments', ts('Value should be a positive number'), 'positiveInteger');
160 }
161 if ($setting == 'maxFileSize') {
162 //temp hack
163 $this->addRule('maxFileSize', ts('Value should be a positive number'), 'positiveInteger');
164 }
165
166 }
167 }
168 // setting_description should be deprecated - see Mail.tpl for metadata based tpl.
169 $this->assign('setting_descriptions', $descriptions);
170 $this->assign('settings_fields', $settingMetaData);
171 }
172
173 /**
174 * Get default entity.
175 *
176 * @return string
177 */
178 public function getDefaultEntity() {
179 return 'Setting';
180 }
181
182 /**
183 * Process the form submission.
184 */
185 public function postProcess() {
186 // store the submitted values in an array
187 $params = $this->controller->exportValues($this->_name);
188
189 self::commonProcess($params);
190 }
191
192 /**
193 * Common Process.
194 *
195 * @todo Document what I do.
196 *
197 * @param array $params
198 */
199 public function commonProcess(&$params) {
200
201 // save autocomplete search options
202 if (!empty($params['contact_autocomplete_options'])) {
203 Civi::settings()->set('contact_autocomplete_options',
204 CRM_Utils_Array::implodePadded(array_keys($params['contact_autocomplete_options'])));
205 unset($params['contact_autocomplete_options']);
206 }
207
208 // save autocomplete contact reference options
209 if (!empty($params['contact_reference_options'])) {
210 Civi::settings()->set('contact_reference_options',
211 CRM_Utils_Array::implodePadded(array_keys($params['contact_reference_options'])));
212 unset($params['contact_reference_options']);
213 }
214
215 // save components to be enabled
216 if (array_key_exists('enableComponents', $params)) {
217 civicrm_api3('setting', 'create', array(
218 'enable_components' => $params['enableComponents'],
219 ));
220 unset($params['enableComponents']);
221 }
222
223 foreach (array('verifySSL', 'enableSSL') as $name) {
224 if (isset($params[$name])) {
225 Civi::settings()->set($name, $params[$name]);
226 unset($params[$name]);
227 }
228 }
229 $settings = array_intersect_key($params, $this->_settings);
230 $result = civicrm_api('setting', 'create', $settings + array('version' => 3));
231 foreach ($settings as $setting => $settingGroup) {
232 //@todo array_diff this
233 unset($params[$setting]);
234 }
235 if (!empty($result['error_message'])) {
236 CRM_Core_Session::setStatus($result['error_message'], ts('Save Failed'), 'error');
237 }
238
239 //CRM_Core_BAO_ConfigSetting::create($params);
240 $params = CRM_Core_BAO_ConfigSetting::filterSkipVars($params);
241 if (!empty($params)) {
242 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)) . ')');
243 }
244
245 CRM_Core_Config::clearDBCache();
246 CRM_Utils_System::flushCache();
247 CRM_Core_Resources::singleton()->resetCacheCode();
248
249 CRM_Core_Session::setStatus(" ", ts('Changes Saved'), "success");
250 }
251
252 public function rebuildMenu() {
253 // ensure config is set with new values
254 $config = CRM_Core_Config::singleton(TRUE, TRUE);
255
256 // rebuild menu items
257 CRM_Core_Menu::store();
258
259 // also delete the IDS file so we can write a new correct one on next load
260 $configFile = $config->uploadDir . 'Config.IDS.ini';
261 @unlink($configFile);
262 }
263
264 /**
265 * Ugh, this shouldn't exist.
266 *
267 * Get the selected values of "contact_reference_options" formatted for checkboxes.
268 *
269 * @return array
270 */
271 public static function getAutocompleteContactReference() {
272 $cRlist = array_flip(CRM_Core_OptionGroup::values('contact_reference_options',
273 FALSE, FALSE, TRUE, NULL, 'name'
274 ));
275 $cRlistEnabled = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
276 'contact_reference_options'
277 );
278 $cRSearchFields = array();
279 if (!empty($cRlist) && !empty($cRlistEnabled)) {
280 $cRSearchFields = array_combine($cRlist, $cRlistEnabled);
281 }
282 return array(
283 '1' => 1,
284 ) + $cRSearchFields;
285 }
286
287 /**
288 * Ugh, this shouldn't exist.
289 *
290 * Get the selected values of "contact_autocomplete_options" formatted for checkboxes.
291 *
292 * @return array
293 */
294 public static function getAutocompleteContactSearch() {
295 $list = array_flip(CRM_Core_OptionGroup::values('contact_autocomplete_options',
296 FALSE, FALSE, TRUE, NULL, 'name'
297 ));
298 $listEnabled = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
299 'contact_autocomplete_options'
300 );
301 $autoSearchFields = array();
302 if (!empty($list) && !empty($listEnabled)) {
303 $autoSearchFields = array_combine($list, $listEnabled);
304 }
305 //Set defaults for autocomplete and contact reference options
306 return array(
307 '1' => 1,
308 ) + $autoSearchFields;
309 }
310
311 /**
312 * Get the metadata relating to the settings on the form, ordered by the keys in $this->_settings.
313 *
314 * @return array
315 */
316 protected function getSettingsMetaData() {
317 $allSettingMetaData = civicrm_api3('setting', 'getfields', array());
318 $settingMetaData = array_intersect_key($allSettingMetaData['values'], $this->_settings);
319 // This array_merge re-orders to the key order of $this->_settings.
320 $settingMetaData = array_merge($this->_settings, $settingMetaData);
321 return $settingMetaData;
322 }
323
324 }