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