3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
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 foreach ($settingNames as $settingName => $options) {
96 $this->_config
->$settingName = $settings->get($settingName);
99 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin', 'reset=1'));
105 public function setDefaultValues() {
106 $this->_defaults
= array();
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);
115 return $this->_defaults
;
119 * @todo deprecate in favour of setting using metadata.
123 public function cbsDefaultValues(&$defaults) {
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
131 $value = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
132 substr($this->_config
->$settingName, 1, -1)
134 if (!empty($value)) {
135 $defaults[$settingName] = array();
136 foreach ($value as $n => $v) {
137 $defaults[$settingName][$v] = 1;
147 * Build the form object.
149 public function buildQuickForm() {
150 parent
::buildQuickForm();
152 if (!empty($this->_varNames
)) {
153 foreach ($this->_varNames
as $groupName => $groupValues) {
154 $formName = CRM_Utils_String
::titleToVar($groupName);
155 $this->assign('formName', $formName);
157 foreach ($groupValues as $fieldName => $fieldValue) {
158 $fields[$fieldName] = $fieldValue;
160 switch ($fieldValue['html_type']) {
162 $this->addElement('text',
164 $fieldValue['title'],
174 $this->add($fieldValue['html_type'],
181 $options = CRM_Core_OptionGroup
::values($fieldName, FALSE, FALSE, TRUE);
182 $this->addRadio($fieldName, $fieldValue['title'], $options, NULL, ' ');
186 $this->addRadio($fieldName, $fieldValue['title'], array(0 => 'No', 1 => 'Yes'), NULL, ' ');
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;
195 $this->addCheckBox($fieldName,
196 $fieldValue['title'],
198 NULL, NULL, NULL, NULL,
199 array(' ', ' ', '<br/>')
204 $this->addElement('select',
206 $fieldValue['title'],
207 $fieldValue['option_values'],
208 CRM_Utils_Array
::value('attributes', $fieldValue)
213 $this->add('wysiwyg', $fieldName, $fieldValue['title'], $fieldValue['attributes']);
216 case 'entity_reference':
217 $this->addEntityRef($fieldName, $fieldValue['title'], CRM_Utils_Array
::value('options', $fieldValue, array()));
221 $fields = CRM_Utils_Array
::crmArraySortByField($fields, 'weight');
222 $this->assign('fields', $fields);
226 $this->addButtons(array(
229 'name' => ts('Save'),
234 'name' => ts('Cancel'),
239 if ($this->_action
== CRM_Core_Action
::VIEW
) {
245 * Process the form submission.
247 public function postProcess() {
248 $config = CRM_Core_Config
::singleton();
249 if ($this->_action
== CRM_Core_Action
::VIEW
) {
253 $this->_params
= $this->controller
->exportValues($this->_name
);
255 $this->postProcessCommon();
259 * Process the form submission.
261 public function postProcessCommon() {
263 $this->saveMetadataDefinedSettings($this->_params
);
264 $this->filterParamsSetByMetadata($this->_params
);
266 catch (CiviCRM_API3_Exception
$e) {
267 CRM_Core_Session
::setStatus($e->getMessage(), ts('Save Failed'), 'error');
270 foreach ($this->_varNames
as $groupName => $groupValues) {
271 foreach ($groupValues as $settingName => $fieldValue) {
272 switch ($fieldValue['html_type']) {
274 if (!empty($this->_params
[$settingName]) &&
275 is_array($this->_params
[$settingName])
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
;
282 $this->_config
->$settingName = NULL;
287 $this->_config
->$settingName = !empty($this->_params
[$settingName]) ?
1 : 0;
294 case 'entity_reference':
295 $this->_config
->$settingName = CRM_Utils_Array
::value($settingName, $this->_params
);
299 $value = CRM_Utils_Array
::value($settingName, $this->_params
);
301 $value = trim($value);
302 $value = str_replace(array("\r\n", "\r"), "\n", $value);
304 $this->_config
->$settingName = $value;
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);
316 // Update any settings stored in dynamic js
317 CRM_Core_Resources
::singleton()->resetCacheCode();
319 CRM_Core_Session
::setStatus(ts('Your changes have been saved.'), ts('Saved'), 'success');