(NFC) Apply upcoming civicrm/coder policies (batch 1)
[civicrm-core.git] / CRM / Admin / Form / Preferences.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33
34/**
ce064e4f 35 * Base class for settings forms.
6a488035
TO
36 */
37class CRM_Admin_Form_Preferences extends CRM_Core_Form {
a55c9b35 38
39 use CRM_Admin_Form_SettingTrait;
40
6a488035
TO
41 protected $_system = FALSE;
42 protected $_contactID = NULL;
3a936dab 43 public $_action = NULL;
6a488035
TO
44
45 protected $_checkbox = NULL;
46
b70c6629 47 protected $_varNames = [];
6a488035
TO
48
49 protected $_config = NULL;
50
51 protected $_params = NULL;
52
00be9182 53 public function preProcess() {
6a488035
TO
54 $this->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive',
55 $this, FALSE
56 );
57 $this->_system = CRM_Utils_Request::retrieve('system', 'Boolean',
58 $this, FALSE, TRUE
59 );
60 $this->_action = CRM_Utils_Request::retrieve('action', 'String',
61 $this, FALSE, 'update'
62 );
63 if (isset($action)) {
64 $this->assign('action', $action);
65 }
66
67 $session = CRM_Core_Session::singleton();
68
69 $this->_config = new CRM_Core_DAO();
70
71 if ($this->_system) {
72 if (CRM_Core_Permission::check('administer CiviCRM')) {
73 $this->_contactID = NULL;
74 }
75 else {
76 CRM_Utils_System::fatal('You do not have permission to edit preferences');
77 }
78 $this->_config->contact_id = NULL;
79 }
80 else {
81 if (!$this->_contactID) {
82 $this->_contactID = $session->get('userID');
83 if (!$this->_contactID) {
84 CRM_Utils_System::fatal('Could not retrieve contact id');
85 }
86 $this->set('cid', $this->_contactID);
87 }
88 $this->_config->contact_id = $this->_contactID;
89 }
90
a55c9b35 91 $this->addFieldsDefinedInSettingsMetadata();
26cc63d3 92 $settings = Civi::settings();
601361a3 93 // @todo replace this by defining all in settings.
6a488035 94 foreach ($this->_varNames as $groupName => $settingNames) {
2f6c641a 95 CRM_Core_Error::deprecatedFunctionWarning('deprecated use of preferences form. This will be removed from core soon');
26cc63d3
TO
96 foreach ($settingNames as $settingName => $options) {
97 $this->_config->$settingName = $settings->get($settingName);
6a488035
TO
98 }
99 }
100 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1'));
101 }
102
e0ef6999
EM
103 /**
104 * @return array
105 */
00be9182 106 public function setDefaultValues() {
be2fb01f 107 $this->_defaults = [];
6a488035 108
601361a3 109 $this->setDefaultsForMetadataDefinedFields();
6a488035 110 foreach ($this->_varNames as $groupName => $settings) {
2f6c641a 111 CRM_Core_Error::deprecatedFunctionWarning('deprecated use of preferences form. This will be removed from core soon');
6a488035 112 foreach ($settings as $settingName => $settingDetails) {
601361a3 113 $this->_defaults[$settingName] = isset($this->_config->$settingName) ? $this->_config->$settingName : CRM_Utils_Array::value('default', $settingDetails, NULL);
6a488035
TO
114 }
115 }
116
601361a3 117 return $this->_defaults;
6a488035
TO
118 }
119
e0ef6999 120 /**
6821aa1d 121 * @todo deprecate in favour of setting using metadata.
122 *
e0ef6999
EM
123 * @param $defaults
124 */
00be9182 125 public function cbsDefaultValues(&$defaults) {
6a488035
TO
126
127 foreach ($this->_varNames as $groupName => $groupValues) {
2f6c641a 128 CRM_Core_Error::deprecatedFunctionWarning('deprecated use of preferences form. This will be removed from core soon');
6a488035
TO
129 foreach ($groupValues as $settingName => $fieldValue) {
130 if ($fieldValue['html_type'] == 'checkboxes') {
131 if (isset($this->_config->$settingName) &&
132 $this->_config->$settingName
133 ) {
134 $value = explode(CRM_Core_DAO::VALUE_SEPARATOR,
135 substr($this->_config->$settingName, 1, -1)
136 );
137 if (!empty($value)) {
be2fb01f 138 $defaults[$settingName] = [];
6a488035
TO
139 foreach ($value as $n => $v) {
140 $defaults[$settingName][$v] = 1;
141 }
142 }
143 }
144 }
145 }
146 }
147 }
148
149 /**
eceb18cc 150 * Build the form object.
6a488035
TO
151 */
152 public function buildQuickForm() {
153 parent::buildQuickForm();
154
6a488035 155 if (!empty($this->_varNames)) {
2f6c641a 156 CRM_Core_Error::deprecatedFunctionWarning('deprecated use of preferences form. This will be removed from core soon');
6a488035
TO
157 foreach ($this->_varNames as $groupName => $groupValues) {
158 $formName = CRM_Utils_String::titleToVar($groupName);
159 $this->assign('formName', $formName);
be2fb01f 160 $fields = [];
6a488035
TO
161 foreach ($groupValues as $fieldName => $fieldValue) {
162 $fields[$fieldName] = $fieldValue;
163
164 switch ($fieldValue['html_type']) {
165 case 'text':
166 $this->addElement('text',
167 $fieldName,
168 $fieldValue['title'],
be2fb01f 169 [
6a488035
TO
170 'maxlength' => 64,
171 'size' => 32,
be2fb01f 172 ]
6a488035
TO
173 );
174 break;
175
176 case 'textarea':
65f9bd70
KJ
177 case 'checkbox':
178 $this->add($fieldValue['html_type'],
6a488035
TO
179 $fieldName,
180 $fieldValue['title']
181 );
182 break;
183
65f9bd70
KJ
184 case 'radio':
185 $options = CRM_Core_OptionGroup::values($fieldName, FALSE, FALSE, TRUE);
186 $this->addRadio($fieldName, $fieldValue['title'], $options, NULL, '&nbsp;&nbsp;');
6a488035
TO
187 break;
188
04c56532 189 case 'YesNo':
be2fb01f 190 $this->addRadio($fieldName, $fieldValue['title'], [0 => 'No', 1 => 'Yes'], NULL, '&nbsp;&nbsp;');
04c56532
SL
191 break;
192
6a488035
TO
193 case 'checkboxes':
194 $options = array_flip(CRM_Core_OptionGroup::values($fieldName, FALSE, FALSE, TRUE));
be2fb01f 195 $newOptions = [];
6a488035
TO
196 foreach ($options as $key => $val) {
197 $newOptions[$key] = $val;
198 }
199 $this->addCheckBox($fieldName,
200 $fieldValue['title'],
201 $newOptions,
202 NULL, NULL, NULL, NULL,
be2fb01f 203 ['&nbsp;&nbsp;', '&nbsp;&nbsp;', '<br/>']
6a488035
TO
204 );
205 break;
8b01c2f2 206
b8ce3328 207 case 'select':
208 $this->addElement('select',
209 $fieldName,
210 $fieldValue['title'],
b5407aa9
JP
211 $fieldValue['option_values'],
212 CRM_Utils_Array::value('attributes', $fieldValue)
b8ce3328 213 );
214 break;
215
fff0f449 216 case 'wysiwyg':
5d51a2f9 217 $this->add('wysiwyg', $fieldName, $fieldValue['title'], $fieldValue['attributes']);
fff0f449 218 break;
219
8b01c2f2 220 case 'entity_reference':
be2fb01f 221 $this->addEntityRef($fieldName, $fieldValue['title'], CRM_Utils_Array::value('options', $fieldValue, []));
6a488035
TO
222 }
223 }
224
225 $fields = CRM_Utils_Array::crmArraySortByField($fields, 'weight');
226 $this->assign('fields', $fields);
227 }
228 }
229
be2fb01f 230 $this->addButtons([
0d48f1cc
TO
231 [
232 'type' => 'next',
233 'name' => ts('Save'),
234 'isDefault' => TRUE,
235 ],
236 [
237 'type' => 'cancel',
238 'name' => ts('Cancel'),
239 ],
240 ]);
6a488035
TO
241
242 if ($this->_action == CRM_Core_Action::VIEW) {
243 $this->freeze();
244 }
245 }
246
247 /**
eceb18cc 248 * Process the form submission.
6a488035
TO
249 */
250 public function postProcess() {
251 $config = CRM_Core_Config::singleton();
252 if ($this->_action == CRM_Core_Action::VIEW) {
253 return;
254 }
255
256 $this->_params = $this->controller->exportValues($this->_name);
257
258 $this->postProcessCommon();
259 }
6a488035
TO
260
261 /**
eceb18cc 262 * Process the form submission.
6a488035
TO
263 */
264 public function postProcessCommon() {
6821aa1d 265 try {
266 $this->saveMetadataDefinedSettings($this->_params);
267 $this->filterParamsSetByMetadata($this->_params);
268 }
269 catch (CiviCRM_API3_Exception $e) {
270 CRM_Core_Session::setStatus($e->getMessage(), ts('Save Failed'), 'error');
271 }
272
6a488035
TO
273 foreach ($this->_varNames as $groupName => $groupValues) {
274 foreach ($groupValues as $settingName => $fieldValue) {
275 switch ($fieldValue['html_type']) {
276 case 'checkboxes':
a7488080 277 if (!empty($this->_params[$settingName]) &&
6a488035
TO
278 is_array($this->_params[$settingName])
279 ) {
280 $this->_config->$settingName = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
353ffa53
TO
281 array_keys($this->_params[$settingName])
282 ) . CRM_Core_DAO::VALUE_SEPARATOR;
6a488035
TO
283 }
284 else {
285 $this->_config->$settingName = NULL;
286 }
287 break;
288
289 case 'checkbox':
0d8afee2 290 $this->_config->$settingName = !empty($this->_params[$settingName]) ? 1 : 0;
6a488035
TO
291 break;
292
293 case 'text':
294 case 'select':
66c3132c 295 case 'radio':
04c56532 296 case 'YesNo':
e2106968 297 case 'entity_reference':
6a488035
TO
298 $this->_config->$settingName = CRM_Utils_Array::value($settingName, $this->_params);
299 break;
300
301 case 'textarea':
302 $value = CRM_Utils_Array::value($settingName, $this->_params);
303 if ($value) {
304 $value = trim($value);
be2fb01f 305 $value = str_replace(["\r\n", "\r"], "\n", $value);
6a488035
TO
306 }
307 $this->_config->$settingName = $value;
308 break;
309 }
310 }
311 }
312
313 foreach ($this->_varNames as $groupName => $groupValues) {
314 foreach ($groupValues as $settingName => $fieldValue) {
315 $settingValue = isset($this->_config->$settingName) ? $this->_config->$settingName : NULL;
08ef4ddd 316 Civi::settings()->set($settingName, $settingValue);
6a488035
TO
317 }
318 }
7b83e312
CW
319 // Update any settings stored in dynamic js
320 CRM_Core_Resources::singleton()->resetCacheCode();
6a488035
TO
321
322 CRM_Core_Session::setStatus(ts('Your changes have been saved.'), ts('Saved'), 'success');
323 }
324
325}