3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 * Base class for settings forms
40 class CRM_Admin_Form_Preferences
extends CRM_Core_Form
{
41 protected $_system = FALSE;
42 protected $_contactID = NULL;
43 protected $_action = NULL;
45 protected $_checkbox = NULL;
47 protected $_varNames = NULL;
49 protected $_config = NULL;
51 protected $_params = NULL;
53 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 foreach ($this->_varNames
as $groupName => $settingNames) {
92 $values = CRM_Core_BAO_Setting
::getItem($groupName);
93 foreach ($values as $name => $value) {
94 $this->_config
->$name = $value;
97 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin', 'reset=1'));
103 function setDefaultValues() {
106 foreach ($this->_varNames
as $groupName => $settings) {
107 foreach ($settings as $settingName => $settingDetails) {
108 $defaults[$settingName] = isset($this->_config
->$settingName) ?
$this->_config
->$settingName : CRM_Utils_Array
::value('default', $settingDetails, NULL);
118 function cbsDefaultValues(&$defaults) {
120 foreach ($this->_varNames
as $groupName => $groupValues) {
121 foreach ($groupValues as $settingName => $fieldValue) {
122 if ($fieldValue['html_type'] == 'checkboxes') {
123 if (isset($this->_config
->$settingName) &&
124 $this->_config
->$settingName
126 $value = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
127 substr($this->_config
->$settingName, 1, -1)
129 if (!empty($value)) {
130 $defaults[$settingName] = array();
131 foreach ($value as $n => $v) {
132 $defaults[$settingName][$v] = 1;
142 * Build the form object
147 public function buildQuickForm() {
148 parent
::buildQuickForm();
151 if (!empty($this->_varNames
)) {
152 foreach ($this->_varNames
as $groupName => $groupValues) {
153 $formName = CRM_Utils_String
::titleToVar($groupName);
154 $this->assign('formName', $formName);
156 foreach ($groupValues as $fieldName => $fieldValue) {
157 $fields[$fieldName] = $fieldValue;
159 switch ($fieldValue['html_type']) {
161 $this->addElement('text',
163 $fieldValue['title'],
173 $this->add($fieldValue['html_type'],
180 $options = CRM_Core_OptionGroup
::values($fieldName, FALSE, FALSE, TRUE);
181 $this->addRadio($fieldName, $fieldValue['title'], $options, NULL, ' ');
185 $options = array_flip(CRM_Core_OptionGroup
::values($fieldName, FALSE, FALSE, TRUE));
186 $newOptions = array();
187 foreach ($options as $key => $val) {
188 $newOptions[$key] = $val;
190 $this->addCheckBox($fieldName,
191 $fieldValue['title'],
193 NULL, NULL, NULL, NULL,
194 array(' ', ' ', '<br/>')
199 $this->addElement('select',
201 $fieldValue['title'],
202 $fieldValue['option_values']
207 $this->addWysiwyg($fieldName, $fieldValue['title'], $fieldValue['attributes']);
210 case 'entity_reference':
211 $this->addEntityRef($fieldName, $fieldValue['title'], CRM_Utils_Array
::value('options', $fieldValue, array()));
215 $fields = CRM_Utils_Array
::crmArraySortByField($fields, 'weight');
216 $this->assign('fields', $fields);
220 $this->addButtons(array(
223 'name' => ts('Save'),
228 'name' => ts('Cancel'),
233 if ($this->_action
== CRM_Core_Action
::VIEW
) {
239 * Process the form submission
245 public function postProcess() {
246 $config = CRM_Core_Config
::singleton();
247 if ($this->_action
== CRM_Core_Action
::VIEW
) {
251 $this->_params
= $this->controller
->exportValues($this->_name
);
253 $this->postProcessCommon();
257 * Process the form submission
263 public function postProcessCommon() {
264 foreach ($this->_varNames
as $groupName => $groupValues) {
265 foreach ($groupValues as $settingName => $fieldValue) {
266 switch ($fieldValue['html_type']) {
268 if (!empty($this->_params
[$settingName]) &&
269 is_array($this->_params
[$settingName])
271 $this->_config
->$settingName = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
272 array_keys($this->_params
[$settingName])
273 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
276 $this->_config
->$settingName = NULL;
281 $this->_config
->$settingName = !empty($this->_params
[$settingName]) ?
1 : 0;
287 case 'entity_reference':
288 $this->_config
->$settingName = CRM_Utils_Array
::value($settingName, $this->_params
);
292 $value = CRM_Utils_Array
::value($settingName, $this->_params
);
294 $value = trim($value);
295 $value = str_replace(array("\r\n", "\r"), "\n", $value);
297 $this->_config
->$settingName = $value;
303 foreach ($this->_varNames
as $groupName => $groupValues) {
304 foreach ($groupValues as $settingName => $fieldValue) {
305 $settingValue = isset($this->_config
->$settingName) ?
$this->_config
->$settingName : NULL;
306 CRM_Core_BAO_Setting
::setItem($settingValue,
313 CRM_Core_Session
::setStatus(ts('Your changes have been saved.'), ts('Saved'), 'success');