Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
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 None | |
52 | */ | |
94c11eeb | 53 | function setDefaultValues() { |
6a488035 TO |
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 | $sql = " | |
113 | SELECT time_format | |
114 | FROM civicrm_preferences_date | |
115 | WHERE time_format IS NOT NULL | |
116 | AND time_format <> '' | |
117 | LIMIT 1 | |
118 | "; | |
119 | $this->_defaults['timeInputFormat'] = CRM_Core_DAO::singleValueQuery($sql); | |
120 | } | |
121 | ||
122 | return $this->_defaults; | |
123 | } | |
124 | ||
125 | /** | |
126 | * Function to actually build the form | |
127 | * | |
128 | * @return None | |
129 | * @access public | |
130 | */ | |
131 | public function buildQuickForm() { | |
132 | $session = CRM_Core_Session::singleton(); | |
133 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1')); | |
134 | $args = func_get_args(); | |
135 | $check = reset($args); | |
136 | $this->addButtons(array( | |
137 | array( | |
138 | 'type' => 'next', | |
139 | 'name' => ts('Save'), | |
140 | 'isDefault' => TRUE, | |
141 | ), | |
142 | array( | |
143 | 'type' => 'cancel', | |
144 | 'name' => ts('Cancel'), | |
145 | ), | |
146 | ) | |
147 | ); | |
148 | ||
149 | foreach ($this->_settings as $setting => $group){ | |
150 | $settingMetaData = civicrm_api('setting', 'getfields', array('version' => 3, 'name' => $setting)); | |
151 | if(isset($settingMetaData['values'][$setting]['quick_form_type'])){ | |
152 | $add = 'add' . $settingMetaData['values'][$setting]['quick_form_type']; | |
153 | if($add == 'addElement'){ | |
154 | $this->$add( | |
155 | $settingMetaData['values'][$setting]['html_type'], | |
156 | $setting, | |
157 | ts($settingMetaData['values'][$setting]['title']), | |
158 | CRM_Utils_Array::value('html_attributes', $settingMetaData['values'][$setting], array()) | |
159 | ); | |
160 | } | |
161 | else{ | |
162 | $this->$add($setting, ts($settingMetaData['values'][$setting]['title'])); | |
163 | } | |
164 | $this->assign("{$setting}_description", ts($settingMetaData['values'][$setting]['description'])); | |
165 | if($setting == 'max_attachments'){ | |
166 | //temp hack @todo fix to get from metadata | |
167 | $this->addRule('max_attachments', ts('Value should be a positive number'), 'positiveInteger'); | |
168 | } | |
169 | if($setting == 'maxFileSize'){ | |
170 | //temp hack | |
171 | $this->addRule('maxFileSize', ts('Value should be a positive number'), 'positiveInteger'); | |
172 | } | |
173 | ||
174 | } | |
175 | } | |
176 | } | |
177 | ||
178 | /** | |
179 | * Function to process the form | |
180 | * | |
181 | * @access public | |
182 | * | |
183 | * @return None | |
184 | */ | |
185 | public function postProcess() { | |
186 | // store the submitted values in an array | |
187 | $params = $this->controller->exportValues($this->_name); | |
188 | ||
189 | self::commonProcess($params); | |
190 | } | |
191 | ||
192 | public function commonProcess(&$params) { | |
193 | ||
194 | // save autocomplete search options | |
195 | if (CRM_Utils_Array::value('autocompleteContactSearch', $params)) { | |
196 | $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
197 | array_keys($params['autocompleteContactSearch']) | |
198 | ) . CRM_Core_DAO::VALUE_SEPARATOR; | |
199 | ||
200 | CRM_Core_BAO_Setting::setItem($value, | |
201 | CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
202 | 'contact_autocomplete_options' | |
203 | ); | |
204 | ||
205 | unset($params['autocompleteContactSearch']); | |
206 | } | |
207 | ||
208 | // save autocomplete contact reference options | |
209 | if (CRM_Utils_Array::value('autocompleteContactReference', $params)) { | |
210 | $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
211 | array_keys($params['autocompleteContactReference']) | |
212 | ) . CRM_Core_DAO::VALUE_SEPARATOR; | |
213 | ||
214 | CRM_Core_BAO_Setting::setItem($value, | |
215 | CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
216 | 'contact_reference_options' | |
217 | ); | |
218 | ||
219 | unset($params['autocompleteContactReference']); | |
220 | } | |
221 | ||
3124edb3 DS |
222 | // save components to be enabled |
223 | if (CRM_Utils_Array::value('enableComponents', $params)) { | |
224 | CRM_Core_BAO_Setting::setItem($params['enableComponents'], | |
225 | CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,'enable_components'); | |
226 | ||
227 | // unset params by emptying the values, so while retrieving we can detect and load from settings table | |
228 | // instead of config-backend for backward compatibility. We could use unset() in later releases. | |
229 | $params['enableComponents'] = $params['enableComponentIDs'] = array(); | |
230 | } | |
231 | ||
6a488035 TO |
232 | // save checksum timeout |
233 | if (CRM_Utils_Array::value('checksumTimeout', $params)) { | |
234 | CRM_Core_BAO_Setting::setItem($params['checksumTimeout'], | |
235 | CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
236 | 'checksum_timeout' | |
237 | ); | |
238 | } | |
239 | ||
240 | // update time for date formats when global time is changed | |
241 | if (CRM_Utils_Array::value('timeInputFormat', $params)) { | |
242 | $query = " | |
243 | UPDATE civicrm_preferences_date | |
244 | SET time_format = %1 | |
245 | WHERE time_format IS NOT NULL | |
246 | AND time_format <> '' | |
247 | "; | |
248 | $sqlParams = array(1 => array($params['timeInputFormat'], 'String')); | |
249 | CRM_Core_DAO::executeQuery($query, $sqlParams); | |
250 | ||
251 | unset($params['timeInputFormat']); | |
252 | } | |
253 | ||
254 | // verify ssl peer option | |
255 | if (isset($params['verifySSL'])) { | |
256 | CRM_Core_BAO_Setting::setItem($params['verifySSL'], | |
257 | CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
258 | 'verifySSL' | |
259 | ); | |
260 | unset($params['verifySSL']); | |
261 | } | |
262 | ||
263 | // force secure URLs | |
264 | if (isset($params['enableSSL'])) { | |
265 | CRM_Core_BAO_Setting::setItem($params['enableSSL'], | |
266 | CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
267 | 'enableSSL' | |
268 | ); | |
269 | unset($params['enableSSL']); | |
270 | } | |
271 | $settings = array_intersect_key($params, $this->_settings); | |
272 | $result = civicrm_api('setting', 'create', $settings + array('version' => 3)); | |
273 | foreach ($settings as $setting => $settingGroup){ | |
274 | //@todo array_diff this | |
275 | unset($params[$setting]); | |
276 | } | |
277 | CRM_Core_BAO_ConfigSetting::create($params); | |
278 | CRM_Core_Session::setStatus(" ", ts('Changes Saved.'), "success"); | |
279 | } | |
280 | ||
281 | public function rebuildMenu() { | |
282 | // ensure config is set with new values | |
283 | $config = CRM_Core_Config::singleton(TRUE, TRUE); | |
284 | ||
285 | // rebuild menu items | |
286 | CRM_Core_Menu::store(); | |
287 | ||
288 | // also delete the IDS file so we can write a new correct one on next load | |
289 | $configFile = $config->uploadDir . 'Config.IDS.ini'; | |
290 | @unlink($configFile); | |
291 | } | |
292 | } | |
293 |