From 2a10929d36e7bddc68c304ebd3492471e853310c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 19 Aug 2016 21:35:41 +0200 Subject: [PATCH] implementing the pagination api and the search preview --- Resources/config/services/search.yml | 1 + Resources/views/Event/list.html.twig | 13 +++++++++++++ Search/EventSearch.php | 25 ++++++++++++++++++++++--- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/Resources/config/services/search.yml b/Resources/config/services/search.yml index a1e0b2f17..2df3db26f 100644 --- a/Resources/config/services/search.yml +++ b/Resources/config/services/search.yml @@ -6,6 +6,7 @@ services: - "@chill_event.repository.event" - "@chill.main.security.authorization.helper" - "@templating" + - "@chill_main.paginator_factory" tags: - { name: chill.search, alias: 'event_regular' } diff --git a/Resources/views/Event/list.html.twig b/Resources/views/Event/list.html.twig index 10d274e2d..6da14a82f 100644 --- a/Resources/views/Event/list.html.twig +++ b/Resources/views/Event/list.html.twig @@ -2,6 +2,7 @@

{% transchoice total with { '%pattern%' : pattern } %}%total% events match the search %pattern%{% endtranschoice %}

+

{{ 'Results %start%-%end% of %total%'|trans({ '%start%' : start, '%end%': start + events|length, '%total%' : total } ) }}

{% if events|length > 0 %} @@ -48,5 +49,17 @@ {{ 'New event'|trans }} + + {% if preview == true and events|length < total %} +
  • + + {{ 'See all results'|trans }} +
  • + {% endif %} + + +{% if preview == false %} +{{ chill_pagination(paginator) }} +{% endif %} diff --git a/Search/EventSearch.php b/Search/EventSearch.php index d04d0e3eb..baf379a5d 100644 --- a/Search/EventSearch.php +++ b/Search/EventSearch.php @@ -10,6 +10,8 @@ use Chill\MainBundle\Security\Authorization\AuthorizationHelper; use Symfony\Component\Templating\EngineInterface as TemplatingEngine; use Doctrine\ORM\QueryBuilder; use Symfony\Component\Security\Core\Role\Role; +use Chill\MainBundle\Pagination\PaginatorFactory; +use Chill\MainBundle\Search\SearchInterface; /** * Search within Events. @@ -55,18 +57,27 @@ class EventSearch extends AbstractSearch */ private $templating; + /** + * + * @var PaginatorFactory + */ + private $paginationFactory; + + const NAME = 'event_regular'; public function __construct( TokenStorageInterface $tokenStorage, EntityRepository $eventRepository, AuthorizationHelper $authorizationHelper, - TemplatingEngine $templating + TemplatingEngine $templating, + PaginatorFactory $paginatorFactory ) { $this->user = $tokenStorage->getToken()->getUser(); $this->er = $eventRepository; $this->helper = $authorizationHelper; $this->templating = $templating; + $this->paginationFactory = $paginatorFactory; } public function supports($domain) @@ -84,13 +95,21 @@ class EventSearch extends AbstractSearch return 3000; } - public function renderResult(array $terms, $start = 0, $limit = 50, array $options = array()) + public function renderResult(array $terms, $start = 0, $limit = 50, + array $options = array()) { + $total = $this->count($terms); + $paginator = $this->paginationFactory->create($total); + return $this->templating->render('ChillEventBundle:Event:list.html.twig', array( 'events' => $this->search($terms, $start, $limit, $options), 'pattern' => $this->recomposePattern($terms, $this->getAvailableTerms(), $terms['_domain']), - 'total' => $this->count($terms) + 'total' => $total, + 'start' => $start, + 'preview' => $options[SearchInterface::SEARCH_PREVIEW], + 'paginator' => $paginator, + 'search_name' => self::NAME )); }