-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathAbstractAwarePickerProvider.php
More file actions
181 lines (160 loc) · 4.92 KB
/
Copy pathAbstractAwarePickerProvider.php
File metadata and controls
181 lines (160 loc) · 4.92 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
<?php
/**
* This file is part of contao-community-alliance/dc-general.
*
* (c) 2013-2024 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 Sven Baumann <baumann.sv@gmail.com>
* @author Ingolf Steinhardt <info@e-spin.de>
* @copyright 2013-2024 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\Picker;
use Contao\BackendUser;
use Contao\CoreBundle\Picker\PickerConfig;
use Contao\CoreBundle\Picker\PickerProviderInterface;
use Knp\Menu\FactoryInterface;
use Knp\Menu\ItemInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Provides the own cca routing.
*/
abstract class AbstractAwarePickerProvider implements PickerProviderInterface
{
/**
* The menu factory.
*
* @var FactoryInterface
*/
private FactoryInterface $menuFactory;
/**
* The router.
*
* @var RouterInterface
*/
private RouterInterface $router;
/**
* The token storage.
*
* @var ?TokenStorageInterface
*/
private ?TokenStorageInterface $tokenStorage = null;
/**
* The translator.
*
* @var TranslatorInterface
*/
private TranslatorInterface $translator;
/**
* Constructor.
*
* @param FactoryInterface $menuFactory The menu factory.
* @param RouterInterface $router The router.
* @param TranslatorInterface $translator The translator.
*/
public function __construct(FactoryInterface $menuFactory, RouterInterface $router, TranslatorInterface $translator)
{
$this->menuFactory = $menuFactory;
$this->router = $router;
$this->translator = $translator;
}
/**
* {@inheritdoc}
*/
public function getUrl(PickerConfig $config): string|null
{
return $this->generateUrl($config, false);
}
/**
* {@inheritdoc}
*/
public function createMenuItem(PickerConfig $config): ItemInterface
{
$name = $this->getName();
return $this->menuFactory->createItem(
$name,
[
'label' => $this->translator->trans('MSC.' . $name, [], 'contao_default'),
'linkAttributes' => ['class' => $name],
'current' => $this->isCurrent($config),
'uri' => $this->generateUrl($config, true)
]
);
}
/**
* Sets the security token storage.
*
* @param TokenStorageInterface $tokenStorage The token storage.
*
* @return void
*/
public function setTokenStorage(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
/**
* {@inheritdoc}
*/
public function isCurrent(PickerConfig $config): bool
{
return $config->getCurrent() === $this->getName();
}
/**
* Returns the back end user object.
*
* @return BackendUser The backend user.
*
* @throws \RuntimeException If the token not provided or the backend user not in the token.
*/
protected function getUser()
{
if (null === $this->tokenStorage) {
throw new \RuntimeException('No token storage provided');
}
$token = $this->tokenStorage->getToken();
if (null === $token) {
throw new \RuntimeException('No token provided');
}
$user = $token->getUser();
if (!$user instanceof BackendUser) {
throw new \RuntimeException('The token does not contain a back end user object');
}
return $user;
}
/**
* Returns the routing parameters for the back end picker.
*
* @param PickerConfig|null $config The picker config.
*
* @return array
*/
abstract protected function getRouteParameters(?PickerConfig $config = null);
/**
* Generates the URL for the picker.
*
* @param PickerConfig $config The configuration.
* @param bool $ignoreValue Determine by get the route parameter the picker config ignore.
*
* @return string
*/
private function generateUrl(PickerConfig $config, $ignoreValue)
{
$params = \array_merge(
$this->getRouteParameters($ignoreValue ? null : $config),
[
'popup' => '1',
'picker' => $config->cloneForCurrent($this->getName())->urlEncode(),
]
);
return $this->router->generate('cca_dc_general_tree', $params);
}
}