3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
36 * This class generates form components generic to CiviCRM settings
38 class CRM_Admin_Form_Setting
extends CRM_Core_Form
{
40 protected $_settings = array();
43 * Set default values for the form.
45 * Default values are retrieved from the database.
47 public function setDefaultValues() {
48 if (!$this->_defaults
) {
49 $this->_defaults
= array();
50 $formArray = array('Component', 'Localization');
52 if (in_array($this->_name
, $formArray)) {
56 CRM_Core_BAO_ConfigSetting
::retrieve($this->_defaults
);
58 CRM_Core_Config_Defaults
::setValues($this->_defaults
, $formMode);
60 $list = array_flip(CRM_Core_OptionGroup
::values('contact_autocomplete_options',
61 FALSE, FALSE, TRUE, NULL, 'name'
64 $cRlist = array_flip(CRM_Core_OptionGroup
::values('contact_reference_options',
65 FALSE, FALSE, TRUE, NULL, 'name'
68 $listEnabled = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
69 'contact_autocomplete_options'
71 $cRlistEnabled = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
72 'contact_reference_options'
75 $autoSearchFields = array();
76 if (!empty($list) && !empty($listEnabled)) {
77 $autoSearchFields = array_combine($list, $listEnabled);
80 $cRSearchFields = array();
81 if (!empty($cRlist) && !empty($cRlistEnabled)) {
82 $cRSearchFields = array_combine($cRlist, $cRlistEnabled);
85 //Set defaults for autocomplete and contact reference options
86 $this->_defaults
['autocompleteContactSearch'] = array(
88 ) +
$autoSearchFields;
89 $this->_defaults
['autocompleteContactReference'] = array(
93 // we can handle all the ones defined in the metadata here. Others to be converted
94 foreach ($this->_settings
as $setting => $group) {
95 $settingMetaData = civicrm_api('setting', 'getfields', array('version' => 3, 'name' => $setting));
96 $this->_defaults
[$setting] = civicrm_api('setting', 'getvalue', array(
100 'default_value' => CRM_Utils_Array
::value('default', $settingMetaData['values'][$setting]),
105 $this->_defaults
['enableSSL'] = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'enableSSL', NULL, 0);
106 $this->_defaults
['verifySSL'] = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'verifySSL', NULL, 1);
109 return $this->_defaults
;
113 * Build the form object.
115 public function buildQuickForm() {
116 $session = CRM_Core_Session
::singleton();
117 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin', 'reset=1'));
118 $args = func_get_args();
119 $check = reset($args);
120 $this->addButtons(array(
123 'name' => ts('Save'),
128 'name' => ts('Cancel'),
133 foreach ($this->_settings
as $setting => $group) {
134 $settingMetaData = civicrm_api('setting', 'getfields', array('version' => 3, 'name' => $setting));
135 $props = $settingMetaData['values'][$setting];
136 if (isset($props['quick_form_type'])) {
137 $add = 'add' . $props['quick_form_type'];
138 if ($add == 'addElement') {
143 CRM_Utils_Array
::value($props['html_type'] == 'select' ?
'option_values' : 'html_attributes', $props, array()),
144 $props['html_type'] == 'select' ? CRM_Utils_Array
::value('html_attributes', $props) : NULL
148 $this->$add($setting, ts($props['title']));
150 $this->assign("{$setting}_description", ts($props['description']));
151 if ($setting == 'max_attachments') {
152 //temp hack @todo fix to get from metadata
153 $this->addRule('max_attachments', ts('Value should be a positive number'), 'positiveInteger');
155 if ($setting == 'maxFileSize') {
157 $this->addRule('maxFileSize', ts('Value should be a positive number'), 'positiveInteger');
165 * Process the form submission.
167 public function postProcess() {
168 // store the submitted values in an array
169 $params = $this->controller
->exportValues($this->_name
);
171 self
::commonProcess($params);
177 * @todo Document what I do.
179 * @param array $params
181 public function commonProcess(&$params) {
183 // save autocomplete search options
184 if (!empty($params['autocompleteContactSearch'])) {
185 $value = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
186 array_keys($params['autocompleteContactSearch'])
187 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
189 CRM_Core_BAO_Setting
::setItem($value,
190 CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
191 'contact_autocomplete_options'
194 unset($params['autocompleteContactSearch']);
197 // save autocomplete contact reference options
198 if (!empty($params['autocompleteContactReference'])) {
199 $value = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
200 array_keys($params['autocompleteContactReference'])
201 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
203 CRM_Core_BAO_Setting
::setItem($value,
204 CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
205 'contact_reference_options'
208 unset($params['autocompleteContactReference']);
211 // save components to be enabled
212 if (array_key_exists('enableComponents', $params)) {
213 civicrm_api3('setting', 'create', array(
214 'enable_components' => $params['enableComponents'],
216 unset($params['enableComponents']);
219 // save checksum timeout
220 if (!empty($params['checksumTimeout'])) {
221 CRM_Core_BAO_Setting
::setItem($params['checksumTimeout'],
222 CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
227 // update time for date formats when global time is changed
228 if (!empty($params['timeInputFormat'])) {
230 UPDATE civicrm_preferences_date
232 WHERE time_format IS NOT NULL
233 AND time_format <> ''
235 $sqlParams = array(1 => array($params['timeInputFormat'], 'String'));
236 CRM_Core_DAO
::executeQuery($query, $sqlParams);
239 // verify ssl peer option
240 if (isset($params['verifySSL'])) {
241 CRM_Core_BAO_Setting
::setItem($params['verifySSL'],
242 CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
245 unset($params['verifySSL']);
249 if (isset($params['enableSSL'])) {
250 CRM_Core_BAO_Setting
::setItem($params['enableSSL'],
251 CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
254 unset($params['enableSSL']);
256 $settings = array_intersect_key($params, $this->_settings
);
257 $result = civicrm_api('setting', 'create', $settings +
array('version' => 3));
258 foreach ($settings as $setting => $settingGroup) {
259 //@todo array_diff this
260 unset($params[$setting]);
262 CRM_Core_BAO_ConfigSetting
::create($params);
264 CRM_Core_Config
::clearDBCache();
265 CRM_Utils_System
::flushCache();
266 CRM_Core_Resources
::singleton()->resetCacheCode();
268 CRM_Core_Session
::setStatus(" ", ts('Changes Saved'), "success");
271 public function rebuildMenu() {
272 // ensure config is set with new values
273 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
275 // rebuild menu items
276 CRM_Core_Menu
::store();
278 // also delete the IDS file so we can write a new correct one on next load
279 $configFile = $config->uploadDir
. 'Config.IDS.ini';
280 @unlink
($configFile);