INFRA-132 - Add space before "{"
[civicrm-core.git] / CRM / Admin / Form / Setting.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class generates form components generic to CiviCRM settings
38 *
39 */
40class CRM_Admin_Form_Setting extends CRM_Core_Form {
41
42 protected $_defaults;
43 protected $_settings = array();
44
45 /**
c490a46a 46 * Set default values for the form.
6a488035
TO
47 * default values are retrieved from the database
48 *
6a488035 49 *
355ba699 50 * @return void
6a488035 51 */
00be9182 52 public function setDefaultValues() {
6a488035
TO
53 if (!$this->_defaults) {
54 $this->_defaults = array();
55 $formArray = array('Component', 'Localization');
56 $formMode = FALSE;
57 if (in_array($this->_name, $formArray)) {
58 $formMode = TRUE;
59 }
60
61 CRM_Core_BAO_ConfigSetting::retrieve($this->_defaults);
62
63 CRM_Core_Config_Defaults::setValues($this->_defaults, $formMode);
64
65 $list = array_flip(CRM_Core_OptionGroup::values('contact_autocomplete_options',
66 FALSE, FALSE, TRUE, NULL, 'name'
67 ));
68
69 $cRlist = array_flip(CRM_Core_OptionGroup::values('contact_reference_options',
70 FALSE, FALSE, TRUE, NULL, 'name'
71 ));
72
73 $listEnabled = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
74 'contact_autocomplete_options'
75 );
76 $cRlistEnabled = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
77 'contact_reference_options'
78 );
79
80 $autoSearchFields = array();
81 if (!empty($list) && !empty($listEnabled)) {
82 $autoSearchFields = array_combine($list, $listEnabled);
83 }
84
85 $cRSearchFields = array();
86 if (!empty($cRlist) && !empty($cRlistEnabled)) {
87 $cRSearchFields = array_combine($cRlist, $cRlistEnabled);
88 }
89
90 //Set defaults for autocomplete and contact reference options
91 $this->_defaults['autocompleteContactSearch'] = array(
92 '1' => 1) + $autoSearchFields;
93 $this->_defaults['autocompleteContactReference'] = array(
94 '1' => 1) + $cRSearchFields;
95
96 // we can handle all the ones defined in the metadata here. Others to be converted
9b873358 97 foreach ($this->_settings as $setting => $group) {
6a488035
TO
98 $settingMetaData = civicrm_api('setting', 'getfields', array('version' => 3, 'name' => $setting));
99 $this->_defaults[$setting] = civicrm_api('setting', 'getvalue', array(
100 'version' => 3,
101 'name' => $setting,
102 'group' => $group,
103 'default_value' => CRM_Utils_Array::value('default', $settingMetaData['values'][$setting])
104 )
105 );
106 }
107
108 $this->_defaults['enableSSL'] = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'enableSSL', NULL, 0);
109 $this->_defaults['verifySSL'] = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'verifySSL', NULL, 1);
6a488035
TO
110 }
111
112 return $this->_defaults;
113 }
114
115 /**
c490a46a 116 * Build the form object
6a488035 117 *
355ba699 118 * @return void
6a488035
TO
119 */
120 public function buildQuickForm() {
121 $session = CRM_Core_Session::singleton();
122 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1'));
123 $args = func_get_args();
124 $check = reset($args);
125 $this->addButtons(array(
126 array(
127 'type' => 'next',
128 'name' => ts('Save'),
129 'isDefault' => TRUE,
130 ),
131 array(
132 'type' => 'cancel',
133 'name' => ts('Cancel'),
134 ),
135 )
136 );
137
9b873358 138 foreach ($this->_settings as $setting => $group) {
6a488035 139 $settingMetaData = civicrm_api('setting', 'getfields', array('version' => 3, 'name' => $setting));
fa8dc18c 140 $props = $settingMetaData['values'][$setting];
9b873358 141 if (isset($props['quick_form_type'])) {
fa8dc18c 142 $add = 'add' . $props['quick_form_type'];
9b873358 143 if ($add == 'addElement') {
6a488035 144 $this->$add(
fa8dc18c 145 $props['html_type'],
6a488035 146 $setting,
fa8dc18c
CW
147 ts($props['title']),
148 CRM_Utils_Array::value($props['html_type'] == 'select' ? 'option_values' : 'html_attributes', $props, array()),
149 $props['html_type'] == 'select' ? CRM_Utils_Array::value('html_attributes', $props) : NULL
6a488035
TO
150 );
151 }
152 else{
fa8dc18c 153 $this->$add($setting, ts($props['title']));
6a488035 154 }
fa8dc18c 155 $this->assign("{$setting}_description", ts($props['description']));
9b873358 156 if ($setting == 'max_attachments') {
6a488035
TO
157 //temp hack @todo fix to get from metadata
158 $this->addRule('max_attachments', ts('Value should be a positive number'), 'positiveInteger');
159 }
9b873358 160 if ($setting == 'maxFileSize') {
6a488035
TO
161 //temp hack
162 $this->addRule('maxFileSize', ts('Value should be a positive number'), 'positiveInteger');
163 }
164
165 }
166 }
167 }
168
169 /**
c490a46a 170 * Process the form submission
6a488035 171 *
6a488035 172 *
355ba699 173 * @return void
6a488035
TO
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
e0ef6999 182 /**
c490a46a 183 * @param array $params
e0ef6999 184 */
6a488035
TO
185 public function commonProcess(&$params) {
186
187 // save autocomplete search options
a7488080 188 if (!empty($params['autocompleteContactSearch'])) {
6a488035
TO
189 $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
190 array_keys($params['autocompleteContactSearch'])
191 ) . CRM_Core_DAO::VALUE_SEPARATOR;
192
193 CRM_Core_BAO_Setting::setItem($value,
194 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
195 'contact_autocomplete_options'
196 );
197
198 unset($params['autocompleteContactSearch']);
199 }
200
201 // save autocomplete contact reference options
a7488080 202 if (!empty($params['autocompleteContactReference'])) {
6a488035
TO
203 $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
204 array_keys($params['autocompleteContactReference'])
205 ) . CRM_Core_DAO::VALUE_SEPARATOR;
206
207 CRM_Core_BAO_Setting::setItem($value,
208 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
209 'contact_reference_options'
210 );
211
212 unset($params['autocompleteContactReference']);
213 }
214
2efcf0c2 215 // save components to be enabled
9dda9790 216 if (array_key_exists('enableComponents', $params)) {
3124edb3 217 CRM_Core_BAO_Setting::setItem($params['enableComponents'],
02fc859b 218 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'enable_components');
3124edb3
DS
219
220 // unset params by emptying the values, so while retrieving we can detect and load from settings table
221 // instead of config-backend for backward compatibility. We could use unset() in later releases.
222 $params['enableComponents'] = $params['enableComponentIDs'] = array();
223 }
224
6a488035 225 // save checksum timeout
a7488080 226 if (!empty($params['checksumTimeout'])) {
6a488035
TO
227 CRM_Core_BAO_Setting::setItem($params['checksumTimeout'],
228 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
229 'checksum_timeout'
230 );
231 }
232
233 // update time for date formats when global time is changed
a7488080 234 if (!empty($params['timeInputFormat'])) {
6a488035
TO
235 $query = "
236UPDATE civicrm_preferences_date
237SET time_format = %1
238WHERE time_format IS NOT NULL
239AND time_format <> ''
240";
241 $sqlParams = array(1 => array($params['timeInputFormat'], 'String'));
242 CRM_Core_DAO::executeQuery($query, $sqlParams);
6a488035
TO
243 }
244
245 // verify ssl peer option
246 if (isset($params['verifySSL'])) {
247 CRM_Core_BAO_Setting::setItem($params['verifySSL'],
248 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
249 'verifySSL'
250 );
251 unset($params['verifySSL']);
252 }
253
254 // force secure URLs
255 if (isset($params['enableSSL'])) {
256 CRM_Core_BAO_Setting::setItem($params['enableSSL'],
257 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
258 'enableSSL'
259 );
260 unset($params['enableSSL']);
261 }
262 $settings = array_intersect_key($params, $this->_settings);
263 $result = civicrm_api('setting', 'create', $settings + array('version' => 3));
9b873358 264 foreach ($settings as $setting => $settingGroup) {
6a488035
TO
265 //@todo array_diff this
266 unset($params[$setting]);
267 }
268 CRM_Core_BAO_ConfigSetting::create($params);
4481526f 269 CRM_Core_Session::setStatus(" ", ts('Changes Saved'), "success");
6a488035
TO
270 }
271
272 public function rebuildMenu() {
273 // ensure config is set with new values
274 $config = CRM_Core_Config::singleton(TRUE, TRUE);
275
276 // rebuild menu items
277 CRM_Core_Menu::store();
278
279 // also delete the IDS file so we can write a new correct one on next load
280 $configFile = $config->uploadDir . 'Config.IDS.ini';
281 @unlink($configFile);
282 }
283}