Completed
Push — middleware-wip ( 5cfd03...f2f782 )
by Romain
05:53
created

FormViewHelperService::getDataAttributes()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.439
c 0
b 0
f 0
cc 5
eloc 16
nc 12
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;
15
16
use Romm\Formz\AssetHandler\Html\DataAttributesAssetHandler;
17
use Romm\Formz\Behaviours\BehavioursManager;
18
use Romm\Formz\Core\Core;
19
use Romm\Formz\Exceptions\DuplicateEntryException;
20
use Romm\Formz\Form\FormObject\FormObject;
21
use Romm\Formz\Validation\Validator\Form\AbstractFormValidator;
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\Configuration\ConfigurationManager;
26
use TYPO3\CMS\Extbase\Error\Result;
27
use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
28
29
/**
30
 * This class contains methods that help view helpers to manipulate data and
31
 * know more things concerning the current form state.
32
 *
33
 * It is mainly configured inside the `FormViewHelper`, and used in other
34
 * view helpers.
35
 */
36
class FormViewHelperService implements SingletonInterface
37
{
38
    /**
39
     * @var bool
40
     */
41
    protected $formContext = false;
42
43
    /**
44
     * @var FormObject
45
     */
46
    protected $formObject;
47
48
    /**
49
     * @var Result
50
     */
51
    protected $result;
52
53
    /**
54
     * Reset every state that can be used by this service.
55
     */
56
    public function resetState()
57
    {
58
        $this->formContext = false;
59
        $this->formObject = null;
60
    }
61
62
    /**
63
     * Will activate the form context, changing the result returned by the
64
     * function `formContextExists()`.
65
     *
66
     * @return FormViewHelperService
67
     * @throws DuplicateEntryException
68
     */
69
    public function activateFormContext()
70
    {
71
        if (true === $this->formContext) {
72
            throw DuplicateEntryException::duplicatedFormContext();
73
        }
74
75
        $this->formContext = true;
76
        $this->result = new Result;
77
78
        return $this;
79
    }
80
81
    /**
82
     * Returns `true` if the `FormViewHelper` context exists.
83
     *
84
     * @return bool
85
     */
86
    public function formContextExists()
87
    {
88
        return $this->formContext;
89
    }
90
91
    /**
92
     * Will loop on the submitted form fields and apply behaviours if their
93
     * configuration contains.
94
     *
95
     * @param ControllerContext $controllerContext
96
     */
97
    public function applyBehavioursOnSubmittedForm(ControllerContext $controllerContext)
98
    {
99
        if ($this->formObject->formWasSubmitted()) {
100
            $request = $controllerContext->getRequest()->getOriginalRequest();
101
            $formName = $this->formObject->getName();
102
103
            if ($request
104
                && $request->hasArgument($formName)
105
            ) {
106
                /** @var BehavioursManager $behavioursManager */
107
                $behavioursManager = GeneralUtility::makeInstance(BehavioursManager::class);
108
109
                /** @var array $originalForm */
110
                $originalForm = $request->getArgument($formName);
111
112
                $formProperties = $behavioursManager->applyBehaviourOnPropertiesArray(
113
                    $originalForm,
114
                    $this->formObject->getDefinition()
115
                );
116
117
                $request->setArgument($formName, $formProperties);
118
            }
119
        }
120
    }
121
122
    /**
123
     * Takes care of injecting data for the form.
124
     *
125
     * If the form was generated using a content object, information about it
126
     * are injected, to be retrieved later to be able for instance to fetch the
127
     * object settings (TypoScript, FlexForm, ...).
128
     */
129
    public function injectFormRequestData()
130
    {
131
        if (false === $this->formObject->hasForm()) {
132
            return;
133
        }
134
135
        /** @var ConfigurationManager $configurationManager */
136
        $configurationManager = Core::instantiate(ConfigurationManager::class);
137
138
        $contentObject = $configurationManager->getContentObject();
139
140
        if (null !== $contentObject) {
141
            $requestData = $this->formObject->getRequestData();
142
143
            $requestData->setContentObjectTable($contentObject->getCurrentTable());
144
            $requestData->setContentObjectUid($contentObject->data['uid']);
145
        }
146
    }
147
148
    /**
149
     * @todo
150
     *
151
     * @param DataAttributesAssetHandler $dataAttributesAssetHandler
152
     * @return array
153
     */
154
    public function getDataAttributes(DataAttributesAssetHandler $dataAttributesAssetHandler)
155
    {
156
        $dataAttributes = [];
157
158
        if ($this->formObject->hasForm()) {
159
            if (false === $this->formObject->formWasValidated()) {
160
                $form = $this->formObject->getForm();
161
                $formValidator = $this->getFormValidator($this->formObject->getName());
162
                $formResult = $formValidator->validate($form);
163
            } else {
164
                $formResult = $this->formObject->getFormResult();
165
            }
166
167
            $dataAttributes += $dataAttributesAssetHandler->getFieldsValuesDataAttributes($formResult);
168
        }
169
170
        if (true === $this->formObject->formWasSubmitted()) {
171
            $dataAttributes += [DataAttributesAssetHandler::getFieldSubmissionDone() => '1'];
172
        }
173
174
        if (true === $this->formObject->formWasValidated()) {
175
            $dataAttributes += $dataAttributesAssetHandler->getFieldsValidDataAttributes();
176
            $dataAttributes += $dataAttributesAssetHandler->getFieldsMessagesDataAttributes();
177
        }
178
179
        return $dataAttributes;
180
    }
181
182
    /**
183
     * @param string $formName
184
     * @return AbstractFormValidator
185
     */
186
    protected function getFormValidator($formName)
187
    {
188
        /** @var AbstractFormValidator $validator */
189
        $validator = Core::instantiate(
190
            DefaultFormValidator::class,
191
            [
192
                'name'  => $formName,
193
                'dummy' => true
194
            ]
195
        );
196
197
        return $validator;
198
    }
199
200
    /**
201
     * @return FormObject
202
     */
203
    public function getFormObject()
204
    {
205
        return $this->formObject;
206
    }
207
208
    /**
209
     * @param FormObject $formObject
210
     */
211
    public function setFormObject(FormObject $formObject)
212
    {
213
        $this->formObject = $formObject;
214
    }
215
216
    /**
217
     * @return Result
218
     */
219
    public function getResult()
220
    {
221
        return $this->result;
222
    }
223
}
224