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
{
38 protected $_system = FALSE;
39 protected $_contactID = NULL;
40 public $_action = NULL;
42 protected $_checkbox = NULL;
44 protected $_varNames = NULL;
46 protected $_config = NULL;
48 protected $_params = NULL;
50 public function preProcess() {
51 $this->_contactID
= CRM_Utils_Request
::retrieve('cid', 'Positive',
54 $this->_system
= CRM_Utils_Request
::retrieve('system', 'Boolean',
57 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String',
58 $this, FALSE, 'update'
61 $this->assign('action', $action);
64 $session = CRM_Core_Session
::singleton();
66 $this->_config
= new CRM_Core_DAO();
69 if (CRM_Core_Permission
::check('administer CiviCRM')) {
70 $this->_contactID
= NULL;
73 CRM_Utils_System
::fatal('You do not have permission to edit preferences');
75 $this->_config
->contact_id
= NULL;
78 if (!$this->_contactID
) {
79 $this->_contactID
= $session->get('userID');
80 if (!$this->_contactID
) {
81 CRM_Utils_System
::fatal('Could not retrieve contact id');
83 $this->set('cid', $this->_contactID
);
85 $this->_config
->contact_id
= $this->_contactID
;
88 $settings = Civi
::settings();
89 foreach ($this->_varNames
as $groupName => $settingNames) {
90 foreach ($settingNames as $settingName => $options) {
91 $this->_config
->$settingName = $settings->get($settingName);
94 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin', 'reset=1'));
100 public function setDefaultValues() {
103 foreach ($this->_varNames
as $groupName => $settings) {
104 foreach ($settings as $settingName => $settingDetails) {
105 $defaults[$settingName] = isset($this->_config
->$settingName) ?
$this->_config
->$settingName : CRM_Utils_Array
::value('default', $settingDetails, NULL);
115 public function cbsDefaultValues(&$defaults) {
117 foreach ($this->_varNames
as $groupName => $groupValues) {
118 foreach ($groupValues as $settingName => $fieldValue) {
119 if ($fieldValue['html_type'] == 'checkboxes') {
120 if (isset($this->_config
->$settingName) &&
121 $this->_config
->$settingName
123 $value = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
124 substr($this->_config
->$settingName, 1, -1)
126 if (!empty($value)) {
127 $defaults[$settingName] = array();
128 foreach ($value as $n => $v) {
129 $defaults[$settingName][$v] = 1;
139 * Build the form object.
141 public function buildQuickForm() {
142 parent
::buildQuickForm();
144 if (!empty($this->_varNames
)) {
145 foreach ($this->_varNames
as $groupName => $groupValues) {
146 $formName = CRM_Utils_String
::titleToVar($groupName);
147 $this->assign('formName', $formName);
149 foreach ($groupValues as $fieldName => $fieldValue) {
150 $fields[$fieldName] = $fieldValue;
152 switch ($fieldValue['html_type']) {
154 $this->addElement('text',
156 $fieldValue['title'],
166 $this->add($fieldValue['html_type'],
173 $options = CRM_Core_OptionGroup
::values($fieldName, FALSE, FALSE, TRUE);
174 $this->addRadio($fieldName, $fieldValue['title'], $options, NULL, ' ');
178 $this->addRadio($fieldName, $fieldValue['title'], array(0 => 'No', 1 => 'Yes'), NULL, ' ');
182 $options = array_flip(CRM_Core_OptionGroup
::values($fieldName, FALSE, FALSE, TRUE));
183 $newOptions = array();
184 foreach ($options as $key => $val) {
185 $newOptions[$key] = $val;
187 $this->addCheckBox($fieldName,
188 $fieldValue['title'],
190 NULL, NULL, NULL, NULL,
191 array(' ', ' ', '<br/>')
196 $this->addElement('select',
198 $fieldValue['title'],
199 $fieldValue['option_values'],
200 CRM_Utils_Array
::value('attributes', $fieldValue)
205 $this->add('wysiwyg', $fieldName, $fieldValue['title'], $fieldValue['attributes']);
208 case 'entity_reference':
209 $this->addEntityRef($fieldName, $fieldValue['title'], CRM_Utils_Array
::value('options', $fieldValue, array()));
213 $fields = CRM_Utils_Array
::crmArraySortByField($fields, 'weight');
214 $this->assign('fields', $fields);
218 $this->addButtons(array(
221 'name' => ts('Save'),
226 'name' => ts('Cancel'),
231 if ($this->_action
== CRM_Core_Action
::VIEW
) {
237 * Process the form submission.
239 public function postProcess() {
240 $config = CRM_Core_Config
::singleton();
241 if ($this->_action
== CRM_Core_Action
::VIEW
) {
245 $this->_params
= $this->controller
->exportValues($this->_name
);
247 $this->postProcessCommon();
251 * Process the form submission.
253 public function postProcessCommon() {
254 foreach ($this->_varNames
as $groupName => $groupValues) {
255 foreach ($groupValues as $settingName => $fieldValue) {
256 switch ($fieldValue['html_type']) {
258 if (!empty($this->_params
[$settingName]) &&
259 is_array($this->_params
[$settingName])
261 $this->_config
->$settingName = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
262 array_keys($this->_params
[$settingName])
263 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
266 $this->_config
->$settingName = NULL;
271 $this->_config
->$settingName = !empty($this->_params
[$settingName]) ?
1 : 0;
278 case 'entity_reference':
279 $this->_config
->$settingName = CRM_Utils_Array
::value($settingName, $this->_params
);
283 $value = CRM_Utils_Array
::value($settingName, $this->_params
);
285 $value = trim($value);
286 $value = str_replace(array("\r\n", "\r"), "\n", $value);
288 $this->_config
->$settingName = $value;
294 foreach ($this->_varNames
as $groupName => $groupValues) {
295 foreach ($groupValues as $settingName => $fieldValue) {
296 $settingValue = isset($this->_config
->$settingName) ?
$this->_config
->$settingName : NULL;
297 Civi
::settings()->set($settingName, $settingValue);
300 // Update any settings stored in dynamic js
301 CRM_Core_Resources
::singleton()->resetCacheCode();
303 CRM_Core_Session
::setStatus(ts('Your changes have been saved.'), ts('Saved'), 'success');