-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuController.php
More file actions
59 lines (50 loc) · 1.65 KB
/
MenuController.php
File metadata and controls
59 lines (50 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
namespace SyncEngine\Controller;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use SyncEngine\Service\Menu\MenuLoader;
class MenuController extends DefaultController
{
public function __construct(
private Security $security,
) {}
#[Route( '/json/menu/{menuName}', name: 'json_menu' )]
public function jsonMenu( string $menuName, MenuLoader $loader ): JsonResponse
{
return $this->json( $this->fetchMenuItems( $menuName, $loader ) );
}
public function renderMainMenu( $currentPath, MenuLoader $loader ): Response
{
$items = $this->fetchMenuItems( 'main', $loader );
return $this->render(
'admin/_partials/menu.twig',
[
'groups' => [ 'dashboards', 'entities', 'system' ],
'items' => $items,
'currentPath' => $currentPath,
]
);
}
public function fetchMenuItems( string $menuName, MenuLoader $loader ): array
{
$items = [];
$isAdmin = $this->security->isGranted( 'ROLE_ADMIN' );
// @todo filter according to the security
foreach ( $loader->getMenuItems( $menuName )->all() as $item ) {
$link = $this->generateUrl( $item->getRoute(), $item->getParameters() );
if ( $isAdmin or ! str_contains( $link, "admin" ) ) {
$items[] = [
'name' => $item->getRoute(),
'link' => $link,
'title' => $this->trans( $item->getLabel() ),
'icon' => $item->getIcon(),
'parent' => $item->getParent() ?: 'system',
'position' => (float) ( is_numeric( $item->getPosition() ) ? $item->getPosition() : 10 ),
];
}
}
return $items;
}
}