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