Merge pull request #12976 from civicrm/5.7
[civicrm-core.git] / CRM / Admin / Form / Preferences.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2018
32 */
33
34 /**
35 * Base class for settings forms.
36 */
37 class CRM_Admin_Form_Preferences extends CRM_Core_Form {
38
39 use CRM_Admin_Form_SettingTrait;
40
41 protected $_system = FALSE;
42 protected $_contactID = NULL;
43 public $_action = NULL;
44
45 protected $_checkbox = NULL;
46
47 protected $_varNames = [];
48
49 protected $_config = NULL;
50
51 protected $_params = NULL;
52
53 public function preProcess() {
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
91 $this->addFieldsDefinedInSettingsMetadata();
92 $settings = Civi::settings();
93 // @todo replace this by defining all in settings.
94 foreach ($this->_varNames as $groupName => $settingNames) {
95 foreach ($settingNames as $settingName => $options) {
96 $this->_config->$settingName = $settings->get($settingName);
97 }
98 }
99 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1'));
100 }
101
102 /**
103 * @return array
104 */
105 public function setDefaultValues() {
106 $this->_defaults = array();
107
108 $this->setDefaultsForMetadataDefinedFields();
109 foreach ($this->_varNames as $groupName => $settings) {
110 foreach ($settings as $settingName => $settingDetails) {
111 $this->_defaults[$settingName] = isset($this->_config->$settingName) ? $this->_config->$settingName : CRM_Utils_Array::value('default', $settingDetails, NULL);
112 }
113 }
114
115 return $this->_defaults;
116 }
117
118 /**
119 * @todo deprecate in favour of setting using metadata.
120 *
121 * @param $defaults
122 */
123 public function cbsDefaultValues(&$defaults) {
124
125 foreach ($this->_varNames as $groupName => $groupValues) {
126 foreach ($groupValues as $settingName => $fieldValue) {
127 if ($fieldValue['html_type'] == 'checkboxes') {
128 if (isset($this->_config->$settingName) &&
129 $this->_config->$settingName
130 ) {
131 $value = explode(CRM_Core_DAO::VALUE_SEPARATOR,
132 substr($this->_config->$settingName, 1, -1)
133 );
134 if (!empty($value)) {
135 $defaults[$settingName] = array();
136 foreach ($value as $n => $v) {
137 $defaults[$settingName][$v] = 1;
138 }
139 }
140 }
141 }
142 }
143 }
144 }
145
146 /**
147 * Build the form object.
148 */
149 public function buildQuickForm() {
150 parent::buildQuickForm();
151
152 if (!empty($this->_varNames)) {
153 foreach ($this->_varNames as $groupName => $groupValues) {
154 $formName = CRM_Utils_String::titleToVar($groupName);
155 $this->assign('formName', $formName);
156 $fields = array();
157 foreach ($groupValues as $fieldName => $fieldValue) {
158 $fields[$fieldName] = $fieldValue;
159
160 switch ($fieldValue['html_type']) {
161 case 'text':
162 $this->addElement('text',
163 $fieldName,
164 $fieldValue['title'],
165 array(
166 'maxlength' => 64,
167 'size' => 32,
168 )
169 );
170 break;
171
172 case 'textarea':
173 case 'checkbox':
174 $this->add($fieldValue['html_type'],
175 $fieldName,
176 $fieldValue['title']
177 );
178 break;
179
180 case 'radio':
181 $options = CRM_Core_OptionGroup::values($fieldName, FALSE, FALSE, TRUE);
182 $this->addRadio($fieldName, $fieldValue['title'], $options, NULL, '&nbsp;&nbsp;');
183 break;
184
185 case 'YesNo':
186 $this->addRadio($fieldName, $fieldValue['title'], array(0 => 'No', 1 => 'Yes'), NULL, '&nbsp;&nbsp;');
187 break;
188
189 case 'checkboxes':
190 $options = array_flip(CRM_Core_OptionGroup::values($fieldName, FALSE, FALSE, TRUE));
191 $newOptions = array();
192 foreach ($options as $key => $val) {
193 $newOptions[$key] = $val;
194 }
195 $this->addCheckBox($fieldName,
196 $fieldValue['title'],
197 $newOptions,
198 NULL, NULL, NULL, NULL,
199 array('&nbsp;&nbsp;', '&nbsp;&nbsp;', '<br/>')
200 );
201 break;
202
203 case 'select':
204 $this->addElement('select',
205 $fieldName,
206 $fieldValue['title'],
207 $fieldValue['option_values'],
208 CRM_Utils_Array::value('attributes', $fieldValue)
209 );
210 break;
211
212 case 'wysiwyg':
213 $this->add('wysiwyg', $fieldName, $fieldValue['title'], $fieldValue['attributes']);
214 break;
215
216 case 'entity_reference':
217 $this->addEntityRef($fieldName, $fieldValue['title'], CRM_Utils_Array::value('options', $fieldValue, array()));
218 }
219 }
220
221 $fields = CRM_Utils_Array::crmArraySortByField($fields, 'weight');
222 $this->assign('fields', $fields);
223 }
224 }
225
226 $this->addButtons(array(
227 array(
228 'type' => 'next',
229 'name' => ts('Save'),
230 'isDefault' => TRUE,
231 ),
232 array(
233 'type' => 'cancel',
234 'name' => ts('Cancel'),
235 ),
236 )
237 );
238
239 if ($this->_action == CRM_Core_Action::VIEW) {
240 $this->freeze();
241 }
242 }
243
244 /**
245 * Process the form submission.
246 */
247 public function postProcess() {
248 $config = CRM_Core_Config::singleton();
249 if ($this->_action == CRM_Core_Action::VIEW) {
250 return;
251 }
252
253 $this->_params = $this->controller->exportValues($this->_name);
254
255 $this->postProcessCommon();
256 }
257
258 /**
259 * Process the form submission.
260 */
261 public function postProcessCommon() {
262 try {
263 $this->saveMetadataDefinedSettings($this->_params);
264 $this->filterParamsSetByMetadata($this->_params);
265 }
266 catch (CiviCRM_API3_Exception $e) {
267 CRM_Core_Session::setStatus($e->getMessage(), ts('Save Failed'), 'error');
268 }
269
270 foreach ($this->_varNames as $groupName => $groupValues) {
271 foreach ($groupValues as $settingName => $fieldValue) {
272 switch ($fieldValue['html_type']) {
273 case 'checkboxes':
274 if (!empty($this->_params[$settingName]) &&
275 is_array($this->_params[$settingName])
276 ) {
277 $this->_config->$settingName = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
278 array_keys($this->_params[$settingName])
279 ) . CRM_Core_DAO::VALUE_SEPARATOR;
280 }
281 else {
282 $this->_config->$settingName = NULL;
283 }
284 break;
285
286 case 'checkbox':
287 $this->_config->$settingName = !empty($this->_params[$settingName]) ? 1 : 0;
288 break;
289
290 case 'text':
291 case 'select':
292 case 'radio':
293 case 'YesNo':
294 case 'entity_reference':
295 $this->_config->$settingName = CRM_Utils_Array::value($settingName, $this->_params);
296 break;
297
298 case 'textarea':
299 $value = CRM_Utils_Array::value($settingName, $this->_params);
300 if ($value) {
301 $value = trim($value);
302 $value = str_replace(array("\r\n", "\r"), "\n", $value);
303 }
304 $this->_config->$settingName = $value;
305 break;
306 }
307 }
308 }
309
310 foreach ($this->_varNames as $groupName => $groupValues) {
311 foreach ($groupValues as $settingName => $fieldValue) {
312 $settingValue = isset($this->_config->$settingName) ? $this->_config->$settingName : NULL;
313 Civi::settings()->set($settingName, $settingValue);
314 }
315 }
316 // Update any settings stored in dynamic js
317 CRM_Core_Resources::singleton()->resetCacheCode();
318
319 CRM_Core_Session::setStatus(ts('Your changes have been saved.'), ts('Saved'), 'success');
320 }
321
322 }