forked from terminal42/contao-mp_forms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMPForms.php
More file actions
494 lines (419 loc) · 12.6 KB
/
Copy pathMPForms.php
File metadata and controls
494 lines (419 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
<?php if (!defined('TL_ROOT')) die('You cannot access this file directly!');
/**
* Contao Open Source CMS
* Copyright (C) 2005-2012 Leo Feyer
*
* Formerly known as TYPOlight Open Source CMS.
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, please visit the Free
* Software Foundation website at <http://www.gnu.org/licenses/>.
*
* PHP version 5
* @copyright Leo Feyer 2005-2012
* @author Leo Feyer <http://www.contao.org>
* @package mp_forms
* @license LGPL
* @filesource
*/
class MPForms extends Controller
{
/**
* Prepare an array that sorts the field ids of a certain form id per page
* @param int form id
* @return array
*/
public static function sortFormFieldsPerPage($intForm)
{
$arrSorted = array();
$i=0;
$db = Database::getInstance();
$objFields = $db->prepare("SELECT id,type FROM tl_form_field WHERE pid=? AND invisible!=1 ORDER BY sorting")->execute($intForm);
while ($objFields->next())
{
if ($objFields->type == 'mp_form_pageswitch')
{
$arrSorted[$i][] = $objFields->id;
$i++;
continue;
}
$arrSorted[$i][] = $objFields->id;
}
return $arrSorted;
}
/**
* Get the number of steps of a form
* @param int form id
* @return int number of steps
*/
public static function getNumberOfSteps($intForm)
{
return count(array_keys(self::sortFormFieldsPerPage($intForm)));
}
/**
* Get the current step of a form
* @param int form id
* @return int number of current step
*/
public static function getCurrentStep($intForm)
{
$objForm = Database::getInstance()->prepare('SELECT * FROM tl_form WHERE id=?')->execute($intForm);
$intStep = Input::getInstance()->get((($objForm->mp_forms_getParam) ? $objForm->mp_forms_getParam : 'step'));
$intStep = ($intStep) ? $intStep : 1;
return $intStep;
}
/**
* Get the widgets of a step
* @param int form id
* @param int step
* @return array
*/
public static function getWidgetsOfStep($intForm, $intStep)
{
$arrSorted = self::sortFormFieldsPerPage($intForm);
return $arrSorted[$intStep];
}
/**
* Make sure the Form class doesn't validate all the widgets but the ones on the current page
* @param Widget
* @param string form id
* @param array form data
*/
public function loadFormField($objWidget, &$formId, $arrData)
{
$intTotalSteps = self::getNumberOfSteps($arrData['id']);
// no steps included, ignore
if (!$intTotalSteps)
{
return $objWidget;
}
$strStepGetParam = ($arrData['mp_forms_getParam']) ? $arrData['mp_forms_getParam'] : 'step';
$blnStepSubmitted = (boolean) $this->Input->post('mpform_submit_' . $arrData['id']) || $this->Input->post('mpform_submit_' . $arrData['id'] . '_x');
// if no parameter is set but we have steps, then we are on step 1
$intCurrentStep = ($this->Input->get($strStepGetParam)) ? $this->Input->get($strStepGetParam) : 1;
// redirect to step 1 if somebody feels like he has to enter step=1000000
if ($intCurrentStep > $intTotalSteps)
{
$this->redirectToStep($strStepGetParam, 1);
}
// -1 because we use arrays but a user doesn't get ?step=0
$intCurrentStep = $intCurrentStep - 1;
// determine in what step the current widget is
$intImInStep = self::findStepOfWidget($objWidget->id, $arrData['id']);
// handle everything that is form submit related first! DO NOT CHANGE!
// validate only fields from that page
if ($blnStepSubmitted && $intImInStep == $intCurrentStep)
{
// validate
$this->validateWidget($objWidget, $formId, $arrData);
// but the value we only store if there are no errors
if ($objWidget->hasErrors())
{
self::storeWidgetErrors($arrData['id'], $objWidget);
}
else
{
self::storeValueInSession($arrData['id'], $objWidget);
self::removeWidgetErrors($arrData['id'], $objWidget->id);
}
// go to the next page if validated and the following conditions apply:
// - if it's the last step of the form there's no redirect
// - only redirect when it's the last widget on that step (otherwise following widgets will not be validated)
// - only redirect if there are no errors for this step
if ($intCurrentStep != $intTotalSteps
&& self::isLastWidgetOfStep($intCurrentStep, $objWidget, $arrData['id'])
&& !self::stepHasErrors($arrData['id'], $intCurrentStep))
{
$this->redirectToStep($strStepGetParam, ($intCurrentStep + 2));
}
// submit the form if it's the last step and everything is fine
// otherwise make sure that the form is not sent so we just modify the $formId
if ($intCurrentStep == $intTotalSteps
&& self::isLastWidgetOfStep($intCurrentStep, $objWidget, $arrData['id'])
&& !self::stepHasErrors($arrData['id'], $intCurrentStep))
{
self::getValueFromSession($arrData['id'], $objWidget);
$formId = ($arrData['formID']) ? 'auto_' . $arrData['formID'] : 'auto_form' . $arrData['id'];
}
else
{
$formId = 'dummy_' . str_replace('dummy_', '', $formId);
}
}
// not submitted form
else
{
// first of all we load the value from the session (if there is any)
self::getValueFromSession($arrData['id'], $objWidget);
// field is in a previous step
if ($intImInStep < $intCurrentStep)
{
// set value as post data because this is what the validator is going to check
$this->Input->setPost($objWidget->name, $objWidget->value);
// validate the field because we cannot go to e.g. step 3 if step 1 is not correct
$this->validateWidget($objWidget, $formId, $arrData);
if ($objWidget->hasErrors())
{
self::storeWidgetErrors($arrData['id'], $objWidget);
}
}
// field is in the current step
if ($intImInStep == $intCurrentStep)
{
// check if a previous step has errors and redirect if there are any
for ($i=0;$i<$intCurrentStep;$i++)
{
if (self::stepHasErrors($arrData['id'], $i))
{
$this->redirectToStep($strStepGetParam, $i);
}
}
// show errors if there are any
if (self::widgetHasErrors($arrData['id'], $objWidget->id))
{
foreach (self::getWidgetErrors($arrData['id'], $objWidget->id) as $strError)
{
$objWidget->addError($strError);
}
}
}
}
// you can make me a dummy as I don't want to be shown other than in the current step
// but not for the page switch form field
if ($intImInStep != $intCurrentStep)
{
$objWidget = $this->createDummyWidget($objWidget);
}
return $objWidget;
}
/**
* Find the step of a certain widget id
* @param int widget id
* @param int form id
* @return int step id
*/
private static function findStepOfWidget($intWidgetId, $intForm)
{
$intStepId = 0;
$arrSorted = self::sortFormFieldsPerPage($intForm);
foreach ($arrSorted as $step => $arrWidgetIds)
{
if (in_array($intWidgetId, $arrWidgetIds))
{
$intStepId = $step;
break;
}
}
return $intStepId;
}
/**
* Check whether a widget is the last on a step (apart from the page switch submit button obviously)
* @param int step
* @param Widget
* @param int form id
* @return boolean
*/
private static function isLastWidgetOfStep($intStep, Widget $objWidget, $intForm)
{
$arrSorted = self::sortFormFieldsPerPage($intForm);
$arrStepWidgets = $arrSorted[$intStep];
// get SECOND last element (because of the page switch submit button)
if ($arrStepWidgets[count($arrStepWidgets) - 2] == $objWidget->id)
{
return true;
}
return false;
}
/**
* Validate a widget
* @param Widget
* @param int form id
* @param array data array
*/
private function validateWidget($objWidget, $formId, $arrData)
{
$objWidget->validate();
// HOOK: validate form field callback
if (isset($GLOBALS['TL_HOOKS']['validateFormField']) && is_array($GLOBALS['TL_HOOKS']['validateFormField']))
{
foreach ($GLOBALS['TL_HOOKS']['validateFormField'] as $callback)
{
$this->import($callback[0]);
$objWidget = $this->$callback[0]->$callback[1]($objWidget, $formId, $arrData);
}
}
}
/**
* Creates a dummy widget
* @param Widget original widget
* @return Widget dummy widget
*/
private function createDummyWidget(Widget $objWidget)
{
$arrDummyData = array
(
'id' => $objWidget->id,
'name' => $objWidget->name,
'value' => $objWidget->id // only store the widget id, the value will get added later
);
return new FormHidden($arrDummyData);
}
/**
* Redirects to a certain step while keeping all other GET params
* @param string url
* @param string GET param
* @param string new value
* @return string
*/
private function redirectToStep($strGet, $intStep)
{
$arrChunks = trimsplit('[?&=]', $this->Environment->request);
$arrGETParams = array();
if (is_array($arrChunks) && !empty($arrChunks))
{
$strUrl = array_shift($arrChunks);
for ($i=0, $count=count($arrChunks);$i<$count;$i=$i+2)
{
if ($arrChunks[$i] != 'step')
{
$arrGETParams[$arrChunks[$i]] = $arrChunks[$i + 1];
}
}
if ($intStep > 0)
{
$arrGETParams[$strGet] = $intStep;
}
$this->redirect(($strUrl . ((count($arrGETParams)) ? '?' : '') . http_build_query($arrGETParams)));
}
}
/**
* Get a widget value from the session
* @param int form id
* @param Widget
*/
private static function getValueFromSession($intForm, Widget $objWidget)
{
if ($_SESSION['MPFORMSTORAGE']['formId_' . $intForm]['widget_value_' . $objWidget->id])
{
$objWidget->value = $_SESSION['MPFORMSTORAGE']['formId_' . $intForm]['widget_value_' . $objWidget->id];
}
}
/**
* Store a widget value into the session
* @param int form id
* @param Widget
*/
private static function storeValueInSession($intForm, Widget $objWidget)
{
$_SESSION['MPFORMSTORAGE']['formId_' . $intForm]['widget_value_' . $objWidget->id] = $objWidget->value;
}
/**
* Store errors of a widget
* @param int form id
* @param Widget
*/
private static function storeWidgetErrors($intForm, Widget $objWidget)
{
$_SESSION['MPFORMSTORAGE']['formId_' . $intForm]['widget_errors_' . $objWidget->id] = $objWidget->getErrors();
}
/**
* Remove errors of a widget
* @param int form id
* @param int widget id
*/
private static function removeWidgetErrors($intForm, $intWidgetId)
{
$_SESSION['MPFORMSTORAGE']['formId_' . $intForm]['widget_errors_' . $intWidgetId] = array();
}
/**
* Get errors of a widget
* @param int form id
* @param int widget id
* @return array
*/
private static function getWidgetErrors($intForm, $intWidgetId)
{
return ($_SESSION['MPFORMSTORAGE']['formId_' . $intForm]['widget_errors_' . $intWidgetId]) ? $_SESSION['MPFORMSTORAGE']['formId_' . $intForm]['widget_errors_' . $intWidgetId] : array();
}
/**
* Checks if a widget has an error
* @param int form id
* @param int widget id
* @return boolean
*/
private static function widgetHasErrors($intForm, $intWidgetId)
{
$arrWidgetErrors = self::getWidgetErrors($intForm, $intWidgetId);
return (!empty($arrWidgetErrors));
}
/**
* Load errors from the session
* @param int form id
* @param int step
* @return array
*/
private static function getStepErrors($intForm, $intStep)
{
$arrWidgets = self::getWidgetsOfStep($intForm, $intStep);
$arrErrors = array();
foreach ($arrWidgets as $intWidgetId)
{
if (self::widgetHasErrors($intForm, $intWidgetId))
{
$arrErrors[] = self::getWidgetErrors($intForm, $intWidgetId);
}
}
return $arrErrors;
}
/**
* Check if a step has errors
* @param int form id
* @param int step
* @return boolean
*/
private static function stepHasErrors($intForm, $intStep)
{
$arrStepErrors = self::getStepErrors($intForm, $intStep);
$blnHasErrors = false;
foreach ($arrStepErrors as $arrWidgetErrors)
{
if (!empty($arrWidgetErrors))
{
$blnHasErrors = true;
}
}
return $blnHasErrors;
}
/**
* Replace InsertTags
* @param string
* @return string|false
*/
public function replaceTags($strTag)
{
if (strpos($strTag, 'mp_forms::') === false)
{
return false;
}
$arrChunks = explode('::', $strTag);
$intForm = $arrChunks[1];
$strMode = $arrChunks[2];
switch ($strMode)
{
case 'current':
return self::getCurrentStep($intForm);
case 'total':
return self::getNumberOfSteps($intForm);
}
}
}