Completed
Push — feature/version-2 ( 045265...d99cde )
by Romain
17s
created

FormViewHelperService::activateFormContext()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
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\AssetHandler\Html\DataAttributesAssetHandler;
17
use Romm\Formz\Behaviours\BehavioursManager;
18
use Romm\Formz\Core\Core;
19
use Romm\Formz\Error\FormResult;
20
use Romm\Formz\Exceptions\DuplicateEntryException;
21
use Romm\Formz\Form\FormObject\FormObject;
22
use Romm\Formz\Validation\Validator\Form\DefaultFormValidator;
23
use TYPO3\CMS\Core\SingletonInterface;
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
26
27
/**
28
 * This class contains methods that help view helpers to manipulate data and
29
 * know more things concerning the current form state.
30
 *
31
 * It is mainly configured inside the `FormViewHelper`, and used in other
32
 * view helpers.
33
 */
34
class FormViewHelperService implements SingletonInterface
35
{
36
    /**
37
     * @var bool
38
     */
39
    protected $formContext = false;
40
41
    /**
42
     * @var FormObject
43
     */
44
    protected $formObject;
45
46
    /**
47
     * Reset every state that can be used by this service.
48
     */
49
    public function resetState()
50
    {
51
        $this->formContext = false;
52
        $this->formObject = null;
53
    }
54
55
    /**
56
     * Will activate the form context, changing the result returned by the
57
     * function `formContextExists()`.
58
     *
59
     * @return FormViewHelperService
60
     * @throws DuplicateEntryException
61
     */
62
    public function activateFormContext()
63
    {
64
        if (true === $this->formContext) {
65
            throw DuplicateEntryException::duplicatedFormContext();
66
        }
67
68
        $this->formContext = true;
69
70
        return $this;
71
    }
72
73
    /**
74
     * Returns `true` if the `FormViewHelper` context exists.
75
     *
76
     * @return bool
77
     */
78
    public function formContextExists()
79
    {
80
        return $this->formContext;
81
    }
82
83
    /**
84
     * Will loop on the submitted form fields and apply behaviours if their
85
     * configuration contains.
86
     *
87
     * @param ControllerContext $controllerContext
88
     */
89
    public function applyBehavioursOnSubmittedForm(ControllerContext $controllerContext)
90
    {
91
        if ($this->formObject->formWasSubmitted()) {
92
            $request = $controllerContext->getRequest()->getOriginalRequest();
93
            $formName = $this->formObject->getName();
94
95
            if ($request
96
                && $request->hasArgument($formName)
97
            ) {
98
                /** @var BehavioursManager $behavioursManager */
99
                $behavioursManager = GeneralUtility::makeInstance(BehavioursManager::class);
100
101
                /** @var array $originalForm */
102
                $originalForm = $request->getArgument($formName);
103
104
                $formProperties = $behavioursManager->applyBehaviourOnPropertiesArray(
105
                    $originalForm,
106
                    $this->formObject->getDefinition()
107
                );
108
109
                $request->setArgument($formName, $formProperties);
110
            }
111
        }
112
    }
113
114
    /**
115
     * Fetches all data attributes that are bound to the form: fields values,
116
     * validation result and others.
117
     *
118
     * @param DataAttributesAssetHandler $dataAttributesAssetHandler
119
     * @return array
120
     */
121
    public function getDataAttributes(DataAttributesAssetHandler $dataAttributesAssetHandler)
122
    {
123
        $dataAttributes = [];
124
125
        if ($this->formObject->hasForm()) {
126
            /*
127
             * Getting the data attributes for the form values. It is needed to
128
             * have a validation result because a field can be deactivated (in
129
             * that case, the data attribute for this field is removed).
130
             */
131
            if (false === $this->formObject->formWasValidated()) {
132
                $formResult = $this->getFormValidationResult();
133
            } else {
134
                $formResult = $this->formObject->getFormResult();
135
            }
136
137
            $dataAttributes += $dataAttributesAssetHandler->getFieldsValuesDataAttributes($formResult);
138
        }
139
140
        if (true === $this->formObject->formWasSubmitted()) {
141
            $dataAttributes += $dataAttributesAssetHandler->getFieldSubmissionDoneDataAttribute();
142
        }
143
144
        if (true === $this->formObject->formWasValidated()) {
145
            $dataAttributes += $dataAttributesAssetHandler->getFieldsValidDataAttributes();
146
            $dataAttributes += $dataAttributesAssetHandler->getFieldsMessagesDataAttributes();
147
        }
148
149
        return $dataAttributes;
150
    }
151
152
    /**
153
     * @return FormObject
154
     */
155
    public function getFormObject()
156
    {
157
        return $this->formObject;
158
    }
159
160
    /**
161
     * @param FormObject $formObject
162
     */
163
    public function setFormObject(FormObject $formObject)
164
    {
165
        $this->formObject = $formObject;
166
    }
167
168
    /**
169
     * @return FormResult
170
     */
171
    protected function getFormValidationResult()
172
    {
173
        $formValidator = $this->getFormValidator($this->formObject->getName());
174
175
        return $formValidator->validate($this->formObject->getForm());
176
    }
177
178
    /**
179
     * @param string $formName
180
     * @return DefaultFormValidator
181
     */
182
    protected function getFormValidator($formName)
183
    {
184
        /** @var DefaultFormValidator $validation */
185
        $validation = Core::instantiate(
186
            DefaultFormValidator::class,
187
            [
188
                'name'  => $formName,
189
                'dummy' => true
190
            ]
191
        );
192
193
        return $validation;
194
    }
195
}
196