-
Notifications
You must be signed in to change notification settings - Fork 12
/
MPForms.php
225 lines (187 loc) · 6.94 KB
/
MPForms.php
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
<?php
/**
* mp_forms extension for Contao Open Source CMS
*
* @copyright Copyright (c) 2015-2016, terminal42 gmbh
* @author terminal42 gmbh <info@terminal42.ch>
* @license http://opensource.org/licenses/lgpl-3.0.html LGPL
* @link https://github.com/terminal42/contao-mp_forms
*/
use Contao\Controller;
use Contao\Form;
use Contao\FormFieldModel;
use Contao\FormModel;
use Contao\Input;
use Contao\Widget;
class MPForms
{
/**
* Adjust form fields to given page.
*
* @param FormFieldModel[] $formFields
* @param string $formId
* @param Form $form
*/
public function compileFormFields($formFields, $formId, Form $form)
{
// Make sure empty form fields arrays are skipped
if (0 === count($formFields)) {
return $formFields;
}
$manager = new MPFormsFormManager($form->id);
// Don't try to render multi page form if no valid combination
if (!$manager->isValidFormFieldCombination()) {
return $manager->getFieldsWithoutPageBreaks();
}
// If there is form data submitted in this step, store the original values here no matter if we're going back or if we continue.
if ($_POST) {
$manager->setPostData($_POST);
}
// Do not let Contao validate anything if user wants to go back
// but still save data already added to the input fields so it is
// there when they come back to the current step
if ('back' === ($_POST['mp_form_pageswitch'] ?? null)) {
$manager->storeData($_POST, [], (array) ($_SESSION['FILES'] ?? []));
$this->redirectToStep($manager, $manager->getPreviousStep());
}
// Validate previous form data if we're not on the first step. This has to be done
// no matter if we're in a POST request right now or not as otherwise you can submit
// a POST request without any previous step data (e.g. by deleting the session cookie
// manually)
if (!$manager->isFirstStep()) {
$vResult = $manager->validateSteps(0, $manager->getCurrentStep() - 1);
if (true !== $vResult) {
$manager->setPreviousStepsWereInvalid();
$this->redirectToStep($manager, $vResult);
}
}
// If someone wanted to skip the page, fake form submission so fields
// are validated and show the error message.
if ($manager->getPreviousStepsWereInvalid()) {
Input::setPost('FORM_SUBMIT', $manager->getFormId());
$manager->resetPreviousStepsWereInvalid();
}
return $manager->getFieldsForStep($manager->getCurrentStep());
}
/**
* Loads the values from the session and adds it as default value to the
* widget.
*
* @param Widget $widget
* @param string $formId
* @param array $formData
* @param Form $form
*
* @return Widget
*/
public function loadValuesFromSession(Widget $widget, $formId, $formData, Form $form)
{
$manager = new MPFormsFormManager($form->id);
if ($manager->isStoredInData($widget->name)) {
$widget->value = $manager->fetchFromData($widget->name);
}
return $widget;
}
/**
* Store the submitted data into the session and redirect to the next step
* unless it's the last.
*
* @param array $submitted
* @param array $labels
* @param $fieldsOrForm
* @param $formOrFields
*/
public function prepareFormData(&$submitted, &$labels, $fieldsOrForm, $formOrFields)
{
// Compat with Contao 4 and 3.5
$form = $fieldsOrForm instanceof Form ? $fieldsOrForm : $formOrFields;
$manager = new MPFormsFormManager($form->id);
// Don't do anything if not valid
if (!$manager->isValidFormFieldCombination()) {
return;
}
$pageSwitchValue = $submitted['mp_form_pageswitch'];
unset($submitted['mp_form_pageswitch']);
// Store data in session
$manager->storeData($submitted, $labels, (array) ($_SESSION['FILES'] ?? []));
// Submit form
if ($manager->isLastStep() && 'continue' === $pageSwitchValue) {
$allData = $manager->getDataOfAllSteps();
// Replace data by reference and then return so the default Contao
// routine kicks in
$submitted = $allData['submitted'];
$labels = $allData['labels'];
$_SESSION['FILES'] = $allData['files'];
// Override $_POST so Contao handles special cases like "email"
// too if the data was submitted in a previous step
$_POST = $submitted;
// Override $_SESSION['FORM_DATA'] so it contains the data of
// previous steps as well
$_SESSION['FORM_DATA'] = $submitted;
// Clear session
$manager->resetData();
return;
} else {
// Make sure the Contao form data session handling doesn't do
// anything at all while we're on a multipage form
$_SESSION['FORM_DATA'] = [];
}
$this->redirectToStep($manager, $manager->getNextStep());
}
/**
* Replace InsertTags.
*
* @param string $tag
*
* @return int|false
*/
public function replaceTags($tag)
{
if (strpos($tag, 'mp_forms::') === false) {
return false;
}
$chunks = explode('::', $tag);
$formId = $chunks[1];
$type = $chunks[2];
$value = $chunks[3] ?? '';
$form = FormModel::findByPk($formId);
$manager = new MPFormsFormManager($form->id);
// BC
if (\in_array($type, ['current', 'total', 'percentage', 'numbers'], true)) {
$value = $type;
$type = 'step';
}
switch ($type) {
case 'step':
return $this->getStepValue($manager, $value);
case 'field_value':
$allData = $manager->getDataOfAllSteps();
return $allData['submitted'][$value] ?? '';
}
return '';
}
private function getStepValue(MPFormsFormManager $manager, $value)
{
switch ($value) {
case 'current':
return (int) $manager->getCurrentStep() + 1;
case 'total':
return $manager->getNumberOfSteps();
case 'percentage':
return ($manager->getCurrentStep() + 1) / ($manager->getNumberOfSteps()) * 100;
case 'numbers':
return ($manager->getCurrentStep() + 1) . ' / ' . ($manager->getNumberOfSteps());
}
return '';
}
/**
* Redirect to step.
*
* @param MPFormsFormManager $manager
* @param int $step
*/
private function redirectToStep(MPFormsFormManager $manager, $step)
{
Controller::redirect($manager->getUrlForStep($step));
}
}