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