From f442bf54b78ec9466ad56239b275a47704882eaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 19 Aug 2016 21:36:35 +0200 Subject: [PATCH 1/2] implements the preview of search --- Resources/config/services.yml | 1 + Resources/views/Person/list.html.twig | 18 ++++++++++++++++++ Search/PersonSearch.php | 26 ++++++++++++++++++++++---- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 5f36aabf5..d2ae37056 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -23,6 +23,7 @@ services: - "@doctrine.orm.entity_manager" - "@security.token_storage" - "@chill.main.security.authorization.helper" + - "@chill_main.paginator_factory" calls: - ['setContainer', ["@service_container"]] tags: diff --git a/Resources/views/Person/list.html.twig b/Resources/views/Person/list.html.twig index 654b49ddf..04b06b9d9 100644 --- a/Resources/views/Person/list.html.twig +++ b/Resources/views/Person/list.html.twig @@ -18,6 +18,8 @@

{{ '%total% persons matching the search %pattern%'|transchoice( total, {'%pattern%': pattern, '%total%' : total}) }}

+

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

+ {% if persons|length > 0 %} @@ -55,4 +57,20 @@ {% endfor %}
+ + + {% endif %} diff --git a/Search/PersonSearch.php b/Search/PersonSearch.php index eceabd4ab..922d35d3a 100644 --- a/Search/PersonSearch.php +++ b/Search/PersonSearch.php @@ -22,13 +22,14 @@ namespace Chill\PersonBundle\Search; use Chill\MainBundle\Search\AbstractSearch; use Doctrine\ORM\EntityManagerInterface; use Chill\PersonBundle\Entity\Person; -use Symfony\Component\DependencyInjection\ContainerInterface; +use Chill\MainBundle\Search\SearchInterface; use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\DependencyInjection\ContainerAwareTrait; use Chill\MainBundle\Search\ParsingException; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; use Chill\MainBundle\Security\Authorization\AuthorizationHelper; use Symfony\Component\Security\Core\Role\Role; +use Chill\MainBundle\Pagination\PaginatorFactory; class PersonSearch extends AbstractSearch implements ContainerAwareInterface { @@ -52,13 +53,25 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface */ private $helper; + /** + * + * @var PaginatorFactory + */ + protected $paginatorFactory; - public function __construct(EntityManagerInterface $em, - TokenStorage $tokenStorage, AuthorizationHelper $helper) + const NAME = "person_regular"; + + + public function __construct( + EntityManagerInterface $em, + TokenStorage $tokenStorage, + AuthorizationHelper $helper, + PaginatorFactory $paginatorFactory) { $this->em = $em; $this->user = $tokenStorage->getToken()->getUser(); $this->helper = $helper; + $this->paginatorFactory = $paginatorFactory; // throw an error if user is not a valid user if (!$this->user instanceof \Chill\MainBundle\Entity\User) { @@ -96,13 +109,18 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface */ public function renderResult(array $terms, $start = 0, $limit = 50, array $options = array()) { + $total = $this->count($terms); + $paginator = $this->paginatorFactory->create($total); return $this->container->get('templating')->render('ChillPersonBundle:Person:list.html.twig', array( 'persons' => $this->search($terms, $start, $limit, $options), 'pattern' => $this->recomposePattern($terms, array('nationality', 'firstname', 'lastname', 'birthdate', 'gender'), $terms['_domain']), - 'total' => $this->count($terms) + 'total' => $total, + 'start' => $start, + 'search_name' => self::NAME, + 'preview' => $options[SearchInterface::SEARCH_PREVIEW] )); } From 10ea0c77e7cb2abd57cd135a4c16ca8e47442389 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 25 Aug 2016 22:16:46 +0200 Subject: [PATCH 2/2] adding pagination on search + renaming option --- Resources/views/Person/list.html.twig | 5 +++++ Search/PersonSearch.php | 23 ++++++++++++----------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/Resources/views/Person/list.html.twig b/Resources/views/Person/list.html.twig index 04b06b9d9..dc90de4bd 100644 --- a/Resources/views/Person/list.html.twig +++ b/Resources/views/Person/list.html.twig @@ -74,3 +74,8 @@ {% endif %} + +{% if preview == false %} +{{ chill_pagination(paginator) }} +{% endif %} + diff --git a/Search/PersonSearch.php b/Search/PersonSearch.php index 922d35d3a..e2678cb6c 100644 --- a/Search/PersonSearch.php +++ b/Search/PersonSearch.php @@ -36,7 +36,7 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface use ContainerAwareTrait; /** - * + * * @var EntityManagerInterface */ private $em; @@ -49,7 +49,7 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface /** * - * @var AuthorizationHelper + * @var AuthorizationHelper */ private $helper; @@ -63,8 +63,8 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface public function __construct( - EntityManagerInterface $em, - TokenStorage $tokenStorage, + EntityManagerInterface $em, + TokenStorage $tokenStorage, AuthorizationHelper $helper, PaginatorFactory $paginatorFactory) { @@ -112,20 +112,21 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface $total = $this->count($terms); $paginator = $this->paginatorFactory->create($total); - return $this->container->get('templating')->render('ChillPersonBundle:Person:list.html.twig', - array( + return $this->container->get('templating')->render('ChillPersonBundle:Person:list.html.twig', + array( 'persons' => $this->search($terms, $start, $limit, $options), 'pattern' => $this->recomposePattern($terms, array('nationality', 'firstname', 'lastname', 'birthdate', 'gender'), $terms['_domain']), 'total' => $total, 'start' => $start, 'search_name' => self::NAME, - 'preview' => $options[SearchInterface::SEARCH_PREVIEW] + 'preview' => $options[SearchInterface::SEARCH_PREVIEW_OPTION], + 'paginator' => $paginator )); } /** - * + * * @param string $pattern * @param int $start * @param int $limit @@ -161,11 +162,11 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface private $_cacheQuery = array(); /** - * + * * @param array $terms * @return \Doctrine\ORM\QueryBuilder */ - protected function createQuery(array $terms) + protected function createQuery(array $terms) { //get from cache $cacheKey = md5(serialize($terms)); @@ -237,7 +238,7 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface } //restraint center for security - $reachableCenters = $this->helper->getReachableCenters($this->user, + $reachableCenters = $this->helper->getReachableCenters($this->user, new Role('CHILL_PERSON_SEE')); $qb->andWhere($qb->expr() ->in('p.center', ':centers'))