mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
239 lines
6.9 KiB
PHP
239 lines
6.9 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Chill is a software for social workers
|
|
*
|
|
* For the full copyright and license information, please view
|
|
* the LICENSE file that was distributed with this source code.
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Chill\EventBundle\Search;
|
|
|
|
use Chill\EventBundle\Entity\Event;
|
|
use Chill\MainBundle\Pagination\PaginatorFactory;
|
|
use Chill\MainBundle\Search\AbstractSearch;
|
|
use Chill\MainBundle\Search\SearchInterface;
|
|
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
|
|
use Doctrine\ORM\EntityRepository;
|
|
use Doctrine\ORM\QueryBuilder;
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
|
use Symfony\Component\Templating\EngineInterface as TemplatingEngine;
|
|
use function count;
|
|
|
|
/**
|
|
* Search within Events.
|
|
*
|
|
* available terms :
|
|
* - name: to search within the name
|
|
* - date: search the event at a the given date
|
|
* - date-from: search the event after the given date
|
|
* - date-to : search the event before the given date
|
|
*
|
|
* Default terms search within the name, but the term "name" has precedence. This
|
|
* means that if the search string is `@event xyz name:"abc"`, the searched
|
|
* string will be "abc" and not xyz
|
|
*/
|
|
class EventSearch extends AbstractSearch
|
|
{
|
|
public const NAME = 'event_regular';
|
|
|
|
/**
|
|
* @var EntityRepository
|
|
*/
|
|
private $er;
|
|
|
|
/**
|
|
* @var AuthorizationHelper
|
|
*/
|
|
private $helper;
|
|
|
|
/**
|
|
* @var PaginatorFactory
|
|
*/
|
|
private $paginationFactory;
|
|
|
|
/**
|
|
* @var TemplatingEngine
|
|
*/
|
|
private $templating;
|
|
|
|
/**
|
|
* @var \Chill\MainBundle\Entity\User
|
|
*/
|
|
private $user;
|
|
|
|
public function __construct(
|
|
TokenStorageInterface $tokenStorage,
|
|
EntityRepository $eventRepository,
|
|
AuthorizationHelper $authorizationHelper,
|
|
TemplatingEngine $templating,
|
|
PaginatorFactory $paginatorFactory
|
|
) {
|
|
$this->user = $tokenStorage->getToken()->getUser();
|
|
$this->er = $eventRepository;
|
|
$this->helper = $authorizationHelper;
|
|
$this->templating = $templating;
|
|
$this->paginationFactory = $paginatorFactory;
|
|
}
|
|
|
|
public function getOrder()
|
|
{
|
|
return 3000;
|
|
}
|
|
|
|
public function isActiveByDefault()
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public function renderResult(
|
|
array $terms,
|
|
$start = 0,
|
|
$limit = 50,
|
|
array $options = [],
|
|
$format = 'html'
|
|
) {
|
|
$total = $this->count($terms);
|
|
$paginator = $this->paginationFactory->create($total);
|
|
|
|
if ('html' === $format) {
|
|
return $this->templating->render(
|
|
'ChillEventBundle:Event:list.html.twig',
|
|
[
|
|
'events' => $this->search($terms, $start, $limit, $options),
|
|
'pattern' => $this->recomposePattern($terms, $this->getAvailableTerms(), $terms['_domain']),
|
|
'total' => $total,
|
|
'start' => $start,
|
|
'preview' => $options[SearchInterface::SEARCH_PREVIEW_OPTION],
|
|
'paginator' => $paginator,
|
|
'search_name' => self::NAME,
|
|
]
|
|
);
|
|
}
|
|
|
|
if ('json' === $format) {
|
|
$results = [];
|
|
$search = $this->search($terms, $start, $limit, $options);
|
|
|
|
foreach ($search as $item) {
|
|
$results[] = [
|
|
'id' => $item->getId(),
|
|
'text' => $item->getDate()->format('d/m/Y, H:i') . ' → ' .
|
|
// $item->getType()->getName()['fr'] . ': ' . // display the type of event
|
|
$item->getName(),
|
|
];
|
|
}
|
|
|
|
return [
|
|
'results' => $results,
|
|
'pagination' => [
|
|
'more' => $paginator->hasNextPage(),
|
|
],
|
|
];
|
|
}
|
|
}
|
|
|
|
public function supports($domain, $format)
|
|
{
|
|
return 'event' === $domain || 'events' === $domain;
|
|
}
|
|
|
|
protected function composeQuery(QueryBuilder &$qb, $terms)
|
|
{
|
|
// add security clauses
|
|
$reachableCenters = $this->helper
|
|
->getReachableCenters($this->user, '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,
|
|
'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);
|
|
}
|
|
|
|
if (
|
|
(isset($terms['name']) || isset($terms['_default']))
|
|
&& (!empty($terms['name']) || !empty($terms['_default']))) {
|
|
// the form with name:"xyz" has precedence
|
|
$name = $terms['name'] ?? $terms['_default'];
|
|
|
|
$where = $qb->expr()->like('UNACCENT(LOWER(e.name))', ':name');
|
|
$qb->setParameter('name', '%' . $name . '%');
|
|
$qb->andWhere($where);
|
|
}
|
|
|
|
if (isset($terms['date'])) {
|
|
$date = $this->parseDate($terms['date']);
|
|
|
|
$where = $qb->expr()->eq('e.date', ':date');
|
|
$qb->setParameter('date', $date);
|
|
$qb->andWhere($where);
|
|
}
|
|
|
|
if (isset($terms['date-from'])) {
|
|
$date = $this->parseDate($terms['date-from']);
|
|
|
|
$where = $qb->expr()->gte('e.date', ':datefrom');
|
|
$qb->setParameter('datefrom', $date);
|
|
$qb->andWhere($where);
|
|
}
|
|
|
|
if (isset($terms['date-to'])) {
|
|
$date = $this->parseDate($terms['date-to']);
|
|
|
|
$where = $qb->expr()->lte('e.date', ':dateto');
|
|
$qb->setParameter('dateto', $date);
|
|
$qb->andWhere($where);
|
|
}
|
|
|
|
return $qb;
|
|
}
|
|
|
|
protected function count(array $terms)
|
|
{
|
|
$qb = $this->er->createQueryBuilder('e');
|
|
$qb->select('COUNT(e)');
|
|
$this->composeQuery($qb, $terms);
|
|
|
|
return $qb->getQuery()->getSingleScalarResult();
|
|
}
|
|
|
|
protected function getAvailableTerms()
|
|
{
|
|
return ['date-from', 'date-to', 'name', 'date'];
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|