/** * @return string */ public function renderPagination(Page $page, Twig_Environment $environment) { $pagerfanta = new Pagerfanta(new PorpaginasAdapter($page)); $pagerfanta->setCurrentPage($page->getCurrentPage()); $pagerfanta->setMaxPerPage($page->getCurrentLimit()); return $environment->getExtension('pagerfanta')->renderPagerfanta($pagerfanta, $this->viewName, $this->options); }
/** * @param Request $request * * @return Response */ public function searchAction(Request $req) { $typeFilter = $req->get('type'); $tagsFilter = $req->get('tags'); if ($req->has('q') || $typeFilter || $tagsFilter) { $solarium = new SolariumClient('localhost:8080'); $select = $solarium->createSelect(); // filter by type if ($typeFilter) { $filterQueryTerm = sprintf('type:%s', $select->getHelper()->escapeTerm($typeFilter)); $filterQuery = $select->createFilterQuery('type')->setQuery($filterQueryTerm); $select->addFilterQuery($filterQuery); } // filter by tags if ($tagsFilter) { $tags = array(); foreach ((array) $tagsFilter as $tag) { $tags[] = $select->getHelper()->escapeTerm($tag); } $filterQueryTerm = sprintf('tags:(%s)', implode(' AND ', $tags)); $filterQuery = $select->createFilterQuery('tags')->setQuery($filterQueryTerm); $select->addFilterQuery($filterQuery); } if ($req->has('q')) { $escapedQuery = $select->getHelper()->escapeTerm($req->get('q')); $select->setQuery($escapedQuery); } $paginator = new Pagerfanta(new SolariumAdapter($solarium, $select)); $paginator->setMaxPerPage(15); $paginator->setCurrentPage($req->get('page', 1), false, true); if ($req->isXmlHttpRequest()) { try { return $this->render('ProductBundle:Search:list.html.twig', array('products' => $paginator->getResults(), 'noLayout' => true)); } catch (\Twig_Error_Runtime $e) { if (!$e->getPrevious() instanceof \Solarium_Client_HttpException) { throw $e; } return new JsonResponse(array('status' => 'error', 'message' => 'Could not connect to the search server'), 500); } } else { return $this->render('ProductBundle:Search:search.html.twig', array('products' => $paginator->getResults())); } } return $this->render('ProductBundle:Search:search.html.twig', array('noLayout' => $req->isXmlHttpRequest())); }
/** * Get results from paginator and get paginator view. */ protected function paginator($queryBuilder) { // Paginator $adapter = new DoctrineORMAdapter($queryBuilder); $pagerfanta = new Pagerfanta($adapter); $currentPage = $this->getRequest()->get('page', 1); $pagerfanta->setCurrentPage($currentPage); $entities = $pagerfanta->getCurrentPageResults(); // Paginator - route generator $me = $this; $routeGenerator = function ($page) use($me) { return $me->generateUrl('admin_role', array('page' => $page)); }; // Paginator - view $translator = $this->get('translator'); $view = new TwitterBootstrap3View(); $pagerHtml = $view->render($pagerfanta, $routeGenerator, array('proximity' => 3, 'prev_message' => $translator->trans('views.index.pagprev'), 'next_message' => $translator->trans('views.index.pagnext'))); return array($entities, $pagerHtml); }