Merge pull request #10427 from mattwire/report_bounce_type
[civicrm-core.git] / CRM / Admin / Form / Setting.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
6a488035
TO
32 */
33
34/**
ce064e4f 35 * This class generates form components generic to CiviCRM settings.
6a488035
TO
36 */
37class CRM_Admin_Form_Setting extends CRM_Core_Form {
38
6a488035
TO
39 protected $_settings = array();
40
41 /**
c490a46a 42 * Set default values for the form.
6a488035 43 *
ee0ce2ef 44 * Default values are retrieved from the database.
6a488035 45 */
00be9182 46 public function setDefaultValues() {
6a488035
TO
47 if (!$this->_defaults) {
48 $this->_defaults = array();
353ffa53
TO
49 $formArray = array('Component', 'Localization');
50 $formMode = FALSE;
6a488035
TO
51 if (in_array($this->_name, $formArray)) {
52 $formMode = TRUE;
53 }
54
55 CRM_Core_BAO_ConfigSetting::retrieve($this->_defaults);
56
6a488035 57 // we can handle all the ones defined in the metadata here. Others to be converted
9b873358 58 foreach ($this->_settings as $setting => $group) {
6a488035 59 $this->_defaults[$setting] = civicrm_api('setting', 'getvalue', array(
353ffa53
TO
60 'version' => 3,
61 'name' => $setting,
62 'group' => $group,
6a488035
TO
63 )
64 );
65 }
66
d434f797
TO
67 $this->_defaults['contact_autocomplete_options'] = self::getAutocompleteContactSearch();
68 $this->_defaults['contact_reference_options'] = self::getAutocompleteContactReference();
aaffa79f 69 $this->_defaults['enableSSL'] = Civi::settings()->get('enableSSL');
70 $this->_defaults['verifySSL'] = Civi::settings()->get('verifySSL');
f4fb2d79 71 $this->_defaults['enableComponents'] = Civi::settings()->get('enable_components');
6a488035
TO
72 }
73
74 return $this->_defaults;
75 }
76
77 /**
567b2076 78 * Build the form object.
6a488035
TO
79 */
80 public function buildQuickForm() {
089360bb 81 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1'));
6a488035
TO
82 $this->addButtons(array(
83 array(
84 'type' => 'next',
85 'name' => ts('Save'),
86 'isDefault' => TRUE,
87 ),
88 array(
89 'type' => 'cancel',
90 'name' => ts('Cancel'),
91 ),
92 )
93 );
94
30fcf833 95 $descriptions = array();
089360bb 96 $settingMetaData = $this->getSettingsMetaData();
97 foreach ($settingMetaData as $setting => $props) {
9b873358 98 if (isset($props['quick_form_type'])) {
8a61528e
TO
99 if (isset($props['pseudoconstant'])) {
100 $options = civicrm_api3('Setting', 'getoptions', array(
101 'field' => $setting,
102 ));
103 }
104 else {
105 $options = NULL;
106 }
107
fa8dc18c 108 $add = 'add' . $props['quick_form_type'];
9b873358 109 if ($add == 'addElement') {
6a488035 110 $this->$add(
fa8dc18c 111 $props['html_type'],
6a488035 112 $setting,
fa8dc18c 113 ts($props['title']),
8a61528e
TO
114 ($options !== NULL) ? $options['values'] : CRM_Utils_Array::value('html_attributes', $props, array()),
115 ($options !== NULL) ? CRM_Utils_Array::value('html_attributes', $props, array()) : NULL
6a488035
TO
116 );
117 }
52b03498 118 elseif ($add == 'addSelect') {
52b03498
TO
119 $this->addElement('select', $setting, ts($props['title']), $options['values'], CRM_Utils_Array::value('html_attributes', $props));
120 }
d434f797
TO
121 elseif ($add == 'addCheckBox') {
122 $this->addCheckBox($setting, ts($props['title']), $options['values'], NULL, CRM_Utils_Array::value('html_attributes', $props), NULL, NULL, array('&nbsp;&nbsp;'));
123 }
8a61528e
TO
124 elseif ($add == 'addChainSelect') {
125 $this->addChainSelect($setting, array(
126 'label' => ts($props['title']),
127 ));
128 }
42f32100 129 elseif ($add == 'addMonthDay') {
130 $this->add('date', $setting, ts($props['title']), CRM_Core_SelectValues::date(NULL, 'M d'));
131 }
92e4c2a5 132 else {
fa8dc18c 133 $this->$add($setting, ts($props['title']));
6a488035 134 }
30fcf833 135 // Migrate to using an array as easier in smart...
136 $descriptions[$setting] = ts($props['description']);
fa8dc18c 137 $this->assign("{$setting}_description", ts($props['description']));
9b873358 138 if ($setting == 'max_attachments') {
6a488035
TO
139 //temp hack @todo fix to get from metadata
140 $this->addRule('max_attachments', ts('Value should be a positive number'), 'positiveInteger');
141 }
9b873358 142 if ($setting == 'maxFileSize') {
6a488035
TO
143 //temp hack
144 $this->addRule('maxFileSize', ts('Value should be a positive number'), 'positiveInteger');
145 }
146
147 }
148 }
089360bb 149 // setting_description should be deprecated - see Mail.tpl for metadata based tpl.
30fcf833 150 $this->assign('setting_descriptions', $descriptions);
089360bb 151 $this->assign('settings_fields', $settingMetaData);
6a488035
TO
152 }
153
ce064e4f 154 /**
155 * Get default entity.
156 *
157 * @return string
158 */
52b03498
TO
159 public function getDefaultEntity() {
160 return 'Setting';
161 }
162
6a488035 163 /**
ee0ce2ef 164 * Process the form submission.
6a488035
TO
165 */
166 public function postProcess() {
167 // store the submitted values in an array
168 $params = $this->controller->exportValues($this->_name);
169
170 self::commonProcess($params);
171 }
172
e0ef6999 173 /**
5c9ff055
EM
174 * Common Process.
175 *
176 * @todo Document what I do.
177 *
c490a46a 178 * @param array $params
e0ef6999 179 */
6a488035
TO
180 public function commonProcess(&$params) {
181
182 // save autocomplete search options
d434f797
TO
183 if (!empty($params['contact_autocomplete_options'])) {
184 Civi::settings()->set('contact_autocomplete_options',
185 CRM_Utils_Array::implodePadded(array_keys($params['contact_autocomplete_options'])));
186 unset($params['contact_autocomplete_options']);
6a488035
TO
187 }
188
189 // save autocomplete contact reference options
d434f797
TO
190 if (!empty($params['contact_reference_options'])) {
191 Civi::settings()->set('contact_reference_options',
192 CRM_Utils_Array::implodePadded(array_keys($params['contact_reference_options'])));
193 unset($params['contact_reference_options']);
6a488035
TO
194 }
195
2efcf0c2 196 // save components to be enabled
9dda9790 197 if (array_key_exists('enableComponents', $params)) {
c8074a93
TO
198 civicrm_api3('setting', 'create', array(
199 'enable_components' => $params['enableComponents'],
200 ));
201 unset($params['enableComponents']);
3124edb3
DS
202 }
203
5e7f101a 204 foreach (array('verifySSL', 'enableSSL') as $name) {
205 if (isset($params[$name])) {
206 Civi::settings()->set($name, $params[$name]);
207 unset($params[$name]);
208 }
6a488035
TO
209 }
210 $settings = array_intersect_key($params, $this->_settings);
211 $result = civicrm_api('setting', 'create', $settings + array('version' => 3));
9b873358 212 foreach ($settings as $setting => $settingGroup) {
6a488035
TO
213 //@todo array_diff this
214 unset($params[$setting]);
215 }
af962699
TO
216 if (!empty($result['error_message'])) {
217 CRM_Core_Session::setStatus($result['error_message'], ts('Save Failed'), 'error');
218 }
219
7e0c769c
TO
220 //CRM_Core_BAO_ConfigSetting::create($params);
221 $params = CRM_Core_BAO_ConfigSetting::filterSkipVars($params);
222 if (!empty($params)) {
223 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)) . ')');
224 }
b08fb110
CW
225
226 CRM_Core_Config::clearDBCache();
227 CRM_Utils_System::flushCache();
228 CRM_Core_Resources::singleton()->resetCacheCode();
229
4481526f 230 CRM_Core_Session::setStatus(" ", ts('Changes Saved'), "success");
6a488035
TO
231 }
232
233 public function rebuildMenu() {
234 // ensure config is set with new values
235 $config = CRM_Core_Config::singleton(TRUE, TRUE);
236
237 // rebuild menu items
238 CRM_Core_Menu::store();
239
240 // also delete the IDS file so we can write a new correct one on next load
241 $configFile = $config->uploadDir . 'Config.IDS.ini';
242 @unlink($configFile);
243 }
96025800 244
d434f797
TO
245 /**
246 * Ugh, this shouldn't exist.
247 *
248 * Get the selected values of "contact_reference_options" formatted for checkboxes.
249 *
250 * @return array
251 */
252 public static function getAutocompleteContactReference() {
253 $cRlist = array_flip(CRM_Core_OptionGroup::values('contact_reference_options',
254 FALSE, FALSE, TRUE, NULL, 'name'
255 ));
256 $cRlistEnabled = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
257 'contact_reference_options'
258 );
259 $cRSearchFields = array();
260 if (!empty($cRlist) && !empty($cRlistEnabled)) {
261 $cRSearchFields = array_combine($cRlist, $cRlistEnabled);
262 }
263 return array(
264 '1' => 1,
265 ) + $cRSearchFields;
266 }
267
268 /**
269 * Ugh, this shouldn't exist.
270 *
271 * Get the selected values of "contact_autocomplete_options" formatted for checkboxes.
272 *
273 * @return array
274 */
275 public static function getAutocompleteContactSearch() {
276 $list = array_flip(CRM_Core_OptionGroup::values('contact_autocomplete_options',
277 FALSE, FALSE, TRUE, NULL, 'name'
278 ));
279 $listEnabled = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
280 'contact_autocomplete_options'
281 );
282 $autoSearchFields = array();
283 if (!empty($list) && !empty($listEnabled)) {
284 $autoSearchFields = array_combine($list, $listEnabled);
285 }
286 //Set defaults for autocomplete and contact reference options
287 return array(
288 '1' => 1,
289 ) + $autoSearchFields;
290 }
291
089360bb 292 /**
293 * Get the metadata relating to the settings on the form, ordered by the keys in $this->_settings.
294 *
295 * @return array
296 */
297 protected function getSettingsMetaData() {
298 $allSettingMetaData = civicrm_api3('setting', 'getfields', array());
299 $settingMetaData = array_intersect_key($allSettingMetaData['values'], $this->_settings);
300 // This array_merge re-orders to the key order of $this->_settings.
301 $settingMetaData = array_merge($this->_settings, $settingMetaData);
302 return $settingMetaData;
303 }
304
6a488035 305}