|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of the Pagerfanta package. |
|
5 * |
|
6 * (c) Pablo Díez <pablodip@gmail.com> |
|
7 * |
|
8 * For the full copyright and license information, please view the LICENSE |
|
9 * file that was distributed with this source code. |
|
10 */ |
|
11 |
|
12 namespace Pagerfanta\View; |
|
13 |
|
14 use Pagerfanta\PagerfantaInterface; |
|
15 |
|
16 /** |
|
17 * TwitterBootstrapView. |
|
18 * |
|
19 * View that can be used with the pagination module |
|
20 * from the Twitter Bootstrap CSS Toolkit |
|
21 * http://twitter.github.com/bootstrap/ |
|
22 * |
|
23 * @author Pablo Díez <pablodip@gmail.com> |
|
24 * @author Jan Sorgalla <jsorgalla@gmail.com> |
|
25 * |
|
26 * @api |
|
27 */ |
|
28 class TwitterBootstrapView implements ViewInterface |
|
29 { |
|
30 /** |
|
31 * {@inheritdoc} |
|
32 */ |
|
33 public function render(PagerfantaInterface $pagerfanta, $routeGenerator, array $options = array()) |
|
34 { |
|
35 $options = array_merge(array( |
|
36 'proximity' => 3, |
|
37 'prev_message' => '← Previous', |
|
38 'prev_disabled_href' => '', |
|
39 'next_message' => 'Next →', |
|
40 'next_disabled_href' => '', |
|
41 'dots_message' => '…', |
|
42 'dots_href' => '', |
|
43 'css_container_class' => 'pagination', |
|
44 'css_prev_class' => 'prev', |
|
45 'css_next_class' => 'next', |
|
46 'css_disabled_class' => 'disabled', |
|
47 'css_dots_class' => 'disabled', |
|
48 'css_active_class' => 'active', |
|
49 ), $options); |
|
50 |
|
51 $currentPage = $pagerfanta->getCurrentPage(); |
|
52 |
|
53 $startPage = $currentPage - $options['proximity']; |
|
54 $endPage = $currentPage + $options['proximity']; |
|
55 |
|
56 if ($startPage < 1) { |
|
57 $endPage = min($endPage + (1 - $startPage), $pagerfanta->getNbPages()); |
|
58 $startPage = 1; |
|
59 } |
|
60 if ($endPage > $pagerfanta->getNbPages()) { |
|
61 $startPage = max($startPage - ($endPage - $pagerfanta->getNbPages()), 1); |
|
62 $endPage = $pagerfanta->getNbPages(); |
|
63 } |
|
64 |
|
65 $pages = array(); |
|
66 |
|
67 // previous |
|
68 $class = $options['css_prev_class']; |
|
69 $url = $options['prev_disabled_href']; |
|
70 if (!$pagerfanta->hasPreviousPage()) { |
|
71 $class .= ' '.$options['css_disabled_class']; |
|
72 } else { |
|
73 $url = $routeGenerator($pagerfanta->getPreviousPage()); |
|
74 } |
|
75 |
|
76 $pages[] = sprintf('<li class="%s"><a href="%s">%s</a></li>', $class, $url, $options['prev_message']); |
|
77 |
|
78 |
|
79 // first |
|
80 if ($startPage > 1) { |
|
81 $pages[] = sprintf('<li><a href="%s">%s</a></li>', $routeGenerator(1), 1); |
|
82 if (3 == $startPage) { |
|
83 $pages[] = sprintf('<li><a href="%s">%s</a></li>', $routeGenerator(2), 2); |
|
84 } elseif (2 != $startPage) { |
|
85 $pages[] = sprintf('<li class="%s"><a href="%s">%s</a></li>', $options['css_dots_class'], $options['dots_href'], $options['dots_message']); |
|
86 } |
|
87 } |
|
88 |
|
89 // pages |
|
90 for ($page = $startPage; $page <= $endPage; $page++) { |
|
91 $class = ''; |
|
92 if ($page == $currentPage) { |
|
93 $class = sprintf(' class="%s"', $options['css_active_class']); |
|
94 } |
|
95 |
|
96 $pages[] = sprintf('<li%s><a href="%s">%s</a></li>', $class, $routeGenerator($page), $page); |
|
97 } |
|
98 |
|
99 // last |
|
100 if ($pagerfanta->getNbPages() > $endPage) { |
|
101 if ($pagerfanta->getNbPages() > ($endPage + 1)) { |
|
102 if ($pagerfanta->getNbPages() > ($endPage + 2)) { |
|
103 $pages[] = sprintf('<li class="%s"><a href="%s">%s</a></li>', $options['css_dots_class'], $options['dots_href'], $options['dots_message']); |
|
104 } else { |
|
105 $pages[] = sprintf('<li><a href="%s">%s</a></li>', $routeGenerator($endPage + 1), $endPage + 1); |
|
106 } |
|
107 } |
|
108 |
|
109 $pages[] = sprintf('<li><a href="%s">%s</a></li>', $routeGenerator($pagerfanta->getNbPages()), $pagerfanta->getNbPages()); |
|
110 } |
|
111 |
|
112 // next |
|
113 $class = $options['css_next_class']; |
|
114 $url = $options['next_disabled_href']; |
|
115 if (!$pagerfanta->hasNextPage()) { |
|
116 $class .= ' '.$options['css_disabled_class']; |
|
117 } else { |
|
118 $url = $routeGenerator($pagerfanta->getNextPage()); |
|
119 } |
|
120 |
|
121 $pages[] = sprintf('<li class="%s"><a href="%s">%s</a></li>', $class, $url, $options['next_message']); |
|
122 |
|
123 return sprintf('<div class="%s"><ul>%s</ul></div>', $options['css_container_class'], implode('', $pages)); |
|
124 } |
|
125 |
|
126 /** |
|
127 * {@inheritdoc} |
|
128 */ |
|
129 public function getName() |
|
130 { |
|
131 return 'twitter_bootstrap'; |
|
132 } |
|
133 } |