src/Event/DiaryMenuSubscriber.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Event;
  3. use App\Entity\Resource;
  4. use App\Repository\ResourceRepository;
  5. use App\Event\Menu\SidebarMenuEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  8. class DiaryMenuSubscriber implements EventSubscriberInterface
  9. {
  10.     /**
  11.      * @var TokenStorageInterface
  12.      */
  13.     private $tokenStorage;
  14.     /**
  15.      * @var ResourceRepository
  16.      */
  17.     private $resourceRepository;
  18.     public function __construct(TokenStorageInterface $tokenStorageResourceRepository $resourceRepository)
  19.     {
  20.         $this->tokenStorage $tokenStorage;
  21.         $this->resourceRepository $resourceRepository;
  22.     }
  23.     public function onSidebarMenuConfigure(SidebarMenuEvent $event)
  24.     {
  25.         $builder $this->resourceRepository->createQueryBuilder('r')
  26.             ->addSelect('organizationUnit')
  27.             ->leftJoin('r.organizationUnit''organizationUnit');
  28.         if ($this->tokenStorage->getToken()->getUser()->getOrganizationUnit()) {
  29.             $path $this->tokenStorage->getToken()->getUser()->getOrganizationUnit()->getPath();
  30.             $builder->andWhere('organizationUnit.path LIKE :tree')
  31.                 ->setParameter('tree'$path '%');
  32.         }
  33.         $resources $builder->getQuery()->getResult();
  34.         $menu $event->getItem();
  35.         if ($menu['Diary']) {
  36.             foreach ($resources as $resource) {
  37.                 $menu['Diary']->addChild(
  38.                     $resource->getName(),
  39.                     [
  40.                         'route'=>'diary_res',
  41.                         'routeParameters'=>['id'=>$resource->getId()]
  42.                     ]
  43.                 );
  44.             }
  45.         }
  46.     }
  47.     public static function getSubscribedEvents(): array
  48.     {
  49.         return [
  50.             SidebarMenuEvent::EVENT => 'onSidebarMenuConfigure',
  51.         ];
  52.     }
  53. }