1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\MultiStepForm; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use SilverStripe\Forms\Tab; |
7
|
|
|
use SilverStripe\Forms\Form; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use SilverStripe\ORM\ArrayList; |
10
|
|
|
use SilverStripe\View\ArrayData; |
11
|
|
|
use SilverStripe\Control\Session; |
12
|
|
|
use SilverStripe\Forms\FieldList; |
13
|
|
|
use SilverStripe\Forms\FormField; |
14
|
|
|
use SilverStripe\Forms\Validator; |
15
|
|
|
use SilverStripe\Forms\FormAction; |
16
|
|
|
use SilverStripe\View\Requirements; |
17
|
|
|
use SilverStripe\Control\Controller; |
18
|
|
|
use SilverStripe\Forms\CompositeField; |
19
|
|
|
use SilverStripe\Forms\RequiredFields; |
20
|
|
|
use SilverStripe\Control\RequestHandler; |
21
|
|
|
use SilverStripe\ORM\ValidationException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Multi step form |
25
|
|
|
* |
26
|
|
|
* - Define a class name with a number in it (MyFormStep1) that extends this class |
27
|
|
|
* - Call definePrevNextActions instead of defining your actions |
28
|
|
|
* - Define a name in getStepTitle for a nicer name |
29
|
|
|
* - In your controller, create the form with classForCurrentStep |
30
|
|
|
* |
31
|
|
|
* @author lekoala |
32
|
|
|
*/ |
33
|
|
|
abstract class MultiStepForm extends Form |
34
|
|
|
{ |
35
|
|
|
private static $include_css = true; |
36
|
|
|
private static $class_active = "current bg-primary text-white"; |
37
|
|
|
private static $class_inactive = "link"; |
38
|
|
|
private static $class_completed = "msf-completed bg-primary text-white"; |
39
|
|
|
private static $class_not_completed = "msf-not-completed bg-light text-muted"; |
40
|
|
|
|
41
|
|
|
protected $validationExemptActions = ["doPrev"]; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param RequestHandler $controller |
45
|
|
|
* @param mixed $name Extended to allow passing objects directly |
46
|
|
|
* @param FieldList $fields |
47
|
|
|
* @param FieldList $actions |
48
|
|
|
* @param Validator $validator |
49
|
|
|
*/ |
50
|
|
|
public function __construct( |
51
|
|
|
RequestHandler $controller = null, |
52
|
|
|
$name = null, |
53
|
|
|
FieldList $fields = null, |
54
|
|
|
FieldList $actions = null, |
55
|
|
|
Validator $validator = null |
56
|
|
|
) { |
57
|
|
|
// Set a default name |
58
|
|
|
if (!$name) { |
59
|
|
|
$name = self::classNameWithoutNumber(); |
60
|
|
|
} |
61
|
|
|
if ($fields) { |
62
|
|
|
throw new InvalidArgumentException("Fields should be defined inside MultiStepForm::buildFields method"); |
63
|
|
|
} |
64
|
|
|
if ($actions) { |
65
|
|
|
throw new InvalidArgumentException("Actions are automatically defined by MultiStepForm"); |
66
|
|
|
} |
67
|
|
|
if ($validator) { |
68
|
|
|
throw new InvalidArgumentException("Validator should be defined inside MultiStepForm::buildValidator method"); |
69
|
|
|
} |
70
|
|
|
$this->setController($controller); |
71
|
|
|
$fields = $this->buildFields(); |
72
|
|
|
$actions = $this->buildActions(); |
73
|
|
|
$validator = $this->buildValidator($fields); |
74
|
|
|
parent::__construct($controller, $name, $fields, $actions, $validator); |
75
|
|
|
|
76
|
|
|
if (self::config()->include_css) { |
77
|
|
|
Requirements::css("lekoala/silverstripe-multi-step-form:css/multi-step-form.css"); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// Loads first submitted data |
81
|
|
|
$data = $this->getTempDataFromSession(); |
82
|
|
|
if (!empty($data)) { |
83
|
|
|
$this->loadDataFrom($data); |
84
|
|
|
} else { |
85
|
|
|
$this->restoreData(); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return FieldList |
91
|
|
|
*/ |
92
|
|
|
abstract protected function buildFields(); |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Call this instead of manually creating your actions |
96
|
|
|
* |
97
|
|
|
* You can easily rename actions by calling $actions->fieldByName('action_doNext')->setTitle('...') |
98
|
|
|
* |
99
|
|
|
* @return FieldList |
100
|
|
|
*/ |
101
|
|
|
protected function buildActions() |
102
|
|
|
{ |
103
|
|
|
$actions = new FieldList(); |
104
|
|
|
|
105
|
|
|
$prev = null; |
106
|
|
|
if (self::classNameNumber() > 1) { |
107
|
|
|
$prevLabel = _t('MultiStepForm.doPrev', 'Previous'); |
108
|
|
|
$actions->push($prev = new FormAction('doPrev', $prevLabel)); |
109
|
|
|
$prev->setUseButtonTag(true); |
110
|
|
|
// this must be supported by your validation client, it works with Zenvalidator |
111
|
|
|
$prev->addExtraClass("ignore-validation"); |
112
|
|
|
$prev->addExtraClass("msf-step-prev"); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$label = _t('MultiStepForm.doNext', 'Next'); |
116
|
|
|
$actions->push($next = new FormAction('doNext', $label)); |
117
|
|
|
$next->setUseButtonTag(true); |
118
|
|
|
$next->addExtraClass('msf-step-next'); |
119
|
|
|
if (!$prev) { |
120
|
|
|
$next->addExtraClass('msf-step-next-single'); |
121
|
|
|
} |
122
|
|
|
if (self::isLastStep()) { |
123
|
|
|
$next->setTitle(_t('MultiStepForm.doFinish', 'Finish')); |
124
|
|
|
$next->addExtraClass('msf-step-last'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if ($prev) { |
128
|
|
|
$actions->push($prev); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$this->addExtraClass('msf'); |
132
|
|
|
|
133
|
|
|
return $actions; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param FieldList $fields |
138
|
|
|
* @return Validator |
139
|
|
|
*/ |
140
|
|
|
protected function buildValidator(FieldList $fields) |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
return new RequiredFields; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function FormAction() |
146
|
|
|
{ |
147
|
|
|
$action = parent::FormAction(); |
148
|
|
|
$action .= '?step=' . self::classNameNumber(); |
149
|
|
|
return $action; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get a class name without namespace |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
|
|
public static function getClassWithoutNamespace() |
157
|
|
|
{ |
158
|
|
|
$parts = explode("\\", get_called_class()); |
159
|
|
|
return array_pop($parts); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Get class name without any number in it |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
|
|
public static function classNameWithoutNumber() |
167
|
|
|
{ |
168
|
|
|
return preg_replace('/[0-9]+/', '', self::getClassWithoutNamespace()); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Get number from class name |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
|
|
public static function classNameNumber() |
176
|
|
|
{ |
177
|
|
|
return preg_replace('/[^0-9]+/', '', self::getClassWithoutNamespace()); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Get class name for current step based on this class name |
182
|
|
|
* @param Controller $controller |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
|
|
public static function classForCurrentStep($controller = null) |
186
|
|
|
{ |
187
|
|
|
if (!$controller) { |
188
|
|
|
$controller = Controller::curr(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$request = $controller->getRequest(); |
192
|
|
|
|
193
|
|
|
// Defaults to step 1 |
194
|
|
|
$step = 1; |
195
|
|
|
|
196
|
|
|
// Check session |
197
|
|
|
$sessionStep = self::getCurrentStep(); |
198
|
|
|
if ($sessionStep) { |
199
|
|
|
$step = $sessionStep; |
200
|
|
|
} |
201
|
|
|
// Override with step set manually |
202
|
|
|
$requestStep = (int)$request->getVar('step'); |
203
|
|
|
if ($requestStep) { |
204
|
|
|
$step = $requestStep; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return str_replace(self::classNameNumber(), $step, self::getClassWithoutNamespace()); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Get all steps as an ArrayList. To be used for your templates. |
212
|
|
|
* @return ArrayList |
213
|
|
|
*/ |
214
|
|
|
public function AllSteps() |
215
|
|
|
{ |
216
|
|
|
$num = self::classNameNumber(); |
217
|
|
|
if (!$num) { |
218
|
|
|
return; |
219
|
|
|
} |
220
|
|
|
$controller = Controller::curr(); |
221
|
|
|
$n = 1; |
222
|
|
|
$curr = self::getCurrentStep($controller->getRequest()->getSession()); |
|
|
|
|
223
|
|
|
if (!$curr) { |
224
|
|
|
$curr = 1; |
225
|
|
|
} |
226
|
|
|
$class = str_replace($num, $n, self::getClassWithoutNamespace()); |
227
|
|
|
$steps = new ArrayList(); |
228
|
|
|
|
229
|
|
|
$baseAction = parent::FormAction(); |
230
|
|
|
$config = self::config(); |
231
|
|
|
|
232
|
|
|
while (class_exists($class)) { |
233
|
|
|
$isCurrent = $isCompleted = $isNotCompleted = false; |
234
|
|
|
$cssClass = $n == $curr ? $config->class_active : $config->class_inactive; |
235
|
|
|
if ($n == 1) { |
236
|
|
|
$isCurrent = true; |
237
|
|
|
$cssClass .= ' first'; |
238
|
|
|
} |
239
|
|
|
if ($class::isLastStep()) { |
240
|
|
|
$cssClass .= ' last'; |
241
|
|
|
} |
242
|
|
|
if ($n < $curr) { |
243
|
|
|
$isCompleted = true; |
244
|
|
|
$cssClass .= ' ' . $config->class_completed; |
245
|
|
|
} |
246
|
|
|
if ($n > $curr) { |
247
|
|
|
$isNotCompleted = true; |
248
|
|
|
$cssClass .= ' ' . $config->class_not_completed; |
249
|
|
|
} |
250
|
|
|
$link = rtrim($baseAction, '/') . '/gotoStep/?step=' . $n; |
251
|
|
|
$steps->push(new ArrayData(array( |
252
|
|
|
'Title' => $class::getStepTitle(), |
253
|
|
|
'Number' => $n, |
254
|
|
|
'Link' => $isNotCompleted ? null : $link, |
255
|
|
|
'Class' => $cssClass, |
256
|
|
|
'IsCurrent' => $isCurrent, |
257
|
|
|
'IsCompleted' => $isCompleted, |
258
|
|
|
'isNotCompleted' => $isNotCompleted, |
259
|
|
|
))); |
260
|
|
|
$n++; |
261
|
|
|
$class = str_replace(self::classNameNumber(), $n, self::getClassWithoutNamespace()); |
262
|
|
|
} |
263
|
|
|
return $steps; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @return DBHTMLText |
|
|
|
|
268
|
|
|
*/ |
269
|
|
|
public function DisplaySteps() |
270
|
|
|
{ |
271
|
|
|
return $this->renderWith('MsfSteps'); |
|
|
|
|
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Clear current step |
276
|
|
|
* @return void |
277
|
|
|
*/ |
278
|
|
|
public static function clearCurrentStep() |
279
|
|
|
{ |
280
|
|
|
$session = self::getStaticSession(); |
281
|
|
|
$session->clear(self::classNameWithoutNumber() . '.step'); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Get current step (defined in session). 0 if not started yet. |
286
|
|
|
* @return int |
287
|
|
|
*/ |
288
|
|
|
public static function getCurrentStep() |
289
|
|
|
{ |
290
|
|
|
$session = self::getStaticSession(); |
291
|
|
|
return (int) $session->get(self::classNameWithoutNumber() . '.step'); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Set max step |
296
|
|
|
* @param int $value |
297
|
|
|
* @return void |
298
|
|
|
*/ |
299
|
|
|
public static function setMaxStep($value) |
300
|
|
|
{ |
301
|
|
|
$session = self::getStaticSession(); |
302
|
|
|
$session->set(self::classNameWithoutNumber() . '.maxStep', (int) $value); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Get max step (defined in session). 0 if not started yet. |
307
|
|
|
* @return int |
308
|
|
|
*/ |
309
|
|
|
public static function getMaxStep() |
310
|
|
|
{ |
311
|
|
|
$session = self::getStaticSession(); |
312
|
|
|
return (int) $session->get(self::classNameWithoutNumber() . '.maxStep'); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Set current step |
317
|
|
|
* @param int $value |
318
|
|
|
* @return void |
319
|
|
|
*/ |
320
|
|
|
public static function setCurrentStep($value) |
321
|
|
|
{ |
322
|
|
|
$session = self::getStaticSession(); |
323
|
|
|
$value = (int) $value; |
324
|
|
|
|
325
|
|
|
// Track highest step for step navigation |
326
|
|
|
if ($value > self::getMaxStep()) { |
327
|
|
|
self::setMaxStep($value); |
328
|
|
|
} |
329
|
|
|
$session->set(self::classNameWithoutNumber() . '.step', $value); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* @return int |
334
|
|
|
*/ |
335
|
|
|
public static function getStepsCount() |
336
|
|
|
{ |
337
|
|
|
$class = self::classNameWithoutNumber(); |
338
|
|
|
$i = 1; |
339
|
|
|
$stepClass = $class . $i; |
340
|
|
|
while (class_exists($stepClass)) { |
341
|
|
|
$i++; |
342
|
|
|
$stepClass = $class . $i; |
343
|
|
|
} |
344
|
|
|
return --$i; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* Increment step |
349
|
|
|
* @return string |
350
|
|
|
*/ |
351
|
|
|
public static function incrementStep() |
352
|
|
|
{ |
353
|
|
|
if (self::isLastStep()) { |
354
|
|
|
return; |
355
|
|
|
} |
356
|
|
|
$next = self::classNameNumber() + 1; |
357
|
|
|
if ($next == 1) { |
358
|
|
|
$next++; |
359
|
|
|
} |
360
|
|
|
return self::setCurrentStep($next); |
|
|
|
|
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* Decrement step |
365
|
|
|
* @return string |
366
|
|
|
*/ |
367
|
|
|
public static function decrementStep() |
368
|
|
|
{ |
369
|
|
|
$session = self::getStaticSession(); |
|
|
|
|
370
|
|
|
$prev = self::classNameNumber() - 1; |
371
|
|
|
if ($prev < 1) { |
372
|
|
|
return; |
373
|
|
|
} |
374
|
|
|
return self::setCurrentStep($prev); |
|
|
|
|
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Go to a step |
379
|
|
|
* @return HTTPResponse |
|
|
|
|
380
|
|
|
*/ |
381
|
|
|
public function gotoStep() |
382
|
|
|
{ |
383
|
|
|
$step = $this->getController()->getRequest()->getVar('step'); |
384
|
|
|
if ($step > 0 && $step <= self::getMaxStep()) { |
385
|
|
|
self::setCurrentStep($step); |
386
|
|
|
} |
387
|
|
|
return $this->getController()->redirectBack(); |
|
|
|
|
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* Check if this is the last step |
392
|
|
|
* @return bool |
393
|
|
|
*/ |
394
|
|
|
public static function isLastStep() |
395
|
|
|
{ |
396
|
|
|
$n = self::classNameNumber(); |
397
|
|
|
$n1 = $n + 1; |
398
|
|
|
$class = str_replace($n, $n1, self::getClassWithoutNamespace()); |
399
|
|
|
return !class_exists($class); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* Return the step name |
404
|
|
|
* @return string |
405
|
|
|
*/ |
406
|
|
|
abstract public static function getStepTitle(); |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* Can be overwritten in child classes to update submitted data |
410
|
|
|
* |
411
|
|
|
* @param array $data |
412
|
|
|
* @return array |
413
|
|
|
*/ |
414
|
|
|
protected function processData(array $data) |
415
|
|
|
{ |
416
|
|
|
return $data; |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* @return bool |
421
|
|
|
*/ |
422
|
|
|
protected function restoreData() |
423
|
|
|
{ |
424
|
|
|
$data = $this->getDataFromSession(); |
425
|
|
|
if (!empty($data)) { |
426
|
|
|
$this->loadDataFrom($data); |
427
|
|
|
return true; |
428
|
|
|
} |
429
|
|
|
return false; |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
|
433
|
|
|
protected function persistData(array $data = []) |
434
|
|
|
{ |
435
|
|
|
$this->saveDataInSession($data); |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* Can be overwritten in child classes to apply custom step validation |
440
|
|
|
* |
441
|
|
|
* @throws ValidationException |
442
|
|
|
* @param array $data |
443
|
|
|
* @return void |
444
|
|
|
*/ |
445
|
|
|
protected function validateData(array $data) |
|
|
|
|
446
|
|
|
{ |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
/** |
450
|
|
|
* @return Session |
451
|
|
|
*/ |
452
|
|
|
public static function getStaticSession() |
453
|
|
|
{ |
454
|
|
|
return Controller::curr()->getRequest()->getSession(); |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
/** |
458
|
|
|
* @param Controller $controller |
459
|
|
|
* @return Session |
460
|
|
|
*/ |
461
|
|
|
public function getSession($controller = null) |
462
|
|
|
{ |
463
|
|
|
if ($controller === null) { |
464
|
|
|
$controller = $this->getController(); |
465
|
|
|
} |
466
|
|
|
if ($controller) { |
467
|
|
|
return $controller->getRequest()->getSession(); |
468
|
|
|
} |
469
|
|
|
return self::getStaticSession(); |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
/** |
473
|
|
|
* A basic previous action that decrements the current step |
474
|
|
|
* @param array $data |
475
|
|
|
* @return HTTPResponse |
476
|
|
|
*/ |
477
|
|
|
public function doPrev($data) |
|
|
|
|
478
|
|
|
{ |
479
|
|
|
$controller = $this->getController(); |
480
|
|
|
self::decrementStep($this->getSession()); |
|
|
|
|
481
|
|
|
return $controller->redirectBack(); |
|
|
|
|
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
/** |
485
|
|
|
* A basic next action that increments the current step and save the data to the session |
486
|
|
|
* @param array $data |
487
|
|
|
* @return HTTPResponse |
488
|
|
|
*/ |
489
|
|
|
public function doNext($data) |
490
|
|
|
{ |
491
|
|
|
$controller = $this->getController(); |
492
|
|
|
|
493
|
|
|
try { |
494
|
|
|
$this->validateData($data); |
495
|
|
|
} catch (ValidationException $ex) { |
496
|
|
|
$this->saveTempDataInSession($data); |
497
|
|
|
$this->sessionError($ex->getMessage()); |
498
|
|
|
return $controller->redirectBack(); |
|
|
|
|
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
$data = $this->processData($data); |
502
|
|
|
|
503
|
|
|
self::incrementStep(); |
504
|
|
|
$this->clearTempDataFromSession(); |
505
|
|
|
|
506
|
|
|
try { |
507
|
|
|
$this->persistData($data); |
508
|
|
|
} catch (Exception $ex) { |
509
|
|
|
$this->sessionError($ex->getMessage()); |
510
|
|
|
return $controller->redirectBack(); |
|
|
|
|
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
if (self::isLastStep()) { |
514
|
|
|
// You will need to clear the current step and redirect to something else on the last step |
515
|
|
|
throw new Exception("Not implemented: please override doNext in your class for last step"); |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
return $controller->redirectBack(); |
|
|
|
|
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
/** |
522
|
|
|
* @param int $step |
523
|
|
|
* @return array |
524
|
|
|
*/ |
525
|
|
|
public static function getDataFromStep($step) |
526
|
|
|
{ |
527
|
|
|
$session = self::getStaticSession(); |
528
|
|
|
return $session->get(self::classNameWithoutNumber() . ".step_" . $step); |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
/** |
532
|
|
|
* @param array $data |
533
|
|
|
*/ |
534
|
|
|
public function saveDataInSession(array $data = null) |
535
|
|
|
{ |
536
|
|
|
$session = $this->getSession(); |
537
|
|
|
if (!$data) { |
538
|
|
|
$data = $this->getData(); |
539
|
|
|
} |
540
|
|
|
$session->set( |
541
|
|
|
self::classNameWithoutNumber() . ".step_" . self::classNameNumber(), |
542
|
|
|
$data |
543
|
|
|
); |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
/** |
547
|
|
|
* @param array $data |
548
|
|
|
*/ |
549
|
|
|
public function saveTempDataInSession(array $data = null) |
550
|
|
|
{ |
551
|
|
|
$session = $this->getSession(); |
552
|
|
|
if (!$data) { |
553
|
|
|
$data = $this->getData(); |
554
|
|
|
} |
555
|
|
|
$session->set( |
556
|
|
|
self::classNameWithoutNumber() . ".temp", |
557
|
|
|
$data |
558
|
|
|
); |
559
|
|
|
} |
560
|
|
|
|
561
|
|
|
/** |
562
|
|
|
* @return array |
563
|
|
|
*/ |
564
|
|
|
public function getDataFromSession() |
565
|
|
|
{ |
566
|
|
|
$session = $this->getSession(); |
567
|
|
|
return $session->get(self::classNameWithoutNumber() . ".step_" . self::classNameNumber()); |
568
|
|
|
} |
569
|
|
|
|
570
|
|
|
/** |
571
|
|
|
* This is the data as submitted by the user |
572
|
|
|
* |
573
|
|
|
* @return array |
574
|
|
|
*/ |
575
|
|
|
public function getTempDataFromSession() |
576
|
|
|
{ |
577
|
|
|
$session = $this->getSession(); |
578
|
|
|
return $session->get(self::classNameWithoutNumber() . ".temp"); |
579
|
|
|
} |
580
|
|
|
|
581
|
|
|
/** |
582
|
|
|
* @param boolean $merge Merge everything into a flat array (true by default) or return a multi dimensional array |
583
|
|
|
* @return array |
584
|
|
|
*/ |
585
|
|
|
public static function getAllDataFromSession($merge = true) |
586
|
|
|
{ |
587
|
|
|
$session = self::getStaticSession(); |
588
|
|
|
$arr = []; |
589
|
|
|
$class = self::classNameWithoutNumber(); |
590
|
|
|
foreach (range(1, self::getStepsCount()) as $i) { |
591
|
|
|
if ($merge) { |
592
|
|
|
$step = $session->get($class . ".step_" . $i); |
593
|
|
|
if ($step) { |
594
|
|
|
$arr = array_merge($arr, $step); |
595
|
|
|
} |
596
|
|
|
} else { |
597
|
|
|
$arr[$i] = $session->get($class . ".step_" . $i); |
598
|
|
|
} |
599
|
|
|
} |
600
|
|
|
return $arr; |
601
|
|
|
} |
602
|
|
|
|
603
|
|
|
/** |
604
|
|
|
* Utility to quickly scaffold cms facing fields |
605
|
|
|
* |
606
|
|
|
* @param FieldList $fields |
607
|
|
|
* @param array $data |
608
|
|
|
* @param array $ignore |
609
|
|
|
* @return void |
610
|
|
|
*/ |
611
|
|
|
public static function getAsTabbedFields(FieldList $fields, $data = [], $ignore = []) |
612
|
|
|
{ |
613
|
|
|
$controller = Controller::curr(); |
614
|
|
|
$class = self::classNameWithoutNumber(); |
615
|
|
|
foreach (range(1, self::getStepsCount()) as $i) { |
616
|
|
|
$classname = $class . $i; |
617
|
|
|
$inst = new $classname($controller); |
618
|
|
|
|
619
|
|
|
$stepFields = $inst->Fields(); |
620
|
|
|
|
621
|
|
|
$tab = new Tab('Step' . $i, $i . " - " . $inst->getStepTitle()); |
622
|
|
|
$fields->addFieldToTab('Root', $tab); |
623
|
|
|
|
624
|
|
|
/** @var FormField $sf */ |
625
|
|
|
foreach ($stepFields as $sf) { |
626
|
|
|
$name = $sf->getName(); |
627
|
|
|
if (in_array($name, $ignore)) { |
628
|
|
|
continue; |
629
|
|
|
} |
630
|
|
|
|
631
|
|
|
$sf->setReadonly(true); |
632
|
|
|
// $sf->setDisabled(true); |
633
|
|
|
if (!empty($data[$name])) { |
634
|
|
|
$sf->setValue($data[$name]); |
635
|
|
|
} |
636
|
|
|
|
637
|
|
|
if ($sf instanceof CompositeField) { |
638
|
|
|
foreach ($sf->getChildren() as $child) { |
639
|
|
|
$childName = $child->getName(); |
640
|
|
|
if (in_array($childName, $ignore)) { |
641
|
|
|
continue; |
642
|
|
|
} |
643
|
|
|
|
644
|
|
|
if (!empty($data[$childName])) { |
645
|
|
|
$child->setValue($data[$childName]); |
646
|
|
|
} |
647
|
|
|
} |
648
|
|
|
} |
649
|
|
|
|
650
|
|
|
$tab->push($sf); |
651
|
|
|
} |
652
|
|
|
} |
653
|
|
|
} |
654
|
|
|
|
655
|
|
|
/** |
656
|
|
|
* @param int $step |
657
|
|
|
* @return array |
658
|
|
|
*/ |
659
|
|
|
public function clearTempDataFromSession() |
660
|
|
|
{ |
661
|
|
|
$session = $this->getSession(); |
662
|
|
|
return $session->clear(self::classNameWithoutNumber() . ".temp"); |
|
|
|
|
663
|
|
|
} |
664
|
|
|
|
665
|
|
|
/** |
666
|
|
|
* @param int $step |
667
|
|
|
* @return array |
668
|
|
|
*/ |
669
|
|
|
public function clearDataFromSession() |
670
|
|
|
{ |
671
|
|
|
$session = $this->getSession(); |
672
|
|
|
return $session->clear(self::classNameWithoutNumber() . ".step_" . self::classNameNumber()); |
|
|
|
|
673
|
|
|
} |
674
|
|
|
|
675
|
|
|
/** |
676
|
|
|
* Clear all infos stored in the session from all steps |
677
|
|
|
*/ |
678
|
|
|
public function clearAllDataFromSession() |
679
|
|
|
{ |
680
|
|
|
$session = $this->getSession(); |
681
|
|
|
self::clearCurrentStep(); |
682
|
|
|
$session->clear(self::classNameWithoutNumber()); |
683
|
|
|
} |
684
|
|
|
|
685
|
|
|
public function buildRequestHandler() |
686
|
|
|
{ |
687
|
|
|
return new MultiStepFormRequestHandler($this); |
688
|
|
|
} |
689
|
|
|
} |
690
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.