/** * Return the key of the current element * @link http://php.net/manual/en/iterator.key.php * @return mixed scalar on success, or null on failure. */ public function key() { $innerKey = $this->getInnerIterator()->key(); $innerKey += 1; //Zend\Paginator\Paginator normalizes 0 to 1 $page = $this->paginator->getCurrentPageNumber(); return $this->paginator->getAbsoluteItemNumber($innerKey, $this->paginator->getCurrentPageNumber()) - 1; }
/** * Returns an array of "local" pages given a page number and range. * * @param \Zend\Paginator\Paginator $paginator * @param integer $pageRange (Optional) Page range * @return array */ public function getPages(\Zend\Paginator\Paginator $paginator, $pageRange = null) { if ($pageRange === null) { $pageRange = $paginator->getPageRange(); } $pageNumber = $paginator->getCurrentPageNumber(); $pageCount = count($paginator); if ($pageRange > $pageCount) { $pageRange = $pageCount; } $delta = ceil($pageRange / 2); if ($pageNumber - $delta > $pageCount - $pageRange) { $lowerBound = $pageCount - $pageRange + 1; $upperBound = $pageCount; } else { if ($pageNumber - $delta < 0) { $delta = $pageNumber; } $offset = $pageNumber - $delta; $lowerBound = $offset + 1; $upperBound = $offset + $pageRange; } return $paginator->getPagesInRange($lowerBound, $upperBound); }
/** * Convert a paginator to array * * @param Paginator $paginator * @param string $key root key for items, leave null to return items in root * @return array */ protected function paginatorToArray(Paginator $paginator, $key = null) { $items = iterator_to_array($paginator->getCurrentItems()); if (null === $key) { return $items; } return array('pages' => $paginator->count(), 'current' => $paginator->getCurrentPageNumber(), 'count' => $paginator->getTotalItemCount(), $key => $items); }
/** * @group ZF-4193 */ public function testCastsIntegerValuesToInteger() { // Current page number $this->paginator->setCurrentPageNumber(3.3); $this->assertTrue($this->paginator->getCurrentPageNumber() == 3); // Item count per page $this->paginator->setItemCountPerPage(3.3); $this->assertTrue($this->paginator->getItemCountPerPage() == 3); // Page range $this->paginator->setPageRange(3.3); $this->assertTrue($this->paginator->getPageRange() == 3); }
/** * Returns an array of "local" pages given a page number and range. * * @param Paginator $paginator * @param int $pageRange Unused * @return array */ public function getPages(Paginator $paginator, $pageRange = null) { $pageRange = $paginator->getPageRange(); $pageNumber = $paginator->getCurrentPageNumber(); $delta = $pageNumber % $pageRange; if ($delta == 0) { $delta = $pageRange; } $offset = $pageNumber - $delta; $lowerBound = $offset + 1; $upperBound = $offset + $pageRange; return $paginator->getPagesInRange($lowerBound, $upperBound); }
/** * Returns an array of "local" pages given a page number and range. * * @param Paginator $paginator * @param int $pageRange Unused * @return array */ public function getPages(Paginator $paginator, $pageRange = null) { $pageRange = $paginator->getPageRange(); $pageNumber = $paginator->getCurrentPageNumber(); $originalPageRange = $pageRange; $pageRange = $pageRange * 2 - 1; if ($originalPageRange + $pageNumber - 1 < $pageRange) { $pageRange = $originalPageRange + $pageNumber - 1; } elseif ($originalPageRange + $pageNumber - 1 > count($paginator)) { $pageRange = $originalPageRange + count($paginator) - $pageNumber; } return parent::getPages($paginator, $pageRange); }
/** * * @return string */ public function __invoke(Paginator $paginator) { $content = <<<HTML \t\t<label class="inline control-label">Displaying: </label> \t\t<span> \t\t\t%1\$s - %2\$s of %3\$s \t\t</span> HTML; $limit = $paginator->getItemCountPerPage(); $page = $paginator->getCurrentPageNumber(); $current = $paginator->getCurrentItemCount(); $total = $paginator->getTotalItemCount(); $offset = $limit * $page - $limit; return sprintf($content, intval($offset) + 1, intval($offset) + intval($current), $total); }
public function indexAction() { /** @var QueryBuilder $qb */ $qb = $this->getObjectManager()->createQueryBuilder(); $qb->select('w')->from(Word::class, 'w'); $form = new WordSearchForm(); $form->setData($this->params()->fromQuery()); $form->setInputFilter(new WordSearchInputFilter()); // it doesn't matter if the form is not valid, as we would fill in default values anyway. This is just to avoid the default error messages $form->isValid(); // set query, direction and order for the querybuilder $direction = strtolower($form->getData()['d']); $direction = $direction == 'desc' ? 'desc' : 'asc'; $orderBy = strtolower($form->getData()['o']); switch ($orderBy) { case 'created': $order = 'createdAt'; break; case 'updated': $order = 'updatedAt'; break; default: $orderBy = $order = 'word'; break; } $qb->orderBy('w.' . $order, $direction); // no need to avoid SQL injection, as doctrine does this for us $query = $form->getData()['q']; $qb->where('w.word LIKE :query')->setParameter('query', '%' . $query . '%'); $itemsPerPage = 50; // set up the paginator $adapter = new DoctrineAdapter(new ORMPaginator($qb->getQuery())); $paginator = new Paginator($adapter); $paginator->setDefaultItemCountPerPage(50); $page = (int) $this->params()->fromQuery('p'); if ($page != null) { $paginator->setCurrentPageNumber($page); } else { $paginator->setCurrentPageNumber(1); } $pages = $paginator->getTotalItemCount() / $itemsPerPage; $paginator->setPageRange($pages); $viewModel = new ViewModel(); $viewModel->setVariables(['words' => $paginator->getCurrentItems(), 'pageCount' => $paginator->getPageRange() + 1, 'currentPage' => $paginator->getCurrentPageNumber(), 'orderBy' => $orderBy, 'direction' => $direction, 'query' => $query, 'form' => $form]); return $viewModel; }
/** * getList * * @return mixed|JsonModel */ public function getList() { //ACCESS CHECK if (!$this->rcmIsAllowed('sites', 'admin')) { $this->getResponse()->setStatusCode(Response::STATUS_CODE_401); return $this->getResponse(); } /** @var \Doctrine\ORM\EntityManagerInterface $entityManager */ $entityManager = $this->getEntityManager(); /** @var \Rcm\Repository\Site $siteRepo */ $siteRepo = $entityManager->getRepository('\\Rcm\\Entity\\Site'); $createQueryBuilder = $siteRepo->createQueryBuilder('site')->select('site')->leftJoin('site.domain', 'domain')->leftJoin('site.country', 'country')->leftJoin('site.language', 'language')->orderBy('domain.domain', 'ASC'); $query = $createQueryBuilder->getQuery(); $searchQuery = $this->params()->fromQuery('q'); if ($searchQuery) { $createQueryBuilder->where($createQueryBuilder->expr()->like('domain.domain', ':searchQuery')); $query = $createQueryBuilder->getQuery(); $query->setParameter('searchQuery', $searchQuery . '%'); } $adaptor = new DoctrinePaginator(new ORMPaginator($query)); $paginator = new Paginator($adaptor); $paginator->setDefaultItemCountPerPage(10); $page = (int) $this->params()->fromQuery('page'); if ($page) { $paginator->setCurrentPageNumber($page); } $pageSize = (int) $this->params()->fromQuery('page_size'); if ($pageSize) { $paginator->setItemCountPerPage($pageSize); } $sitesObjects = $paginator->getCurrentItems(); $sites = []; /** @var \Rcm\Entity\Site $site */ foreach ($sitesObjects as $site) { $sites[] = $site->toArray(); } $list['items'] = $sites; $list['itemCount'] = $paginator->getTotalItemCount(); $list['pageCount'] = $paginator->count(); $list['currentPage'] = $paginator->getCurrentPageNumber(); return new ApiJsonModel($list, 0, 'Success'); }
/** * Return list of resources * * @param callable $queryBuilderCallback * @param callable $queryCallback * @throws \Application\Library\Exception\ValidationException * @return mixed */ public function getList(\Closure $queryBuilderCallback = null, \Closure $queryCallback = null) { $form = new ModelFilterForm(); $form->setData($this->params()->fromQuery()); if (!$form->isValid()) { throw new ValidationException($form->getMessages(), 400); } $limit = $form->getData()['rows']; $page = $form->getData()['page']; $queryBuilder = $this->getEntityManager()->createQueryBuilder(); $queryBuilder->select('c')->from($this->entityClass, 'c'); if (!is_null($queryBuilderCallback)) { $queryBuilderCallback($queryBuilder); } $query = $queryBuilder->getQuery()->setHydrationMode(Query::HYDRATE_ARRAY); if (!is_null($queryCallback)) { $queryCallback($query); } $paginator = new Paginator(new DoctrinePaginator(new ORMPaginator($query))); $paginator->setCurrentPageNumber($page)->setItemCountPerPage($limit); $return = array('page' => $paginator->getCurrentPageNumber(), 'total' => ceil($paginator->getTotalItemCount() / $limit), 'records' => $paginator->getTotalItemCount(), 'rows' => $paginator->getCurrentItems()->getArrayCopy()); return new JsonModel($return); }
/** * @param Paginator $paginator * @return array */ public function __invoke(Paginator $paginator) { return ['limit' => $paginator->getItemCountPerPage(), 'offset' => ($paginator->getCurrentPageNumber() - 1) * $paginator->getItemCountPerPage(), 'total' => $paginator->getTotalItemCount()]; }
/** * Get the current page. * * @return int */ public function getCurrentPage() { return $this->paginator->getCurrentPageNumber(); }
/** * Checks if provided paginator is in last page * * @param Paginator $paginator * @return bool */ protected function isLastPage(Paginator $paginator) { return $paginator->getCurrentPageNumber() >= $paginator->count(); }