Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
ce064e4f | 35 | * Base class for settings forms. |
6a488035 TO |
36 | */ |
37 | class CRM_Admin_Form_Preferences extends CRM_Core_Form { | |
38 | protected $_system = FALSE; | |
39 | protected $_contactID = NULL; | |
3a936dab | 40 | public $_action = NULL; |
6a488035 TO |
41 | |
42 | protected $_checkbox = NULL; | |
43 | ||
44 | protected $_varNames = NULL; | |
45 | ||
46 | protected $_config = NULL; | |
47 | ||
48 | protected $_params = NULL; | |
49 | ||
00be9182 | 50 | public function preProcess() { |
6a488035 TO |
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 | ||
26cc63d3 | 88 | $settings = Civi::settings(); |
6a488035 | 89 | foreach ($this->_varNames as $groupName => $settingNames) { |
26cc63d3 TO |
90 | foreach ($settingNames as $settingName => $options) { |
91 | $this->_config->$settingName = $settings->get($settingName); | |
6a488035 TO |
92 | } |
93 | } | |
94 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1')); | |
95 | } | |
96 | ||
e0ef6999 EM |
97 | /** |
98 | * @return array | |
99 | */ | |
00be9182 | 100 | public function setDefaultValues() { |
6a488035 TO |
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 | ||
e0ef6999 EM |
112 | /** |
113 | * @param $defaults | |
114 | */ | |
00be9182 | 115 | public function cbsDefaultValues(&$defaults) { |
6a488035 TO |
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 | /** | |
eceb18cc | 139 | * Build the form object. |
6a488035 TO |
140 | */ |
141 | public function buildQuickForm() { | |
142 | parent::buildQuickForm(); | |
143 | ||
6a488035 TO |
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': | |
65f9bd70 KJ |
165 | case 'checkbox': |
166 | $this->add($fieldValue['html_type'], | |
6a488035 TO |
167 | $fieldName, |
168 | $fieldValue['title'] | |
169 | ); | |
170 | break; | |
171 | ||
65f9bd70 KJ |
172 | case 'radio': |
173 | $options = CRM_Core_OptionGroup::values($fieldName, FALSE, FALSE, TRUE); | |
174 | $this->addRadio($fieldName, $fieldValue['title'], $options, NULL, ' '); | |
6a488035 TO |
175 | break; |
176 | ||
04c56532 SL |
177 | case 'YesNo': |
178 | $this->addRadio($fieldName, $fieldValue['title'], array(0 => 'No', 1 => 'Yes'), NULL, ' '); | |
179 | break; | |
180 | ||
6a488035 TO |
181 | case 'checkboxes': |
182 | $options = array_flip(CRM_Core_OptionGroup::values($fieldName, FALSE, FALSE, TRUE)); | |
183 | $newOptions = array(); | |
184 | foreach ($options as $key => $val) { | |
185 | $newOptions[$key] = $val; | |
186 | } | |
187 | $this->addCheckBox($fieldName, | |
188 | $fieldValue['title'], | |
189 | $newOptions, | |
190 | NULL, NULL, NULL, NULL, | |
191 | array(' ', ' ', '<br/>') | |
192 | ); | |
193 | break; | |
8b01c2f2 | 194 | |
b8ce3328 | 195 | case 'select': |
196 | $this->addElement('select', | |
197 | $fieldName, | |
198 | $fieldValue['title'], | |
b5407aa9 JP |
199 | $fieldValue['option_values'], |
200 | CRM_Utils_Array::value('attributes', $fieldValue) | |
b8ce3328 | 201 | ); |
202 | break; | |
203 | ||
fff0f449 | 204 | case 'wysiwyg': |
5d51a2f9 | 205 | $this->add('wysiwyg', $fieldName, $fieldValue['title'], $fieldValue['attributes']); |
fff0f449 | 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 TO |
238 | */ |
239 | public function postProcess() { | |
240 | $config = CRM_Core_Config::singleton(); | |
241 | if ($this->_action == CRM_Core_Action::VIEW) { | |
242 | return; | |
243 | } | |
244 | ||
245 | $this->_params = $this->controller->exportValues($this->_name); | |
246 | ||
247 | $this->postProcessCommon(); | |
248 | } | |
6a488035 TO |
249 | |
250 | /** | |
eceb18cc | 251 | * Process the form submission. |
6a488035 TO |
252 | */ |
253 | public function postProcessCommon() { | |
254 | foreach ($this->_varNames as $groupName => $groupValues) { | |
255 | foreach ($groupValues as $settingName => $fieldValue) { | |
256 | switch ($fieldValue['html_type']) { | |
257 | case 'checkboxes': | |
a7488080 | 258 | if (!empty($this->_params[$settingName]) && |
6a488035 TO |
259 | is_array($this->_params[$settingName]) |
260 | ) { | |
261 | $this->_config->$settingName = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
353ffa53 TO |
262 | array_keys($this->_params[$settingName]) |
263 | ) . CRM_Core_DAO::VALUE_SEPARATOR; | |
6a488035 TO |
264 | } |
265 | else { | |
266 | $this->_config->$settingName = NULL; | |
267 | } | |
268 | break; | |
269 | ||
270 | case 'checkbox': | |
0d8afee2 | 271 | $this->_config->$settingName = !empty($this->_params[$settingName]) ? 1 : 0; |
6a488035 TO |
272 | break; |
273 | ||
274 | case 'text': | |
275 | case 'select': | |
66c3132c | 276 | case 'radio': |
04c56532 | 277 | case 'YesNo': |
e2106968 | 278 | case 'entity_reference': |
6a488035 TO |
279 | $this->_config->$settingName = CRM_Utils_Array::value($settingName, $this->_params); |
280 | break; | |
281 | ||
282 | case 'textarea': | |
283 | $value = CRM_Utils_Array::value($settingName, $this->_params); | |
284 | if ($value) { | |
285 | $value = trim($value); | |
286 | $value = str_replace(array("\r\n", "\r"), "\n", $value); | |
287 | } | |
288 | $this->_config->$settingName = $value; | |
289 | break; | |
290 | } | |
291 | } | |
292 | } | |
293 | ||
294 | foreach ($this->_varNames as $groupName => $groupValues) { | |
295 | foreach ($groupValues as $settingName => $fieldValue) { | |
296 | $settingValue = isset($this->_config->$settingName) ? $this->_config->$settingName : NULL; | |
08ef4ddd | 297 | Civi::settings()->set($settingName, $settingValue); |
6a488035 TO |
298 | } |
299 | } | |
7b83e312 CW |
300 | // Update any settings stored in dynamic js |
301 | CRM_Core_Resources::singleton()->resetCacheCode(); | |
6a488035 TO |
302 | |
303 | CRM_Core_Session::setStatus(ts('Your changes have been saved.'), ts('Saved'), 'success'); | |
304 | } | |
305 | ||
306 | } |