Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
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 | ||
00be9182 | 53 | public function preProcess() { |
6a488035 TO |
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 | */ | |
00be9182 | 103 | public function setDefaultValues() { |
6a488035 TO |
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 | */ | |
00be9182 | 118 | public function cbsDefaultValues(&$defaults) { |
6a488035 TO |
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 | /** | |
eceb18cc | 142 | * Build the form object. |
6a488035 | 143 | * |
355ba699 | 144 | * @return void |
6a488035 TO |
145 | */ |
146 | public function buildQuickForm() { | |
147 | parent::buildQuickForm(); | |
148 | ||
6a488035 TO |
149 | if (!empty($this->_varNames)) { |
150 | foreach ($this->_varNames as $groupName => $groupValues) { | |
151 | $formName = CRM_Utils_String::titleToVar($groupName); | |
152 | $this->assign('formName', $formName); | |
153 | $fields = array(); | |
154 | foreach ($groupValues as $fieldName => $fieldValue) { | |
155 | $fields[$fieldName] = $fieldValue; | |
156 | ||
157 | switch ($fieldValue['html_type']) { | |
158 | case 'text': | |
159 | $this->addElement('text', | |
160 | $fieldName, | |
161 | $fieldValue['title'], | |
162 | array( | |
163 | 'maxlength' => 64, | |
164 | 'size' => 32, | |
165 | ) | |
166 | ); | |
167 | break; | |
168 | ||
169 | case 'textarea': | |
65f9bd70 KJ |
170 | case 'checkbox': |
171 | $this->add($fieldValue['html_type'], | |
6a488035 TO |
172 | $fieldName, |
173 | $fieldValue['title'] | |
174 | ); | |
175 | break; | |
176 | ||
65f9bd70 KJ |
177 | case 'radio': |
178 | $options = CRM_Core_OptionGroup::values($fieldName, FALSE, FALSE, TRUE); | |
179 | $this->addRadio($fieldName, $fieldValue['title'], $options, NULL, ' '); | |
6a488035 TO |
180 | break; |
181 | ||
182 | case 'checkboxes': | |
183 | $options = array_flip(CRM_Core_OptionGroup::values($fieldName, FALSE, FALSE, TRUE)); | |
184 | $newOptions = array(); | |
185 | foreach ($options as $key => $val) { | |
186 | $newOptions[$key] = $val; | |
187 | } | |
188 | $this->addCheckBox($fieldName, | |
189 | $fieldValue['title'], | |
190 | $newOptions, | |
191 | NULL, NULL, NULL, NULL, | |
192 | array(' ', ' ', '<br/>') | |
193 | ); | |
194 | break; | |
8b01c2f2 | 195 | |
b8ce3328 | 196 | case 'select': |
197 | $this->addElement('select', | |
198 | $fieldName, | |
199 | $fieldValue['title'], | |
200 | $fieldValue['option_values'] | |
201 | ); | |
202 | break; | |
203 | ||
fff0f449 | 204 | case 'wysiwyg': |
205 | $this->addWysiwyg($fieldName, $fieldValue['title'], $fieldValue['attributes']); | |
206 | break; | |
207 | ||
8b01c2f2 | 208 | case 'entity_reference': |
c85faa08 | 209 | $this->addEntityRef($fieldName, $fieldValue['title'], CRM_Utils_Array::value('options', $fieldValue, array())); |
6a488035 TO |
210 | } |
211 | } | |
212 | ||
213 | $fields = CRM_Utils_Array::crmArraySortByField($fields, 'weight'); | |
214 | $this->assign('fields', $fields); | |
215 | } | |
216 | } | |
217 | ||
218 | $this->addButtons(array( | |
219 | array( | |
220 | 'type' => 'next', | |
221 | 'name' => ts('Save'), | |
222 | 'isDefault' => TRUE, | |
223 | ), | |
224 | array( | |
225 | 'type' => 'cancel', | |
226 | 'name' => ts('Cancel'), | |
227 | ), | |
228 | ) | |
229 | ); | |
230 | ||
231 | if ($this->_action == CRM_Core_Action::VIEW) { | |
232 | $this->freeze(); | |
233 | } | |
234 | } | |
235 | ||
236 | /** | |
eceb18cc | 237 | * Process the form submission. |
6a488035 | 238 | * |
6a488035 | 239 | * |
355ba699 | 240 | * @return void |
6a488035 TO |
241 | */ |
242 | public function postProcess() { | |
243 | $config = CRM_Core_Config::singleton(); | |
244 | if ($this->_action == CRM_Core_Action::VIEW) { | |
245 | return; | |
246 | } | |
247 | ||
248 | $this->_params = $this->controller->exportValues($this->_name); | |
249 | ||
250 | $this->postProcessCommon(); | |
251 | } | |
6a488035 TO |
252 | |
253 | /** | |
eceb18cc | 254 | * Process the form submission. |
6a488035 | 255 | * |
6a488035 | 256 | * |
355ba699 | 257 | * @return void |
6a488035 TO |
258 | */ |
259 | public function postProcessCommon() { | |
260 | foreach ($this->_varNames as $groupName => $groupValues) { | |
261 | foreach ($groupValues as $settingName => $fieldValue) { | |
262 | switch ($fieldValue['html_type']) { | |
263 | case 'checkboxes': | |
a7488080 | 264 | if (!empty($this->_params[$settingName]) && |
6a488035 TO |
265 | is_array($this->_params[$settingName]) |
266 | ) { | |
267 | $this->_config->$settingName = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
353ffa53 TO |
268 | array_keys($this->_params[$settingName]) |
269 | ) . CRM_Core_DAO::VALUE_SEPARATOR; | |
6a488035 TO |
270 | } |
271 | else { | |
272 | $this->_config->$settingName = NULL; | |
273 | } | |
274 | break; | |
275 | ||
276 | case 'checkbox': | |
0d8afee2 | 277 | $this->_config->$settingName = !empty($this->_params[$settingName]) ? 1 : 0; |
6a488035 TO |
278 | break; |
279 | ||
280 | case 'text': | |
281 | case 'select': | |
66c3132c | 282 | case 'radio': |
e2106968 | 283 | case 'entity_reference': |
6a488035 TO |
284 | $this->_config->$settingName = CRM_Utils_Array::value($settingName, $this->_params); |
285 | break; | |
286 | ||
287 | case 'textarea': | |
288 | $value = CRM_Utils_Array::value($settingName, $this->_params); | |
289 | if ($value) { | |
290 | $value = trim($value); | |
291 | $value = str_replace(array("\r\n", "\r"), "\n", $value); | |
292 | } | |
293 | $this->_config->$settingName = $value; | |
294 | break; | |
295 | } | |
296 | } | |
297 | } | |
298 | ||
299 | foreach ($this->_varNames as $groupName => $groupValues) { | |
300 | foreach ($groupValues as $settingName => $fieldValue) { | |
301 | $settingValue = isset($this->_config->$settingName) ? $this->_config->$settingName : NULL; | |
302 | CRM_Core_BAO_Setting::setItem($settingValue, | |
303 | $groupName, | |
304 | $settingName | |
305 | ); | |
306 | } | |
307 | } | |
7b83e312 CW |
308 | // Update any settings stored in dynamic js |
309 | CRM_Core_Resources::singleton()->resetCacheCode(); | |
6a488035 TO |
310 | |
311 | CRM_Core_Session::setStatus(ts('Your changes have been saved.'), ts('Saved'), 'success'); | |
312 | } | |
313 | ||
314 | } |