<?php
namespace App\EventListener;
use App\Entity\User;
use Sogec\BOBundle\Event\CRUDEvents;
use Sogec\BOBundle\Event\GetCRUDListQueryEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
/**
* Class CRUDListQBSubscriber.
*/
class CRUDListQBSubscriber implements EventSubscriberInterface
{
/**
* @var TokenStorageInterface
*/
private $tokenStorage;
/**
* ParticipationListQBSubscriber constructor.
*
* @param TokenStorageInterface $tokenStorage
*/
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return array(
CRUDEvents::LIST_INITIALIZE => array(
array('getAdminParticipationListQB'),
),
);
}
/**
* Override partner participation list query builder.
*
* @param GetCRUDListQueryEvent $event
*/
public function getAdminParticipationListQB(GetCRUDListQueryEvent $event)
{
$query = $event->getQuery();
$alias = $event->getCRUDAlias();
$user = $this->tokenStorage->getToken()->getUser();
if ($user instanceof User && ('participations' === $alias || 'partenaires' === $alias)) {
$sectorsId = $user->getAllSectorsId();
$query
->andWhere($query->expr()->in('sector.id', ':my_sectors'))
->setParameter('my_sectors', $sectorsId);
}
}
}