Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
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 | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
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 { | |
a55c9b35 | 38 | |
39 | use CRM_Admin_Form_SettingTrait; | |
40 | ||
6a488035 TO |
41 | protected $_system = FALSE; |
42 | protected $_contactID = NULL; | |
3a936dab | 43 | public $_action = NULL; |
6a488035 TO |
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 | ||
a55c9b35 | 91 | $this->addFieldsDefinedInSettingsMetadata(); |
26cc63d3 | 92 | $settings = Civi::settings(); |
601361a3 | 93 | // @todo replace this by defining all in settings. |
6a488035 | 94 | foreach ($this->_varNames as $groupName => $settingNames) { |
26cc63d3 TO |
95 | foreach ($settingNames as $settingName => $options) { |
96 | $this->_config->$settingName = $settings->get($settingName); | |
6a488035 TO |
97 | } |
98 | } | |
99 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1')); | |
100 | } | |
101 | ||
e0ef6999 EM |
102 | /** |
103 | * @return array | |
104 | */ | |
00be9182 | 105 | public function setDefaultValues() { |
601361a3 | 106 | $this->_defaults = array(); |
6a488035 | 107 | |
601361a3 | 108 | $this->setDefaultsForMetadataDefinedFields(); |
6a488035 TO |
109 | foreach ($this->_varNames as $groupName => $settings) { |
110 | foreach ($settings as $settingName => $settingDetails) { | |
601361a3 | 111 | $this->_defaults[$settingName] = isset($this->_config->$settingName) ? $this->_config->$settingName : CRM_Utils_Array::value('default', $settingDetails, NULL); |
6a488035 TO |
112 | } |
113 | } | |
114 | ||
601361a3 | 115 | return $this->_defaults; |
6a488035 TO |
116 | } |
117 | ||
e0ef6999 EM |
118 | /** |
119 | * @param $defaults | |
120 | */ | |
00be9182 | 121 | public function cbsDefaultValues(&$defaults) { |
6a488035 TO |
122 | |
123 | foreach ($this->_varNames as $groupName => $groupValues) { | |
124 | foreach ($groupValues as $settingName => $fieldValue) { | |
125 | if ($fieldValue['html_type'] == 'checkboxes') { | |
126 | if (isset($this->_config->$settingName) && | |
127 | $this->_config->$settingName | |
128 | ) { | |
129 | $value = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
130 | substr($this->_config->$settingName, 1, -1) | |
131 | ); | |
132 | if (!empty($value)) { | |
133 | $defaults[$settingName] = array(); | |
134 | foreach ($value as $n => $v) { | |
135 | $defaults[$settingName][$v] = 1; | |
136 | } | |
137 | } | |
138 | } | |
139 | } | |
140 | } | |
141 | } | |
142 | } | |
143 | ||
144 | /** | |
eceb18cc | 145 | * Build the form object. |
6a488035 TO |
146 | */ |
147 | public function buildQuickForm() { | |
148 | parent::buildQuickForm(); | |
149 | ||
6a488035 TO |
150 | if (!empty($this->_varNames)) { |
151 | foreach ($this->_varNames as $groupName => $groupValues) { | |
152 | $formName = CRM_Utils_String::titleToVar($groupName); | |
153 | $this->assign('formName', $formName); | |
154 | $fields = array(); | |
155 | foreach ($groupValues as $fieldName => $fieldValue) { | |
156 | $fields[$fieldName] = $fieldValue; | |
157 | ||
158 | switch ($fieldValue['html_type']) { | |
159 | case 'text': | |
160 | $this->addElement('text', | |
161 | $fieldName, | |
162 | $fieldValue['title'], | |
163 | array( | |
164 | 'maxlength' => 64, | |
165 | 'size' => 32, | |
166 | ) | |
167 | ); | |
168 | break; | |
169 | ||
170 | case 'textarea': | |
65f9bd70 KJ |
171 | case 'checkbox': |
172 | $this->add($fieldValue['html_type'], | |
6a488035 TO |
173 | $fieldName, |
174 | $fieldValue['title'] | |
175 | ); | |
176 | break; | |
177 | ||
65f9bd70 KJ |
178 | case 'radio': |
179 | $options = CRM_Core_OptionGroup::values($fieldName, FALSE, FALSE, TRUE); | |
180 | $this->addRadio($fieldName, $fieldValue['title'], $options, NULL, ' '); | |
6a488035 TO |
181 | break; |
182 | ||
04c56532 SL |
183 | case 'YesNo': |
184 | $this->addRadio($fieldName, $fieldValue['title'], array(0 => 'No', 1 => 'Yes'), NULL, ' '); | |
185 | break; | |
186 | ||
6a488035 TO |
187 | case 'checkboxes': |
188 | $options = array_flip(CRM_Core_OptionGroup::values($fieldName, FALSE, FALSE, TRUE)); | |
189 | $newOptions = array(); | |
190 | foreach ($options as $key => $val) { | |
191 | $newOptions[$key] = $val; | |
192 | } | |
193 | $this->addCheckBox($fieldName, | |
194 | $fieldValue['title'], | |
195 | $newOptions, | |
196 | NULL, NULL, NULL, NULL, | |
197 | array(' ', ' ', '<br/>') | |
198 | ); | |
199 | break; | |
8b01c2f2 | 200 | |
b8ce3328 | 201 | case 'select': |
202 | $this->addElement('select', | |
203 | $fieldName, | |
204 | $fieldValue['title'], | |
b5407aa9 JP |
205 | $fieldValue['option_values'], |
206 | CRM_Utils_Array::value('attributes', $fieldValue) | |
b8ce3328 | 207 | ); |
208 | break; | |
209 | ||
fff0f449 | 210 | case 'wysiwyg': |
5d51a2f9 | 211 | $this->add('wysiwyg', $fieldName, $fieldValue['title'], $fieldValue['attributes']); |
fff0f449 | 212 | break; |
213 | ||
8b01c2f2 | 214 | case 'entity_reference': |
c85faa08 | 215 | $this->addEntityRef($fieldName, $fieldValue['title'], CRM_Utils_Array::value('options', $fieldValue, array())); |
6a488035 TO |
216 | } |
217 | } | |
218 | ||
219 | $fields = CRM_Utils_Array::crmArraySortByField($fields, 'weight'); | |
220 | $this->assign('fields', $fields); | |
221 | } | |
222 | } | |
223 | ||
224 | $this->addButtons(array( | |
225 | array( | |
226 | 'type' => 'next', | |
227 | 'name' => ts('Save'), | |
228 | 'isDefault' => TRUE, | |
229 | ), | |
230 | array( | |
231 | 'type' => 'cancel', | |
232 | 'name' => ts('Cancel'), | |
233 | ), | |
234 | ) | |
235 | ); | |
236 | ||
237 | if ($this->_action == CRM_Core_Action::VIEW) { | |
238 | $this->freeze(); | |
239 | } | |
240 | } | |
241 | ||
242 | /** | |
eceb18cc | 243 | * Process the form submission. |
6a488035 TO |
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 | } | |
6a488035 TO |
255 | |
256 | /** | |
eceb18cc | 257 | * Process the form submission. |
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': |
04c56532 | 283 | case 'YesNo': |
e2106968 | 284 | case 'entity_reference': |
6a488035 TO |
285 | $this->_config->$settingName = CRM_Utils_Array::value($settingName, $this->_params); |
286 | break; | |
287 | ||
288 | case 'textarea': | |
289 | $value = CRM_Utils_Array::value($settingName, $this->_params); | |
290 | if ($value) { | |
291 | $value = trim($value); | |
292 | $value = str_replace(array("\r\n", "\r"), "\n", $value); | |
293 | } | |
294 | $this->_config->$settingName = $value; | |
295 | break; | |
296 | } | |
297 | } | |
298 | } | |
299 | ||
300 | foreach ($this->_varNames as $groupName => $groupValues) { | |
301 | foreach ($groupValues as $settingName => $fieldValue) { | |
302 | $settingValue = isset($this->_config->$settingName) ? $this->_config->$settingName : NULL; | |
08ef4ddd | 303 | Civi::settings()->set($settingName, $settingValue); |
6a488035 TO |
304 | } |
305 | } | |
7b83e312 CW |
306 | // Update any settings stored in dynamic js |
307 | CRM_Core_Resources::singleton()->resetCacheCode(); | |
6a488035 TO |
308 | |
309 | CRM_Core_Session::setStatus(ts('Your changes have been saved.'), ts('Saved'), 'success'); | |
310 | } | |
311 | ||
312 | } |