Passed
Push — master ( f0d813...d082f5 )
by Thomas
03:22
created

MultiStepForm::persistData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace LeKoala\MultiStepForm;
4
5
use Exception;
6
use SilverStripe\Forms\Form;
7
use InvalidArgumentException;
8
use SilverStripe\ORM\ArrayList;
9
use SilverStripe\View\ArrayData;
10
use SilverStripe\Control\Session;
11
use SilverStripe\Forms\FieldList;
12
use SilverStripe\Forms\Validator;
13
use SilverStripe\Forms\FormAction;
14
use SilverStripe\View\Requirements;
15
use SilverStripe\Control\Controller;
16
use SilverStripe\Forms\CompositeField;
17
use SilverStripe\Forms\RequiredFields;
18
use SilverStripe\Control\RequestHandler;
19
use SilverStripe\ORM\ValidationException;
20
21
/**
22
 * Multi step form
23
 *
24
 * - Define a class name with a number in it (MyFormStep1) that extends this class
25
 * - Call definePrevNextActions instead of defining your actions
26
 * - Define a name in getStepTitle for a nicer name
27
 * - In your controller, create the form with classForCurrentStep
28
 *
29
 * @author lekoala
30
 */
31
abstract class MultiStepForm extends Form
32
{
33
    private static $include_css = true;
34
    private static $class_active = "current bg-primary text-white";
35
    private static $class_inactive = "link";
36
    private static $class_completed = "msf-completed bg-primary text-white";
37
    private static $class_not_completed = "msf-not-completed bg-light text-muted";
38
39
    protected $validationExemptActions = ["doPrev"];
40
41
    /**
42
     * @param RequestHandler $controller
43
     * @param mixed $name Extended to allow passing objects directly
44
     * @param FieldList $fields
45
     * @param FieldList $actions
46
     * @param Validator $validator
47
     */
48
    public function __construct(
49
        RequestHandler $controller = null,
50
        $name = null,
51
        FieldList $fields = null,
52
        FieldList $actions = null,
53
        Validator $validator = null
54
    ) {
55
        // Set a default name
56
        if (!$name) {
57
            $name = self::classNameWithoutNumber();
58
        }
59
        if ($fields) {
60
            throw new InvalidArgumentException("Fields should be defined inside MultiStepForm::buildFields method");
61
        }
62
        if ($actions) {
63
            throw new InvalidArgumentException("Actions are automatically defined by MultiStepForm");
64
        }
65
        if ($validator) {
66
            throw new InvalidArgumentException("Validator should be defined inside MultiStepForm::buildValidator method");
67
        }
68
        $this->setController($controller);
69
        $fields = $this->buildFields();
70
        $actions = $this->buildActions();
71
        $validator = $this->buildValidator($fields);
72
        parent::__construct($controller, $name, $fields, $actions, $validator);
73
74
        if (self::config()->include_css) {
75
            Requirements::css("lekoala/silverstripe-multi-step-form:css/multi-step-form.css");
76
        }
77
78
        // Loads first submitted data
79
        $data = $this->getTempDataFromSession();
80
        if (!empty($data)) {
81
            $this->loadDataFrom($data);
82
        } else {
83
            $this->restoreData();
84
        }
85
    }
86
87
    /**
88
     * @return FieldList
89
     */
90
    abstract protected function buildFields();
91
92
    /**
93
     * Call this instead of manually creating your actions
94
     *
95
     * You can easily rename actions by calling $actions->fieldByName('action_doNext')->setTitle('...')
96
     *
97
     * @return FieldList
98
     */
99
    protected function buildActions()
100
    {
101
        $actions = new FieldList();
102
103
        $prev = null;
104
        if (self::classNameNumber() > 1) {
105
            $prevLabel = _t('MultiStepForm.doPrev', 'Previous');
106
            $actions->push($prev = new FormAction('doPrev', $prevLabel));
107
            $prev->setUseButtonTag(true);
108
            // this must be supported by your validation client, it works with Zenvalidator
109
            $prev->addExtraClass("ignore-validation");
110
            $prev->addExtraClass("msf-step-prev");
111
        }
112
113
        $label = _t('MultiStepForm.doNext', 'Next');
114
        $actions->push($next = new FormAction('doNext', $label));
115
        $next->setUseButtonTag(true);
116
        $next->addExtraClass('msf-step-next');
117
        if (!$prev) {
118
            $next->addExtraClass('msf-step-next-single');
119
        }
120
        if (self::isLastStep()) {
121
            $next->setTitle(_t('MultiStepForm.doFinish', 'Finish'));
122
            $next->addExtraClass('msf-step-last');
123
        }
124
125
        if ($prev) {
126
            $actions->push($prev);
127
        }
128
129
        $this->addExtraClass('msf');
130
131
        return $actions;
132
    }
133
134
    /**
135
     * @param FieldList $fields
136
     * @return Validator
137
     */
138
    protected function buildValidator(FieldList $fields)
0 ignored issues
show
Unused Code introduced by
The parameter $fields is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

138
    protected function buildValidator(/** @scrutinizer ignore-unused */ FieldList $fields)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
139
    {
140
        return new RequiredFields;
141
    }
142
143
    public function FormAction()
144
    {
145
        $action = parent::FormAction();
146
        $action .= '?step=' . self::classNameNumber();
147
        return $action;
148
    }
149
150
    /**
151
     * Get a class name without namespace
152
     * @return string
153
     */
154
    public static function getClassWithoutNamespace()
155
    {
156
        $parts = explode("\\", get_called_class());
157
        return array_pop($parts);
158
    }
159
160
    /**
161
     * Get class name without any number in it
162
     * @return string
163
     */
164
    public static function classNameWithoutNumber()
165
    {
166
        return preg_replace('/[0-9]+/', '', self::getClassWithoutNamespace());
167
    }
168
169
    /**
170
     * Get number from class name
171
     * @return string
172
     */
173
    public static function classNameNumber()
174
    {
175
        return preg_replace('/[^0-9]+/', '', self::getClassWithoutNamespace());
176
    }
177
178
    /**
179
     * Get class name for current step based on this class name
180
     * @param Controller $controller
181
     * @return string
182
     */
183
    public static function classForCurrentStep($controller = null)
184
    {
185
        if (!$controller) {
186
            $controller = Controller::curr();
187
        }
188
189
        $request = $controller->getRequest();
190
191
        // Defaults to step 1
192
        $step = 1;
193
194
        // Check session
195
        $sessionStep = self::getCurrentStep();
196
        if ($sessionStep) {
197
            $step = $sessionStep;
198
        }
199
        // Override with step set manually
200
        $requestStep = $request->getVar('step');
201
        if ($requestStep) {
202
            $step = $requestStep;
203
        }
204
205
        return str_replace(self::classNameNumber(), $step, self::getClassWithoutNamespace());
206
    }
207
208
    /**
209
     * Get all steps as an ArrayList. To be used for your templates.
210
     * @return ArrayList
211
     */
212
    public function AllSteps()
213
    {
214
        $num = self::classNameNumber();
215
        if (!$num) {
216
            return;
217
        }
218
        $controller = Controller::curr();
219
        $n = 1;
220
        $curr = self::getCurrentStep($controller->getRequest()->getSession());
0 ignored issues
show
Unused Code introduced by
The call to LeKoala\MultiStepForm\Mu...pForm::getCurrentStep() has too many arguments starting with $controller->getRequest()->getSession(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

220
        /** @scrutinizer ignore-call */ 
221
        $curr = self::getCurrentStep($controller->getRequest()->getSession());

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
221
        if (!$curr) {
222
            $curr = 1;
223
        }
224
        $class = str_replace($num, $n, self::getClassWithoutNamespace());
225
        $steps = new ArrayList();
226
227
        $baseAction = parent::FormAction();
228
        $config = self::config();
229
230
        while (class_exists($class)) {
231
            $isCurrent = $isCompleted = $isNotCompleted = false;
232
            $cssClass = $n == $curr ? $config->class_active : $config->class_inactive;
233
            if ($n == 1) {
234
                $isCurrent = true;
235
                $cssClass .= ' first';
236
            }
237
            if ($class::isLastStep()) {
238
                $cssClass .= ' last';
239
            }
240
            if ($n < $curr) {
241
                $isCompleted = true;
242
                $cssClass .= ' ' . $config->class_completed;
243
            }
244
            if ($n > $curr) {
245
                $isNotCompleted = true;
246
                $cssClass .= ' ' . $config->class_not_completed;
247
            }
248
            $link = rtrim($baseAction, '/') . '/gotoStep/?step=' . $n;
249
            $steps->push(new ArrayData(array(
250
                'Title' => $class::getStepTitle(),
251
                'Number' => $n,
252
                'Link' => $isNotCompleted ? null : $link,
253
                'Class' => $cssClass,
254
                'IsCurrent' => $isCurrent,
255
                'IsCompleted' => $isCompleted,
256
                'isNotCompleted' => $isNotCompleted,
257
            )));
258
            $n++;
259
            $class = str_replace(self::classNameNumber(), $n, self::getClassWithoutNamespace());
260
        }
261
        return $steps;
262
    }
263
264
    /**
265
     * @return DBHTMLText
0 ignored issues
show
Bug introduced by
The type LeKoala\MultiStepForm\DBHTMLText was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
266
     */
267
    public function DisplaySteps()
268
    {
269
        return $this->renderWith('MsfSteps');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->renderWith('MsfSteps') returns the type SilverStripe\ORM\FieldType\DBHTMLText which is incompatible with the documented return type LeKoala\MultiStepForm\DBHTMLText.
Loading history...
270
    }
271
272
    /**
273
     * Clear current step
274
     * @return void
275
     */
276
    public static function clearCurrentStep()
277
    {
278
        $session = self::getStaticSession();
279
        $session->clear(self::classNameWithoutNumber() . '.step');
280
    }
281
282
    /**
283
     * Get current step (defined in session). 0 if not started yet.
284
     * @return int
285
     */
286
    public static function getCurrentStep()
287
    {
288
        $session = self::getStaticSession();
289
        return (int) $session->get(self::classNameWithoutNumber() . '.step');
290
    }
291
292
    /**
293
     * Set max step
294
     * @param int $value
295
     * @return void
296
     */
297
    public static function setMaxStep($value)
298
    {
299
        $session = self::getStaticSession();
300
        $session->set(self::classNameWithoutNumber() . '.maxStep', (int) $value);
301
    }
302
303
    /**
304
     * Get max step (defined in session). 0 if not started yet.
305
     * @return int
306
     */
307
    public static function getMaxStep()
308
    {
309
        $session = self::getStaticSession();
310
        return (int) $session->get(self::classNameWithoutNumber() . '.maxStep');
311
    }
312
313
    /**
314
     * Set current step
315
     * @param int $value
316
     * @return void
317
     */
318
    public static function setCurrentStep($value)
319
    {
320
        $session = self::getStaticSession();
321
        $value = (int) $value;
322
323
        // Track highest step for step navigation
324
        if ($value > self::getMaxStep()) {
325
            self::setMaxStep($value);
326
        }
327
        $session->set(self::classNameWithoutNumber() . '.step', $value);
328
    }
329
330
    /**
331
     * @return int
332
     */
333
    public static function getStepsCount()
334
    {
335
        $class = self::classNameWithoutNumber();
336
        $i = 1;
337
        $stepClass = $class . $i;
338
        while (class_exists($stepClass)) {
339
            $i++;
340
            $stepClass = $class . $i;
341
        }
342
        return --$i;
343
    }
344
345
    /**
346
     * Increment step
347
     * @return string
348
     */
349
    public static function incrementStep()
350
    {
351
        if (self::isLastStep()) {
352
            return;
353
        }
354
        $next = self::classNameNumber() + 1;
355
        if ($next == 1) {
356
            $next++;
357
        }
358
        return self::setCurrentStep($next);
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::setCurrentStep($next) returns the type void which is incompatible with the documented return type string.
Loading history...
Bug introduced by
Are you sure the usage of self::setCurrentStep($next) targeting LeKoala\MultiStepForm\Mu...pForm::setCurrentStep() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
359
    }
360
361
    /**
362
     * Decrement step
363
     * @return string
364
     */
365
    public static function decrementStep()
366
    {
367
        $session = self::getStaticSession();
0 ignored issues
show
Unused Code introduced by
The assignment to $session is dead and can be removed.
Loading history...
368
        $prev = self::classNameNumber() - 1;
369
        if ($prev < 1) {
370
            return;
371
        }
372
        return self::setCurrentStep($prev);
0 ignored issues
show
Bug introduced by
Are you sure the usage of self::setCurrentStep($prev) targeting LeKoala\MultiStepForm\Mu...pForm::setCurrentStep() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug Best Practice introduced by
The expression return self::setCurrentStep($prev) returns the type void which is incompatible with the documented return type string.
Loading history...
373
    }
374
375
    /**
376
     * Go to a step
377
     * @return HTTPResponse
0 ignored issues
show
Bug introduced by
The type LeKoala\MultiStepForm\HTTPResponse was not found. Did you mean HTTPResponse? If so, make sure to prefix the type with \.
Loading history...
378
     */
379
    public function gotoStep()
380
    {
381
        $step = $this->getController()->getRequest()->getVar('step');
382
        if ($step > 0 && $step <= self::getMaxStep()) {
383
            self::setCurrentStep($step);
384
        }
385
        return $this->getController()->redirectBack();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getController()->redirectBack() returns the type SilverStripe\Control\HTTPResponse which is incompatible with the documented return type LeKoala\MultiStepForm\HTTPResponse.
Loading history...
386
    }
387
388
    /**
389
     * Check if this is the last step
390
     * @return bool
391
     */
392
    public static function isLastStep()
393
    {
394
        $n = self::classNameNumber();
395
        $n1 = $n + 1;
396
        $class = str_replace($n, $n1, self::getClassWithoutNamespace());
397
        return !class_exists($class);
398
    }
399
400
    /**
401
     * Return the step name
402
     * @return string
403
     */
404
    abstract public static function getStepTitle();
405
406
    /**
407
     * Can be overwritten in child classes to update submitted data
408
     *
409
     * @param array $data
410
     * @return array
411
     */
412
    protected function processData(array $data)
413
    {
414
        return $data;
415
    }
416
417
    /**
418
     * @return array
419
     */
420
    protected function restoreData()
421
    {
422
        $data = $this->getDataFromSession();
423
        if (!empty($data)) {
424
            $this->loadDataFrom($data);
425
            return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the documented return type array.
Loading history...
426
        }
427
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type array.
Loading history...
428
    }
429
430
431
    protected function persistData(array $data = [])
432
    {
433
        $this->saveDataInSession($data);
434
    }
435
436
    /**
437
     * Can be overwritten in child classes to apply custom step validation
438
     *
439
     * @throws ValidationException
440
     * @param array $data
441
     * @return void
442
     */
443
    protected function validateData(array $data)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

443
    protected function validateData(/** @scrutinizer ignore-unused */ array $data)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
444
    {
445
    }
446
447
    /**
448
     * @return Session
449
     */
450
    public static function getStaticSession()
451
    {
452
        return Controller::curr()->getRequest()->getSession();
453
    }
454
455
    /**
456
     * @param Controller $controller
457
     * @return Session
458
     */
459
    public function getSession($controller = null)
460
    {
461
        if ($controller === null) {
462
            $controller = $this->getController();
463
        }
464
        if ($controller) {
465
            return $controller->getRequest()->getSession();
466
        }
467
        return self::getStaticSession();
468
    }
469
470
    /**
471
     * A basic previous action that decrements the current step
472
     * @param array $data
473
     * @return HTTPResponse
474
     */
475
    public function doPrev($data)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

475
    public function doPrev(/** @scrutinizer ignore-unused */ $data)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
476
    {
477
        $controller = $this->getController();
478
        self::decrementStep($this->getSession());
0 ignored issues
show
Unused Code introduced by
The call to LeKoala\MultiStepForm\Mu...epForm::decrementStep() has too many arguments starting with $this->getSession(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

478
        self::/** @scrutinizer ignore-call */ 
479
              decrementStep($this->getSession());

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
479
        return $controller->redirectBack();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $controller->redirectBack() returns the type SilverStripe\Control\HTTPResponse which is incompatible with the documented return type LeKoala\MultiStepForm\HTTPResponse.
Loading history...
480
    }
481
482
    /**
483
     * A basic next action that increments the current step and save the data to the session
484
     * @param array $data
485
     * @return HTTPResponse
486
     */
487
    public function doNext($data)
488
    {
489
        $controller = $this->getController();
490
491
        try {
492
            $this->validateData($data);
493
        } catch (ValidationException $ex) {
494
            $this->saveTempDataInSession($data);
495
            $this->sessionError($ex->getMessage());
496
            return $controller->redirectBack();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $controller->redirectBack() returns the type SilverStripe\Control\HTTPResponse which is incompatible with the documented return type LeKoala\MultiStepForm\HTTPResponse.
Loading history...
497
        }
498
499
        $data = $this->processData($data);
500
501
        self::incrementStep();
502
        $this->clearTempDataFromSession();
503
504
        try {
505
            $this->persistData($data);
506
        } catch (Exception $ex) {
507
            $this->sessionError($ex->getMessage());
508
            return $controller->redirectBack();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $controller->redirectBack() returns the type SilverStripe\Control\HTTPResponse which is incompatible with the documented return type LeKoala\MultiStepForm\HTTPResponse.
Loading history...
509
        }
510
511
        if (self::isLastStep()) {
512
            // You will need to clear the current step and redirect to something else on the last step
513
            throw new Exception("Not implemented: please override doNext in your class");
514
        }
515
516
        return $controller->redirectBack();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $controller->redirectBack() returns the type SilverStripe\Control\HTTPResponse which is incompatible with the documented return type LeKoala\MultiStepForm\HTTPResponse.
Loading history...
517
    }
518
519
    /**
520
     * @param int $step
521
     * @return array
522
     */
523
    public static function getDataFromStep($step)
524
    {
525
        $session = self::getStaticSession();
526
        return $session->get(self::classNameWithoutNumber() . ".step_" . $step);
527
    }
528
529
    /**
530
     * @param array $data
531
     */
532
    public function saveDataInSession(array $data = null)
533
    {
534
        $session = $this->getSession();
535
        if (!$data) {
536
            $data = $this->getData();
537
        }
538
        $session->set(
539
            self::classNameWithoutNumber() . ".step_" . self::classNameNumber(),
540
            $data
541
        );
542
    }
543
544
    /**
545
     * @param array $data
546
     */
547
    public function saveTempDataInSession(array $data = null)
548
    {
549
        $session = $this->getSession();
550
        if (!$data) {
551
            $data = $this->getData();
552
        }
553
        $session->set(
554
            self::classNameWithoutNumber() . ".temp",
555
            $data
556
        );
557
    }
558
559
    /**
560
     * @return array
561
     */
562
    public function getDataFromSession()
563
    {
564
        $session = $this->getSession();
565
        return $session->get(self::classNameWithoutNumber() . ".step_" . self::classNameNumber());
566
    }
567
568
    /**
569
     * This is the data as submitted by the user
570
     *
571
     * @return array
572
     */
573
    public function getTempDataFromSession()
574
    {
575
        $session = $this->getSession();
576
        return $session->get(self::classNameWithoutNumber() . ".temp");
577
    }
578
579
    /**
580
     * @param boolean $merge Merge everything into a flat array (true by default) or return a multi dimensional array
581
     * @return array
582
     */
583
    public static function getAllDataFromSession($merge = true)
584
    {
585
        $session = self::getStaticSession();
586
        $arr = [];
587
        $class = self::classNameWithoutNumber();
588
        foreach (range(1, self::getStepsCount()) as $i) {
589
            if ($merge) {
590
                $step = $session->get($class . ".step_" . $i);
591
                if ($step) {
592
                    $arr = array_merge($arr, $step);
593
                }
594
            } else {
595
                $arr[$i] = $session->get($class . ".step_" . $i);
596
            }
597
        }
598
        return $arr;
599
    }
600
601
    /**
602
     * Utility to quickly scaffold cms facing fields
603
     *
604
     * @param FieldList $fields
605
     * @param array $data
606
     * @param array $ignore
607
     * @return void
608
     */
609
    public static function getAsTabbedFields(FieldList $fields, $data = [], $ignore = [])
610
    {
611
        $controller = Controller::curr();
612
        $class = self::classNameWithoutNumber();
613
        foreach (range(1, self::getStepsCount()) as $i) {
614
            $classname = $class . $i;
615
            $inst = new $classname($controller);
616
617
            $stepFields = $inst->Fields();
618
619
            foreach ($stepFields as $sf) {
620
                $name = $sf->getName();
621
                if (in_array($name, $ignore)) {
622
                    continue;
623
                }
624
625
                $sf->setReadonly(true);
626
                if (!empty($data[$name])) {
627
                    $sf->setValue($data[$name]);
628
                }
629
630
                if ($sf instanceof CompositeField) {
631
                    foreach ($sf->getChildren() as $child) {
632
                        $childName = $child->getName();
633
                        if (in_array($childName, $ignore)) {
634
                            continue;
635
                        }
636
637
                        if (!empty($data[$childName])) {
638
                            $child->setValue($data[$childName]);
639
                        }
640
                    }
641
                }
642
643
                $fields->addFieldsToTab('Root.Step' . $i, $sf);
644
            }
645
        }
646
    }
647
648
    /**
649
     * @param int $step
650
     * @return array
651
     */
652
    public function clearTempDataFromSession()
653
    {
654
        $session = $this->getSession();
655
        return $session->clear(self::classNameWithoutNumber() . ".temp");
0 ignored issues
show
Bug Best Practice introduced by
The expression return $session->clear(s...houtNumber() . '.temp') returns the type SilverStripe\Control\Session which is incompatible with the documented return type array.
Loading history...
656
    }
657
658
    /**
659
     * @param int $step
660
     * @return array
661
     */
662
    public function clearDataFromSession()
663
    {
664
        $session = $this->getSession();
665
        return $session->clear(self::classNameWithoutNumber() . ".step_" . self::classNameNumber());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $session->clear(s...elf::classNameNumber()) returns the type SilverStripe\Control\Session which is incompatible with the documented return type array.
Loading history...
666
    }
667
668
    /**
669
     * Clear all infos stored in the session from all steps
670
     */
671
    public function clearAllDataFromSession()
672
    {
673
        $session = $this->getSession();
674
        self::clearCurrentStep();
675
        $session->clear(self::classNameWithoutNumber());
676
    }
677
678
    public function buildRequestHandler()
679
    {
680
        return new MultiStepFormRequestHandler($this);
681
    }
682
}
683