1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the adminbsb-material-design-bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) 2018 WEBEWEB |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WBW\Bundle\AdminBSBBundle\Twig\Extension\Menu; |
13
|
|
|
|
14
|
|
|
use Twig\Environment; |
15
|
|
|
use WBW\Bundle\AdminBSBBundle\Twig\Extension\AbstractTwigExtension; |
16
|
|
|
use WBW\Bundle\AdminBSBBundle\Twig\Extension\RendererTwigExtension; |
17
|
|
|
use WBW\Bundle\CoreBundle\Component\Translation\BaseTranslatorInterface; |
18
|
|
|
use WBW\Bundle\CoreBundle\Navigation\AbstractNavigationNode; |
19
|
|
|
use WBW\Bundle\CoreBundle\Navigation\HeaderNode; |
20
|
|
|
use WBW\Bundle\CoreBundle\Navigation\NavigationNode; |
21
|
|
|
use WBW\Bundle\CoreBundle\Navigation\NavigationTree; |
22
|
|
|
use WBW\Bundle\CoreBundle\Service\TranslatorTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Abstract multi level menu Twig extension. |
26
|
|
|
* |
27
|
|
|
* @author webeweb <https://github.com/webeweb/> |
28
|
|
|
* @package WBW\Bundle\AdminBSBBundle\Twig\Extension\Menu |
29
|
|
|
* @abstract |
30
|
|
|
*/ |
31
|
|
|
abstract class AbstractMenuTwigExtension extends AbstractTwigExtension { |
32
|
|
|
|
33
|
|
|
use TranslatorTrait; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Constructor. |
37
|
|
|
* |
38
|
|
|
* @param Environment $twigEnvironment The Twig environment. |
39
|
|
|
* @param BaseTranslatorInterface $translator The translator. |
40
|
|
|
*/ |
41
|
|
|
public function __construct(Environment $twigEnvironment, $translator) { |
42
|
|
|
parent::__construct($twigEnvironment); |
43
|
|
|
$this->setTranslator($translator); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Displays an AdminBSB menu. |
48
|
|
|
* |
49
|
|
|
* @param NavigationTree $tree The navigation tree. |
50
|
|
|
* @return string Returns the Admin BSB menu. |
51
|
|
|
*/ |
52
|
|
|
protected function adminBSBMenu(NavigationTree $tree): string { |
53
|
|
|
|
54
|
|
|
$templates = []; |
55
|
|
|
|
56
|
|
|
foreach ($tree->getNodes() as $current) { |
57
|
|
|
$templates[] = $this->renderNode($current); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return implode("\n", $templates); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Determines if a node is a menu toggle. |
65
|
|
|
* |
66
|
|
|
* @param NavigationNode $node |
67
|
|
|
* @return bool Returns true in case of success, false otherwise. |
68
|
|
|
*/ |
69
|
|
|
protected function isMenuToggle(NavigationNode $node): bool { |
70
|
|
|
|
71
|
|
|
foreach ($node->getNodes() as $current) { |
72
|
|
|
|
73
|
|
|
if (true === ($current instanceof NavigationNode) && true === $current->isDisplayable()) { |
74
|
|
|
return true; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return false; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Render an anchor. |
83
|
|
|
* |
84
|
|
|
* @param NavigationNode $node The navigation node. |
85
|
|
|
* @return string Returns the rendered anchor. |
86
|
|
|
*/ |
87
|
|
|
private function renderAnchor(NavigationNode $node): string { |
88
|
|
|
|
89
|
|
|
$attributes = [ |
90
|
|
|
"href" => $node->getUri(), |
91
|
|
|
"target" => $node->getTarget(), |
92
|
|
|
]; |
93
|
|
|
|
94
|
|
|
if ($this->isMenuToggle($node)) { |
95
|
|
|
$attributes["class"] = "menu-toggle"; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$icon = $this->renderIcon($node); |
99
|
|
|
$span = $this->renderSpan($node); |
100
|
|
|
|
101
|
|
|
return static::coreHTMLElement("a", "\n${icon}{$span}", $attributes) . "\n"; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Render a dropdown. |
106
|
|
|
* |
107
|
|
|
* @param NavigationNode $node The navigation node. |
108
|
|
|
* @param array $items The items. |
109
|
|
|
* @return string Returns the rendered dropdown. |
110
|
|
|
*/ |
111
|
|
|
private function renderDropdown(NavigationNode $node, array $items): string { |
|
|
|
|
112
|
|
|
|
113
|
|
|
$innerHTML = implode("\n", $items); |
114
|
|
|
|
115
|
|
|
$dropdown = static::coreHTMLElement("ul", "\n{$innerHTML}", ["class" => "ml-menu"]); |
116
|
|
|
|
117
|
|
|
return "{$dropdown}\n"; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Render an header node. |
122
|
|
|
* |
123
|
|
|
* @param HeaderNode $node The header node. |
124
|
|
|
* @return string Returns the rendered header node. |
125
|
|
|
*/ |
126
|
|
|
private function renderHeader(HeaderNode $node): string { |
127
|
|
|
return static::coreHTMLElement("li", $node->getLabel(), ["class" => "header"]); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Render an icon. |
132
|
|
|
* |
133
|
|
|
* @param NavigationNode $node The navigation node. |
134
|
|
|
* @return string Returns the rendered icon. |
135
|
|
|
*/ |
136
|
|
|
private function renderIcon(NavigationNode $node): string { |
137
|
|
|
if (null === $node->getIcon()) { |
138
|
|
|
return ""; |
139
|
|
|
} |
140
|
|
|
return RendererTwigExtension::renderIcon($this->getTwigEnvironment(), $node->getIcon()) . "\n"; |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Render a navigation node. |
145
|
|
|
* |
146
|
|
|
* @param NavigationNode $node The navigation node. |
147
|
|
|
* @param int $level The level. |
148
|
|
|
* @return string Returns the rendered navigation node. |
149
|
|
|
*/ |
150
|
|
|
private function renderNavigation(NavigationNode $node, int $level = 0): string { |
151
|
|
|
|
152
|
|
|
$anchor = $this->renderAnchor($node); |
153
|
|
|
|
154
|
|
|
$attributes = true === $node->getActive() ? ["class" => "active"] : []; |
155
|
|
|
|
156
|
|
|
if (0 === $node->size()) { |
157
|
|
|
return static::coreHTMLElement("li", "\n{$anchor}", $attributes); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$items = []; |
161
|
|
|
foreach ($node->getNodes() as $current) { |
162
|
|
|
$items[] = $this->renderNode($current, $level + 1); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$dropdown = $this->renderDropdown($node, $items); |
166
|
|
|
|
167
|
|
|
return static::coreHTMLElement("li", "\n{$anchor}{$dropdown}", $attributes); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Render a node. |
172
|
|
|
* |
173
|
|
|
* @param AbstractNavigationNode $node The node. |
174
|
|
|
* @param int $level The level. |
175
|
|
|
* @return string Returns the rendered node. |
176
|
|
|
*/ |
177
|
|
|
private function renderNode(AbstractNavigationNode $node, int $level = 0): string { |
178
|
|
|
|
179
|
|
|
if (false === $node->isDisplayable()) { |
180
|
|
|
return ""; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
if (true === ($node instanceof HeaderNode)) { |
184
|
|
|
return $this->renderHeader($node); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if (true === ($node instanceof NavigationNode)) { |
188
|
|
|
return $this->renderNavigation($node, $level); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return ""; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Render a span. |
196
|
|
|
* |
197
|
|
|
* @param NavigationNode $node The navigation node. |
198
|
|
|
* @return string Returns the rendered span. |
199
|
|
|
*/ |
200
|
|
|
private function renderSpan(NavigationNode $node): string { |
201
|
|
|
|
202
|
|
|
$innerHTML = null !== $node->getId() ? $this->translate($node->getLabel()) : ""; |
203
|
|
|
|
204
|
|
|
return static::coreHTMLElement("span", $innerHTML) . "\n"; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Translate. |
209
|
|
|
* |
210
|
|
|
* @param string $id The translation id. |
211
|
|
|
* @return string Returns the translation in case of success, id otherwise. |
212
|
|
|
*/ |
213
|
|
|
private function translate(string $id): string { |
214
|
|
|
|
215
|
|
|
$core = $this->getTranslator()->trans($id, [], "WBWCoreBundle"); |
216
|
|
|
if ($id !== $core) { |
217
|
|
|
return $core; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$bootstrap = $this->getTranslator()->trans($id, [], "WBWBootstrapBundle"); |
221
|
|
|
if ($id !== $bootstrap) { |
222
|
|
|
return $bootstrap; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
$adminBSB = $this->getTranslator()->trans($id, [], "WBWAdminBSBBundle"); |
226
|
|
|
if ($id !== $adminBSB) { |
227
|
|
|
return $adminBSB; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return $this->getTranslator()->trans($id); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.