-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathToggleHandler.php
More file actions
275 lines (232 loc) · 9.45 KB
/
Copy pathToggleHandler.php
File metadata and controls
275 lines (232 loc) · 9.45 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
/**
* This file is part of contao-community-alliance/dc-general.
*
* (c) 2013-2023 Contao Community Alliance.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* This project is provided in good faith and hope to be usable by anyone.
*
* @package contao-community-alliance/dc-general
* @author Christian Schiffler <c.schiffler@cyberspectrum.de>
* @author Sven Baumann <baumann.sv@gmail.com>
* @author David Molineus <david.molineus@netzmacht.de>
* @author Benedict Zinke <bz@presentprogressive.de>
* @author Ingolf Steinhardt <info@e-spin.de>
* @copyright 2013-2023 Contao Community Alliance.
* @license https://github.com/contao-community-alliance/dc-general/blob/master/LICENSE LGPL-3.0-or-later
* @filesource
*/
namespace ContaoCommunityAlliance\DcGeneral\Contao\View\Contao2BackendView\ActionHandler;
use ContaoCommunityAlliance\DcGeneral\Action;
use ContaoCommunityAlliance\DcGeneral\Contao\DataDefinition\Definition\Contao2BackendViewDefinitionInterface;
use ContaoCommunityAlliance\DcGeneral\Contao\RequestScopeDeterminator;
use ContaoCommunityAlliance\DcGeneral\Contao\RequestScopeDeterminatorAwareTrait;
use ContaoCommunityAlliance\DcGeneral\Data\ConfigInterface;
use ContaoCommunityAlliance\DcGeneral\Data\DataProviderInterface;
use ContaoCommunityAlliance\DcGeneral\Data\ModelId;
use ContaoCommunityAlliance\DcGeneral\Data\ModelIdInterface;
use ContaoCommunityAlliance\DcGeneral\Data\ModelInterface;
use ContaoCommunityAlliance\DcGeneral\Data\MultiLanguageDataProviderInterface;
use ContaoCommunityAlliance\DcGeneral\DataDefinition\ContainerInterface;
use ContaoCommunityAlliance\DcGeneral\DataDefinition\Definition\View\CommandInterface;
use ContaoCommunityAlliance\DcGeneral\DataDefinition\Definition\View\ToggleCommandInterface;
use ContaoCommunityAlliance\DcGeneral\DataDefinition\Definition\View\TranslatedToggleCommandInterface;
use ContaoCommunityAlliance\DcGeneral\EnvironmentInterface;
use ContaoCommunityAlliance\DcGeneral\Event\ActionEvent;
use ContaoCommunityAlliance\DcGeneral\Event\PostPersistModelEvent;
use ContaoCommunityAlliance\DcGeneral\Event\PrePersistModelEvent;
use ContaoCommunityAlliance\DcGeneral\InputProviderInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* This class handles toggle commands.
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ToggleHandler
{
use RequestScopeDeterminatorAwareTrait;
/**
* ToggleHandler constructor.
*
* @param RequestScopeDeterminator $scopeDeterminator The request mode determinator.
*/
public function __construct(RequestScopeDeterminator $scopeDeterminator)
{
$this->setScopeDeterminator($scopeDeterminator);
}
/**
* Handle the event to process the action.
*
* @param ActionEvent $event The action event.
*
* @return void
*/
public function handleEvent(ActionEvent $event)
{
if (!$this->getScopeDeterminator()->currentScopeIsBackend()) {
return;
}
$environment = $event->getEnvironment();
if (null === ($serializedId = $this->getModelId($environment))) {
return;
}
$operation = $this->getOperation($event->getAction(), $environment);
if (!($operation instanceof ToggleCommandInterface)) {
return;
}
if (false === $this->checkPermission($event, $operation)) {
$event->stopPropagation();
return;
}
$this->process($environment, $operation, $serializedId);
}
/**
* Process the action.
*
* @param EnvironmentInterface $environment The environment.
* @param ToggleCommandInterface $operation The operation.
* @param ModelIdInterface|null $modelId The model id.
*
* @return void
*
* @SuppressWarnings(PHPMD.ExitExpression)
* @SuppressWarnings(PHPMD.Superglobals)
* @SuppressWarnings(PHPMD.CamelCaseVariableName)
*/
protected function process(
EnvironmentInterface $environment,
ToggleCommandInterface $operation,
?ModelIdInterface $modelId = null
) {
$dataProvider = $environment->getDataProvider();
assert($dataProvider instanceof DataProviderInterface);
$inputProvider = $environment->getInputProvider();
assert($inputProvider instanceof InputProviderInterface);
$newState = $this->determineNewState($inputProvider, $operation->isInverse());
// Override the language for language aware toggling.
if (
($operation instanceof TranslatedToggleCommandInterface)
&& ($dataProvider instanceof MultiLanguageDataProviderInterface)
) {
$language = $dataProvider->getCurrentLanguage();
/** @var TranslatedToggleCommandInterface $operation */
$dataProvider->setCurrentLanguage($operation->getLanguage());
}
$provider = $dataProvider->getEmptyConfig();
assert($provider instanceof ConfigInterface);
assert($modelId instanceof ModelIdInterface);
$config = $provider->setId($modelId->getId());
assert($config instanceof ConfigInterface);
$model = $dataProvider->fetch($config);
assert($model instanceof ModelInterface);
$originalModel = clone $model;
$originalModel->setId($model->getId());
$model->setProperty($operation->getToggleProperty(), $newState);
$dispatcher = $environment->getEventDispatcher();
assert($dispatcher instanceof EventDispatcherInterface);
$dispatcher->dispatch(
new PrePersistModelEvent($environment, $model, $originalModel),
PrePersistModelEvent::NAME
);
$dataProvider->save($model);
$dispatcher->dispatch(
new PostPersistModelEvent($environment, $model, $originalModel),
PostPersistModelEvent::NAME
);
// Select the previous language.
if (isset($language)) {
/** @var MultiLanguageDataProviderInterface $dataProvider */
$dataProvider->setCurrentLanguage($language);
}
}
/**
* Check permission for toggle property.
*
* @param ActionEvent $event The action event.
*
* @param ToggleCommandInterface $command The executed operation.
*
* @return bool
*/
private function checkPermission(ActionEvent $event, ToggleCommandInterface $command)
{
$environment = $event->getEnvironment();
$definition = $environment->getDataDefinition();
assert($definition instanceof ContainerInterface);
if (true === !$command->isDisabled() && $definition->getBasicDefinition()->isEditable()) {
return true;
}
$operation = $this->getOperation($event->getAction(), $environment);
assert($operation instanceof ToggleCommandInterface);
$event->setResponse(
\sprintf(
'<div style="text-align:center; font-weight:bold; padding:40px;">
You have no permission for toggle %s.
</div>',
$operation->getToggleProperty()
)
);
return false;
}
/**
* Retrieve the model id from the input provider and validate it.
*
* @param EnvironmentInterface $environment The environment.
*
* @return ModelIdInterface|null
*/
private function getModelId(EnvironmentInterface $environment)
{
$inputProvider = $environment->getInputProvider();
assert($inputProvider instanceof InputProviderInterface);
if ($inputProvider->hasParameter('id') && $inputProvider->getParameter('id')) {
$modelId = ModelId::fromSerialized($inputProvider->getParameter('id'));
}
$definition = $environment->getDataDefinition();
assert($definition instanceof ContainerInterface);
if (!(isset($modelId) && ($definition->getName() === $modelId->getDataProviderName()))) {
return null;
}
return $modelId;
}
/**
* Retrieve the toggle operation being executed.
*
* @param Action $action The action.
* @param EnvironmentInterface $environment The environment.
*
* @return CommandInterface|null
*/
private function getOperation(Action $action, EnvironmentInterface $environment)
{
$dataDefinition = $environment->getDataDefinition();
assert($dataDefinition instanceof ContainerInterface);
$definition = $dataDefinition->getDefinition(Contao2BackendViewDefinitionInterface::NAME);
assert($definition instanceof Contao2BackendViewDefinitionInterface);
$name = $action->getName();
$commands = $definition->getModelCommands();
if (!$commands->hasCommandNamed($name)) {
return null;
}
return $commands->getCommandNamed($name);
}
/**
* Determine the new state from the input data.
*
* @param InputProviderInterface $inputProvider The input provider.
* @param bool $isInverse Flag if the state shall be evaluated as inverse toggler.
*
* @return string
*/
private function determineNewState(InputProviderInterface $inputProvider, $isInverse)
{
$state = 1 === (int) $inputProvider->getParameter('state');
if ($isInverse) {
return $state ? '' : '1';
}
return $state ? '1' : '';
}
}