* @author Champs Libres */ class EventSearch extends AbstractSearch { /** * * @var EntityRepository */ private $er; /** * * @var \Chill\MainBundle\Entity\User */ private $user; /** * * @var AuthorizationHelper */ private $helper; /** * * @var TemplatingEngine */ private $templating; public function __construct( TokenStorageInterface $tokenStorage, EntityRepository $eventRepository, AuthorizationHelper $authorizationHelper, TemplatingEngine $templating ) { $this->user = $tokenStorage->getToken()->getUser(); $this->er = $eventRepository; $this->helper = $authorizationHelper; $this->templating = $templating; } public function supports($domain) { return 'event' === $domain or 'events' === $domain; } public function isActiveByDefault() { return false; } public function getOrder() { return 3000; } public function renderResult(array $terms, $start = 0, $limit = 50, array $options = array()) { return $this->templating->render('ChillEventBundle:Event:list.html.twig', array( 'events' => $this->search($terms, $start, $limit, $options), 'pattern' => $this->recomposePattern($terms, array(), $terms['_domain']), 'total' => $this->count($terms) )); } protected function search(array $terms, $start, $limit, $options) { $qb = $this->er->createQueryBuilder('e'); $qb->select('e'); $this->composeQuery($qb, $terms) ->setMaxResults($limit) ->setFirstResult($start) ->orderBy('e.date', 'DESC') ; return $qb->getQuery()->getResult(); } protected function count(array $terms) { $qb = $this->er->createQueryBuilder('e'); $qb->select('COUNT(e)'); $this->composeQuery($qb, $terms) ; return $qb->getQuery()->getSingleScalarResult(); } protected function composeQuery(QueryBuilder &$qb, $terms) { // add security clauses $reachableCenters = $this->helper ->getReachableCenters($this->user, new Role('CHILL_EVENT_SEE')); if (count($reachableCenters) === 0) { // add a clause to block all events $where = $qb->expr()->isNull('e.center'); $qb->andWhere($where); } else { $n = 0; $orWhere = $qb->expr()->orX(); foreach ($reachableCenters as $center) { $circles = $this->helper->getReachableScopes($this->user, new Role('CHILL_EVENT_SEE'), $center); $where = $qb->expr()->andX( $qb->expr()->eq('e.center', ':center_'.$n), $qb->expr()->in('e.circle', ':circle_'.$n) ); $qb->setParameter('center_'.$n, $center); $qb->setParameter('circle_'.$n, $circles); $orWhere->add($where); } $qb->andWhere($orWhere); } return $qb; } }