3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Base class for settings forms.
21 class CRM_Admin_Form_Preferences
extends CRM_Core_Form
{
23 use CRM_Admin_Form_SettingTrait
;
25 protected $_system = FALSE;
26 protected $_contactID = NULL;
27 public $_action = NULL;
29 protected $_checkbox = NULL;
31 protected $_varNames = [];
33 protected $_config = NULL;
35 protected $_params = NULL;
37 public function preProcess() {
38 $this->_contactID
= CRM_Utils_Request
::retrieve('cid', 'Positive',
41 $this->_system
= CRM_Utils_Request
::retrieve('system', 'Boolean',
44 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String',
45 $this, FALSE, 'update'
48 $this->assign('action', $action);
51 $session = CRM_Core_Session
::singleton();
53 $this->_config
= new CRM_Core_DAO();
56 if (CRM_Core_Permission
::check('administer CiviCRM')) {
57 $this->_contactID
= NULL;
60 CRM_Utils_System
::fatal('You do not have permission to edit preferences');
62 $this->_config
->contact_id
= NULL;
65 if (!$this->_contactID
) {
66 $this->_contactID
= $session->get('userID');
67 if (!$this->_contactID
) {
68 CRM_Utils_System
::fatal('Could not retrieve contact id');
70 $this->set('cid', $this->_contactID
);
72 $this->_config
->contact_id
= $this->_contactID
;
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);
84 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin', 'reset=1'));
90 public function setDefaultValues() {
91 $this->_defaults
= [];
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);
101 return $this->_defaults
;
105 * @todo deprecate in favour of setting using metadata.
109 public function cbsDefaultValues(&$defaults) {
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
118 $value = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
119 substr($this->_config
->$settingName, 1, -1)
121 if (!empty($value)) {
122 $defaults[$settingName] = [];
123 foreach ($value as $n => $v) {
124 $defaults[$settingName][$v] = 1;
134 * Build the form object.
136 public function buildQuickForm() {
137 parent
::buildQuickForm();
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);
145 foreach ($groupValues as $fieldName => $fieldValue) {
146 $fields[$fieldName] = $fieldValue;
148 switch ($fieldValue['html_type']) {
150 $this->addElement('text',
152 $fieldValue['title'],
162 $this->add($fieldValue['html_type'],
169 $options = CRM_Core_OptionGroup
::values($fieldName, FALSE, FALSE, TRUE);
170 $this->addRadio($fieldName, $fieldValue['title'], $options, NULL, ' ');
174 $this->addRadio($fieldName, $fieldValue['title'], [0 => 'No', 1 => 'Yes'], NULL, ' ');
178 $options = array_flip(CRM_Core_OptionGroup
::values($fieldName, FALSE, FALSE, TRUE));
180 foreach ($options as $key => $val) {
181 $newOptions[$key] = $val;
183 $this->addCheckBox($fieldName,
184 $fieldValue['title'],
186 NULL, NULL, NULL, NULL,
187 [' ', ' ', '<br/>']
192 $this->addElement('select',
194 $fieldValue['title'],
195 $fieldValue['option_values'],
196 CRM_Utils_Array
::value('attributes', $fieldValue)
201 $this->add('wysiwyg', $fieldName, $fieldValue['title'], $fieldValue['attributes']);
204 case 'entity_reference':
205 $this->addEntityRef($fieldName, $fieldValue['title'], CRM_Utils_Array
::value('options', $fieldValue, []));
209 $fields = CRM_Utils_Array
::crmArraySortByField($fields, 'weight');
210 $this->assign('fields', $fields);
217 'name' => ts('Save'),
222 'name' => ts('Cancel'),
226 if ($this->_action
== CRM_Core_Action
::VIEW
) {
232 * Process the form submission.
234 public function postProcess() {
235 $config = CRM_Core_Config
::singleton();
236 if ($this->_action
== CRM_Core_Action
::VIEW
) {
240 $this->_params
= $this->controller
->exportValues($this->_name
);
242 $this->postProcessCommon();
246 * Process the form submission.
248 public function postProcessCommon() {
250 $this->saveMetadataDefinedSettings($this->_params
);
251 $this->filterParamsSetByMetadata($this->_params
);
253 catch (CiviCRM_API3_Exception
$e) {
254 CRM_Core_Session
::setStatus($e->getMessage(), ts('Save Failed'), 'error');
257 foreach ($this->_varNames
as $groupName => $groupValues) {
258 foreach ($groupValues as $settingName => $fieldValue) {
259 switch ($fieldValue['html_type']) {
261 if (!empty($this->_params
[$settingName]) &&
262 is_array($this->_params
[$settingName])
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
;
269 $this->_config
->$settingName = NULL;
274 $this->_config
->$settingName = !empty($this->_params
[$settingName]) ?
1 : 0;
281 case 'entity_reference':
282 $this->_config
->$settingName = CRM_Utils_Array
::value($settingName, $this->_params
);
286 $value = CRM_Utils_Array
::value($settingName, $this->_params
);
288 $value = trim($value);
289 $value = str_replace(["\r\n", "\r"], "\n", $value);
291 $this->_config
->$settingName = $value;
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);
303 // Update any settings stored in dynamic js
304 CRM_Core_Resources
::singleton()->resetCacheCode();
306 CRM_Core_Session
::setStatus(ts('Your changes have been saved.'), ts('Saved'), 'success');