3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
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 // we can handle all the ones defined in the metadata here. Others to be converted
58 foreach ($this->_settings
as $setting => $group) {
59 $this->_defaults
[$setting] = civicrm_api('setting', 'getvalue', array(
67 $this->_defaults
['contact_autocomplete_options'] = self
::getAutocompleteContactSearch();
68 $this->_defaults
['contact_reference_options'] = self
::getAutocompleteContactReference();
69 $this->_defaults
['enableSSL'] = Civi
::settings()->get('enableSSL');
70 $this->_defaults
['verifySSL'] = Civi
::settings()->get('verifySSL');
71 $this->_defaults
['environment'] = CRM_Core_Config
::environment();
72 $this->_defaults
['enableComponents'] = Civi
::settings()->get('enable_components');
75 return $this->_defaults
;
79 * Build the form object.
81 public function buildQuickForm() {
82 CRM_Core_Session
::singleton()->pushUserContext(CRM_Utils_System
::url('civicrm/admin', 'reset=1'));
83 $this->addButtons(array(
91 'name' => ts('Cancel'),
96 $descriptions = array();
97 $settingMetaData = $this->getSettingsMetaData();
98 foreach ($settingMetaData as $setting => $props) {
99 if (isset($props['quick_form_type'])) {
100 if (isset($props['pseudoconstant'])) {
101 $options = civicrm_api3('Setting', 'getoptions', array(
108 //Load input as readonly whose values are overridden in civicrm.settings.php.
109 if (Civi
::settings()->getMandatory($setting)) {
110 $props['html_attributes']['readonly'] = TRUE;
114 $add = 'add' . $props['quick_form_type'];
115 if ($add == 'addElement') {
120 ($options !== NULL) ?
$options['values'] : CRM_Utils_Array
::value('html_attributes', $props, array()),
121 ($options !== NULL) ? CRM_Utils_Array
::value('html_attributes', $props, array()) : NULL
124 elseif ($add == 'addSelect') {
125 $this->addElement('select', $setting, ts($props['title']), $options['values'], CRM_Utils_Array
::value('html_attributes', $props));
127 elseif ($add == 'addCheckBox') {
128 $this->addCheckBox($setting, ts($props['title']), $options['values'], NULL, CRM_Utils_Array
::value('html_attributes', $props), NULL, NULL, array(' '));
130 elseif ($add == 'addChainSelect') {
131 $this->addChainSelect($setting, array(
132 'label' => ts($props['title']),
135 elseif ($add == 'addMonthDay') {
136 $this->add('date', $setting, ts($props['title']), CRM_Core_SelectValues
::date(NULL, 'M d'));
139 $this->$add($setting, ts($props['title']));
141 // Migrate to using an array as easier in smart...
142 $descriptions[$setting] = ts($props['description']);
143 $this->assign("{$setting}_description", ts($props['description']));
144 if ($setting == 'max_attachments') {
145 //temp hack @todo fix to get from metadata
146 $this->addRule('max_attachments', ts('Value should be a positive number'), 'positiveInteger');
148 if ($setting == 'maxFileSize') {
150 $this->addRule('maxFileSize', ts('Value should be a positive number'), 'positiveInteger');
155 if (!empty($setStatus)) {
156 CRM_Core_Session
::setStatus(ts("Some fields are loaded as 'readonly' as they have been set (overridden) in civicrm.settings.php."), '', 'info', array('expires' => 0));
158 // setting_description should be deprecated - see Mail.tpl for metadata based tpl.
159 $this->assign('setting_descriptions', $descriptions);
160 $this->assign('settings_fields', $settingMetaData);
164 * Get default entity.
168 public function getDefaultEntity() {
173 * Process the form submission.
175 public function postProcess() {
176 // store the submitted values in an array
177 $params = $this->controller
->exportValues($this->_name
);
179 self
::commonProcess($params);
185 * @todo Document what I do.
187 * @param array $params
189 public function commonProcess(&$params) {
191 // save autocomplete search options
192 if (!empty($params['contact_autocomplete_options'])) {
193 Civi
::settings()->set('contact_autocomplete_options',
194 CRM_Utils_Array
::implodePadded(array_keys($params['contact_autocomplete_options'])));
195 unset($params['contact_autocomplete_options']);
198 // save autocomplete contact reference options
199 if (!empty($params['contact_reference_options'])) {
200 Civi
::settings()->set('contact_reference_options',
201 CRM_Utils_Array
::implodePadded(array_keys($params['contact_reference_options'])));
202 unset($params['contact_reference_options']);
205 // save components to be enabled
206 if (array_key_exists('enableComponents', $params)) {
207 civicrm_api3('setting', 'create', array(
208 'enable_components' => $params['enableComponents'],
210 unset($params['enableComponents']);
213 foreach (array('verifySSL', 'enableSSL') as $name) {
214 if (isset($params[$name])) {
215 Civi
::settings()->set($name, $params[$name]);
216 unset($params[$name]);
219 $settings = array_intersect_key($params, $this->_settings
);
220 $result = civicrm_api('setting', 'create', $settings +
array('version' => 3));
221 foreach ($settings as $setting => $settingGroup) {
222 //@todo array_diff this
223 unset($params[$setting]);
225 if (!empty($result['error_message'])) {
226 CRM_Core_Session
::setStatus($result['error_message'], ts('Save Failed'), 'error');
229 //CRM_Core_BAO_ConfigSetting::create($params);
230 $params = CRM_Core_BAO_ConfigSetting
::filterSkipVars($params);
231 if (!empty($params)) {
232 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)) . ')');
235 CRM_Core_Config
::clearDBCache();
236 CRM_Utils_System
::flushCache();
237 CRM_Core_Resources
::singleton()->resetCacheCode();
239 CRM_Core_Session
::setStatus(" ", ts('Changes Saved'), "success");
242 public function rebuildMenu() {
243 // ensure config is set with new values
244 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
246 // rebuild menu items
247 CRM_Core_Menu
::store();
249 // also delete the IDS file so we can write a new correct one on next load
250 $configFile = $config->uploadDir
. 'Config.IDS.ini';
251 @unlink
($configFile);
255 * Ugh, this shouldn't exist.
257 * Get the selected values of "contact_reference_options" formatted for checkboxes.
261 public static function getAutocompleteContactReference() {
262 $cRlist = array_flip(CRM_Core_OptionGroup
::values('contact_reference_options',
263 FALSE, FALSE, TRUE, NULL, 'name'
265 $cRlistEnabled = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
266 'contact_reference_options'
268 $cRSearchFields = array();
269 if (!empty($cRlist) && !empty($cRlistEnabled)) {
270 $cRSearchFields = array_combine($cRlist, $cRlistEnabled);
278 * Ugh, this shouldn't exist.
280 * Get the selected values of "contact_autocomplete_options" formatted for checkboxes.
284 public static function getAutocompleteContactSearch() {
285 $list = array_flip(CRM_Core_OptionGroup
::values('contact_autocomplete_options',
286 FALSE, FALSE, TRUE, NULL, 'name'
288 $listEnabled = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
289 'contact_autocomplete_options'
291 $autoSearchFields = array();
292 if (!empty($list) && !empty($listEnabled)) {
293 $autoSearchFields = array_combine($list, $listEnabled);
295 //Set defaults for autocomplete and contact reference options
298 ) +
$autoSearchFields;
302 * Get the metadata relating to the settings on the form, ordered by the keys in $this->_settings.
306 protected function getSettingsMetaData() {
307 $allSettingMetaData = civicrm_api3('setting', 'getfields', array());
308 $settingMetaData = array_intersect_key($allSettingMetaData['values'], $this->_settings
);
309 // This array_merge re-orders to the key order of $this->_settings.
310 $settingMetaData = array_merge($this->_settings
, $settingMetaData);
311 return $settingMetaData;