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