Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
ce064e4f | 19 | * Base class for settings forms. |
6a488035 TO |
20 | */ |
21 | class CRM_Admin_Form_Preferences extends CRM_Core_Form { | |
a55c9b35 | 22 | |
23 | use CRM_Admin_Form_SettingTrait; | |
24 | ||
6a488035 TO |
25 | protected $_system = FALSE; |
26 | protected $_contactID = NULL; | |
3a936dab | 27 | public $_action = NULL; |
6a488035 TO |
28 | |
29 | protected $_checkbox = NULL; | |
30 | ||
b70c6629 | 31 | protected $_varNames = []; |
6a488035 TO |
32 | |
33 | protected $_config = NULL; | |
34 | ||
35 | protected $_params = NULL; | |
36 | ||
00be9182 | 37 | public function preProcess() { |
6a488035 TO |
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 | ||
a55c9b35 | 75 | $this->addFieldsDefinedInSettingsMetadata(); |
26cc63d3 | 76 | $settings = Civi::settings(); |
601361a3 | 77 | // @todo replace this by defining all in settings. |
6a488035 | 78 | foreach ($this->_varNames as $groupName => $settingNames) { |
2f6c641a | 79 | CRM_Core_Error::deprecatedFunctionWarning('deprecated use of preferences form. This will be removed from core soon'); |
26cc63d3 TO |
80 | foreach ($settingNames as $settingName => $options) { |
81 | $this->_config->$settingName = $settings->get($settingName); | |
6a488035 TO |
82 | } |
83 | } | |
84 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1')); | |
85 | } | |
86 | ||
e0ef6999 EM |
87 | /** |
88 | * @return array | |
89 | */ | |
00be9182 | 90 | public function setDefaultValues() { |
be2fb01f | 91 | $this->_defaults = []; |
6a488035 | 92 | |
601361a3 | 93 | $this->setDefaultsForMetadataDefinedFields(); |
6a488035 | 94 | foreach ($this->_varNames as $groupName => $settings) { |
2f6c641a | 95 | CRM_Core_Error::deprecatedFunctionWarning('deprecated use of preferences form. This will be removed from core soon'); |
6a488035 | 96 | foreach ($settings as $settingName => $settingDetails) { |
601361a3 | 97 | $this->_defaults[$settingName] = isset($this->_config->$settingName) ? $this->_config->$settingName : CRM_Utils_Array::value('default', $settingDetails, NULL); |
6a488035 TO |
98 | } |
99 | } | |
100 | ||
601361a3 | 101 | return $this->_defaults; |
6a488035 TO |
102 | } |
103 | ||
e0ef6999 | 104 | /** |
6821aa1d | 105 | * @todo deprecate in favour of setting using metadata. |
106 | * | |
e0ef6999 EM |
107 | * @param $defaults |
108 | */ | |
00be9182 | 109 | public function cbsDefaultValues(&$defaults) { |
6a488035 TO |
110 | |
111 | foreach ($this->_varNames as $groupName => $groupValues) { | |
2f6c641a | 112 | CRM_Core_Error::deprecatedFunctionWarning('deprecated use of preferences form. This will be removed from core soon'); |
6a488035 TO |
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)) { | |
be2fb01f | 122 | $defaults[$settingName] = []; |
6a488035 TO |
123 | foreach ($value as $n => $v) { |
124 | $defaults[$settingName][$v] = 1; | |
125 | } | |
126 | } | |
127 | } | |
128 | } | |
129 | } | |
130 | } | |
131 | } | |
132 | ||
133 | /** | |
eceb18cc | 134 | * Build the form object. |
6a488035 TO |
135 | */ |
136 | public function buildQuickForm() { | |
137 | parent::buildQuickForm(); | |
138 | ||
6a488035 | 139 | if (!empty($this->_varNames)) { |
2f6c641a | 140 | CRM_Core_Error::deprecatedFunctionWarning('deprecated use of preferences form. This will be removed from core soon'); |
6a488035 TO |
141 | foreach ($this->_varNames as $groupName => $groupValues) { |
142 | $formName = CRM_Utils_String::titleToVar($groupName); | |
143 | $this->assign('formName', $formName); | |
be2fb01f | 144 | $fields = []; |
6a488035 TO |
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'], | |
be2fb01f | 153 | [ |
6a488035 TO |
154 | 'maxlength' => 64, |
155 | 'size' => 32, | |
be2fb01f | 156 | ] |
6a488035 TO |
157 | ); |
158 | break; | |
159 | ||
160 | case 'textarea': | |
65f9bd70 KJ |
161 | case 'checkbox': |
162 | $this->add($fieldValue['html_type'], | |
6a488035 TO |
163 | $fieldName, |
164 | $fieldValue['title'] | |
165 | ); | |
166 | break; | |
167 | ||
65f9bd70 KJ |
168 | case 'radio': |
169 | $options = CRM_Core_OptionGroup::values($fieldName, FALSE, FALSE, TRUE); | |
170 | $this->addRadio($fieldName, $fieldValue['title'], $options, NULL, ' '); | |
6a488035 TO |
171 | break; |
172 | ||
04c56532 | 173 | case 'YesNo': |
be2fb01f | 174 | $this->addRadio($fieldName, $fieldValue['title'], [0 => 'No', 1 => 'Yes'], NULL, ' '); |
04c56532 SL |
175 | break; |
176 | ||
6a488035 TO |
177 | case 'checkboxes': |
178 | $options = array_flip(CRM_Core_OptionGroup::values($fieldName, FALSE, FALSE, TRUE)); | |
be2fb01f | 179 | $newOptions = []; |
6a488035 TO |
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, | |
be2fb01f | 187 | [' ', ' ', '<br/>'] |
6a488035 TO |
188 | ); |
189 | break; | |
8b01c2f2 | 190 | |
b8ce3328 | 191 | case 'select': |
192 | $this->addElement('select', | |
193 | $fieldName, | |
194 | $fieldValue['title'], | |
b5407aa9 JP |
195 | $fieldValue['option_values'], |
196 | CRM_Utils_Array::value('attributes', $fieldValue) | |
b8ce3328 | 197 | ); |
198 | break; | |
199 | ||
fff0f449 | 200 | case 'wysiwyg': |
5d51a2f9 | 201 | $this->add('wysiwyg', $fieldName, $fieldValue['title'], $fieldValue['attributes']); |
fff0f449 | 202 | break; |
203 | ||
8b01c2f2 | 204 | case 'entity_reference': |
be2fb01f | 205 | $this->addEntityRef($fieldName, $fieldValue['title'], CRM_Utils_Array::value('options', $fieldValue, [])); |
6a488035 TO |
206 | } |
207 | } | |
208 | ||
209 | $fields = CRM_Utils_Array::crmArraySortByField($fields, 'weight'); | |
210 | $this->assign('fields', $fields); | |
211 | } | |
212 | } | |
213 | ||
be2fb01f | 214 | $this->addButtons([ |
0d48f1cc TO |
215 | [ |
216 | 'type' => 'next', | |
217 | 'name' => ts('Save'), | |
218 | 'isDefault' => TRUE, | |
219 | ], | |
220 | [ | |
221 | 'type' => 'cancel', | |
222 | 'name' => ts('Cancel'), | |
223 | ], | |
224 | ]); | |
6a488035 TO |
225 | |
226 | if ($this->_action == CRM_Core_Action::VIEW) { | |
227 | $this->freeze(); | |
228 | } | |
229 | } | |
230 | ||
231 | /** | |
eceb18cc | 232 | * Process the form submission. |
6a488035 TO |
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 | } | |
6a488035 TO |
244 | |
245 | /** | |
eceb18cc | 246 | * Process the form submission. |
6a488035 TO |
247 | */ |
248 | public function postProcessCommon() { | |
6821aa1d | 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 | ||
6a488035 TO |
257 | foreach ($this->_varNames as $groupName => $groupValues) { |
258 | foreach ($groupValues as $settingName => $fieldValue) { | |
259 | switch ($fieldValue['html_type']) { | |
260 | case 'checkboxes': | |
a7488080 | 261 | if (!empty($this->_params[$settingName]) && |
6a488035 TO |
262 | is_array($this->_params[$settingName]) |
263 | ) { | |
264 | $this->_config->$settingName = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
353ffa53 TO |
265 | array_keys($this->_params[$settingName]) |
266 | ) . CRM_Core_DAO::VALUE_SEPARATOR; | |
6a488035 TO |
267 | } |
268 | else { | |
269 | $this->_config->$settingName = NULL; | |
270 | } | |
271 | break; | |
272 | ||
273 | case 'checkbox': | |
0d8afee2 | 274 | $this->_config->$settingName = !empty($this->_params[$settingName]) ? 1 : 0; |
6a488035 TO |
275 | break; |
276 | ||
277 | case 'text': | |
278 | case 'select': | |
66c3132c | 279 | case 'radio': |
04c56532 | 280 | case 'YesNo': |
e2106968 | 281 | case 'entity_reference': |
6a488035 TO |
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); | |
be2fb01f | 289 | $value = str_replace(["\r\n", "\r"], "\n", $value); |
6a488035 TO |
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; | |
08ef4ddd | 300 | Civi::settings()->set($settingName, $settingValue); |
6a488035 TO |
301 | } |
302 | } | |
7b83e312 CW |
303 | // Update any settings stored in dynamic js |
304 | CRM_Core_Resources::singleton()->resetCacheCode(); | |
6a488035 TO |
305 | |
306 | CRM_Core_Session::setStatus(ts('Your changes have been saved.'), ts('Saved'), 'success'); | |
307 | } | |
308 | ||
309 | } |