copyright and version fixes
[civicrm-core.git] / CRM / Admin / Form / Preferences.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
31 * @copyright CiviCRM LLC (c) 2004-2013
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;
191 }
192 }
193
194 $fields = CRM_Utils_Array::crmArraySortByField($fields, 'weight');
195 $this->assign('fields', $fields);
196 }
197 }
198
199 $this->addButtons(array(
200 array(
201 'type' => 'next',
202 'name' => ts('Save'),
203 'isDefault' => TRUE,
204 ),
205 array(
206 'type' => 'cancel',
207 'name' => ts('Cancel'),
208 ),
209 )
210 );
211
212 if ($this->_action == CRM_Core_Action::VIEW) {
213 $this->freeze();
214 }
215 }
216
217 /**
218 * Function to process the form
219 *
220 * @access public
221 *
355ba699 222 * @return void
6a488035
TO
223 */
224 public function postProcess() {
225 $config = CRM_Core_Config::singleton();
226 if ($this->_action == CRM_Core_Action::VIEW) {
227 return;
228 }
229
230 $this->_params = $this->controller->exportValues($this->_name);
231
232 $this->postProcessCommon();
233 }
234 //end of function
235
236 /**
237 * Function to process the form
238 *
239 * @access public
240 *
355ba699 241 * @return void
6a488035
TO
242 */
243 public function postProcessCommon() {
244 foreach ($this->_varNames as $groupName => $groupValues) {
245 foreach ($groupValues as $settingName => $fieldValue) {
246 switch ($fieldValue['html_type']) {
247 case 'checkboxes':
a7488080 248 if (!empty($this->_params[$settingName]) &&
6a488035
TO
249 is_array($this->_params[$settingName])
250 ) {
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;
254 }
255 else {
256 $this->_config->$settingName = NULL;
257 }
258 break;
259
260 case 'checkbox':
0d8afee2 261 $this->_config->$settingName = !empty($this->_params[$settingName]) ? 1 : 0;
6a488035
TO
262 break;
263
264 case 'text':
265 case 'select':
66c3132c 266 case 'radio':
6a488035
TO
267 $this->_config->$settingName = CRM_Utils_Array::value($settingName, $this->_params);
268 break;
269
270 case 'textarea':
271 $value = CRM_Utils_Array::value($settingName, $this->_params);
272 if ($value) {
273 $value = trim($value);
274 $value = str_replace(array("\r\n", "\r"), "\n", $value);
275 }
276 $this->_config->$settingName = $value;
277 break;
278 }
279 }
280 }
281
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,
286 $groupName,
287 $settingName
288 );
289 }
290 }
291
292 CRM_Core_Session::setStatus(ts('Your changes have been saved.'), ts('Saved'), 'success');
293 }
294
295}
296