/**
  * @return Pagerfanta
  */
 public function getPagerfanta()
 {
     if (is_null($this->pagerfanta)) {
         $adapter = new DoctrineORMAdapter($this->getQuery());
         $this->pagerfanta = new Pagerfanta($adapter);
         $this->pagerfanta->setNormalizeOutOfRangePages(true);
         $this->pagerfanta->setMaxPerPage($this->getLimit());
         $this->pagerfanta->setCurrentPage($this->getPage());
     }
     return $this->pagerfanta;
 }
Exemple #2
0
 /**
  * {@inheritdoc}
  */
 public function getData(Parameters $parameters)
 {
     // Use output walkers option in DoctrineORMAdapter should be false as it affects performance greatly. (see #3775)
     $paginator = new Pagerfanta(new DoctrineORMAdapter($this->queryBuilder, true, false));
     $paginator->setNormalizeOutOfRangePages(true);
     $paginator->setCurrentPage($parameters->get('page', 1));
     return $paginator;
 }
Exemple #3
0
 public function testSetCurrentPageShouldNormalizePageWhenOutOfRangePageAndIndicatingNormalizeOutOfRangePages()
 {
     $this->setAdapterNbResultsAny(100);
     $this->pagerfanta->setMaxPerPage(10);
     $this->pagerfanta->setAllowOutOfRangePages(false);
     $this->pagerfanta->setNormalizeOutOfRangePages(true);
     $this->pagerfanta->setCurrentPage(11);
     $this->assertSame(10, $this->pagerfanta->getCurrentPage());
 }
Exemple #4
0
 /**
  * {@inheritdoc}
  */
 public function getData(Parameters $parameters)
 {
     $countQueryBuilderModifier = function ($queryBuilder) {
         $queryBuilder->select('COUNT(DISTINCT o.id) AS total_results')->setMaxResults(1);
     };
     $paginator = new Pagerfanta(new DoctrineDbalAdapter($this->queryBuilder, $countQueryBuilderModifier));
     $paginator->setNormalizeOutOfRangePages(true);
     $paginator->setCurrentPage($parameters->get('page', 1));
     return $paginator;
 }
 /**
  * Injects the pager with related content into the view.
  *
  * @param \eZ\Publish\Core\MVC\Symfony\View\Event\FilterViewParametersEvent $event
  */
 public function injectPager(FilterViewParametersEvent $event)
 {
     $view = $event->getView();
     if (!$view instanceof TagView) {
         return;
     }
     if ($this->adapter instanceof TagAdapterInterface) {
         $this->adapter->setTag($view->getTag());
     }
     $pager = new Pagerfanta($this->adapter);
     $pager->setNormalizeOutOfRangePages(true);
     $builderParameters = $event->getBuilderParameters();
     $pager->setMaxPerPage($this->pagerLimit > 0 ? $this->pagerLimit : 10);
     $pager->setCurrentPage($builderParameters['page'] > 0 ? $builderParameters['page'] : 1);
     $event->getParameterBag()->set('related_content', $pager);
 }
 /**
  * @param JsonApiSerializationVisitor $visitor
  * @param Pagerfanta                  $pagerfanta
  * @param array                       $type
  * @param Context                     $context
  * @return Pagerfanta
  */
 public function serializePagerfanta(JsonApiSerializationVisitor $visitor, Pagerfanta $pagerfanta, array $type, Context $context)
 {
     $request = $this->requestStack->getCurrentRequest();
     $pagerfanta->setNormalizeOutOfRangePages(true);
     $pagerfanta->setAllowOutOfRangePages(true);
     $pagerfanta->setMaxPerPage($request->get('page[limit]', $this->paginationOptions['limit'], true));
     $pagerfanta->setCurrentPage($request->get('page[number]', 1, true));
     $results = $pagerfanta->getCurrentPageResults();
     if ($results instanceof \ArrayIterator) {
         $results = $results->getArrayCopy();
     }
     $data = $context->accept($results);
     $root = $visitor->getRoot();
     $root['meta'] = array('page' => $pagerfanta->getCurrentPage(), 'limit' => $pagerfanta->getMaxPerPage(), 'pages' => $pagerfanta->getNbPages(), 'total' => $pagerfanta->getNbResults());
     $root['links'] = array('first' => $this->getUriForPage(1), 'last' => $this->getUriForPage($pagerfanta->getNbPages()), 'prev' => $pagerfanta->hasPreviousPage() ? $this->getUriForPage($pagerfanta->getPreviousPage()) : null, 'next' => $pagerfanta->hasNextPage() ? $this->getUriForPage($pagerfanta->getNextPage()) : null);
     $visitor->setRoot($root);
     return $data;
 }
Exemple #7
0
 /**
  * Creates and returns the table view.
  *
  * @param Request $request
  * @throws RuntimeException
  * @return TableView
  */
 public function createView(Request $request = null)
 {
     if (null !== $request) {
         $this->requestHelper->setRequest($request);
     }
     $view = new TableView();
     $view->name = $this->getName();
     $view->options = ['selector' => $this->config->getSelector()];
     $view->selection_form = $this->config->getSelector();
     if (!$this->config->hasColumns()) {
         throw new RuntimeException('Table has no columns and cannot be generated.');
     }
     $class = $this->config->getDataClass();
     $alias = strtolower(substr($class, strrpos($class, '\\') + 1, 1));
     // TODO What if data is already set ?
     $queryBuilder = $this->entityManager->createQueryBuilder()->select($alias)->from($this->config->getDataClass(), $alias);
     if (null !== ($customizeQb = $this->config->getCustomizeQb())) {
         $customizeQb($queryBuilder, $alias);
     }
     $this->generateFilters($queryBuilder, $view);
     $this->generateColumns($queryBuilder, $view);
     $currentPage = $this->requestHelper->getVar($this->getName() . '_page', 1);
     /*$dql = $queryBuilder->getQuery()->getDQL();
       var_dump($dql);
       exit();*/
     $adapter = new DoctrineORMAdapter($queryBuilder);
     $pager = new Pagerfanta($adapter);
     $pager->setNormalizeOutOfRangePages(true)->setMaxPerPage($this->config->getMaxPerPage())->setCurrentPage($currentPage);
     $this->setData($pager->getCurrentPageResults());
     if ($currentPage != $pager->getCurrentPage()) {
         $this->requestHelper->setVar($this->getName() . '_page', $pager->getCurrentPage());
     }
     $view->pager = $pager;
     $this->generateCells($view);
     return $view;
 }