This repository has been archived by the owner on Aug 13, 2022. It is now read-only.
/
MenuBuilder.php
158 lines (141 loc) · 5.03 KB
/
MenuBuilder.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
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
<?php
/**
* Content.
*
* @copyright Axel Guckelsberger (Zikula)
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
* @author Axel Guckelsberger <info@ziku.la>.
*
* @see https://ziku.la
*
* @version Generated by ModuleStudio 1.5.0 (https://modulestudio.de).
*/
declare(strict_types=1);
namespace Zikula\ContentModule\Menu;
use Knp\Menu\ItemInterface;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Zikula\ContentModule\Entity\PageEntity;
use Zikula\ContentModule\Menu\Base\AbstractMenuBuilder;
use Zikula\ExtensionsModule\Api\ApiInterface\VariableApiInterface;
/**
* Menu builder implementation class.
*/
class MenuBuilder extends AbstractMenuBuilder
{
/**
* @var TranslatorInterface
*/
private $translator;
/**
* @var CsrfTokenManagerInterface
*/
private $csrfTokenManager;
/**
* @var bool
*/
protected $multilingual;
public function createItemActionsMenu(array $options = []): ItemInterface
{
$menu = parent::createItemActionsMenu($options);
if (!isset($options['entity'], $options['area'], $options['context'])) {
return $menu;
}
$entity = $options['entity'];
if (!($entity instanceof PageEntity)) {
return $menu;
}
$hasEditPermissions = $this->permissionHelper->mayEdit($entity);
$hasContentPermissions = $this->permissionHelper->mayManagePageContent($entity);
if (!$hasEditPermissions && !$hasContentPermissions) {
return $menu;
}
$searchLabel = $this->translator->trans('Details');
$reuseLabel = $this->translator->trans('Reuse', [], 'page');
if ($hasEditPermissions) {
$searchLabel = $reuseLabel;
}
$searchFound = false;
$reappendChildren = [];
foreach ($menu->getChildren() as $item) {
if (!$searchFound) {
if ($searchLabel === $item->getName()) {
$searchFound = true;
if ($searchLabel === $reuseLabel) {
$menu->removeChild($item);
}
}
continue;
}
$reappendChildren[] = $item;
$menu->removeChild($item);
}
$routePrefix = 'zikulacontentmodule_page_';
$routeArea = $options['area'];
$context = $options['context'];
if ($hasContentPermissions) {
$menu->addChild('Manage content', [
'route' => $routePrefix . $routeArea . 'managecontent',
'routeParameters' => $entity->createUrlArgs(),
])
->setLinkAttribute(
'title',
'Manage content elements of page'
)
->setLinkAttribute('class', 'display' === $context ? 'btn btn-sm btn-secondary' : '')
->setAttribute('icon', 'fas fa-cubes')
->setExtra('translation_domain', 'page')
;
}
if ($hasEditPermissions) {
$routeParameters = $entity->createUrlArgs();
$routeParameters['token'] = $this->getCsrfToken('duplicate-page');
$menu->addChild('Duplicate', [
'route' => $routePrefix . $routeArea . 'duplicate',
'routeParameters' => $routeParameters,
])
->setLinkAttribute(
'title',
'Duplicate this page'
)
->setLinkAttribute('class', 'display' === $context ? 'btn btn-sm btn-secondary' : '')
->setAttribute('icon', 'fas fa-copy')
->setExtra('translation_domain', 'page')
;
}
if ($this->multilingual && $hasEditPermissions && $hasContentPermissions) {
$menu->addChild('Translate', [
'route' => $routePrefix . $routeArea . 'translate',
'routeParameters' => $entity->createUrlArgs(),
])
->setLinkAttribute(
'title',
'Translate this page'
)
->setLinkAttribute('class', 'display' === $context ? 'btn btn-sm btn-secondary' : '')
->setAttribute('icon', 'fas fa-language')
->setExtra('translation_domain', 'page')
;
}
foreach ($reappendChildren as $item) {
$menu->addChild($item);
}
return $menu;
}
/**
* @required
*/
public function setAdditionalDependencies(
TranslatorInterface $translator,
CsrfTokenManagerInterface $csrfTokenManager,
VariableApiInterface $variableApi,
): void {
$this->translator = $translator;
$this->csrfTokenManager = $csrfTokenManager;
$this->multilingual = $variableApi->getSystemVar('multilingual', true);
}
private function getCsrfToken(string $tokenId): string
{
return $this->csrfTokenManager->getToken($tokenId)->getValue();
}
}