3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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'));
100 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);
112 function cbsDefaultValues(&$defaults) {
114 foreach ($this->_varNames
as $groupName => $groupValues) {
115 foreach ($groupValues as $settingName => $fieldValue) {
116 if ($fieldValue['html_type'] == 'checkboxes') {
117 if (isset($this->_config
->$settingName) &&
118 $this->_config
->$settingName
120 $value = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
121 substr($this->_config
->$settingName, 1, -1)
123 if (!empty($value)) {
124 $defaults[$settingName] = array();
125 foreach ($value as $n => $v) {
126 $defaults[$settingName][$v] = 1;
136 * Function to build the form
141 public function buildQuickForm() {
142 parent
::buildQuickForm();
145 if (!empty($this->_varNames
)) {
146 foreach ($this->_varNames
as $groupName => $groupValues) {
147 $formName = CRM_Utils_String
::titleToVar($groupName);
148 $this->assign('formName', $formName);
150 foreach ($groupValues as $fieldName => $fieldValue) {
151 $fields[$fieldName] = $fieldValue;
153 switch ($fieldValue['html_type']) {
155 $this->addElement('text',
157 $fieldValue['title'],
167 $this->add($fieldValue['html_type'],
174 $options = CRM_Core_OptionGroup
::values($fieldName, FALSE, FALSE, TRUE);
175 $this->addRadio($fieldName, $fieldValue['title'], $options, NULL, ' ');
179 $options = array_flip(CRM_Core_OptionGroup
::values($fieldName, FALSE, FALSE, TRUE));
180 $newOptions = array();
181 foreach ($options as $key => $val) {
182 $newOptions[$key] = $val;
184 $this->addCheckBox($fieldName,
185 $fieldValue['title'],
187 NULL, NULL, NULL, NULL,
188 array(' ', ' ', '<br/>')
194 $fields = CRM_Utils_Array
::crmArraySortByField($fields, 'weight');
195 $this->assign('fields', $fields);
199 $this->addButtons(array(
202 'name' => ts('Save'),
207 'name' => ts('Cancel'),
212 if ($this->_action
== CRM_Core_Action
::VIEW
) {
218 * Function to process the form
224 public function postProcess() {
225 $config = CRM_Core_Config
::singleton();
226 if ($this->_action
== CRM_Core_Action
::VIEW
) {
230 $this->_params
= $this->controller
->exportValues($this->_name
);
232 $this->postProcessCommon();
237 * Function to process the form
243 public function postProcessCommon() {
244 foreach ($this->_varNames
as $groupName => $groupValues) {
245 foreach ($groupValues as $settingName => $fieldValue) {
246 switch ($fieldValue['html_type']) {
248 if (CRM_Utils_Array
::value($settingName, $this->_params
) &&
249 is_array($this->_params
[$settingName])
251 $this->_config
->$settingName = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
252 array_keys($this->_params
[$settingName])
253 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
256 $this->_config
->$settingName = NULL;
261 $this->_config
->$settingName = !empty($this->_params
[$settingName]) ?
1 : 0;
267 $this->_config
->$settingName = CRM_Utils_Array
::value($settingName, $this->_params
);
271 $value = CRM_Utils_Array
::value($settingName, $this->_params
);
273 $value = trim($value);
274 $value = str_replace(array("\r\n", "\r"), "\n", $value);
276 $this->_config
->$settingName = $value;
282 foreach ($this->_varNames
as $groupName => $groupValues) {
283 foreach ($groupValues as $settingName => $fieldValue) {
284 $settingValue = isset($this->_config
->$settingName) ?
$this->_config
->$settingName : NULL;
285 CRM_Core_BAO_Setting
::setItem($settingValue,
292 CRM_Core_Session
::setStatus(ts('Your changes have been saved.'), ts('Saved'), 'success');