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 Symfony\Component\Translation\TranslatorInterface; |
15
|
|
|
use WBW\Bundle\AdminBSBBundle\Twig\Extension\AbstractAdminBSBTwigExtension; |
16
|
|
|
use WBW\Bundle\AdminBSBBundle\Twig\Extension\UI\IconUITwigExtension; |
17
|
|
|
use WBW\Bundle\BootstrapBundle\Navigation\NavigationNode; |
18
|
|
|
use WBW\Bundle\BootstrapBundle\Navigation\NavigationTree; |
19
|
|
|
use WBW\Library\Core\Utility\Argument\StringUtility; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Abstract multi level menu Twig extension. |
23
|
|
|
* |
24
|
|
|
* @author webeweb <https://github.com/webeweb/> |
25
|
|
|
* @package WBW\Bundle\AdminBSBBundle\Twig\Extension\Menu |
26
|
|
|
* @abstract |
27
|
|
|
*/ |
28
|
|
|
abstract class AbstractMenuTwigExtension extends AbstractAdminBSBTwigExtension { |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Translator; |
32
|
|
|
* |
33
|
|
|
* @var TranslatorInterface |
34
|
|
|
*/ |
35
|
|
|
protected $translator; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Constructor. |
39
|
|
|
* |
40
|
|
|
* @param TranslatorInterface $translator The translator. |
41
|
|
|
*/ |
42
|
|
|
protected function __construct(TranslatorInterface $translator) { |
43
|
|
|
parent::__construct(); |
44
|
|
|
$this->translator = $translator; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Displays an AdminBSB menu. |
49
|
|
|
* |
50
|
|
|
* @param NavigationTree $tree The tree. |
51
|
|
|
* @return string Returns the AdminBSB menu. |
52
|
|
|
*/ |
53
|
|
|
protected function adminBSBMenu(NavigationTree $tree) { |
54
|
|
|
|
55
|
|
|
// Initialize the template. |
56
|
|
|
$template = []; |
57
|
|
|
|
58
|
|
|
$template[] = $this->adminBSBMenuHeader($tree); |
59
|
|
|
foreach ($tree->getNodes() as $current) { |
60
|
|
|
if (false === $this->isValidNode($current)) { |
61
|
|
|
continue; |
62
|
|
|
} |
63
|
|
|
$template[] = $this->adminBSBMenuItem($current); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// Return the HTML. |
67
|
|
|
return implode("\n", $template); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Displays an AdminBSB menu header. |
72
|
|
|
* |
73
|
|
|
* @param NavigationTree $tree The tree. |
74
|
|
|
* @return string Returns the AdminBSB menu header. |
75
|
|
|
*/ |
76
|
|
|
private function adminBSBMenuHeader(NavigationTree $tree) { |
77
|
|
|
|
78
|
|
|
// Initialize the template. |
79
|
|
|
$template = "<li %attributes%>%innerHTML%</li>"; |
80
|
|
|
|
81
|
|
|
// Initialize the attributes. |
82
|
|
|
$attributes = []; |
83
|
|
|
|
84
|
|
|
$attributes["class"] = "menu-header"; |
85
|
|
|
|
86
|
|
|
// Initialize the parameters. |
87
|
|
|
$innerHTML = null !== $tree->getId() ? $this->translate($tree->getId()) : ""; |
88
|
|
|
|
89
|
|
|
// Return the HTML. |
90
|
|
|
return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $innerHTML]); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Displays an AdminBSB menu item. |
95
|
|
|
* |
96
|
|
|
* @param string $content The menu item content. |
|
|
|
|
97
|
|
|
* @param string $url The menu item URL. |
|
|
|
|
98
|
|
|
* @param string $icon The menu item icon. |
|
|
|
|
99
|
|
|
* @param boolean $active Menu item active ? |
|
|
|
|
100
|
|
|
* @param array $nodes The menu item nodes. |
|
|
|
|
101
|
|
|
* @return string Returns the AdminBSB menu item. |
102
|
|
|
*/ |
103
|
|
|
private function adminBSBMenuItem(NavigationNode $node) { |
104
|
|
|
|
105
|
|
|
// Initialize the multi level menu. |
106
|
|
|
$multiLevelMenu = false; |
107
|
|
|
foreach ($node->getNodes() as $current) { |
108
|
|
|
if (false === $this->isValidNode($current)) { |
109
|
|
|
continue; |
110
|
|
|
} |
111
|
|
|
$multiLevelMenu = true; |
112
|
|
|
break; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// Initialize the template. |
116
|
|
|
$template = []; |
117
|
|
|
|
118
|
|
|
$template[] = "<li" . (true === $node->getActive() ? ' class="active"' : "") . ">"; |
119
|
|
|
$template[] = $this->adminBSBMenuItemLink($node, (true === $multiLevelMenu ? "menu-toggle" : null)); |
120
|
|
|
|
121
|
|
|
if (true === $multiLevelMenu) { |
122
|
|
|
$template[] = '<ul class="ml-menu">'; |
123
|
|
|
|
124
|
|
|
foreach ($node->getNodes() as $current) { |
125
|
|
|
if (false === $this->isValidNode($current)) { |
126
|
|
|
continue; |
127
|
|
|
} |
128
|
|
|
$template[] = $this->adminBSBMenuItem($current); |
129
|
|
|
} |
130
|
|
|
$template[] = "</ul>"; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$template [] = "</li>"; |
134
|
|
|
|
135
|
|
|
// Return the HTML. |
136
|
|
|
return implode("\n", $template); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Displays an AdminBSB menu item label. |
141
|
|
|
* |
142
|
|
|
* @param NavigationNode $node The node. |
143
|
|
|
* @return string Returns the AdminBSB menu label. |
144
|
|
|
*/ |
145
|
|
|
private function adminBSBMenuItemLabel(NavigationNode $node) { |
146
|
|
|
|
147
|
|
|
// Initialize the parameters. |
148
|
|
|
$innerHTML = null !== $node->getId() ? $this->translate($node->getId()) : ""; |
149
|
|
|
|
150
|
|
|
// Check the icon. |
151
|
|
|
if (null === $node->getIcon()) { |
152
|
|
|
return $innerHTML; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
// Initialize the template. |
156
|
|
|
$template = "%icon%<span>%innerHTML%</span>"; |
157
|
|
|
|
158
|
|
|
// Initialize the parameters. |
159
|
|
|
$glyphicon = (new IconUITwigExtension())->adminBSBBasicIconFunction(["name" => $node->getIcon()]); |
160
|
|
|
|
161
|
|
|
// Return the HTML. |
162
|
|
|
return StringUtility::replace($template, ["%icon%", "%innerHTML%"], [$glyphicon, $innerHTML]); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Displays an AdminBSB menu item link. |
167
|
|
|
* |
168
|
|
|
* @param NavigationNode $node The node. |
169
|
|
|
* @param string $class The node class. |
170
|
|
|
* @return string Returns the AdminBSB menu item link. |
171
|
|
|
*/ |
172
|
|
|
private function adminBSBMenuItemLink(NavigationNode $node, $class) { |
173
|
|
|
|
174
|
|
|
// Initialize the template. |
175
|
|
|
$template = "<a %attributes%>%innerHTML%</a>"; |
176
|
|
|
|
177
|
|
|
// Initialize the attributes. |
178
|
|
|
$attributes = []; |
179
|
|
|
|
180
|
|
|
$attributes["class"] = $class; |
181
|
|
|
$attributes["href"] = $node->getUrl(); |
182
|
|
|
$attributes["target"] = $node->getTarget(); |
183
|
|
|
|
184
|
|
|
// Initialize the parameters. |
185
|
|
|
$innerHTML = $this->adminBSBMenuItemLabel($node); |
186
|
|
|
|
187
|
|
|
// Return the HTML. |
188
|
|
|
return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $innerHTML]); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Determines if a node is valid. |
193
|
|
|
* |
194
|
|
|
* @param mixed $node The node. |
195
|
|
|
* @return boolean Returns true in case of success, false otherwise. |
196
|
|
|
*/ |
197
|
|
|
private function isValidNode($node) { |
198
|
|
|
return true === ($node instanceof NavigationNode) && true === $node->isDisplayable(); |
|
|
|
|
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Translate. |
203
|
|
|
* |
204
|
|
|
* @param string $id The translation id. |
205
|
|
|
* @return string Returns the translation in case of succes, id otherwise. |
206
|
|
|
*/ |
207
|
|
|
private function translate($id) { |
208
|
|
|
|
209
|
|
|
// Translate with Bootstrap bundle. |
210
|
|
|
$outputB = $this->translator->trans($id, [], "BootstrapBundle"); |
211
|
|
|
if ($id === $outputB) { |
212
|
|
|
return $outputB; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
// Translate with AdminBSB bundle. |
216
|
|
|
$outputA = $this->translator->trans($id, [], "AdminBSBBundle"); |
217
|
|
|
if ($id === $outputA) { |
218
|
|
|
return $outputA; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
// Translate. |
222
|
|
|
return $this->translator->trans($id); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
} |
226
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.