These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Kunstmaan\NodeBundle\Helper\Menu; |
||
4 | |||
5 | use Doctrine\ORM\EntityManager; |
||
6 | use FOS\UserBundle\Model\UserInterface; |
||
7 | use Knp\Menu\FactoryInterface; |
||
8 | use Knp\Menu\ItemInterface; |
||
9 | use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionMap; |
||
10 | use Kunstmaan\NodeBundle\Entity\NodeVersion; |
||
11 | use Kunstmaan\NodeBundle\Event\ConfigureActionMenuEvent; |
||
12 | use Kunstmaan\NodeBundle\Event\Events; |
||
13 | use Kunstmaan\NodeBundle\Helper\PagesConfiguration; |
||
14 | use Kunstmaan\PagePartBundle\Helper\HasPageTemplateInterface; |
||
15 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
||
16 | use Symfony\Component\Routing\RouterInterface; |
||
17 | use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
||
18 | |||
19 | class ActionsMenuBuilder |
||
20 | { |
||
21 | /** |
||
22 | * @var FactoryInterface |
||
23 | */ |
||
24 | private $factory; |
||
25 | |||
26 | /** |
||
27 | * @var NodeVersion |
||
28 | */ |
||
29 | private $activeNodeVersion; |
||
30 | |||
31 | /** |
||
32 | * @var EntityManager |
||
33 | */ |
||
34 | private $em; |
||
35 | |||
36 | /** |
||
37 | * @var RouterInterface |
||
38 | */ |
||
39 | private $router; |
||
40 | |||
41 | /** |
||
42 | * @var EventDispatcherInterface |
||
43 | */ |
||
44 | private $dispatcher; |
||
45 | |||
46 | /** |
||
47 | * @var AuthorizationCheckerInterface |
||
48 | */ |
||
49 | private $authorizationChecker; |
||
50 | |||
51 | /** |
||
52 | * @var PagesConfiguration |
||
53 | */ |
||
54 | private $pagesConfiguration; |
||
55 | |||
56 | /** |
||
57 | * @var bool |
||
58 | */ |
||
59 | private $isEditableNode = true; |
||
60 | |||
61 | /** |
||
62 | * @var bool |
||
63 | */ |
||
64 | private $enableExportPageTemplate; |
||
65 | |||
66 | /** |
||
67 | * @param FactoryInterface $factory The factory |
||
68 | * @param EntityManager $em The entity manager |
||
69 | * @param RouterInterface $router The router |
||
70 | * @param EventDispatcherInterface $dispatcher The event dispatcher |
||
71 | * @param AuthorizationCheckerInterface $authorizationChecker The security authorization checker |
||
72 | * @param PagesConfiguration $pagesConfiguration |
||
73 | * @param bool $enableExportPageTemplate |
||
74 | */ |
||
75 | 8 | public function __construct( |
|
76 | FactoryInterface $factory, |
||
77 | EntityManager $em, |
||
78 | RouterInterface $router, |
||
79 | EventDispatcherInterface $dispatcher, |
||
80 | AuthorizationCheckerInterface $authorizationChecker, |
||
81 | PagesConfiguration $pagesConfiguration, |
||
82 | $enableExportPageTemplate = true |
||
83 | ) { |
||
84 | 8 | $this->factory = $factory; |
|
85 | 8 | $this->em = $em; |
|
86 | 8 | $this->router = $router; |
|
87 | 8 | $this->dispatcher = $dispatcher; |
|
88 | 8 | $this->authorizationChecker = $authorizationChecker; |
|
89 | 8 | $this->pagesConfiguration = $pagesConfiguration; |
|
90 | 8 | $this->enableExportPageTemplate = $enableExportPageTemplate; |
|
91 | 8 | } |
|
92 | |||
93 | /** |
||
94 | * @return ItemInterface |
||
95 | */ |
||
96 | 1 | public function createSubActionsMenu() |
|
97 | { |
||
98 | 1 | $activeNodeVersion = $this->getActiveNodeVersion(); |
|
99 | 1 | $menu = $this->factory->createItem('root'); |
|
100 | 1 | $menu->setChildrenAttribute('class', 'page-sub-actions'); |
|
101 | |||
102 | 1 | if (null !== $activeNodeVersion && $this->isEditableNode) { |
|
103 | 1 | $menu->addChild( |
|
104 | 1 | 'subaction.versions', |
|
105 | [ |
||
106 | 'linkAttributes' => [ |
||
107 | 1 | 'data-toggle' => 'modal', |
|
108 | 'data-keyboard' => 'true', |
||
109 | 'data-target' => '#versions', |
||
110 | ], |
||
111 | ] |
||
112 | ); |
||
113 | } |
||
114 | |||
115 | 1 | $this->dispatcher->dispatch( |
|
116 | 1 | Events::CONFIGURE_SUB_ACTION_MENU, |
|
117 | 1 | new ConfigureActionMenuEvent( |
|
118 | 1 | $this->factory, |
|
119 | $menu, |
||
120 | $activeNodeVersion |
||
121 | ) |
||
122 | ); |
||
123 | |||
124 | 1 | return $menu; |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * @return ItemInterface |
||
129 | */ |
||
130 | 6 | public function createActionsMenu() |
|
131 | { |
||
132 | 6 | $activeNodeVersion = $this->getActiveNodeVersion(); |
|
133 | |||
134 | 6 | $translations = $activeNodeVersion->getNodeTranslation()->getNode()->getNodeTranslations(true); |
|
135 | 6 | $canRecopy = false; |
|
136 | 6 | foreach ($translations as $translation) { |
|
137 | 1 | if ($translation->getLang() != $activeNodeVersion->getNodeTranslation()->getLang()) { |
|
138 | 1 | $canRecopy = true; |
|
139 | } |
||
140 | } |
||
141 | |||
142 | 6 | $menu = $this->factory->createItem('root'); |
|
143 | 6 | $menu->setChildrenAttribute( |
|
144 | 6 | 'class', |
|
145 | 6 | 'page-main-actions js-auto-collapse-buttons' |
|
146 | ); |
||
147 | 6 | $menu->setChildrenAttribute( |
|
148 | 6 | 'data-visible-buttons', |
|
149 | 6 | '3' |
|
150 | ); |
||
151 | |||
152 | 6 | if (null === $activeNodeVersion) { |
|
153 | $this->dispatcher->dispatch( |
||
154 | Events::CONFIGURE_ACTION_MENU, |
||
155 | new ConfigureActionMenuEvent( |
||
156 | $this->factory, |
||
157 | $menu, |
||
158 | $activeNodeVersion |
||
159 | ) |
||
160 | ); |
||
161 | |||
162 | return $menu; |
||
163 | } |
||
164 | |||
165 | 6 | $activeNodeTranslation = $activeNodeVersion->getNodeTranslation(); |
|
166 | 6 | $node = $activeNodeTranslation->getNode(); |
|
167 | 6 | $queuedNodeTranslationAction = $this->em->getRepository( |
|
168 | 6 | 'KunstmaanNodeBundle:QueuedNodeTranslationAction' |
|
169 | 6 | )->findOneBy(['nodeTranslation' => $activeNodeTranslation]); |
|
170 | |||
171 | 6 | $isFirst = true; |
|
172 | 6 | $canEdit = $this->authorizationChecker->isGranted(PermissionMap::PERMISSION_EDIT, $node); |
|
173 | 6 | $canPublish = $this->authorizationChecker->isGranted(PermissionMap::PERMISSION_PUBLISH, $node); |
|
174 | 6 | $isSuperAdmin = $this->authorizationChecker->isGranted(UserInterface::ROLE_SUPER_ADMIN); |
|
175 | |||
176 | 6 | if ($activeNodeVersion->isDraft() && $this->isEditableNode) { |
|
177 | 1 | View Code Duplication | if ($canEdit) { |
178 | 1 | $menu->addChild( |
|
179 | 1 | 'action.saveasdraft', |
|
180 | [ |
||
181 | 'linkAttributes' => [ |
||
182 | 1 | 'type' => 'submit', |
|
183 | 'class' => 'js-save-btn btn btn--raise-on-hover btn-primary', |
||
184 | 'value' => 'save', |
||
185 | 'name' => 'save', |
||
186 | ], |
||
187 | 'extras' => ['renderType' => 'button'], |
||
188 | ] |
||
189 | ); |
||
190 | 1 | if ($this->enableExportPageTemplate && $isSuperAdmin && is_subclass_of($node->getRefEntityName(), HasPageTemplateInterface::class)) { |
|
191 | $menu->addChild( |
||
192 | 'action.exportpagetemplate', |
||
193 | [ |
||
194 | 'linkAttributes' => [ |
||
195 | 'class' => 'btn btn-default btn--raise-on-hover', |
||
196 | 'data-toggle' => 'modal', |
||
197 | 'data-keyboard' => 'true', |
||
198 | 'data-target' => '#exportPagetemplate', |
||
199 | ], |
||
200 | ] |
||
201 | ); |
||
202 | } |
||
203 | 1 | if ($canRecopy) { |
|
204 | $menu->addChild( |
||
205 | 'action.recopyfromlanguage', |
||
206 | [ |
||
207 | 'linkAttributes' => [ |
||
208 | 'class' => 'btn btn-default btn--raise-on-hover', |
||
209 | 'data-toggle' => 'modal', |
||
210 | 'data-keyboard' => 'true', |
||
211 | 'data-target' => '#recopy', |
||
212 | ], |
||
213 | ] |
||
214 | ); |
||
215 | } |
||
216 | 1 | $isFirst = false; |
|
217 | } |
||
218 | |||
219 | 1 | $menu->addChild( |
|
220 | 1 | 'action.preview', |
|
221 | [ |
||
222 | 1 | 'uri' => $this->router->generate( |
|
223 | 1 | '_slug_preview', |
|
224 | [ |
||
225 | 1 | 'url' => $activeNodeTranslation->getUrl(), |
|
226 | 1 | 'version' => $activeNodeVersion->getId(), |
|
227 | ] |
||
228 | ), |
||
229 | 'linkAttributes' => [ |
||
230 | 'target' => '_blank', |
||
231 | 'class' => 'btn btn-default btn--raise-on-hover', |
||
232 | ], |
||
233 | ] |
||
234 | ); |
||
235 | |||
236 | 1 | View Code Duplication | if (empty($queuedNodeTranslationAction) && $canPublish) { |
237 | 1 | $menu->addChild( |
|
238 | 1 | 'action.publish', |
|
239 | [ |
||
240 | 'linkAttributes' => [ |
||
241 | 1 | 'data-toggle' => 'modal', |
|
242 | 1 | 'data-target' => '#pub', |
|
243 | 1 | 'class' => 'btn btn--raise-on-hover'.($isFirst ? ' btn-primary btn-save' : ' btn-default'), |
|
244 | ], |
||
245 | ] |
||
246 | ); |
||
247 | } |
||
248 | } else { |
||
249 | 5 | if ($canEdit && $canPublish) { |
|
250 | 5 | $menu->addChild( |
|
251 | 5 | 'action.save', |
|
252 | [ |
||
253 | 'linkAttributes' => [ |
||
254 | 5 | 'type' => 'submit', |
|
255 | 'class' => 'js-save-btn btn btn--raise-on-hover btn-primary', |
||
256 | 'value' => 'save', |
||
257 | 'name' => 'save', |
||
258 | ], |
||
259 | 'extras' => ['renderType' => 'button'], |
||
260 | ] |
||
261 | ); |
||
262 | 5 | $isFirst = false; |
|
263 | } |
||
264 | |||
265 | 5 | if ($this->isEditableNode) { |
|
266 | 4 | $menu->addChild( |
|
267 | 4 | 'action.preview', |
|
268 | [ |
||
269 | 4 | 'uri' => $this->router->generate( |
|
270 | 4 | '_slug_preview', |
|
271 | 4 | ['url' => $activeNodeTranslation->getUrl()] |
|
272 | ), |
||
273 | 'linkAttributes' => [ |
||
274 | 'target' => '_blank', |
||
275 | 'class' => 'btn btn-default btn--raise-on-hover', |
||
276 | ], |
||
277 | ] |
||
278 | ); |
||
279 | |||
280 | 4 | if (empty($queuedNodeTranslationAction) |
|
281 | 4 | && $activeNodeTranslation->isOnline() |
|
282 | 1 | && $this->authorizationChecker->isGranted( |
|
283 | 4 | PermissionMap::PERMISSION_UNPUBLISH, |
|
284 | $node |
||
285 | ) |
||
286 | ) { |
||
287 | 1 | $menu->addChild( |
|
288 | 1 | 'action.unpublish', |
|
289 | [ |
||
290 | 'linkAttributes' => [ |
||
291 | 1 | 'class' => 'btn btn-default btn--raise-on-hover', |
|
292 | 'data-toggle' => 'modal', |
||
293 | 'data-keyboard' => 'true', |
||
294 | 'data-target' => '#unpub', |
||
295 | ], |
||
296 | ] |
||
297 | ); |
||
298 | 4 | View Code Duplication | } elseif (empty($queuedNodeTranslationAction) |
299 | 4 | && !$activeNodeTranslation->isOnline() |
|
300 | 4 | && $canPublish |
|
301 | ) { |
||
302 | 4 | $menu->addChild( |
|
303 | 4 | 'action.publish', |
|
304 | [ |
||
305 | 'linkAttributes' => [ |
||
306 | 4 | 'class' => 'btn btn-default btn--raise-on-hover', |
|
307 | 'data-toggle' => 'modal', |
||
308 | 'data-keyboard' => 'true', |
||
309 | 'data-target' => '#pub', |
||
310 | ], |
||
311 | ] |
||
312 | ); |
||
313 | } |
||
314 | |||
315 | 4 | View Code Duplication | if ($canEdit) { |
316 | 4 | $menu->addChild( |
|
317 | 4 | 'action.saveasdraft', |
|
318 | [ |
||
319 | 'linkAttributes' => [ |
||
320 | 4 | 'type' => 'submit', |
|
321 | 4 | 'class' => 'btn btn--raise-on-hover'.($isFirst ? ' btn-primary btn-save' : ' btn-default'), |
|
322 | 4 | 'value' => 'saveasdraft', |
|
323 | 4 | 'name' => 'saveasdraft', |
|
324 | ], |
||
325 | 'extras' => ['renderType' => 'button'], |
||
326 | ] |
||
327 | ); |
||
328 | 4 | if ($this->enableExportPageTemplate && $isSuperAdmin && is_subclass_of($node->getRefEntityName(), HasPageTemplateInterface::class)) { |
|
329 | $menu->addChild( |
||
330 | 'action.exportpagetemplate', |
||
331 | [ |
||
332 | 'linkAttributes' => [ |
||
333 | 'class' => 'btn btn-default btn--raise-on-hover', |
||
334 | 'data-toggle' => 'modal', |
||
335 | 'data-keyboard' => 'true', |
||
336 | 'data-target' => '#exportPagetemplate', |
||
337 | ], |
||
338 | ] |
||
339 | ); |
||
340 | } |
||
341 | 4 | if ($canRecopy) { |
|
342 | 1 | $menu->addChild( |
|
343 | 1 | 'action.recopyfromlanguage', |
|
344 | [ |
||
345 | 'linkAttributes' => [ |
||
346 | 1 | 'class' => 'btn btn-default btn--raise-on-hover', |
|
347 | 'data-toggle' => 'modal', |
||
348 | 'data-keyboard' => 'true', |
||
349 | 'data-target' => '#recopy', |
||
350 | ], |
||
351 | ] |
||
352 | ); |
||
353 | } |
||
354 | } |
||
355 | } |
||
356 | } |
||
357 | |||
358 | 6 | View Code Duplication | if ($this->pagesConfiguration->getPossibleChildTypes( |
359 | 6 | $node->getRefEntityName() |
|
360 | ) |
||
361 | ) { |
||
362 | $menu->addChild( |
||
363 | 'action.addsubpage', |
||
364 | [ |
||
365 | 'linkAttributes' => [ |
||
366 | 'type' => 'button', |
||
367 | 'class' => 'btn btn-default btn--raise-on-hover', |
||
368 | 'data-toggle' => 'modal', |
||
369 | 'data-keyboard' => 'true', |
||
370 | 'data-target' => '#add-subpage-modal', |
||
371 | ], |
||
372 | 'extras' => ['renderType' => 'button'], |
||
373 | ] |
||
374 | ); |
||
375 | } |
||
376 | |||
377 | 6 | View Code Duplication | if (null !== $node->getParent() && $canEdit) { |
0 ignored issues
–
show
|
|||
378 | 1 | $menu->addChild( |
|
379 | 1 | 'action.duplicate', |
|
380 | [ |
||
381 | 'linkAttributes' => [ |
||
382 | 1 | 'type' => 'button', |
|
383 | 'class' => 'btn btn-default btn--raise-on-hover', |
||
384 | 'data-toggle' => 'modal', |
||
385 | 'data-keyboard' => 'true', |
||
386 | 'data-target' => '#duplicate-page-modal', |
||
387 | ], |
||
388 | 'extras' => ['renderType' => 'button'], |
||
389 | ] |
||
390 | ); |
||
391 | } |
||
392 | |||
393 | 6 | if ((null !== $node->getParent() || $node->getChildren()->isEmpty()) |
|
394 | 6 | && $this->authorizationChecker->isGranted( |
|
395 | 6 | PermissionMap::PERMISSION_DELETE, |
|
396 | $node |
||
397 | ) |
||
398 | ) { |
||
399 | 6 | $menu->addChild( |
|
400 | 6 | 'action.delete', |
|
401 | [ |
||
402 | 'linkAttributes' => [ |
||
403 | 6 | 'type' => 'button', |
|
404 | 'class' => 'btn btn-default btn--raise-on-hover', |
||
405 | 'onClick' => 'oldEdited = isEdited; isEdited=false', |
||
406 | 'data-toggle' => 'modal', |
||
407 | 'data-keyboard' => 'true', |
||
408 | 'data-target' => '#delete-page-modal', |
||
409 | ], |
||
410 | 'extras' => ['renderType' => 'button'], |
||
411 | ] |
||
412 | ); |
||
413 | } |
||
414 | |||
415 | 6 | $this->dispatcher->dispatch( |
|
416 | 6 | Events::CONFIGURE_ACTION_MENU, |
|
417 | 6 | new ConfigureActionMenuEvent( |
|
418 | 6 | $this->factory, |
|
419 | $menu, |
||
420 | $activeNodeVersion |
||
421 | ) |
||
422 | ); |
||
423 | |||
424 | 6 | return $menu; |
|
425 | } |
||
426 | |||
427 | /** |
||
428 | * @return ItemInterface |
||
429 | */ |
||
430 | 1 | public function createTopActionsMenu() |
|
431 | { |
||
432 | 1 | $menu = $this->createActionsMenu(); |
|
433 | 1 | $menu->setChildrenAttribute('id', 'page-main-actions-top'); |
|
434 | 1 | $menu->setChildrenAttribute( |
|
435 | 1 | 'class', |
|
436 | 1 | 'page-main-actions page-main-actions--top' |
|
437 | ); |
||
438 | |||
439 | 1 | return $menu; |
|
440 | } |
||
441 | |||
442 | /** |
||
443 | * @return ItemInterface |
||
444 | */ |
||
445 | public function createHomeActionsMenu() |
||
446 | { |
||
447 | $menu = $this->factory->createItem('root'); |
||
448 | $menu->setChildrenAttribute( |
||
449 | 'class', |
||
450 | 'page-main-actions js-auto-collapse-buttons' |
||
451 | ); |
||
452 | $menu->addChild( |
||
453 | 'action.addhomepage', |
||
454 | [ |
||
455 | 'linkAttributes' => [ |
||
456 | 'type' => 'button', |
||
457 | 'class' => 'btn btn-default btn--raise-on-hover', |
||
458 | 'data-toggle' => 'modal', |
||
459 | 'data-keyboard' => 'true', |
||
460 | 'data-target' => '#add-homepage-modal', |
||
461 | ], |
||
462 | 'extras' => ['renderType' => 'button'], |
||
463 | ] |
||
464 | ); |
||
465 | |||
466 | return $menu; |
||
467 | } |
||
468 | |||
469 | /** |
||
470 | * @return ItemInterface |
||
471 | */ |
||
472 | public function createTopHomeActionsMenu() |
||
473 | { |
||
474 | $menu = $this->createHomeActionsMenu(); |
||
475 | $menu->setChildrenAttribute('id', 'page-main-actions-top'); |
||
476 | $menu->setChildrenAttribute( |
||
477 | 'class', |
||
478 | 'page-main-actions page-main-actions--top' |
||
479 | ); |
||
480 | |||
481 | return $menu; |
||
482 | } |
||
483 | |||
484 | /** |
||
485 | * Set activeNodeVersion |
||
486 | * |
||
487 | * @param NodeVersion $activeNodeVersion |
||
488 | * |
||
489 | * @return ActionsMenuBuilder |
||
490 | */ |
||
491 | 8 | public function setActiveNodeVersion(NodeVersion $activeNodeVersion) |
|
492 | { |
||
493 | 8 | $this->activeNodeVersion = $activeNodeVersion; |
|
494 | |||
495 | 8 | return $this; |
|
496 | } |
||
497 | |||
498 | /** |
||
499 | * Get activeNodeVersion |
||
500 | * |
||
501 | * @return NodeVersion |
||
502 | */ |
||
503 | 8 | public function getActiveNodeVersion() |
|
504 | { |
||
505 | 8 | return $this->activeNodeVersion; |
|
506 | } |
||
507 | |||
508 | /** |
||
509 | * @param bool $value |
||
510 | */ |
||
511 | 1 | public function setEditableNode($value) |
|
512 | { |
||
513 | 1 | $this->isEditableNode = $value; |
|
514 | 1 | } |
|
515 | } |
||
516 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.