Completed
Push — development ( 9be619...e22509 )
by Romain
02:55
created

applyBehavioursOnSubmittedForm()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 12
nc 3
nop 1
1
<?php
2
/*
3
 * 2017 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 FormZ project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\Formz\Service\ViewHelper\Form;
15
16
use Romm\Formz\Behaviours\BehavioursManager;
17
use Romm\Formz\Exceptions\DuplicateEntryException;
18
use Romm\Formz\Form\FormObject;
19
use TYPO3\CMS\Core\SingletonInterface;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
22
23
/**
24
 * This class contains methods that help view helpers to manipulate data and
25
 * know more things concerning the current form state.
26
 *
27
 * It is mainly configured inside the `FormViewHelper`, and used in other
28
 * view helpers.
29
 */
30
class FormViewHelperService implements SingletonInterface
31
{
32
    /**
33
     * @var bool
34
     */
35
    protected $formContext = false;
36
37
    /**
38
     * @var FormObject
39
     */
40
    protected $formObject;
41
42
    /**
43
     * Reset every state that can be used by this service.
44
     */
45
    public function resetState()
46
    {
47
        $this->formContext = false;
48
        $this->formObject = null;
49
    }
50
51
    /**
52
     * Will activate the form context, changing the result returned by the
53
     * function `formContextExists()`.
54
     *
55
     * @return FormViewHelperService
56
     * @throws DuplicateEntryException
57
     */
58
    public function activateFormContext()
59
    {
60
        if (true === $this->formContext) {
61
            throw DuplicateEntryException::duplicatedFormContext();
62
        }
63
64
        $this->formContext = true;
65
66
        return $this;
67
    }
68
69
    /**
70
     * Will loop on the submitted form fields and apply behaviours if their
71
     * configuration contains.
72
     *
73
     * @param ControllerContext $controllerContext
74
     */
75
    public function applyBehavioursOnSubmittedForm(ControllerContext $controllerContext)
76
    {
77
        if ($this->formObject->formWasSubmitted()) {
78
            $request = $controllerContext->getRequest()->getOriginalRequest();
79
            $formName = $this->formObject->getName();
80
81
            if ($request
82
                && $request->hasArgument($formName)
83
            ) {
84
                /** @var BehavioursManager $behavioursManager */
85
                $behavioursManager = GeneralUtility::makeInstance(BehavioursManager::class);
86
87
                /** @var array $originalForm */
88
                $originalForm = $request->getArgument($formName);
89
90
                $formProperties = $behavioursManager->applyBehaviourOnPropertiesArray(
91
                    $originalForm,
92
                    $this->formObject->getConfiguration()
93
                );
94
95
                $request->setArgument($formName, $formProperties);
96
            }
97
        }
98
    }
99
100
    /**
101
     * Returns `true` if the `FormViewHelper` context exists.
102
     *
103
     * @return bool
104
     */
105
    public function formContextExists()
106
    {
107
        return $this->formContext;
108
    }
109
110
    /**
111
     * @return FormObject
112
     */
113
    public function getFormObject()
114
    {
115
        return $this->formObject;
116
    }
117
118
    /**
119
     * @param FormObject $formObject
120
     */
121
    public function setFormObject(FormObject $formObject)
122
    {
123
        $this->formObject = $formObject;
124
    }
125
}
126