Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2014-11-24-15-59-25
[civicrm-core.git] / CRM / Admin / Form / Preferences.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * Base class for settings forms
38 *
39 */
40 class 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 /**
101 * @return array
102 */
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
115 /**
116 * @param $defaults
117 */
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 *
144 * @return void
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':
172 case 'checkbox':
173 $this->add($fieldValue['html_type'],
174 $fieldName,
175 $fieldValue['title']
176 );
177 break;
178
179 case 'radio':
180 $options = CRM_Core_OptionGroup::values($fieldName, FALSE, FALSE, TRUE);
181 $this->addRadio($fieldName, $fieldValue['title'], $options, NULL, '&nbsp;&nbsp;');
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;
197
198 case 'select':
199 $this->addElement('select',
200 $fieldName,
201 $fieldValue['title'],
202 $fieldValue['option_values']
203 );
204 break;
205
206 case 'wysiwyg':
207 $this->addWysiwyg($fieldName, $fieldValue['title'], $fieldValue['attributes']);
208 break;
209
210 case 'entity_reference':
211 $this->addEntityRef($fieldName, $fieldValue['title'], CRM_Utils_Array::value('options', $fieldValue, array()));
212 }
213 }
214
215 $fields = CRM_Utils_Array::crmArraySortByField($fields, 'weight');
216 $this->assign('fields', $fields);
217 }
218 }
219
220 $this->addButtons(array(
221 array(
222 'type' => 'next',
223 'name' => ts('Save'),
224 'isDefault' => TRUE,
225 ),
226 array(
227 'type' => 'cancel',
228 'name' => ts('Cancel'),
229 ),
230 )
231 );
232
233 if ($this->_action == CRM_Core_Action::VIEW) {
234 $this->freeze();
235 }
236 }
237
238 /**
239 * Function to process the form
240 *
241 * @access public
242 *
243 * @return void
244 */
245 public function postProcess() {
246 $config = CRM_Core_Config::singleton();
247 if ($this->_action == CRM_Core_Action::VIEW) {
248 return;
249 }
250
251 $this->_params = $this->controller->exportValues($this->_name);
252
253 $this->postProcessCommon();
254 }
255 //end of function
256
257 /**
258 * Function to process the form
259 *
260 * @access public
261 *
262 * @return void
263 */
264 public function postProcessCommon() {
265 foreach ($this->_varNames as $groupName => $groupValues) {
266 foreach ($groupValues as $settingName => $fieldValue) {
267 switch ($fieldValue['html_type']) {
268 case 'checkboxes':
269 if (!empty($this->_params[$settingName]) &&
270 is_array($this->_params[$settingName])
271 ) {
272 $this->_config->$settingName = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
273 array_keys($this->_params[$settingName])
274 ) . CRM_Core_DAO::VALUE_SEPARATOR;
275 }
276 else {
277 $this->_config->$settingName = NULL;
278 }
279 break;
280
281 case 'checkbox':
282 $this->_config->$settingName = !empty($this->_params[$settingName]) ? 1 : 0;
283 break;
284
285 case 'text':
286 case 'select':
287 case 'radio':
288 case 'entity_reference':
289 $this->_config->$settingName = CRM_Utils_Array::value($settingName, $this->_params);
290 break;
291
292 case 'textarea':
293 $value = CRM_Utils_Array::value($settingName, $this->_params);
294 if ($value) {
295 $value = trim($value);
296 $value = str_replace(array("\r\n", "\r"), "\n", $value);
297 }
298 $this->_config->$settingName = $value;
299 break;
300 }
301 }
302 }
303
304 foreach ($this->_varNames as $groupName => $groupValues) {
305 foreach ($groupValues as $settingName => $fieldValue) {
306 $settingValue = isset($this->_config->$settingName) ? $this->_config->$settingName : NULL;
307 CRM_Core_BAO_Setting::setItem($settingValue,
308 $groupName,
309 $settingName
310 );
311 }
312 }
313
314 CRM_Core_Session::setStatus(ts('Your changes have been saved.'), ts('Saved'), 'success');
315 }
316
317 }
318