CRM-12882 - Allow all admins to view payment processors page. Clarify error message...
[civicrm-core.git] / CRM / Admin / Form / Preferences.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * Base class for settings forms
38 *
39 */
40class CRM_Admin_Form_Preferences extends CRM_Core_Form {
41 protected $_system = FALSE;
42 protected $_contactID = NULL;
43 protected $_action = NULL;
44
45 protected $_checkbox = NULL;
46
47 protected $_varNames = NULL;
48
49 protected $_config = NULL;
50
51 protected $_params = NULL;
52
53 function preProcess() {
54 $this->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive',
55 $this, FALSE
56 );
57 $this->_system = CRM_Utils_Request::retrieve('system', 'Boolean',
58 $this, FALSE, TRUE
59 );
60 $this->_action = CRM_Utils_Request::retrieve('action', 'String',
61 $this, FALSE, 'update'
62 );
63 if (isset($action)) {
64 $this->assign('action', $action);
65 }
66
67 $session = CRM_Core_Session::singleton();
68
69 $this->_config = new CRM_Core_DAO();
70
71 if ($this->_system) {
72 if (CRM_Core_Permission::check('administer CiviCRM')) {
73 $this->_contactID = NULL;
74 }
75 else {
76 CRM_Utils_System::fatal('You do not have permission to edit preferences');
77 }
78 $this->_config->contact_id = NULL;
79 }
80 else {
81 if (!$this->_contactID) {
82 $this->_contactID = $session->get('userID');
83 if (!$this->_contactID) {
84 CRM_Utils_System::fatal('Could not retrieve contact id');
85 }
86 $this->set('cid', $this->_contactID);
87 }
88 $this->_config->contact_id = $this->_contactID;
89 }
90
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;
95 }
96 }
97 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1'));
98 }
99
100 function setDefaultValues() {
101 $defaults = array();
102
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);
106 }
107 }
108
109 return $defaults;
110 }
111
112 function cbsDefaultValues(&$defaults) {
113
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
119 ) {
120 $value = explode(CRM_Core_DAO::VALUE_SEPARATOR,
121 substr($this->_config->$settingName, 1, -1)
122 );
123 if (!empty($value)) {
124 $defaults[$settingName] = array();
125 foreach ($value as $n => $v) {
126 $defaults[$settingName][$v] = 1;
127 }
128 }
129 }
130 }
131 }
132 }
133 }
134
135 /**
136 * Function to build the form
137 *
355ba699 138 * @return void
6a488035
TO
139 * @access public
140 */
141 public function buildQuickForm() {
142 parent::buildQuickForm();
143
144
145 if (!empty($this->_varNames)) {
146 foreach ($this->_varNames as $groupName => $groupValues) {
147 $formName = CRM_Utils_String::titleToVar($groupName);
148 $this->assign('formName', $formName);
149 $fields = array();
150 foreach ($groupValues as $fieldName => $fieldValue) {
151 $fields[$fieldName] = $fieldValue;
152
153 switch ($fieldValue['html_type']) {
154 case 'text':
155 $this->addElement('text',
156 $fieldName,
157 $fieldValue['title'],
158 array(
159 'maxlength' => 64,
160 'size' => 32,
161 )
162 );
163 break;
164
165 case 'textarea':
65f9bd70
KJ
166 case 'checkbox':
167 $this->add($fieldValue['html_type'],
6a488035
TO
168 $fieldName,
169 $fieldValue['title']
170 );
171 break;
172
65f9bd70
KJ
173 case 'radio':
174 $options = CRM_Core_OptionGroup::values($fieldName, FALSE, FALSE, TRUE);
175 $this->addRadio($fieldName, $fieldValue['title'], $options, NULL, '&nbsp;&nbsp;');
6a488035
TO
176 break;
177
178 case 'checkboxes':
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;
183 }
184 $this->addCheckBox($fieldName,
185 $fieldValue['title'],
186 $newOptions,
187 NULL, NULL, NULL, NULL,
188 array('&nbsp;&nbsp;', '&nbsp;&nbsp;', '<br/>')
189 );
190 break;
8b01c2f2
EM
191
192 case 'entity_reference':
c85faa08 193 $this->addEntityRef($fieldName, $fieldValue['title'], CRM_Utils_Array::value('options', $fieldValue, array()));
6a488035
TO
194 }
195 }
196
197 $fields = CRM_Utils_Array::crmArraySortByField($fields, 'weight');
198 $this->assign('fields', $fields);
199 }
200 }
201
202 $this->addButtons(array(
203 array(
204 'type' => 'next',
205 'name' => ts('Save'),
206 'isDefault' => TRUE,
207 ),
208 array(
209 'type' => 'cancel',
210 'name' => ts('Cancel'),
211 ),
212 )
213 );
214
215 if ($this->_action == CRM_Core_Action::VIEW) {
216 $this->freeze();
217 }
218 }
219
220 /**
221 * Function to process the form
222 *
223 * @access public
224 *
355ba699 225 * @return void
6a488035
TO
226 */
227 public function postProcess() {
228 $config = CRM_Core_Config::singleton();
229 if ($this->_action == CRM_Core_Action::VIEW) {
230 return;
231 }
232
233 $this->_params = $this->controller->exportValues($this->_name);
234
235 $this->postProcessCommon();
236 }
237 //end of function
238
239 /**
240 * Function to process the form
241 *
242 * @access public
243 *
355ba699 244 * @return void
6a488035
TO
245 */
246 public function postProcessCommon() {
247 foreach ($this->_varNames as $groupName => $groupValues) {
248 foreach ($groupValues as $settingName => $fieldValue) {
249 switch ($fieldValue['html_type']) {
250 case 'checkboxes':
a7488080 251 if (!empty($this->_params[$settingName]) &&
6a488035
TO
252 is_array($this->_params[$settingName])
253 ) {
254 $this->_config->$settingName = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
255 array_keys($this->_params[$settingName])
256 ) . CRM_Core_DAO::VALUE_SEPARATOR;
257 }
258 else {
259 $this->_config->$settingName = NULL;
260 }
261 break;
262
263 case 'checkbox':
0d8afee2 264 $this->_config->$settingName = !empty($this->_params[$settingName]) ? 1 : 0;
6a488035
TO
265 break;
266
267 case 'text':
268 case 'select':
66c3132c 269 case 'radio':
e2106968 270 case 'entity_reference':
6a488035
TO
271 $this->_config->$settingName = CRM_Utils_Array::value($settingName, $this->_params);
272 break;
273
274 case 'textarea':
275 $value = CRM_Utils_Array::value($settingName, $this->_params);
276 if ($value) {
277 $value = trim($value);
278 $value = str_replace(array("\r\n", "\r"), "\n", $value);
279 }
280 $this->_config->$settingName = $value;
281 break;
282 }
283 }
284 }
285
286 foreach ($this->_varNames as $groupName => $groupValues) {
287 foreach ($groupValues as $settingName => $fieldValue) {
288 $settingValue = isset($this->_config->$settingName) ? $this->_config->$settingName : NULL;
289 CRM_Core_BAO_Setting::setItem($settingValue,
290 $groupName,
291 $settingName
292 );
293 }
294 }
295
296 CRM_Core_Session::setStatus(ts('Your changes have been saved.'), ts('Saved'), 'success');
297 }
298
299}
300