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
35 * This class generates form components generic to CiviCRM settings.
37 class CRM_Admin_Form_Setting
extends CRM_Core_Form
{
39 protected $_settings = array();
42 * Set default values for the form.
44 * Default values are retrieved from the database.
46 public function setDefaultValues() {
47 if (!$this->_defaults
) {
48 $this->_defaults
= array();
49 $formArray = array('Component', 'Localization');
51 if (in_array($this->_name
, $formArray)) {
55 CRM_Core_BAO_ConfigSetting
::retrieve($this->_defaults
);
57 $list = array_flip(CRM_Core_OptionGroup
::values('contact_autocomplete_options',
58 FALSE, FALSE, TRUE, NULL, 'name'
61 $cRlist = array_flip(CRM_Core_OptionGroup
::values('contact_reference_options',
62 FALSE, FALSE, TRUE, NULL, 'name'
65 $listEnabled = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
66 'contact_autocomplete_options'
68 $cRlistEnabled = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
69 'contact_reference_options'
72 $autoSearchFields = array();
73 if (!empty($list) && !empty($listEnabled)) {
74 $autoSearchFields = array_combine($list, $listEnabled);
77 $cRSearchFields = array();
78 if (!empty($cRlist) && !empty($cRlistEnabled)) {
79 $cRSearchFields = array_combine($cRlist, $cRlistEnabled);
82 //Set defaults for autocomplete and contact reference options
83 $this->_defaults
['autocompleteContactSearch'] = array(
85 ) +
$autoSearchFields;
86 $this->_defaults
['autocompleteContactReference'] = array(
90 // we can handle all the ones defined in the metadata here. Others to be converted
91 foreach ($this->_settings
as $setting => $group) {
92 $settingMetaData = civicrm_api('setting', 'getfields', array('version' => 3, 'name' => $setting));
93 $this->_defaults
[$setting] = civicrm_api('setting', 'getvalue', array(
97 'default_value' => CRM_Utils_Array
::value('default', $settingMetaData['values'][$setting]),
102 $this->_defaults
['enableSSL'] = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'enableSSL');
103 $this->_defaults
['verifySSL'] = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'verifySSL');
104 $this->_defaults
['enableComponents'] = Civi
::settings()->get('enable_components');
107 return $this->_defaults
;
111 * Build the form object.
113 public function buildQuickForm() {
114 $session = CRM_Core_Session
::singleton();
115 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin', 'reset=1'));
116 $args = func_get_args();
117 $check = reset($args);
118 $this->addButtons(array(
121 'name' => ts('Save'),
126 'name' => ts('Cancel'),
131 foreach ($this->_settings
as $setting => $group) {
132 $settingMetaData = civicrm_api('setting', 'getfields', array('version' => 3, 'name' => $setting));
133 $props = $settingMetaData['values'][$setting];
134 if (isset($props['quick_form_type'])) {
135 if (isset($props['pseudoconstant'])) {
136 $options = civicrm_api3('Setting', 'getoptions', array(
144 $add = 'add' . $props['quick_form_type'];
145 if ($add == 'addElement') {
150 ($options !== NULL) ?
$options['values'] : CRM_Utils_Array
::value('html_attributes', $props, array()),
151 ($options !== NULL) ? CRM_Utils_Array
::value('html_attributes', $props, array()) : NULL
154 elseif ($add == 'addSelect') {
155 $this->addElement('select', $setting, ts($props['title']), $options['values'], CRM_Utils_Array
::value('html_attributes', $props));
157 elseif ($add == 'addChainSelect') {
158 $this->addChainSelect($setting, array(
159 'label' => ts($props['title']),
163 $this->$add($setting, ts($props['title']));
165 $this->assign("{$setting}_description", ts($props['description']));
166 if ($setting == 'max_attachments') {
167 //temp hack @todo fix to get from metadata
168 $this->addRule('max_attachments', ts('Value should be a positive number'), 'positiveInteger');
170 if ($setting == 'maxFileSize') {
172 $this->addRule('maxFileSize', ts('Value should be a positive number'), 'positiveInteger');
180 * Get default entity.
184 public function getDefaultEntity() {
189 * Process the form submission.
191 public function postProcess() {
192 // store the submitted values in an array
193 $params = $this->controller
->exportValues($this->_name
);
195 self
::commonProcess($params);
201 * @todo Document what I do.
203 * @param array $params
205 public function commonProcess(&$params) {
207 // save autocomplete search options
208 if (!empty($params['autocompleteContactSearch'])) {
209 $value = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
210 array_keys($params['autocompleteContactSearch'])
211 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
213 CRM_Core_BAO_Setting
::setItem($value,
214 CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
215 'contact_autocomplete_options'
218 unset($params['autocompleteContactSearch']);
221 // save autocomplete contact reference options
222 if (!empty($params['autocompleteContactReference'])) {
223 $value = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
224 array_keys($params['autocompleteContactReference'])
225 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
227 CRM_Core_BAO_Setting
::setItem($value,
228 CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
229 'contact_reference_options'
232 unset($params['autocompleteContactReference']);
235 // save components to be enabled
236 if (array_key_exists('enableComponents', $params)) {
237 civicrm_api3('setting', 'create', array(
238 'enable_components' => $params['enableComponents'],
240 unset($params['enableComponents']);
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
,
249 unset($params['verifySSL']);
253 if (isset($params['enableSSL'])) {
254 CRM_Core_BAO_Setting
::setItem($params['enableSSL'],
255 CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
258 unset($params['enableSSL']);
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]);
266 if (!empty($result['error_message'])) {
267 CRM_Core_Session
::setStatus($result['error_message'], ts('Save Failed'), 'error');
270 //CRM_Core_BAO_ConfigSetting::create($params);
271 $params = CRM_Core_BAO_ConfigSetting
::filterSkipVars($params);
272 if (!empty($params)) {
273 CRM_Core_Error
::fatal('Unrecognized setting. This may be a config field which has not been properly migrated to a setting. (' . implode(', ', array_keys($params)) . ')');
276 CRM_Core_Config
::clearDBCache();
277 CRM_Utils_System
::flushCache();
278 CRM_Core_Resources
::singleton()->resetCacheCode();
280 CRM_Core_Session
::setStatus(" ", ts('Changes Saved'), "success");
283 public function rebuildMenu() {
284 // ensure config is set with new values
285 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
287 // rebuild menu items
288 CRM_Core_Menu
::store();
290 // also delete the IDS file so we can write a new correct one on next load
291 $configFile = $config->uploadDir
. 'Config.IDS.ini';
292 @unlink
($configFile);