diff options
author | Nicolas Lœuillet <nicolas.loeuillet@gmail.com> | 2013-08-08 09:36:10 -0700 |
---|---|---|
committer | Nicolas Lœuillet <nicolas.loeuillet@gmail.com> | 2013-08-08 09:36:10 -0700 |
commit | 9a8b4ff4edf84d7df60de1b6fd1e493b59f88273 (patch) | |
tree | 3c8ab8086fd8a2750270f8aeaee1f1ce016167cb /inc/3rdparty/paginator.php | |
parent | 85ebc80c7eaf88e4d57a52adb8e4c32d8cc34b64 (diff) | |
parent | 572e758bf2e76308a3fa3eda9a8d9e9be8b53ecc (diff) | |
download | wallabag-9a8b4ff4edf84d7df60de1b6fd1e493b59f88273.tar.gz wallabag-9a8b4ff4edf84d7df60de1b6fd1e493b59f88273.tar.zst wallabag-9a8b4ff4edf84d7df60de1b6fd1e493b59f88273.zip |
Merge pull request #109 from inthepoche/dev
merge dev into master
Diffstat (limited to 'inc/3rdparty/paginator.php')
-rw-r--r-- | inc/3rdparty/paginator.php | 202 |
1 files changed, 202 insertions, 0 deletions
diff --git a/inc/3rdparty/paginator.php b/inc/3rdparty/paginator.php new file mode 100644 index 00000000..306756c0 --- /dev/null +++ b/inc/3rdparty/paginator.php | |||
@@ -0,0 +1,202 @@ | |||
1 | <?php | ||
2 | /* | ||
3 | * PHP Pagination Class | ||
4 | * | ||
5 | * @author David Carr - dave@daveismyname.com - http://www.daveismyname.com | ||
6 | * @version 1.0 | ||
7 | * @date October 20, 2013 | ||
8 | */ | ||
9 | class Paginator{ | ||
10 | |||
11 | /** | ||
12 | * set the number of items per page. | ||
13 | * | ||
14 | * @var numeric | ||
15 | */ | ||
16 | private $_perPage; | ||
17 | |||
18 | /** | ||
19 | * set get parameter for fetching the page number | ||
20 | * | ||
21 | * @var string | ||
22 | */ | ||
23 | private $_instance; | ||
24 | |||
25 | /** | ||
26 | * sets the page number. | ||
27 | * | ||
28 | * @var numeric | ||
29 | */ | ||
30 | private $_page; | ||
31 | |||
32 | /** | ||
33 | * set the limit for the data source | ||
34 | * | ||
35 | * @var string | ||
36 | */ | ||
37 | private $_limit; | ||
38 | |||
39 | /** | ||
40 | * set the total number of records/items. | ||
41 | * | ||
42 | * @var numeric | ||
43 | */ | ||
44 | private $_totalRows = 0; | ||
45 | |||
46 | |||
47 | |||
48 | /** | ||
49 | * __construct | ||
50 | * | ||
51 | * pass values when class is istantiated | ||
52 | * | ||
53 | * @param numeric $_perPage sets the number of iteems per page | ||
54 | * @param numeric $_instance sets the instance for the GET parameter | ||
55 | */ | ||
56 | public function __construct($perPage,$instance){ | ||
57 | $this->_instance = $instance; | ||
58 | $this->_perPage = $perPage; | ||
59 | $this->set_instance(); | ||
60 | } | ||
61 | |||
62 | /** | ||
63 | * get_start | ||
64 | * | ||
65 | * creates the starting point for limiting the dataset | ||
66 | * @return numeric | ||
67 | */ | ||
68 | private function get_start(){ | ||
69 | return ($this->_page * $this->_perPage) - $this->_perPage; | ||
70 | } | ||
71 | |||
72 | /** | ||
73 | * set_instance | ||
74 | * | ||
75 | * sets the instance parameter, if numeric value is 0 then set to 1 | ||
76 | * | ||
77 | * @var numeric | ||
78 | */ | ||
79 | private function set_instance(){ | ||
80 | $this->_page = (int) (!isset($_GET[$this->_instance]) ? 1 : $_GET[$this->_instance]); | ||
81 | $this->_page = ($this->_page == 0 ? 1 : $this->_page); | ||
82 | } | ||
83 | |||
84 | /** | ||
85 | * set_total | ||
86 | * | ||
87 | * collect a numberic value and assigns it to the totalRows | ||
88 | * | ||
89 | * @var numeric | ||
90 | */ | ||
91 | public function set_total($_totalRows){ | ||
92 | $this->_totalRows = $_totalRows; | ||
93 | } | ||
94 | |||
95 | /** | ||
96 | * get_limit | ||
97 | * | ||
98 | * returns the limit for the data source, calling the get_start method and passing in the number of items perp page | ||
99 | * | ||
100 | * @return string | ||
101 | */ | ||
102 | public function get_limit(){ | ||
103 | if (STORAGE == 'postgres') { | ||
104 | return "LIMIT ".$this->_perPage." OFFSET ".$this->get_start(); | ||
105 | } else { | ||
106 | return "LIMIT ".$this->get_start().",".$this->_perPage; | ||
107 | } | ||
108 | } | ||
109 | |||
110 | /** | ||
111 | * page_links | ||
112 | * | ||
113 | * create the html links for navigating through the dataset | ||
114 | * | ||
115 | * @var sting $path optionally set the path for the link | ||
116 | * @var sting $ext optionally pass in extra parameters to the GET | ||
117 | * @return string returns the html menu | ||
118 | */ | ||
119 | public function page_links($path='?',$ext=null) | ||
120 | { | ||
121 | $adjacents = "2"; | ||
122 | $prev = $this->_page - 1; | ||
123 | $next = $this->_page + 1; | ||
124 | $lastpage = ceil($this->_totalRows/$this->_perPage); | ||
125 | $lpm1 = $lastpage - 1; | ||
126 | |||
127 | $pagination = ""; | ||
128 | if($lastpage > 1) | ||
129 | { | ||
130 | $pagination .= "<div class='pagination'>"; | ||
131 | if ($this->_page > 1) | ||
132 | $pagination.= "<a href='".$path."$this->_instance=$prev"."$ext'>« previous</a>"; | ||
133 | else | ||
134 | $pagination.= "<span class='disabled'>« previous</span>"; | ||
135 | |||
136 | if ($lastpage < 7 + ($adjacents * 2)) | ||
137 | { | ||
138 | for ($counter = 1; $counter <= $lastpage; $counter++) | ||
139 | { | ||
140 | if ($counter == $this->_page) | ||
141 | $pagination.= "<span class='current'>$counter</span>"; | ||
142 | else | ||
143 | $pagination.= "<a href='".$path."$this->_instance=$counter"."$ext'>$counter</a>"; | ||
144 | } | ||
145 | } | ||
146 | elseif($lastpage > 5 + ($adjacents * 2)) | ||
147 | { | ||
148 | if($this->_page < 1 + ($adjacents * 2)) | ||
149 | { | ||
150 | for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) | ||
151 | { | ||
152 | if ($counter == $this->_page) | ||
153 | $pagination.= "<span class='current'>$counter</span>"; | ||
154 | else | ||
155 | $pagination.= "<a href='".$path."$this->_instance=$counter"."$ext'>$counter</a>"; | ||
156 | } | ||
157 | $pagination.= "..."; | ||
158 | $pagination.= "<a href='".$path."$this->_instance=$lpm1"."$ext'>$lpm1</a>"; | ||
159 | $pagination.= "<a href='".$path."$this->_instance=$lastpage"."$ext'>$lastpage</a>"; | ||
160 | } | ||
161 | elseif($lastpage - ($adjacents * 2) > $this->_page && $this->_page > ($adjacents * 2)) | ||
162 | { | ||
163 | $pagination.= "<a href='".$path."$this->_instance=1"."$ext'>1</a>"; | ||
164 | $pagination.= "<a href='".$path."$this->_instance=2"."$ext'>2</a>"; | ||
165 | $pagination.= "..."; | ||
166 | for ($counter = $this->_page - $adjacents; $counter <= $this->_page + $adjacents; $counter++) | ||
167 | { | ||
168 | if ($counter == $this->_page) | ||
169 | $pagination.= "<span class='current'>$counter</span>"; | ||
170 | else | ||
171 | $pagination.= "<a href='".$path."$this->_instance=$counter"."$ext'>$counter</a>"; | ||
172 | } | ||
173 | $pagination.= ".."; | ||
174 | $pagination.= "<a href='".$path."$this->_instance=$lpm1"."$ext'>$lpm1</a>"; | ||
175 | $pagination.= "<a href='".$path."$this->_instance=$lastpage"."$ext'>$lastpage</a>"; | ||
176 | } | ||
177 | else | ||
178 | { | ||
179 | $pagination.= "<a href='".$path."$this->_instance=1"."$ext'>1</a>"; | ||
180 | $pagination.= "<a href='".$path."$this->_instance=2"."$ext'>2</a>"; | ||
181 | $pagination.= ".."; | ||
182 | for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) | ||
183 | { | ||
184 | if ($counter == $this->_page) | ||
185 | $pagination.= "<span class='current'>$counter</span>"; | ||
186 | else | ||
187 | $pagination.= "<a href='".$path."$this->_instance=$counter"."$ext'>$counter</a>"; | ||
188 | } | ||
189 | } | ||
190 | } | ||
191 | |||
192 | if ($this->_page < $counter - 1) | ||
193 | $pagination.= "<a href='".$path."$this->_instance=$next"."$ext'>next »</a>"; | ||
194 | else | ||
195 | $pagination.= "<span class='disabled'>next »</span>"; | ||
196 | $pagination.= "</div>\n"; | ||
197 | } | ||
198 | |||
199 | |||
200 | return $pagination; | ||
201 | } | ||
202 | } | ||