Merge pull request #11773 from civicrm/4.7.31-rc
[civicrm-core.git] / CRM / Admin / Form / Setting.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
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 }
108
fa8dc18c 109 $add = 'add' . $props['quick_form_type'];
9b873358 110 if ($add == 'addElement') {
6a488035 111 $this->$add(
fa8dc18c 112 $props['html_type'],
6a488035 113 $setting,
fa8dc18c 114 ts($props['title']),
8a61528e
TO
115 ($options !== NULL) ? $options['values'] : CRM_Utils_Array::value('html_attributes', $props, array()),
116 ($options !== NULL) ? CRM_Utils_Array::value('html_attributes', $props, array()) : NULL
6a488035
TO
117 );
118 }
52b03498 119 elseif ($add == 'addSelect') {
03c5ceba 120 $this->addElement('select', $setting, ts($props['title']), $options['values'], CRM_Utils_Array::value('html_attributes', $props));
52b03498 121 }
d434f797
TO
122 elseif ($add == 'addCheckBox') {
123 $this->addCheckBox($setting, ts($props['title']), $options['values'], NULL, CRM_Utils_Array::value('html_attributes', $props), NULL, NULL, array('&nbsp;&nbsp;'));
124 }
8a61528e
TO
125 elseif ($add == 'addChainSelect') {
126 $this->addChainSelect($setting, array(
127 'label' => ts($props['title']),
128 ));
129 }
42f32100 130 elseif ($add == 'addMonthDay') {
131 $this->add('date', $setting, ts($props['title']), CRM_Core_SelectValues::date(NULL, 'M d'));
132 }
92e4c2a5 133 else {
fa8dc18c 134 $this->$add($setting, ts($props['title']));
6a488035 135 }
30fcf833 136 // Migrate to using an array as easier in smart...
137 $descriptions[$setting] = ts($props['description']);
fa8dc18c 138 $this->assign("{$setting}_description", ts($props['description']));
9b873358 139 if ($setting == 'max_attachments') {
6a488035
TO
140 //temp hack @todo fix to get from metadata
141 $this->addRule('max_attachments', ts('Value should be a positive number'), 'positiveInteger');
142 }
9b873358 143 if ($setting == 'maxFileSize') {
6a488035
TO
144 //temp hack
145 $this->addRule('maxFileSize', ts('Value should be a positive number'), 'positiveInteger');
146 }
147
148 }
149 }
089360bb 150 // setting_description should be deprecated - see Mail.tpl for metadata based tpl.
30fcf833 151 $this->assign('setting_descriptions', $descriptions);
089360bb 152 $this->assign('settings_fields', $settingMetaData);
6a488035
TO
153 }
154
ce064e4f 155 /**
156 * Get default entity.
157 *
158 * @return string
159 */
52b03498
TO
160 public function getDefaultEntity() {
161 return 'Setting';
162 }
163
6a488035 164 /**
ee0ce2ef 165 * Process the form submission.
6a488035
TO
166 */
167 public function postProcess() {
168 // store the submitted values in an array
169 $params = $this->controller->exportValues($this->_name);
170
171 self::commonProcess($params);
172 }
173
e0ef6999 174 /**
5c9ff055
EM
175 * Common Process.
176 *
177 * @todo Document what I do.
178 *
c490a46a 179 * @param array $params
e0ef6999 180 */
6a488035
TO
181 public function commonProcess(&$params) {
182
183 // save autocomplete search options
d434f797
TO
184 if (!empty($params['contact_autocomplete_options'])) {
185 Civi::settings()->set('contact_autocomplete_options',
186 CRM_Utils_Array::implodePadded(array_keys($params['contact_autocomplete_options'])));
187 unset($params['contact_autocomplete_options']);
6a488035
TO
188 }
189
190 // save autocomplete contact reference options
d434f797
TO
191 if (!empty($params['contact_reference_options'])) {
192 Civi::settings()->set('contact_reference_options',
193 CRM_Utils_Array::implodePadded(array_keys($params['contact_reference_options'])));
194 unset($params['contact_reference_options']);
6a488035
TO
195 }
196
2efcf0c2 197 // save components to be enabled
9dda9790 198 if (array_key_exists('enableComponents', $params)) {
c8074a93
TO
199 civicrm_api3('setting', 'create', array(
200 'enable_components' => $params['enableComponents'],
201 ));
202 unset($params['enableComponents']);
3124edb3
DS
203 }
204
5e7f101a 205 foreach (array('verifySSL', 'enableSSL') as $name) {
206 if (isset($params[$name])) {
207 Civi::settings()->set($name, $params[$name]);
208 unset($params[$name]);
209 }
6a488035
TO
210 }
211 $settings = array_intersect_key($params, $this->_settings);
212 $result = civicrm_api('setting', 'create', $settings + array('version' => 3));
9b873358 213 foreach ($settings as $setting => $settingGroup) {
6a488035
TO
214 //@todo array_diff this
215 unset($params[$setting]);
216 }
af962699
TO
217 if (!empty($result['error_message'])) {
218 CRM_Core_Session::setStatus($result['error_message'], ts('Save Failed'), 'error');
219 }
220
7e0c769c
TO
221 //CRM_Core_BAO_ConfigSetting::create($params);
222 $params = CRM_Core_BAO_ConfigSetting::filterSkipVars($params);
223 if (!empty($params)) {
224 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)) . ')');
225 }
b08fb110
CW
226
227 CRM_Core_Config::clearDBCache();
228 CRM_Utils_System::flushCache();
229 CRM_Core_Resources::singleton()->resetCacheCode();
230
4481526f 231 CRM_Core_Session::setStatus(" ", ts('Changes Saved'), "success");
6a488035
TO
232 }
233
234 public function rebuildMenu() {
235 // ensure config is set with new values
236 $config = CRM_Core_Config::singleton(TRUE, TRUE);
237
238 // rebuild menu items
239 CRM_Core_Menu::store();
240
241 // also delete the IDS file so we can write a new correct one on next load
242 $configFile = $config->uploadDir . 'Config.IDS.ini';
243 @unlink($configFile);
244 }
96025800 245
d434f797
TO
246 /**
247 * Ugh, this shouldn't exist.
248 *
249 * Get the selected values of "contact_reference_options" formatted for checkboxes.
250 *
251 * @return array
252 */
253 public static function getAutocompleteContactReference() {
254 $cRlist = array_flip(CRM_Core_OptionGroup::values('contact_reference_options',
255 FALSE, FALSE, TRUE, NULL, 'name'
256 ));
257 $cRlistEnabled = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
258 'contact_reference_options'
259 );
260 $cRSearchFields = array();
261 if (!empty($cRlist) && !empty($cRlistEnabled)) {
262 $cRSearchFields = array_combine($cRlist, $cRlistEnabled);
263 }
264 return array(
265 '1' => 1,
266 ) + $cRSearchFields;
267 }
268
269 /**
270 * Ugh, this shouldn't exist.
271 *
272 * Get the selected values of "contact_autocomplete_options" formatted for checkboxes.
273 *
274 * @return array
275 */
276 public static function getAutocompleteContactSearch() {
277 $list = array_flip(CRM_Core_OptionGroup::values('contact_autocomplete_options',
278 FALSE, FALSE, TRUE, NULL, 'name'
279 ));
280 $listEnabled = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
281 'contact_autocomplete_options'
282 );
283 $autoSearchFields = array();
284 if (!empty($list) && !empty($listEnabled)) {
285 $autoSearchFields = array_combine($list, $listEnabled);
286 }
287 //Set defaults for autocomplete and contact reference options
288 return array(
289 '1' => 1,
290 ) + $autoSearchFields;
291 }
292
089360bb 293 /**
294 * Get the metadata relating to the settings on the form, ordered by the keys in $this->_settings.
295 *
296 * @return array
297 */
298 protected function getSettingsMetaData() {
299 $allSettingMetaData = civicrm_api3('setting', 'getfields', array());
300 $settingMetaData = array_intersect_key($allSettingMetaData['values'], $this->_settings);
301 // This array_merge re-orders to the key order of $this->_settings.
302 $settingMetaData = array_merge($this->_settings, $settingMetaData);
303 return $settingMetaData;
304 }
305
6a488035 306}