INFRA-132 - Batch #7
[civicrm-core.git] / CRM / Admin / Form / Setting.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class generates form components generic to CiviCRM settings
38 *
39 */
40 class CRM_Admin_Form_Setting extends CRM_Core_Form {
41
42 protected $_defaults;
43 protected $_settings = array();
44
45 /**
46 * Set default values for the form.
47 * default values are retrieved from the database
48 *
49 *
50 * @return void
51 */
52 public function setDefaultValues() {
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
93 ) + $autoSearchFields;
94 $this->_defaults['autocompleteContactReference'] = array(
95 '1' => 1
96 ) + $cRSearchFields;
97
98 // we can handle all the ones defined in the metadata here. Others to be converted
99 foreach ($this->_settings as $setting => $group) {
100 $settingMetaData = civicrm_api('setting', 'getfields', array('version' => 3, 'name' => $setting));
101 $this->_defaults[$setting] = civicrm_api('setting', 'getvalue', array(
102 'version' => 3,
103 'name' => $setting,
104 'group' => $group,
105 'default_value' => CRM_Utils_Array::value('default', $settingMetaData['values'][$setting]),
106 )
107 );
108 }
109
110 $this->_defaults['enableSSL'] = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'enableSSL', NULL, 0);
111 $this->_defaults['verifySSL'] = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'verifySSL', NULL, 1);
112 }
113
114 return $this->_defaults;
115 }
116
117 /**
118 * Build the form object
119 *
120 * @return void
121 */
122 public function buildQuickForm() {
123 $session = CRM_Core_Session::singleton();
124 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1'));
125 $args = func_get_args();
126 $check = reset($args);
127 $this->addButtons(array(
128 array(
129 'type' => 'next',
130 'name' => ts('Save'),
131 'isDefault' => TRUE,
132 ),
133 array(
134 'type' => 'cancel',
135 'name' => ts('Cancel'),
136 ),
137 )
138 );
139
140 foreach ($this->_settings as $setting => $group) {
141 $settingMetaData = civicrm_api('setting', 'getfields', array('version' => 3, 'name' => $setting));
142 $props = $settingMetaData['values'][$setting];
143 if (isset($props['quick_form_type'])) {
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 CRM_Utils_Array::value($props['html_type'] == 'select' ? 'option_values' : 'html_attributes', $props, array()),
151 $props['html_type'] == 'select' ? CRM_Utils_Array::value('html_attributes', $props) : NULL
152 );
153 }
154 else {
155 $this->$add($setting, ts($props['title']));
156 }
157 $this->assign("{$setting}_description", ts($props['description']));
158 if ($setting == 'max_attachments') {
159 //temp hack @todo fix to get from metadata
160 $this->addRule('max_attachments', ts('Value should be a positive number'), 'positiveInteger');
161 }
162 if ($setting == 'maxFileSize') {
163 //temp hack
164 $this->addRule('maxFileSize', ts('Value should be a positive number'), 'positiveInteger');
165 }
166
167 }
168 }
169 }
170
171 /**
172 * Process the form submission
173 *
174 *
175 * @return void
176 */
177 public function postProcess() {
178 // store the submitted values in an array
179 $params = $this->controller->exportValues($this->_name);
180
181 self::commonProcess($params);
182 }
183
184 /**
185 * @param array $params
186 */
187 public function commonProcess(&$params) {
188
189 // save autocomplete search options
190 if (!empty($params['autocompleteContactSearch'])) {
191 $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
192 array_keys($params['autocompleteContactSearch'])
193 ) . CRM_Core_DAO::VALUE_SEPARATOR;
194
195 CRM_Core_BAO_Setting::setItem($value,
196 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
197 'contact_autocomplete_options'
198 );
199
200 unset($params['autocompleteContactSearch']);
201 }
202
203 // save autocomplete contact reference options
204 if (!empty($params['autocompleteContactReference'])) {
205 $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
206 array_keys($params['autocompleteContactReference'])
207 ) . CRM_Core_DAO::VALUE_SEPARATOR;
208
209 CRM_Core_BAO_Setting::setItem($value,
210 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
211 'contact_reference_options'
212 );
213
214 unset($params['autocompleteContactReference']);
215 }
216
217 // save components to be enabled
218 if (array_key_exists('enableComponents', $params)) {
219 CRM_Core_BAO_Setting::setItem($params['enableComponents'],
220 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'enable_components');
221
222 // unset params by emptying the values, so while retrieving we can detect and load from settings table
223 // instead of config-backend for backward compatibility. We could use unset() in later releases.
224 $params['enableComponents'] = $params['enableComponentIDs'] = array();
225 }
226
227 // save checksum timeout
228 if (!empty($params['checksumTimeout'])) {
229 CRM_Core_BAO_Setting::setItem($params['checksumTimeout'],
230 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
231 'checksum_timeout'
232 );
233 }
234
235 // update time for date formats when global time is changed
236 if (!empty($params['timeInputFormat'])) {
237 $query = "
238 UPDATE civicrm_preferences_date
239 SET time_format = %1
240 WHERE time_format IS NOT NULL
241 AND time_format <> ''
242 ";
243 $sqlParams = array(1 => array($params['timeInputFormat'], 'String'));
244 CRM_Core_DAO::executeQuery($query, $sqlParams);
245 }
246
247 // verify ssl peer option
248 if (isset($params['verifySSL'])) {
249 CRM_Core_BAO_Setting::setItem($params['verifySSL'],
250 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
251 'verifySSL'
252 );
253 unset($params['verifySSL']);
254 }
255
256 // force secure URLs
257 if (isset($params['enableSSL'])) {
258 CRM_Core_BAO_Setting::setItem($params['enableSSL'],
259 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
260 'enableSSL'
261 );
262 unset($params['enableSSL']);
263 }
264 $settings = array_intersect_key($params, $this->_settings);
265 $result = civicrm_api('setting', 'create', $settings + array('version' => 3));
266 foreach ($settings as $setting => $settingGroup) {
267 //@todo array_diff this
268 unset($params[$setting]);
269 }
270 CRM_Core_BAO_ConfigSetting::create($params);
271 CRM_Core_Session::setStatus(" ", ts('Changes Saved'), "success");
272 }
273
274 public function rebuildMenu() {
275 // ensure config is set with new values
276 $config = CRM_Core_Config::singleton(TRUE, TRUE);
277
278 // rebuild menu items
279 CRM_Core_Menu::store();
280
281 // also delete the IDS file so we can write a new correct one on next load
282 $configFile = $config->uploadDir . 'Config.IDS.ini';
283 @unlink($configFile);
284 }
285 }