CRM add remaining missing comment blocks (autogenerated)
[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
e0ef6999
EM
100 /**
101 * @return array
102 */
6a488035
TO
103 function setDefaultValues() {
104 $defaults = array();
105
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);
109 }
110 }
111
112 return $defaults;
113 }
114
e0ef6999
EM
115 /**
116 * @param $defaults
117 */
6a488035
TO
118 function cbsDefaultValues(&$defaults) {
119
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
125 ) {
126 $value = explode(CRM_Core_DAO::VALUE_SEPARATOR,
127 substr($this->_config->$settingName, 1, -1)
128 );
129 if (!empty($value)) {
130 $defaults[$settingName] = array();
131 foreach ($value as $n => $v) {
132 $defaults[$settingName][$v] = 1;
133 }
134 }
135 }
136 }
137 }
138 }
139 }
140
141 /**
142 * Function to build the form
143 *
355ba699 144 * @return void
6a488035
TO
145 * @access public
146 */
147 public function buildQuickForm() {
148 parent::buildQuickForm();
149
150
151 if (!empty($this->_varNames)) {
152 foreach ($this->_varNames as $groupName => $groupValues) {
153 $formName = CRM_Utils_String::titleToVar($groupName);
154 $this->assign('formName', $formName);
155 $fields = array();
156 foreach ($groupValues as $fieldName => $fieldValue) {
157 $fields[$fieldName] = $fieldValue;
158
159 switch ($fieldValue['html_type']) {
160 case 'text':
161 $this->addElement('text',
162 $fieldName,
163 $fieldValue['title'],
164 array(
165 'maxlength' => 64,
166 'size' => 32,
167 )
168 );
169 break;
170
171 case 'textarea':
65f9bd70
KJ
172 case 'checkbox':
173 $this->add($fieldValue['html_type'],
6a488035
TO
174 $fieldName,
175 $fieldValue['title']
176 );
177 break;
178
65f9bd70
KJ
179 case 'radio':
180 $options = CRM_Core_OptionGroup::values($fieldName, FALSE, FALSE, TRUE);
181 $this->addRadio($fieldName, $fieldValue['title'], $options, NULL, '&nbsp;&nbsp;');
6a488035
TO
182 break;
183
184 case 'checkboxes':
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;
189 }
190 $this->addCheckBox($fieldName,
191 $fieldValue['title'],
192 $newOptions,
193 NULL, NULL, NULL, NULL,
194 array('&nbsp;&nbsp;', '&nbsp;&nbsp;', '<br/>')
195 );
196 break;
8b01c2f2
EM
197
198 case 'entity_reference':
c85faa08 199 $this->addEntityRef($fieldName, $fieldValue['title'], CRM_Utils_Array::value('options', $fieldValue, array()));
6a488035
TO
200 }
201 }
202
203 $fields = CRM_Utils_Array::crmArraySortByField($fields, 'weight');
204 $this->assign('fields', $fields);
205 }
206 }
207
208 $this->addButtons(array(
209 array(
210 'type' => 'next',
211 'name' => ts('Save'),
212 'isDefault' => TRUE,
213 ),
214 array(
215 'type' => 'cancel',
216 'name' => ts('Cancel'),
217 ),
218 )
219 );
220
221 if ($this->_action == CRM_Core_Action::VIEW) {
222 $this->freeze();
223 }
224 }
225
226 /**
227 * Function to process the form
228 *
229 * @access public
230 *
355ba699 231 * @return void
6a488035
TO
232 */
233 public function postProcess() {
234 $config = CRM_Core_Config::singleton();
235 if ($this->_action == CRM_Core_Action::VIEW) {
236 return;
237 }
238
239 $this->_params = $this->controller->exportValues($this->_name);
240
241 $this->postProcessCommon();
242 }
243 //end of function
244
245 /**
246 * Function to process the form
247 *
248 * @access public
249 *
355ba699 250 * @return void
6a488035
TO
251 */
252 public function postProcessCommon() {
253 foreach ($this->_varNames as $groupName => $groupValues) {
254 foreach ($groupValues as $settingName => $fieldValue) {
255 switch ($fieldValue['html_type']) {
256 case 'checkboxes':
a7488080 257 if (!empty($this->_params[$settingName]) &&
6a488035
TO
258 is_array($this->_params[$settingName])
259 ) {
260 $this->_config->$settingName = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
261 array_keys($this->_params[$settingName])
262 ) . CRM_Core_DAO::VALUE_SEPARATOR;
263 }
264 else {
265 $this->_config->$settingName = NULL;
266 }
267 break;
268
269 case 'checkbox':
0d8afee2 270 $this->_config->$settingName = !empty($this->_params[$settingName]) ? 1 : 0;
6a488035
TO
271 break;
272
273 case 'text':
274 case 'select':
66c3132c 275 case 'radio':
e2106968 276 case 'entity_reference':
6a488035
TO
277 $this->_config->$settingName = CRM_Utils_Array::value($settingName, $this->_params);
278 break;
279
280 case 'textarea':
281 $value = CRM_Utils_Array::value($settingName, $this->_params);
282 if ($value) {
283 $value = trim($value);
284 $value = str_replace(array("\r\n", "\r"), "\n", $value);
285 }
286 $this->_config->$settingName = $value;
287 break;
288 }
289 }
290 }
291
292 foreach ($this->_varNames as $groupName => $groupValues) {
293 foreach ($groupValues as $settingName => $fieldValue) {
294 $settingValue = isset($this->_config->$settingName) ? $this->_config->$settingName : NULL;
295 CRM_Core_BAO_Setting::setItem($settingValue,
296 $groupName,
297 $settingName
298 );
299 }
300 }
301
302 CRM_Core_Session::setStatus(ts('Your changes have been saved.'), ts('Saved'), 'success');
303 }
304
305}
306