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