Completed
Push — middleware-wip ( 55159c...844477 )
by Romain
02:34
created

FormViewHelperService::injectFormRequestData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
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;
15
16
use Romm\Formz\Behaviours\BehavioursManager;
17
use Romm\Formz\Core\Core;
18
use Romm\Formz\Exceptions\DuplicateEntryException;
19
use Romm\Formz\Form\FormObject;
20
use TYPO3\CMS\Core\SingletonInterface;
21
use TYPO3\CMS\Core\Utility\GeneralUtility;
22
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
23
use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
24
25
/**
26
 * This class contains methods that help view helpers to manipulate data and
27
 * know more things concerning the current form state.
28
 *
29
 * It is mainly configured inside the `FormViewHelper`, and used in other
30
 * view helpers.
31
 */
32
class FormViewHelperService implements SingletonInterface
33
{
34
    /**
35
     * @var bool
36
     */
37
    protected $formContext = false;
38
39
    /**
40
     * @var FormObject
41
     */
42
    protected $formObject;
43
44
    /**
45
     * Reset every state that can be used by this service.
46
     */
47
    public function resetState()
48
    {
49
        $this->formContext = false;
50
        $this->formObject = null;
51
    }
52
53
    /**
54
     * Will activate the form context, changing the result returned by the
55
     * function `formContextExists()`.
56
     *
57
     * @return FormViewHelperService
58
     * @throws DuplicateEntryException
59
     */
60
    public function activateFormContext()
61
    {
62
        if (true === $this->formContext) {
63
            throw DuplicateEntryException::duplicatedFormContext();
64
        }
65
66
        $this->formContext = true;
67
68
        return $this;
69
    }
70
71
    /**
72
     * Will loop on the submitted form fields and apply behaviours if their
73
     * configuration contains.
74
     *
75
     * @param ControllerContext $controllerContext
76
     */
77
    public function applyBehavioursOnSubmittedForm(ControllerContext $controllerContext)
78
    {
79
        if ($this->formObject->formWasSubmitted()) {
80
            $request = $controllerContext->getRequest()->getOriginalRequest();
81
            $formName = $this->formObject->getName();
82
83
            if ($request
84
                && $request->hasArgument($formName)
85
            ) {
86
                /** @var BehavioursManager $behavioursManager */
87
                $behavioursManager = GeneralUtility::makeInstance(BehavioursManager::class);
88
89
                /** @var array $originalForm */
90
                $originalForm = $request->getArgument($formName);
91
92
                $formProperties = $behavioursManager->applyBehaviourOnPropertiesArray(
93
                    $originalForm,
94
                    $this->formObject->getConfiguration()
95
                );
96
97
                $request->setArgument($formName, $formProperties);
98
            }
99
        }
100
    }
101
102
    /**
103
     * @todo
104
     */
105
    public function injectFormRequestData()
106
    {
107
        /** @var ConfigurationManager $configurationManager */
108
        $configurationManager = Core::instantiate(ConfigurationManager::class);
109
110
        $contentObject = $configurationManager->getContentObject();
111
        $requestData = $this->formObject->getRequestData();
112
113
        $requestData->setContentObjectTable($contentObject->getCurrentTable());
114
        $requestData->setContentObjectUid($contentObject->data['uid']);
115
    }
116
117
    /**
118
     * Returns `true` if the `FormViewHelper` context exists.
119
     *
120
     * @return bool
121
     */
122
    public function formContextExists()
123
    {
124
        return $this->formContext;
125
    }
126
127
    /**
128
     * @return FormObject
129
     */
130
    public function getFormObject()
131
    {
132
        return $this->formObject;
133
    }
134
135
    /**
136
     * @param FormObject $formObject
137
     */
138
    public function setFormObject(FormObject $formObject)
139
    {
140
        $this->formObject = $formObject;
141
    }
142
}
143