Merge pull request #12406 from jitendrapurohit/core-212
[civicrm-core.git] / CRM / Admin / Form / Setting.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
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 $options = civicrm_api3('Setting', 'getoptions', array(
102 'field' => $setting,
103 ));
104 }
105 else {
106 $options = NULL;
107 }
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 }
113
114 $add = 'add' . $props['quick_form_type'];
115 if ($add == 'addElement') {
116 $this->$add(
117 $props['html_type'],
118 $setting,
119 ts($props['title']),
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
122 );
123 }
124 elseif ($add == 'addSelect') {
125 $this->addElement('select', $setting, ts($props['title']), $options['values'], CRM_Utils_Array::value('html_attributes', $props));
126 }
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 }
130 elseif ($add == 'addChainSelect') {
131 $this->addChainSelect($setting, array(
132 'label' => ts($props['title']),
133 ));
134 }
135 elseif ($add == 'addMonthDay') {
136 $this->add('date', $setting, ts($props['title']), CRM_Core_SelectValues::date(NULL, 'M d'));
137 }
138 else {
139 $this->$add($setting, ts($props['title']));
140 }
141 // Migrate to using an array as easier in smart...
142 $descriptions[$setting] = ts($props['description']);
143 $this->assign("{$setting}_description", ts($props['description']));
144 if ($setting == 'max_attachments') {
145 //temp hack @todo fix to get from metadata
146 $this->addRule('max_attachments', ts('Value should be a positive number'), 'positiveInteger');
147 }
148 if ($setting == 'maxFileSize') {
149 //temp hack
150 $this->addRule('maxFileSize', ts('Value should be a positive number'), 'positiveInteger');
151 }
152
153 }
154 }
155 if (!empty($setStatus)) {
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));
157 }
158 // setting_description should be deprecated - see Mail.tpl for metadata based tpl.
159 $this->assign('setting_descriptions', $descriptions);
160 $this->assign('settings_fields', $settingMetaData);
161 }
162
163 /**
164 * Get default entity.
165 *
166 * @return string
167 */
168 public function getDefaultEntity() {
169 return 'Setting';
170 }
171
172 /**
173 * Process the form submission.
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
182 /**
183 * Common Process.
184 *
185 * @todo Document what I do.
186 *
187 * @param array $params
188 */
189 public function commonProcess(&$params) {
190
191 // save autocomplete search options
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']);
196 }
197
198 // save autocomplete contact reference options
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']);
203 }
204
205 // save components to be enabled
206 if (array_key_exists('enableComponents', $params)) {
207 civicrm_api3('setting', 'create', array(
208 'enable_components' => $params['enableComponents'],
209 ));
210 unset($params['enableComponents']);
211 }
212
213 foreach (array('verifySSL', 'enableSSL') as $name) {
214 if (isset($params[$name])) {
215 Civi::settings()->set($name, $params[$name]);
216 unset($params[$name]);
217 }
218 }
219 $settings = array_intersect_key($params, $this->_settings);
220 $result = civicrm_api('setting', 'create', $settings + array('version' => 3));
221 foreach ($settings as $setting => $settingGroup) {
222 //@todo array_diff this
223 unset($params[$setting]);
224 }
225 if (!empty($result['error_message'])) {
226 CRM_Core_Session::setStatus($result['error_message'], ts('Save Failed'), 'error');
227 }
228
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 }
234
235 CRM_Core_Config::clearDBCache();
236 Civi::cache('session')->clear(); // This doesn't make a lot of sense to me, but it maintains pre-existing behavior.
237 CRM_Utils_System::flushCache();
238 CRM_Core_Resources::singleton()->resetCacheCode();
239
240 CRM_Core_Session::setStatus(" ", ts('Changes Saved'), "success");
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 }
254
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
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
315 }