diff -r fc78844c8a76 -r 99ad73ef7385 vendor/bundles/Pagerfanta/View/TwitterBootstrapView.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/bundles/Pagerfanta/View/TwitterBootstrapView.php Fri Oct 21 17:10:54 2011 +0200 @@ -0,0 +1,133 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Pagerfanta\View; + +use Pagerfanta\PagerfantaInterface; + +/** + * TwitterBootstrapView. + * + * View that can be used with the pagination module + * from the Twitter Bootstrap CSS Toolkit + * http://twitter.github.com/bootstrap/ + * + * @author Pablo Díez + * @author Jan Sorgalla + * + * @api + */ +class TwitterBootstrapView implements ViewInterface +{ + /** + * {@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', + ), $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']; + } else { + $url = $routeGenerator($pagerfanta->getPreviousPage()); + } + + $pages[] = sprintf('
  • %s
  • ', $class, $url, $options['prev_message']); + + + // first + if ($startPage > 1) { + $pages[] = sprintf('
  • %s
  • ', $routeGenerator(1), 1); + if (3 == $startPage) { + $pages[] = sprintf('
  • %s
  • ', $routeGenerator(2), 2); + } elseif (2 != $startPage) { + $pages[] = sprintf('
  • %s
  • ', $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']); + } + + $pages[] = sprintf('%s', $class, $routeGenerator($page), $page); + } + + // last + if ($pagerfanta->getNbPages() > $endPage) { + if ($pagerfanta->getNbPages() > ($endPage + 1)) { + if ($pagerfanta->getNbPages() > ($endPage + 2)) { + $pages[] = sprintf('
  • %s
  • ', $options['css_dots_class'], $options['dots_href'], $options['dots_message']); + } else { + $pages[] = sprintf('
  • %s
  • ', $routeGenerator($endPage + 1), $endPage + 1); + } + } + + $pages[] = sprintf('
  • %s
  • ', $routeGenerator($pagerfanta->getNbPages()), $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()); + } + + $pages[] = sprintf('
  • %s
  • ', $class, $url, $options['next_message']); + + return sprintf('
      %s
    ', $options['css_container_class'], implode('', $pages)); + } + + /** + * {@inheritdoc} + */ + public function getName() + { + return 'twitter_bootstrap'; + } +}