Merge pull request #2452 from totten/lolas-freeform-CRM-14126
[civicrm-core.git] / CRM / Admin / Form / Setting.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 * This function sets the default values for the form.
47 * default values are retrieved from the database
48 *
49 * @access public
50 *
51 * @return void
52 */
53 function setDefaultValues() {
54 if (!$this->_defaults) {
55 $this->_defaults = array();
56 $formArray = array('Component', 'Localization');
57 $formMode = FALSE;
58 if (in_array($this->_name, $formArray)) {
59 $formMode = TRUE;
60 }
61
62 CRM_Core_BAO_ConfigSetting::retrieve($this->_defaults);
63
64 CRM_Core_Config_Defaults::setValues($this->_defaults, $formMode);
65
66 $list = array_flip(CRM_Core_OptionGroup::values('contact_autocomplete_options',
67 FALSE, FALSE, TRUE, NULL, 'name'
68 ));
69
70 $cRlist = array_flip(CRM_Core_OptionGroup::values('contact_reference_options',
71 FALSE, FALSE, TRUE, NULL, 'name'
72 ));
73
74 $listEnabled = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
75 'contact_autocomplete_options'
76 );
77 $cRlistEnabled = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
78 'contact_reference_options'
79 );
80
81 $autoSearchFields = array();
82 if (!empty($list) && !empty($listEnabled)) {
83 $autoSearchFields = array_combine($list, $listEnabled);
84 }
85
86 $cRSearchFields = array();
87 if (!empty($cRlist) && !empty($cRlistEnabled)) {
88 $cRSearchFields = array_combine($cRlist, $cRlistEnabled);
89 }
90
91 //Set defaults for autocomplete and contact reference options
92 $this->_defaults['autocompleteContactSearch'] = array(
93 '1' => 1) + $autoSearchFields;
94 $this->_defaults['autocompleteContactReference'] = array(
95 '1' => 1) + $cRSearchFields;
96
97 // we can handle all the ones defined in the metadata here. Others to be converted
98 foreach ($this->_settings as $setting => $group){
99 $settingMetaData = civicrm_api('setting', 'getfields', array('version' => 3, 'name' => $setting));
100 $this->_defaults[$setting] = civicrm_api('setting', 'getvalue', array(
101 'version' => 3,
102 'name' => $setting,
103 'group' => $group,
104 'default_value' => CRM_Utils_Array::value('default', $settingMetaData['values'][$setting])
105 )
106 );
107 }
108
109 $this->_defaults['enableSSL'] = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'enableSSL', NULL, 0);
110 $this->_defaults['verifySSL'] = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'verifySSL', NULL, 1);
111 }
112
113 return $this->_defaults;
114 }
115
116 /**
117 * Function to actually build the form
118 *
119 * @return void
120 * @access public
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 if(isset($settingMetaData['values'][$setting]['quick_form_type'])){
143 $add = 'add' . $settingMetaData['values'][$setting]['quick_form_type'];
144 if($add == 'addElement'){
145 $this->$add(
146 $settingMetaData['values'][$setting]['html_type'],
147 $setting,
148 ts($settingMetaData['values'][$setting]['title']),
149 CRM_Utils_Array::value('html_attributes', $settingMetaData['values'][$setting], array())
150 );
151 }
152 else{
153 $this->$add($setting, ts($settingMetaData['values'][$setting]['title']));
154 }
155 $this->assign("{$setting}_description", ts($settingMetaData['values'][$setting]['description']));
156 if($setting == 'max_attachments'){
157 //temp hack @todo fix to get from metadata
158 $this->addRule('max_attachments', ts('Value should be a positive number'), 'positiveInteger');
159 }
160 if($setting == 'maxFileSize'){
161 //temp hack
162 $this->addRule('maxFileSize', ts('Value should be a positive number'), 'positiveInteger');
163 }
164
165 }
166 }
167 }
168
169 /**
170 * Function to process the form
171 *
172 * @access public
173 *
174 * @return void
175 */
176 public function postProcess() {
177 // store the submitted values in an array
178 $params = $this->controller->exportValues($this->_name);
179
180 self::commonProcess($params);
181 }
182
183 public function commonProcess(&$params) {
184
185 // save autocomplete search options
186 if (!empty($params['autocompleteContactSearch'])) {
187 $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
188 array_keys($params['autocompleteContactSearch'])
189 ) . CRM_Core_DAO::VALUE_SEPARATOR;
190
191 CRM_Core_BAO_Setting::setItem($value,
192 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
193 'contact_autocomplete_options'
194 );
195
196 unset($params['autocompleteContactSearch']);
197 }
198
199 // save autocomplete contact reference options
200 if (!empty($params['autocompleteContactReference'])) {
201 $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
202 array_keys($params['autocompleteContactReference'])
203 ) . CRM_Core_DAO::VALUE_SEPARATOR;
204
205 CRM_Core_BAO_Setting::setItem($value,
206 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
207 'contact_reference_options'
208 );
209
210 unset($params['autocompleteContactReference']);
211 }
212
213 // save components to be enabled
214 if (array_key_exists('enableComponents', $params)) {
215 CRM_Core_BAO_Setting::setItem($params['enableComponents'],
216 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,'enable_components');
217
218 // unset params by emptying the values, so while retrieving we can detect and load from settings table
219 // instead of config-backend for backward compatibility. We could use unset() in later releases.
220 $params['enableComponents'] = $params['enableComponentIDs'] = array();
221 }
222
223 // save checksum timeout
224 if (!empty($params['checksumTimeout'])) {
225 CRM_Core_BAO_Setting::setItem($params['checksumTimeout'],
226 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
227 'checksum_timeout'
228 );
229 }
230
231 // update time for date formats when global time is changed
232 if (!empty($params['timeInputFormat'])) {
233 $query = "
234 UPDATE civicrm_preferences_date
235 SET time_format = %1
236 WHERE time_format IS NOT NULL
237 AND time_format <> ''
238 ";
239 $sqlParams = array(1 => array($params['timeInputFormat'], 'String'));
240 CRM_Core_DAO::executeQuery($query, $sqlParams);
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 CRM_Core_BAO_ConfigSetting::create($params);
267 CRM_Core_Session::setStatus(" ", ts('Changes Saved.'), "success");
268 }
269
270 public function rebuildMenu() {
271 // ensure config is set with new values
272 $config = CRM_Core_Config::singleton(TRUE, TRUE);
273
274 // rebuild menu items
275 CRM_Core_Menu::store();
276
277 // also delete the IDS file so we can write a new correct one on next load
278 $configFile = $config->uploadDir . 'Config.IDS.ini';
279 @unlink($configFile);
280 }
281 }
282