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