3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 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 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 public 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 public 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.
146 public function buildQuickForm() {
147 parent
::buildQuickForm();
149 if (!empty($this->_varNames
)) {
150 foreach ($this->_varNames
as $groupName => $groupValues) {
151 $formName = CRM_Utils_String
::titleToVar($groupName);
152 $this->assign('formName', $formName);
154 foreach ($groupValues as $fieldName => $fieldValue) {
155 $fields[$fieldName] = $fieldValue;
157 switch ($fieldValue['html_type']) {
159 $this->addElement('text',
161 $fieldValue['title'],
171 $this->add($fieldValue['html_type'],
178 $options = CRM_Core_OptionGroup
::values($fieldName, FALSE, FALSE, TRUE);
179 $this->addRadio($fieldName, $fieldValue['title'], $options, NULL, ' ');
183 $options = array_flip(CRM_Core_OptionGroup
::values($fieldName, FALSE, FALSE, TRUE));
184 $newOptions = array();
185 foreach ($options as $key => $val) {
186 $newOptions[$key] = $val;
188 $this->addCheckBox($fieldName,
189 $fieldValue['title'],
191 NULL, NULL, NULL, NULL,
192 array(' ', ' ', '<br/>')
197 $this->addElement('select',
199 $fieldValue['title'],
200 $fieldValue['option_values']
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.
242 public function postProcess() {
243 $config = CRM_Core_Config
::singleton();
244 if ($this->_action
== CRM_Core_Action
::VIEW
) {
248 $this->_params
= $this->controller
->exportValues($this->_name
);
250 $this->postProcessCommon();
254 * Process the form submission.
259 public function postProcessCommon() {
260 foreach ($this->_varNames
as $groupName => $groupValues) {
261 foreach ($groupValues as $settingName => $fieldValue) {
262 switch ($fieldValue['html_type']) {
264 if (!empty($this->_params
[$settingName]) &&
265 is_array($this->_params
[$settingName])
267 $this->_config
->$settingName = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
268 array_keys($this->_params
[$settingName])
269 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
272 $this->_config
->$settingName = NULL;
277 $this->_config
->$settingName = !empty($this->_params
[$settingName]) ?
1 : 0;
283 case 'entity_reference':
284 $this->_config
->$settingName = CRM_Utils_Array
::value($settingName, $this->_params
);
288 $value = CRM_Utils_Array
::value($settingName, $this->_params
);
290 $value = trim($value);
291 $value = str_replace(array("\r\n", "\r"), "\n", $value);
293 $this->_config
->$settingName = $value;
299 foreach ($this->_varNames
as $groupName => $groupValues) {
300 foreach ($groupValues as $settingName => $fieldValue) {
301 $settingValue = isset($this->_config
->$settingName) ?
$this->_config
->$settingName : NULL;
302 CRM_Core_BAO_Setting
::setItem($settingValue,
308 // Update any settings stored in dynamic js
309 CRM_Core_Resources
::singleton()->resetCacheCode();
311 CRM_Core_Session
::setStatus(ts('Your changes have been saved.'), ts('Saved'), 'success');