src/EventListener/CRUDListQBSubscriber.php line 48

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\User;
  4. use Sogec\BOBundle\Event\CRUDEvents;
  5. use Sogec\BOBundle\Event\GetCRUDListQueryEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  8. /**
  9.  * Class CRUDListQBSubscriber.
  10.  */
  11. class CRUDListQBSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var TokenStorageInterface
  15.      */
  16.     private $tokenStorage;
  17.     /**
  18.      * ParticipationListQBSubscriber constructor.
  19.      *
  20.      * @param TokenStorageInterface $tokenStorage
  21.      */
  22.     public function __construct(TokenStorageInterface $tokenStorage)
  23.     {
  24.         $this->tokenStorage $tokenStorage;
  25.     }
  26.     /**
  27.      * {@inheritdoc}
  28.      */
  29.     public static function getSubscribedEvents()
  30.     {
  31.         return array(
  32.             CRUDEvents::LIST_INITIALIZE => array(
  33.                 array('getAdminParticipationListQB'),
  34.             ),
  35.         );
  36.     }
  37.     /**
  38.      * Override partner participation list query builder.
  39.      *
  40.      * @param GetCRUDListQueryEvent $event
  41.      */
  42.     public function getAdminParticipationListQB(GetCRUDListQueryEvent $event)
  43.     {
  44.         $query $event->getQuery();
  45.         $alias $event->getCRUDAlias();
  46.         $user $this->tokenStorage->getToken()->getUser();
  47.         if ($user instanceof User && ('participations' === $alias || 'partenaires' === $alias)) {
  48.             $sectorsId $user->getAllSectorsId();
  49.             $query
  50.                 ->andWhere($query->expr()->in('sector.id'':my_sectors'))
  51.                 ->setParameter('my_sectors'$sectorsId);
  52.         }
  53.     }
  54. }