/** * Generates the url for a given page in a pagerfanta instance. * * @param \Pagerfanta\PagerfantaInterface $pagerfanta * @param $page * @param array $options * * @return string The url of the given page * * @throws \InvalidArgumentException */ public function getPageUrl(PagerfantaInterface $pagerfanta, $page, array $options = array()) { if ($page < 0 || $page > $pagerfanta->count()) { throw new \InvalidArgumentException("Page '{$page}' is out of bounds"); } $routeGenerator = $this->createRouteGenerator($options); return $routeGenerator($page); }
public function addPagerNavigationRelations(PagerfantaInterface $pager, $route, $routeParameters = array(), $pageParameterName = null, $limitParameterName = null) { if (null === $pageParameterName) { $pageParameterName = $this->defaultPageParameterName; } if (null === $limitParameterName) { $limitParameterName = $this->defaultLimitParameterName; } if (!isset($routeParameters[$pageParameterName])) { $routeParameters[$pageParameterName] = $pager->getCurrentPage(); } if (!isset($routeParameters[$limitParameterName])) { $routeParameters[$limitParameterName] = $pager->getMaxPerPage(); } $this->add('self', array('route' => $route, 'parameters' => $routeParameters)); $this->add('first', array('route' => $route, 'parameters' => array_merge($routeParameters, array($pageParameterName => '1')))); $this->add('last', array('route' => $route, 'parameters' => array_merge($routeParameters, array($pageParameterName => $pager->getNbPages() > 0 ? $pager->getNbPages() : 1)))); if ($pager->hasPreviousPage()) { $this->add('previous', array('route' => $route, 'parameters' => array_merge($routeParameters, array($pageParameterName => $pager->getPreviousPage())))); } if ($pager->hasNextPage()) { $this->add('next', array('route' => $route, 'parameters' => array_merge($routeParameters, array($pageParameterName => $pager->getNextPage())))); } }
/** * {@inheritdoc} */ public function render(PagerfantaInterface $pagerfanta, $routeGenerator, array $options = array()) { $options = array_merge(array('proximity' => 2, 'previous_message' => 'Previous', 'next_message' => 'Next', 'css_disabled_class' => 'disabled', 'css_dots_class' => 'dots', 'css_current_class' => 'current'), $options); $currentPage = $pagerfanta->getCurrentPage(); $startPage = $currentPage - $options['proximity']; $endPage = $currentPage + $options['proximity']; if ($startPage < 1) { $endPage = min($endPage + (1 - $startPage), $pagerfanta->getNbPages()); $startPage = 1; } if ($endPage > $pagerfanta->getNbPages()) { $startPage = max($startPage - ($endPage - $pagerfanta->getNbPages()), 1); $endPage = $pagerfanta->getNbPages(); } $pages = array(); // previous if ($pagerfanta->hasPreviousPage()) { $pages[] = array($pagerfanta->getPreviousPage(), $options['previous_message']); } else { $pages[] = sprintf('<span class="%s">%s</span>', $options['css_disabled_class'], $options['previous_message']); } // first if ($startPage > 1) { $pages[] = array(1, 1); if (3 == $startPage) { $pages[] = array(2, 2); } elseif (2 != $startPage) { $pages[] = sprintf('<span class="%s">...</span>', $options['css_dots_class']); } } // pages for ($page = $startPage; $page <= $endPage; $page++) { if ($page == $currentPage) { $pages[] = sprintf('<span class="%s">%s</span>', $options['css_current_class'], $page); } else { $pages[] = array($page, $page); } } // last if ($pagerfanta->getNbPages() > $endPage) { if ($pagerfanta->getNbPages() > $endPage + 1) { if ($pagerfanta->getNbPages() > $endPage + 2) { $pages[] = sprintf('<span class="%s">...</span>', $options['css_dots_class']); } else { $pages[] = array($endPage + 1, $endPage + 1); } } $pages[] = array($pagerfanta->getNbPages(), $pagerfanta->getNbPages()); } // next if ($pagerfanta->hasNextPage()) { $pages[] = array($pagerfanta->getNextPage(), $options['next_message']); } else { $pages[] = sprintf('<span class="%s">%s</span>', $options['css_disabled_class'], $options['next_message']); } // process $pagesHtml = ''; foreach ($pages as $page) { if (is_string($page)) { $pagesHtml .= $page; } else { if (strstr($routeGenerator, 'javascript')) { $pagesHtml .= '<a href="' . $routeGenerator . '' . $page[0] . ')">' . $page[1] . '</a>'; } else { $pagesHtml .= '<a href="' . trim($routeGenerator) . '' . $page[0] . '">' . $page[1] . '</a>'; } } } return '<nav>' . $pagesHtml . '</nav>'; }
/** * {@inheritdoc} */ public function render(PagerfantaInterface $pagerfanta, $routeGenerator, array $options = array()) { $options = array_merge(array('proximity' => 3, 'prev_message' => '← Previous', 'prev_disabled_href' => '', 'next_message' => 'Next →', 'next_disabled_href' => '', 'dots_message' => '…', 'dots_href' => '', 'css_container_class' => 'pagination', 'css_prev_class' => 'prev', 'css_next_class' => 'next', 'css_disabled_class' => 'disabled', 'css_dots_class' => 'disabled', 'css_active_class' => 'active', 'route' => ''), $options); $currentPage = $pagerfanta->getCurrentPage(); $startPage = $currentPage - $options['proximity']; $endPage = $currentPage + $options['proximity']; if ($startPage < 1) { $endPage = min($endPage + (1 - $startPage), $pagerfanta->getNbPages()); $startPage = 1; } if ($endPage > $pagerfanta->getNbPages()) { $startPage = max($startPage - ($endPage - $pagerfanta->getNbPages()), 1); $endPage = $pagerfanta->getNbPages(); } $pages = array(); // previous $class = $options['css_prev_class']; $url = $options['prev_disabled_href']; if (!$pagerfanta->hasPreviousPage()) { $class .= ' ' . $options['css_disabled_class']; $rel = "prev"; } else { $url = $routeGenerator($pagerfanta->getPreviousPage(), $options['route']); if ($pagerfanta->getPreviousPage() == 1) { $rel = "canonical"; } else { $rel = "prev"; } } $pages[] = sprintf('<li class="%s"><a href="%s" rel="%s">%s</a></li>', $class, $url, $rel, $options['prev_message']); // first if ($startPage > 1) { $pages[] = sprintf('<li><a href="%s" rel="canonical">%s</a></li>', $routeGenerator(1, $options['route']), 1); if (3 == $startPage) { $pages[] = sprintf('<li><a href="%s" rel="canonical">%s</a></li>', $routeGenerator(2, $options['route']), 2); } elseif (2 != $startPage) { $pages[] = sprintf('<li class="%s"><a href="%s">%s</a></li>', $options['css_dots_class'], $options['dots_href'], $options['dots_message']); } } // pages for ($page = $startPage; $page <= $endPage; $page++) { $class = ''; if ($page == $currentPage) { $class = sprintf(' class="%s"', $options['css_active_class']); } $rel = ""; if ($page == 1) { $rel = "canonical"; } elseif ($page < $currentPage) { $rel = "prev"; } elseif ($page > $currentPage) { $rel = "next"; } $pages[] = sprintf('<li%s><a href="%s" rel="%s">%s</a></li>', $class, $routeGenerator($page, $options['route']), $rel, $page); } // last if ($pagerfanta->getNbPages() > $endPage) { if ($pagerfanta->getNbPages() > $endPage + 1) { if ($pagerfanta->getNbPages() > $endPage + 2) { $pages[] = sprintf('<li class="%s"><a href="%s">%s</a></li>', $options['css_dots_class'], $options['dots_href'], $options['dots_message']); } else { $pages[] = sprintf('<li><a href="%s" rel="next">%s</a></li>', $routeGenerator($endPage + 1, $options['route']), $endPage + 1); } } $pages[] = sprintf('<li><a href="%s" rel="next">%s</a></li>', $routeGenerator($pagerfanta->getNbPages(), $options['route']), $pagerfanta->getNbPages()); } // next $class = $options['css_next_class']; $url = $options['next_disabled_href']; if (!$pagerfanta->hasNextPage()) { $class .= ' ' . $options['css_disabled_class']; } else { $url = $routeGenerator($pagerfanta->getNextPage(), $options['route']); } $pages[] = sprintf('<li class="%s"><a href="%s" rel="next">%s</a></li>', $class, $url, $options['next_message']); return sprintf('<div class="%s"><ul>%s</ul></div>', $options['css_container_class'], implode('', $pages)); }
private function initializePagerfanta(PagerfantaInterface $pagerfanta) { $this->pagerfanta = $pagerfanta; $this->currentPage = $pagerfanta->getCurrentPage(); $this->nbPages = $pagerfanta->getNbPages(); }
/** * {@inheritdoc} */ public function render(PagerfantaInterface $pagerfanta, $routeGenerator, array $options = array()) { $options = array_merge(array('proximity' => 2, 'previous_message' => $this->translator->trans('pagerfanta.previous', array(), 'Admingenerator'), 'next_message' => $this->translator->trans('pagerfanta.next', array(), 'Admingenerator'), 'css_disabled_class' => 'disabled', 'css_dots_class' => 'dots', 'css_current_class' => 'current'), $options); $currentPage = $pagerfanta->getCurrentPage(); $startPage = $currentPage - $options['proximity']; $endPage = $currentPage + $options['proximity']; if ($startPage < 1) { $endPage = min($endPage + (1 - $startPage), $pagerfanta->getNbPages()); $startPage = 1; } if ($endPage > $pagerfanta->getNbPages()) { $startPage = max($startPage - ($endPage - $pagerfanta->getNbPages()), 1); $endPage = $pagerfanta->getNbPages(); } $pages = array(); // previous if ($pagerfanta->hasPreviousPage()) { $pages[] = array($pagerfanta->getPreviousPage(), $options['previous_message']); } else { // $pages[] = sprintf('<span class="%s">%s</span>', $options['css_disabled_class'], $options['previous_message']); } // first if ($startPage > 1) { $pages[] = array(1, 1); if (3 == $startPage) { $pages[] = array(2, 2); } elseif (2 != $startPage) { $pages[] = sprintf('<span class="%s">...</span>', $options['css_dots_class']); } } // pages for ($page = $startPage; $page <= $endPage; $page++) { if ($page == $currentPage) { $pages[] = sprintf('<li><a href="#" class="number %s">%s</a></li>', $options['css_current_class'], $page); } else { $pages[] = array($page, $page); } } // last if ($pagerfanta->getNbPages() > $endPage) { if ($pagerfanta->getNbPages() > $endPage + 1) { if ($pagerfanta->getNbPages() > $endPage + 2) { $pages[] = sprintf('<span class="%s">...</span>', $options['css_dots_class']); } else { $pages[] = array($endPage + 1, $endPage + 1); } } $pages[] = array($pagerfanta->getNbPages(), $pagerfanta->getNbPages()); } // next if ($pagerfanta->hasNextPage()) { $pages[] = array($pagerfanta->getNextPage(), $options['next_message']); } else { // $pages[] = sprintf('<span class="%s">%s</span>', $options['css_disabled_class'], $options['next_message']); } // process $pagesHtml = ''; foreach ($pages as $page) { if (is_string($page)) { $pagesHtml .= $page; } else { if (is_string($page[1])) { $pagesHtml .= '<li><a href="' . $routeGenerator($page[0]) . '">' . $page[1] . '</a></li>'; } else { $pagesHtml .= '<li><a href="' . $routeGenerator($page[0]) . '" class="number">' . $page[1] . '</a></li>'; } } } return '<ul class="pagination">' . $pagesHtml . '</ul>'; }
/** * {@inheritdoc} */ public function render(PagerfantaInterface $pagerfanta, $routeGenerator, array $options = array()) { $options = array_merge(array('proximity' => 2, 'previous_message' => 'Anterior', 'next_message' => 'Siguiente', 'css_disabled_class' => 'disabled', 'css_current_class' => 'active'), $options); $currentPage = $pagerfanta->getCurrentPage(); $startPage = $currentPage - $options['proximity']; $endPage = $currentPage + $options['proximity']; if ($startPage < 1) { $endPage = min($endPage + (1 - $startPage), $pagerfanta->getNbPages()); $startPage = 1; } if ($endPage > $pagerfanta->getNbPages()) { $startPage = max($startPage - ($endPage - $pagerfanta->getNbPages()), 1); $endPage = $pagerfanta->getNbPages(); } $pages = array(); // previous if ($pagerfanta->hasPreviousPage()) { $pages[] = array($pagerfanta->getPreviousPage(), $options['previous_message']); } else { $pages[] = sprintf('<li class="%s prev"><a href="#">%s</a>', $options['css_disabled_class'], $options['previous_message']); } // first if ($startPage > 1) { $pages[] = array(1, 1); if (3 == $startPage) { $pages[] = array(2, 2); } elseif (2 != $startPage) { //$pages[] = sprintf('<span class="%s">...</span>', $options['css_dots_class']); } } // pages for ($page = $startPage; $page <= $endPage; $page++) { if ($page == $currentPage) { $pages[] = sprintf('<li class="%s"><a>%s</a></li>', $options['css_current_class'], $page); } else { $pages[] = array($page, $page); } } // last if ($pagerfanta->getNbPages() > $endPage) { if ($pagerfanta->getNbPages() > $endPage + 1) { if ($pagerfanta->getNbPages() > $endPage + 2) { //$pages[] = sprintf('<span class="%s">...</span>', $options['css_dots_class']); } else { $pages[] = array($endPage + 1, $endPage + 1); } } $pages[] = array($pagerfanta->getNbPages(), $pagerfanta->getNbPages()); } // next if ($pagerfanta->hasNextPage()) { $pages[] = array($pagerfanta->getNextPage(), $options['next_message']); } else { $pages[] = sprintf('<li class="%s next"><a href="#">%s</a></li>', $options['css_disabled_class'], $options['next_message']); } // process $pagesHtml = ''; foreach ($pages as $page) { if (is_string($page)) { $pagesHtml .= $page; } else { if (!isset($options['category_id'])) { $options['category_id'] = false; } if (!isset($options['type'])) { $options['type'] = false; } $pagesHtml .= '<li><a href="' . $routeGenerator($page[0], $options['category_id'], $options['type']) . '">' . $page[1] . '</a></li>'; } } return '<div class="pagination"><ul>' . $pagesHtml . '</ul></div>'; }