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 * Function to build the form
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']
206 case 'entity_reference':
207 $this->addEntityRef($fieldName, $fieldValue['title'], CRM_Utils_Array
::value('options', $fieldValue, array()));
211 $fields = CRM_Utils_Array
::crmArraySortByField($fields, 'weight');
212 $this->assign('fields', $fields);
216 $this->addButtons(array(
219 'name' => ts('Save'),
224 'name' => ts('Cancel'),
229 if ($this->_action
== CRM_Core_Action
::VIEW
) {
235 * Function to process the form
241 public function postProcess() {
242 $config = CRM_Core_Config
::singleton();
243 if ($this->_action
== CRM_Core_Action
::VIEW
) {
247 $this->_params
= $this->controller
->exportValues($this->_name
);
249 $this->postProcessCommon();
254 * Function to process the form
260 public function postProcessCommon() {
261 foreach ($this->_varNames
as $groupName => $groupValues) {
262 foreach ($groupValues as $settingName => $fieldValue) {
263 switch ($fieldValue['html_type']) {
265 if (!empty($this->_params
[$settingName]) &&
266 is_array($this->_params
[$settingName])
268 $this->_config
->$settingName = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
269 array_keys($this->_params
[$settingName])
270 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
273 $this->_config
->$settingName = NULL;
278 $this->_config
->$settingName = !empty($this->_params
[$settingName]) ?
1 : 0;
284 case 'entity_reference':
285 $this->_config
->$settingName = CRM_Utils_Array
::value($settingName, $this->_params
);
289 $value = CRM_Utils_Array
::value($settingName, $this->_params
);
291 $value = trim($value);
292 $value = str_replace(array("\r\n", "\r"), "\n", $value);
294 $this->_config
->$settingName = $value;
300 foreach ($this->_varNames
as $groupName => $groupValues) {
301 foreach ($groupValues as $settingName => $fieldValue) {
302 $settingValue = isset($this->_config
->$settingName) ?
$this->_config
->$settingName : NULL;
303 CRM_Core_BAO_Setting
::setItem($settingValue,
310 CRM_Core_Session
::setStatus(ts('Your changes have been saved.'), ts('Saved'), 'success');