<?php
namespace App\Event;
use App\Entity\Resource;
use App\Repository\ResourceRepository;
use App\Event\Menu\SidebarMenuEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class DiaryMenuSubscriber implements EventSubscriberInterface
{
/**
* @var TokenStorageInterface
*/
private $tokenStorage;
/**
* @var ResourceRepository
*/
private $resourceRepository;
public function __construct(TokenStorageInterface $tokenStorage, ResourceRepository $resourceRepository)
{
$this->tokenStorage = $tokenStorage;
$this->resourceRepository = $resourceRepository;
}
public function onSidebarMenuConfigure(SidebarMenuEvent $event)
{
$builder = $this->resourceRepository->createQueryBuilder('r')
->addSelect('organizationUnit')
->leftJoin('r.organizationUnit', 'organizationUnit');
if ($this->tokenStorage->getToken()->getUser()->getOrganizationUnit()) {
$path = $this->tokenStorage->getToken()->getUser()->getOrganizationUnit()->getPath();
$builder->andWhere('organizationUnit.path LIKE :tree')
->setParameter('tree', $path . '%');
}
$resources = $builder->getQuery()->getResult();
$menu = $event->getItem();
if ($menu['Diary']) {
foreach ($resources as $resource) {
$menu['Diary']->addChild(
$resource->getName(),
[
'route'=>'diary_res',
'routeParameters'=>['id'=>$resource->getId()]
]
);
}
}
}
public static function getSubscribedEvents(): array
{
return [
SidebarMenuEvent::EVENT => 'onSidebarMenuConfigure',
];
}
}