highlight menus on sub-pages (#1220)

This commit is contained in:
Kevin Papst
2019-11-07 11:19:10 +01:00
committed by GitHub
parent 140fa4b7c0
commit 0705c26513
3 changed files with 69 additions and 39 deletions

View File

@@ -10,6 +10,7 @@
namespace App\EventSubscriber; namespace App\EventSubscriber;
use App\Event\ConfigureMainMenuEvent; use App\Event\ConfigureMainMenuEvent;
use App\Utils\MenuItemModel as KimaiMenuItemModel;
use KevinPapst\AdminLTEBundle\Event\SidebarMenuEvent; use KevinPapst\AdminLTEBundle\Event\SidebarMenuEvent;
use KevinPapst\AdminLTEBundle\Model\MenuItemModel; use KevinPapst\AdminLTEBundle\Model\MenuItemModel;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -104,6 +105,13 @@ class MenuBuilderSubscriber implements EventSubscriberInterface
} else { } else {
if ($item->getRoute() == $route) { if ($item->getRoute() == $route) {
$item->setIsActive(true); $item->setIsActive(true);
continue;
}
if ($item instanceof KimaiMenuItemModel) {
if ($item->isChildRoute($route)) {
$item->setIsActive(true);
continue;
}
} }
} }
} }

View File

@@ -11,8 +11,9 @@ namespace App\EventSubscriber;
use App\Event\ConfigureMainMenuEvent; use App\Event\ConfigureMainMenuEvent;
use App\Twig\IconExtension; use App\Twig\IconExtension;
use App\Utils\MenuItemModel;
use KevinPapst\AdminLTEBundle\Event\SidebarMenuEvent; use KevinPapst\AdminLTEBundle\Event\SidebarMenuEvent;
use KevinPapst\AdminLTEBundle\Model\MenuItemModel; use KevinPapst\AdminLTEBundle\Model\MenuItemInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
@@ -36,9 +37,6 @@ final class MenuSubscriber implements EventSubscriberInterface
$this->icons = new IconExtension(); $this->icons = new IconExtension();
} }
/**
* @return array
*/
public static function getSubscribedEvents(): array public static function getSubscribedEvents(): array
{ {
return [ return [
@@ -46,9 +44,6 @@ final class MenuSubscriber implements EventSubscriberInterface
]; ];
} }
/**
* @param \App\Event\ConfigureMainMenuEvent $event
*/
public function onMainMenuConfigure(ConfigureMainMenuEvent $event) public function onMainMenuConfigure(ConfigureMainMenuEvent $event)
{ {
$auth = $this->security; $auth = $this->security;
@@ -62,17 +57,14 @@ final class MenuSubscriber implements EventSubscriberInterface
$this->configureSystemMenu($event->getSystemMenu()); $this->configureSystemMenu($event->getSystemMenu());
} }
/**
* @param SidebarMenuEvent $menu
*/
private function configureMainMenu(SidebarMenuEvent $menu) private function configureMainMenu(SidebarMenuEvent $menu)
{ {
$auth = $this->security; $auth = $this->security;
if ($auth->isGranted('view_own_timesheet')) { if ($auth->isGranted('view_own_timesheet')) {
$menu->addItem( $timesheets = new MenuItemModel('timesheet', 'menu.timesheet', 'timesheet', [], $this->getIcon('timesheet'));
new MenuItemModel('timesheet', 'menu.timesheet', 'timesheet', [], $this->getIcon('timesheet')) $timesheets->setChildRoutes(['timesheet_export', 'timesheet_edit', 'timesheet_create']);
); $menu->addItem($timesheets);
$menu->addItem( $menu->addItem(
new MenuItemModel('calendar', 'calendar.title', 'calendar', [], $this->getIcon('calendar')) new MenuItemModel('calendar', 'calendar.title', 'calendar', [], $this->getIcon('calendar'))
); );
@@ -91,35 +83,32 @@ final class MenuSubscriber implements EventSubscriberInterface
} }
} }
/** private function configureAdminMenu(MenuItemInterface $menu)
* @param MenuItemModel $menu
*/
private function configureAdminMenu(MenuItemModel $menu)
{ {
$auth = $this->security; $auth = $this->security;
if ($auth->isGranted('view_other_timesheet')) { if ($auth->isGranted('view_other_timesheet')) {
$menu->addChild( $timesheets = new MenuItemModel('timesheet_admin', 'menu.admin_timesheet', 'admin_timesheet', [], $this->getIcon('timesheet-team'));
new MenuItemModel('timesheet_admin', 'menu.admin_timesheet', 'admin_timesheet', [], $this->getIcon('timesheet-team')) $timesheets->setChildRoutes(['admin_timesheet_export', 'admin_timesheet_edit', 'admin_timesheet_create']);
); $menu->addChild($timesheets);
} }
if ($auth->isGranted('view_customer')) { if ($auth->isGranted('view_customer')) {
$menu->addChild( $customers = new MenuItemModel('customer_admin', 'menu.admin_customer', 'admin_customer', [], $this->getIcon('customer'));
new MenuItemModel('customer_admin', 'menu.admin_customer', 'admin_customer', [], $this->getIcon('customer')) $customers->setChildRoutes(['admin_customer_create', 'admin_customer_permissions', 'admin_customer_budget', 'admin_customer_edit', 'admin_customer_delete']);
); $menu->addChild($customers);
} }
if ($auth->isGranted('view_project')) { if ($auth->isGranted('view_project')) {
$menu->addChild( $projects = new MenuItemModel('project_admin', 'menu.admin_project', 'admin_project', [], $this->getIcon('project'));
new MenuItemModel('project_admin', 'menu.admin_project', 'admin_project', [], $this->getIcon('project')) $projects->setChildRoutes(['admin_project_permissions', 'admin_project_create', 'admin_project_budget', 'admin_project_edit', 'admin_project_delete']);
); $menu->addChild($projects);
} }
if ($auth->isGranted('view_activity')) { if ($auth->isGranted('view_activity')) {
$menu->addChild( $activities = new MenuItemModel('activity_admin', 'menu.admin_activity', 'admin_activity', [], $this->getIcon('activity'));
new MenuItemModel('activity_admin', 'menu.admin_activity', 'admin_activity', [], $this->getIcon('activity')) $activities->setChildRoutes(['admin_activity_create', 'admin_activity_budget', 'admin_activity_edit', 'admin_activity_delete']);
); $menu->addChild($activities);
} }
if ($auth->isGranted('view_tag')) { if ($auth->isGranted('view_tag')) {
@@ -129,23 +118,20 @@ final class MenuSubscriber implements EventSubscriberInterface
} }
} }
/** private function configureSystemMenu(MenuItemInterface $menu)
* @param MenuItemModel $menu
*/
private function configureSystemMenu(MenuItemModel $menu)
{ {
$auth = $this->security; $auth = $this->security;
if ($auth->isGranted('view_user')) { if ($auth->isGranted('view_user')) {
$menu->addChild( $users = new MenuItemModel('user_admin', 'menu.admin_user', 'admin_user', [], $this->getIcon('user'));
new MenuItemModel('user_admin', 'menu.admin_user', 'admin_user', [], $this->getIcon('user')) $users->setChildRoutes(['admin_user_create', 'admin_user_delete', 'admin_user_permissions', 'user_profile', 'user_profile_edit', 'user_profile_password', 'user_profile_api_token', 'user_profile_roles', 'user_profile_teams', 'user_profile_preferences']);
); $menu->addChild($users);
} }
if ($auth->isGranted('view_team')) { if ($auth->isGranted('view_team')) {
$menu->addChild( $teams = new MenuItemModel('user_team', 'menu.admin_team', 'admin_team', [], $this->getIcon('team'));
new MenuItemModel('user_team', 'menu.admin_team', 'admin_team', [], $this->getIcon('team')) $teams->setChildRoutes(['admin_team_create', 'admin_team_edit']);
); $menu->addChild($teams);
} }
if ($auth->isGranted('plugins')) { if ($auth->isGranted('plugins')) {

View File

@@ -0,0 +1,36 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Utils;
use KevinPapst\AdminLTEBundle\Model\MenuItemModel as BaseMenuItemModel;
class MenuItemModel extends BaseMenuItemModel
{
private $childRoutes = [];
public function setChildRoutes(array $routes): MenuItemModel
{
$this->childRoutes = $routes;
return $this;
}
public function addChildRoute(string $route): MenuItemModel
{
$this->childRoutes[] = $route;
return $this;
}
public function isChildRoute(string $route): bool
{
return in_array($route, $this->childRoutes);
}
}