|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace eXpansion\Bundle\Menu\Plugins\Gui; |
|
4
|
|
|
|
|
5
|
|
|
use eXpansion\Bundle\Menu\DataProviders\MenuItemProvider; |
|
6
|
|
|
use eXpansion\Bundle\Menu\Gui\MenuTabsFactory; |
|
7
|
|
|
use eXpansion\Bundle\Menu\Model\Menu\ItemInterface; |
|
8
|
|
|
use eXpansion\Bundle\Menu\Model\Menu\ParentItem; |
|
9
|
|
|
use eXpansion\Framework\Core\Model\Gui\Manialink; |
|
10
|
|
|
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface; |
|
11
|
|
|
use eXpansion\Framework\Core\Model\Gui\ManiaScriptFactory; |
|
12
|
|
|
use eXpansion\Framework\Core\Model\Gui\WidgetFactoryContext; |
|
13
|
|
|
use eXpansion\Framework\Core\Plugins\Gui\WidgetFactory; |
|
14
|
|
|
use eXpansion\Framework\Gui\Components\uiButton; |
|
15
|
|
|
use eXpansion\Framework\Gui\Components\uiLabel; |
|
16
|
|
|
use FML\Controls\Frame; |
|
17
|
|
|
use FML\Controls\Label; |
|
18
|
|
|
use FML\Controls\Quad; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class MenuContentFactory |
|
23
|
|
|
* |
|
24
|
|
|
* @package eXpansion\Bundle\Menu\Plugins\Gui; |
|
25
|
|
|
* @author oliver de Cramer <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
class MenuContentFactory extends WidgetFactory |
|
28
|
|
|
{ |
|
29
|
|
|
/** @var MenuItemProvider */ |
|
30
|
|
|
protected $menuItemProvider; |
|
31
|
|
|
|
|
32
|
|
|
/** @var ManiaScriptFactory */ |
|
33
|
|
|
protected $menuScriptFactory; |
|
34
|
|
|
|
|
35
|
|
|
/** @var MenuTabsFactory */ |
|
36
|
|
|
protected $menuTabsFactory; |
|
37
|
|
|
|
|
38
|
|
|
/** @var string */ |
|
39
|
|
|
protected $currentPath = 'admin'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* MenuContentFactory constructor. |
|
43
|
|
|
* |
|
44
|
|
|
* @param $name |
|
45
|
|
|
* @param $sizeX |
|
46
|
|
|
* @param $sizeY |
|
47
|
|
|
* @param null $posX |
|
48
|
|
|
* @param null $posY |
|
49
|
|
|
* @param WidgetFactoryContext $context |
|
50
|
|
|
* @param ManiaScriptFactory $maniaScriptFactory |
|
51
|
|
|
* @param MenuItemProvider $menuItemProvider |
|
52
|
|
|
* @param MenuTabsFactory $menuTabsFactory |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct( |
|
55
|
|
|
$name, |
|
56
|
|
|
$sizeX, |
|
57
|
|
|
$sizeY, |
|
58
|
|
|
$posX, |
|
59
|
|
|
$posY, |
|
60
|
|
|
WidgetFactoryContext $context, |
|
61
|
|
|
ManiaScriptFactory $maniaScriptFactory, |
|
62
|
|
|
MenuItemProvider $menuItemProvider, |
|
63
|
|
|
MenuTabsFactory $menuTabsFactory |
|
64
|
|
|
) { |
|
65
|
|
|
parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context); |
|
66
|
|
|
|
|
67
|
|
|
$this->menuItemProvider = $menuItemProvider; |
|
68
|
|
|
$this->menuScriptFactory = $maniaScriptFactory; |
|
69
|
|
|
$this->menuTabsFactory = $menuTabsFactory; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @inheritdoc |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function createContent(ManialinkInterface $manialink) |
|
76
|
|
|
{ |
|
77
|
|
|
parent::createContent($manialink); |
|
78
|
|
|
|
|
79
|
|
|
$tabsFrame = new Frame('tabs'); |
|
80
|
|
|
$tabsFrame->setPosition(-144, 82); |
|
81
|
|
|
$manialink->getContentFrame()->setZ(101); |
|
82
|
|
|
$manialink->getContentFrame()->addChild($tabsFrame); |
|
83
|
|
|
$manialink->setData('tabs_frame', $tabsFrame); |
|
84
|
|
|
|
|
85
|
|
|
$contentFrame = new Frame('menu_content'); |
|
86
|
|
|
$contentFrame->setPosition(0, 72); |
|
87
|
|
|
$manialink->getContentFrame()->addChild($contentFrame); |
|
88
|
|
|
$manialink->setData('menu_content_frame', $contentFrame); |
|
89
|
|
|
|
|
90
|
|
|
$backGroundFrame = new Frame('background'); |
|
91
|
|
|
$manialink->getContentFrame()->addChild($backGroundFrame); |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
/* |
|
95
|
|
|
* Adding background frame |
|
96
|
|
|
*/ |
|
97
|
|
|
$bgFrame = Frame::create("background"); |
|
98
|
|
|
|
|
99
|
|
|
$quad = new Quad(); |
|
100
|
|
|
$quad->addClass("bg") |
|
101
|
|
|
->setId("mainBg") |
|
102
|
|
|
->setPosition(0, 0) |
|
103
|
|
|
->setSize(322, 182); |
|
104
|
|
|
$quad->setAlign("center", "center") |
|
105
|
|
|
->setStyles("Bgs1", "BgDialogBlur"); |
|
106
|
|
|
$bgFrame->addChild($quad); |
|
107
|
|
|
|
|
108
|
|
|
$manialink->getContentFrame()->addChild($bgFrame); |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Adding script |
|
112
|
|
|
*/ |
|
113
|
|
|
$manialink->getFmlManialink()->addChild($this->menuScriptFactory->createScript([])); |
|
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @inheritdoc |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function updateContent(ManialinkInterface $manialink) |
|
120
|
|
|
{ |
|
121
|
|
|
parent::updateContent($manialink); |
|
122
|
|
|
|
|
123
|
|
|
$currentPath = $manialink->getData('current_path'); |
|
124
|
|
|
if (is_null($currentPath)) { |
|
125
|
|
|
$currentPath = $this->currentPath; |
|
126
|
|
|
$manialink->setData('current_path', $currentPath); |
|
127
|
|
|
} |
|
128
|
|
|
$currentPath = trim($currentPath, '/'); |
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
$rootItem = $this->menuItemProvider->getRootItem(); |
|
132
|
|
|
$pathParts = explode('/', $currentPath); |
|
133
|
|
|
|
|
134
|
|
|
$this->createTabsMenu($manialink, $rootItem, $pathParts[0]); |
|
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
/** @var Frame $contentFrame */ |
|
137
|
|
|
$contentFrame = $manialink->getData('menu_content_frame'); |
|
138
|
|
|
$contentFrame->removeAllChildren(); |
|
139
|
|
|
|
|
140
|
|
|
$displayLevel = 0; |
|
141
|
|
|
$breadcrumb = []; |
|
142
|
|
|
for ($i = count($pathParts) - 1; $i >= 0; $i--) { |
|
143
|
|
|
$path = implode('/', array_slice($pathParts, 0, $i + 1)); |
|
144
|
|
|
|
|
145
|
|
|
/** @var ParentItem $parentItem */ |
|
146
|
|
|
$parentItem = $rootItem->getChild($path); |
|
147
|
|
|
|
|
148
|
|
|
$action = $this->actionFactory->createManialinkAction( |
|
149
|
|
|
$manialink, |
|
150
|
|
|
[$this, 'callbackItemClick'], |
|
151
|
|
|
['item' => $parentItem, 'ml' => $manialink] |
|
152
|
|
|
); |
|
153
|
|
|
|
|
154
|
|
|
$breadcrumb[] = [ |
|
155
|
|
|
"label" => $parentItem->getLabelId(), |
|
156
|
|
|
"action" => $action, |
|
157
|
|
|
]; |
|
158
|
|
|
|
|
159
|
|
|
$this->createSubMenu($manialink, $contentFrame, $parentItem, $displayLevel++); |
|
|
|
|
|
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
$contentFrame->addChild($this->createBreadcrumb($breadcrumb)); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Create tabs level menu. |
|
167
|
|
|
* |
|
168
|
|
|
* @param ManialinkInterface $manialink |
|
169
|
|
|
* @param ParentItem $rootItem |
|
170
|
|
|
* @param $openId |
|
171
|
|
|
*/ |
|
172
|
|
|
protected function createTabsMenu(ManialinkInterface $manialink, ParentItem $rootItem, $openId) |
|
173
|
|
|
{ |
|
174
|
|
|
/** @var Frame $tabsFrame */ |
|
175
|
|
|
$tabsFrame = $manialink->getData('tabs_frame'); |
|
176
|
|
|
$tabsFrame->removeAllChildren(); |
|
177
|
|
|
$this->menuTabsFactory->createTabsMenu($manialink, $tabsFrame, $rootItem, $openId); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Create content for sub menu. |
|
182
|
|
|
* |
|
183
|
|
|
* @param Manialink $manialink |
|
184
|
|
|
* @param Frame $frame |
|
185
|
|
|
* @param ParentItem $parentItem |
|
186
|
|
|
* @param $displayLevel |
|
187
|
|
|
*/ |
|
188
|
|
|
protected function createSubMenu(Manialink $manialink, Frame $frame, ParentItem $parentItem, $displayLevel) |
|
189
|
|
|
{ |
|
190
|
|
|
if ($displayLevel > 0) { |
|
191
|
|
|
return; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
$posX = $displayLevel * (-160.0 / 3); |
|
195
|
|
|
$posY = ($displayLevel * (-100.0 / 3)) * 0.5; |
|
196
|
|
|
$scale = (0.5 / ($displayLevel + 1)) + 0.5; |
|
197
|
|
|
|
|
198
|
|
|
$contentFrame = new Frame(); |
|
199
|
|
|
$contentFrame->setScale($scale); |
|
200
|
|
|
$contentFrame->setPosition($posX, $posY); |
|
201
|
|
|
$frame->addChild($contentFrame); |
|
202
|
|
|
|
|
203
|
|
|
if ($displayLevel > 0) { |
|
204
|
|
|
$overlay = new Quad(); |
|
205
|
|
|
$overlay->setSize(60, 120); |
|
206
|
|
|
$overlay->setPosition(-30, 0); |
|
207
|
|
|
$overlay->setStyles("Bgs1", "BgDialogBlur"); |
|
208
|
|
|
|
|
209
|
|
|
$action = $this->actionFactory->createManialinkAction( |
|
210
|
|
|
$manialink, |
|
211
|
|
|
[$this, 'callbackItemClick'], |
|
212
|
|
|
['item' => $parentItem, 'ml' => $manialink] |
|
213
|
|
|
); |
|
214
|
|
|
|
|
215
|
|
|
|
|
216
|
|
|
$contentFrame->addChild($overlay); |
|
217
|
|
|
$overlay->setAction($action); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/* TITLE */ |
|
221
|
|
|
$titleLabel = $this->uiFactory->createLabel($parentItem->getLabelId(), uiLabel::TYPE_TITLE); |
|
222
|
|
|
$titleLabel->setTextSize(9) |
|
223
|
|
|
->setSize(60, 12) |
|
224
|
|
|
->setPosition(0, 0) |
|
225
|
|
|
->setTranslate(true) |
|
226
|
|
|
->setTextColor('fff') |
|
227
|
|
|
->setHorizontalAlign("center"); |
|
228
|
|
|
|
|
229
|
|
|
$contentFrame->addChild($titleLabel); |
|
230
|
|
|
|
|
231
|
|
|
$titleLine = $this->uiFactory->createLine(-60 * $scale, -12); |
|
232
|
|
|
$titleLine->setLength(120 * $scale); |
|
233
|
|
|
$titleLine->setStroke(0.33)->setColor('fff'); |
|
234
|
|
|
|
|
235
|
|
|
$contentFrame->addChild($titleLine); |
|
236
|
|
|
|
|
237
|
|
|
$posY = -30; |
|
238
|
|
|
foreach ($parentItem->getChilds() as $item) { |
|
239
|
|
|
if ($item->isVisibleFor($manialink->getUserGroup())) { |
|
|
|
|
|
|
240
|
|
|
$button = $this->uiFactory->createLabel($item->getLabelId()); |
|
241
|
|
|
$button->setPosition(0, $posY); |
|
242
|
|
|
$button->setSize(50, 8); |
|
243
|
|
|
$button->setTranslate(true); |
|
244
|
|
|
$button->setTextSize(4); |
|
245
|
|
|
$button->setAlign("center", "center2"); |
|
246
|
|
|
$button->addClass('menuItem'); |
|
247
|
|
|
if ($item instanceof $parentItem) { |
|
248
|
|
|
$button->setTextPrefix("⏵ "); |
|
249
|
|
|
} |
|
250
|
|
|
if ($displayLevel == 0) { |
|
251
|
|
|
$action = $this->actionFactory->createManialinkAction( |
|
252
|
|
|
$manialink, |
|
253
|
|
|
[$this, 'callbackItemClick'], |
|
254
|
|
|
['item' => $item, 'ml' => $manialink] |
|
255
|
|
|
); |
|
256
|
|
|
$button->setAction($action); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
$contentFrame->addChild($button); |
|
260
|
|
|
$posY -= 12; |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
if ($displayLevel == 0) { |
|
265
|
|
|
|
|
266
|
|
|
$button = $this->uiFactory->createButton('expansion_menu.menu_close', uiButton::TYPE_DECORATED); |
|
267
|
|
|
$button->setBorderColor(uiButton::COLOR_WARNING)->setFocusColor(uiButton::COLOR_WARNING); |
|
268
|
|
|
$button->setPosition(-25, $posY - 12); |
|
269
|
|
|
$button->setSize(50, 8); |
|
270
|
|
|
$button->setTranslate(true); |
|
271
|
|
|
$action = $this->actionFactory->createManialinkAction($manialink, [$this, 'callbackClose'], |
|
272
|
|
|
['ml' => $manialink]); |
|
273
|
|
|
$button->setAction($action); |
|
274
|
|
|
$contentFrame->addChild($button); |
|
275
|
|
|
} |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
public function createBreadcrumb($items) { |
|
279
|
|
|
$items = array_reverse($items); |
|
280
|
|
|
|
|
281
|
|
|
$frame = $this->uiFactory->createLayoutLine(-60, -16, [], 2); |
|
282
|
|
|
$frame->setId("breadcrumb"); |
|
283
|
|
|
|
|
284
|
|
|
foreach ($items as $i => $item) { |
|
285
|
|
|
$element = $this->uiFactory->createLabel($item['label'], uiLabel::TYPE_HEADER); |
|
286
|
|
|
$element->setTranslate(true); |
|
287
|
|
|
$element->setAlign("left", "center2"); |
|
288
|
|
|
$element->setTextSize(4) |
|
289
|
|
|
->addClass("item") |
|
290
|
|
|
->setWidth(30); |
|
291
|
|
|
|
|
292
|
|
|
if ($i < sizeof($items) - 1) { |
|
293
|
|
|
$element->setAction($item['action']) |
|
294
|
|
|
->addClass("menuItem"); |
|
295
|
|
|
} else { |
|
296
|
|
|
$element->setTextColor("aaa"); |
|
297
|
|
|
} |
|
298
|
|
|
$frame->addChild($element); |
|
299
|
|
|
|
|
300
|
|
|
if ($i < sizeof($items) - 1) { |
|
301
|
|
|
$element = $this->uiFactory->createLabel("", uiLabel::TYPE_HEADER); |
|
302
|
|
|
$element->setAlign("left", "center2"); |
|
303
|
|
|
$element->setTextSize(4); |
|
304
|
|
|
$element->setWidth(2)->addClass("item"); |
|
305
|
|
|
$frame->addChild($element); |
|
306
|
|
|
} |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
return $frame; |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
|
|
313
|
|
|
/** |
|
314
|
|
|
* Callback when an item of the menu is clicked on. |
|
315
|
|
|
* |
|
316
|
|
|
* @param $login |
|
317
|
|
|
* @param $params |
|
318
|
|
|
* @param $args |
|
319
|
|
|
*/ |
|
320
|
|
|
public function callbackItemClick( |
|
321
|
|
|
$login, |
|
322
|
|
|
$params, |
|
323
|
|
|
$args |
|
324
|
|
|
) { |
|
325
|
|
|
/** @var ItemInterface $item */ |
|
326
|
|
|
$item = $args['item']; |
|
327
|
|
|
$item->execute($this, $args['ml'], $login, $params, $args); |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
/** |
|
331
|
|
|
* Callback when the close button is clicked. |
|
332
|
|
|
* |
|
333
|
|
|
* @param $login |
|
334
|
|
|
* @param $params |
|
335
|
|
|
* @param $args |
|
336
|
|
|
*/ |
|
337
|
|
|
public function callbackClose( |
|
338
|
|
|
$login, |
|
339
|
|
|
$params, |
|
340
|
|
|
$args |
|
341
|
|
|
) { |
|
342
|
|
|
$this->destroy($args['ml']->getUserGroup()); |
|
343
|
|
|
} |
|
344
|
|
|
} |
|
345
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: