3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * Base class for settings forms.
37 class CRM_Admin_Form_Preferences
extends CRM_Core_Form
{
39 use CRM_Admin_Form_SettingTrait
;
41 protected $_system = FALSE;
42 protected $_contactID = NULL;
43 public $_action = NULL;
45 protected $_checkbox = NULL;
47 protected $_varNames = [];
49 protected $_config = NULL;
51 protected $_params = NULL;
53 public function preProcess() {
54 $this->_contactID
= CRM_Utils_Request
::retrieve('cid', 'Positive',
57 $this->_system
= CRM_Utils_Request
::retrieve('system', 'Boolean',
60 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String',
61 $this, FALSE, 'update'
64 $this->assign('action', $action);
67 $session = CRM_Core_Session
::singleton();
69 $this->_config
= new CRM_Core_DAO();
72 if (CRM_Core_Permission
::check('administer CiviCRM')) {
73 $this->_contactID
= NULL;
76 CRM_Utils_System
::fatal('You do not have permission to edit preferences');
78 $this->_config
->contact_id
= NULL;
81 if (!$this->_contactID
) {
82 $this->_contactID
= $session->get('userID');
83 if (!$this->_contactID
) {
84 CRM_Utils_System
::fatal('Could not retrieve contact id');
86 $this->set('cid', $this->_contactID
);
88 $this->_config
->contact_id
= $this->_contactID
;
91 $this->addFieldsDefinedInSettingsMetadata();
92 $settings = Civi
::settings();
93 // @todo replace this by defining all in settings.
94 foreach ($this->_varNames
as $groupName => $settingNames) {
95 CRM_Core_Error
::deprecatedFunctionWarning('deprecated use of preferences form. This will be removed from core soon');
96 foreach ($settingNames as $settingName => $options) {
97 $this->_config
->$settingName = $settings->get($settingName);
100 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin', 'reset=1'));
106 public function setDefaultValues() {
107 $this->_defaults
= array();
109 $this->setDefaultsForMetadataDefinedFields();
110 foreach ($this->_varNames
as $groupName => $settings) {
111 CRM_Core_Error
::deprecatedFunctionWarning('deprecated use of preferences form. This will be removed from core soon');
112 foreach ($settings as $settingName => $settingDetails) {
113 $this->_defaults
[$settingName] = isset($this->_config
->$settingName) ?
$this->_config
->$settingName : CRM_Utils_Array
::value('default', $settingDetails, NULL);
117 return $this->_defaults
;
121 * @todo deprecate in favour of setting using metadata.
125 public function cbsDefaultValues(&$defaults) {
127 foreach ($this->_varNames
as $groupName => $groupValues) {
128 CRM_Core_Error
::deprecatedFunctionWarning('deprecated use of preferences form. This will be removed from core soon');
129 foreach ($groupValues as $settingName => $fieldValue) {
130 if ($fieldValue['html_type'] == 'checkboxes') {
131 if (isset($this->_config
->$settingName) &&
132 $this->_config
->$settingName
134 $value = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
135 substr($this->_config
->$settingName, 1, -1)
137 if (!empty($value)) {
138 $defaults[$settingName] = array();
139 foreach ($value as $n => $v) {
140 $defaults[$settingName][$v] = 1;
150 * Build the form object.
152 public function buildQuickForm() {
153 parent
::buildQuickForm();
155 if (!empty($this->_varNames
)) {
156 CRM_Core_Error
::deprecatedFunctionWarning('deprecated use of preferences form. This will be removed from core soon');
157 foreach ($this->_varNames
as $groupName => $groupValues) {
158 $formName = CRM_Utils_String
::titleToVar($groupName);
159 $this->assign('formName', $formName);
161 foreach ($groupValues as $fieldName => $fieldValue) {
162 $fields[$fieldName] = $fieldValue;
164 switch ($fieldValue['html_type']) {
166 $this->addElement('text',
168 $fieldValue['title'],
178 $this->add($fieldValue['html_type'],
185 $options = CRM_Core_OptionGroup
::values($fieldName, FALSE, FALSE, TRUE);
186 $this->addRadio($fieldName, $fieldValue['title'], $options, NULL, ' ');
190 $this->addRadio($fieldName, $fieldValue['title'], array(0 => 'No', 1 => 'Yes'), NULL, ' ');
194 $options = array_flip(CRM_Core_OptionGroup
::values($fieldName, FALSE, FALSE, TRUE));
195 $newOptions = array();
196 foreach ($options as $key => $val) {
197 $newOptions[$key] = $val;
199 $this->addCheckBox($fieldName,
200 $fieldValue['title'],
202 NULL, NULL, NULL, NULL,
203 array(' ', ' ', '<br/>')
208 $this->addElement('select',
210 $fieldValue['title'],
211 $fieldValue['option_values'],
212 CRM_Utils_Array
::value('attributes', $fieldValue)
217 $this->add('wysiwyg', $fieldName, $fieldValue['title'], $fieldValue['attributes']);
220 case 'entity_reference':
221 $this->addEntityRef($fieldName, $fieldValue['title'], CRM_Utils_Array
::value('options', $fieldValue, array()));
225 $fields = CRM_Utils_Array
::crmArraySortByField($fields, 'weight');
226 $this->assign('fields', $fields);
230 $this->addButtons(array(
233 'name' => ts('Save'),
238 'name' => ts('Cancel'),
243 if ($this->_action
== CRM_Core_Action
::VIEW
) {
249 * Process the form submission.
251 public function postProcess() {
252 $config = CRM_Core_Config
::singleton();
253 if ($this->_action
== CRM_Core_Action
::VIEW
) {
257 $this->_params
= $this->controller
->exportValues($this->_name
);
259 $this->postProcessCommon();
263 * Process the form submission.
265 public function postProcessCommon() {
267 $this->saveMetadataDefinedSettings($this->_params
);
268 $this->filterParamsSetByMetadata($this->_params
);
270 catch (CiviCRM_API3_Exception
$e) {
271 CRM_Core_Session
::setStatus($e->getMessage(), ts('Save Failed'), 'error');
274 foreach ($this->_varNames
as $groupName => $groupValues) {
275 foreach ($groupValues as $settingName => $fieldValue) {
276 switch ($fieldValue['html_type']) {
278 if (!empty($this->_params
[$settingName]) &&
279 is_array($this->_params
[$settingName])
281 $this->_config
->$settingName = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
282 array_keys($this->_params
[$settingName])
283 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
286 $this->_config
->$settingName = NULL;
291 $this->_config
->$settingName = !empty($this->_params
[$settingName]) ?
1 : 0;
298 case 'entity_reference':
299 $this->_config
->$settingName = CRM_Utils_Array
::value($settingName, $this->_params
);
303 $value = CRM_Utils_Array
::value($settingName, $this->_params
);
305 $value = trim($value);
306 $value = str_replace(array("\r\n", "\r"), "\n", $value);
308 $this->_config
->$settingName = $value;
314 foreach ($this->_varNames
as $groupName => $groupValues) {
315 foreach ($groupValues as $settingName => $fieldValue) {
316 $settingValue = isset($this->_config
->$settingName) ?
$this->_config
->$settingName : NULL;
317 Civi
::settings()->set($settingName, $settingValue);
320 // Update any settings stored in dynamic js
321 CRM_Core_Resources
::singleton()->resetCacheCode();
323 CRM_Core_Session
::setStatus(ts('Your changes have been saved.'), ts('Saved'), 'success');