Nickbur /
Sunrise-CMS
| 1 | <?php |
||
| 2 | /* Divine CMS - Open source CMS for widespread use. |
||
| 3 | Copyright (c) 2019 Mykola Burakov ([email protected]) |
||
| 4 | |||
| 5 | See SOURCE.txt for other and additional information. |
||
| 6 | |||
| 7 | This file is part of Divine CMS. |
||
| 8 | |||
| 9 | This program is free software: you can redistribute it and/or modify |
||
| 10 | it under the terms of the GNU General Public License as published by |
||
| 11 | the Free Software Foundation, either version 3 of the License, or |
||
| 12 | (at your option) any later version. |
||
| 13 | |||
| 14 | This program is distributed in the hope that it will be useful, |
||
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 17 | GNU General Public License for more details. |
||
| 18 | |||
| 19 | You should have received a copy of the GNU General Public License |
||
| 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
||
| 21 | |||
| 22 | namespace Divine\Engine\Library; |
||
| 23 | |||
| 24 | class Pagination |
||
| 25 | { |
||
| 26 | public $total = 0; |
||
| 27 | public $page = 1; |
||
| 28 | public $limit = 20; |
||
| 29 | public $num_links = 8; |
||
| 30 | public $url = ''; |
||
| 31 | public $text_first = '<span uk-icon="chevron-double-left"></span>'; |
||
| 32 | public $text_last = '<span uk-icon="chevron-double-right"></span>'; |
||
| 33 | |||
| 34 | public function render() |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 35 | { |
||
| 36 | $total = $this->total; |
||
| 37 | |||
| 38 | if ($this->page < 1) { |
||
| 39 | $page = 1; |
||
| 40 | } else { |
||
| 41 | $page = $this->page; |
||
| 42 | } |
||
| 43 | |||
| 44 | if (!(int) $this->limit) { |
||
| 45 | $limit = 10; |
||
| 46 | } else { |
||
| 47 | $limit = $this->limit; |
||
| 48 | } |
||
| 49 | |||
| 50 | $num_links = $this->num_links; |
||
| 51 | $num_pages = ceil($total / $limit); |
||
| 52 | |||
| 53 | $this->url = str_replace('%7Bpage%7D', '{page}', $this->url); |
||
| 54 | |||
| 55 | $output = '<ul class="uk-pagination uk-flex-center uk-padding-small uk-padding-remove-bottom">'; |
||
| 56 | |||
| 57 | if ($page > 1) { |
||
| 58 | $output .= '<li><a href="' . str_replace(array('&page={page}', '&page={page}', '?page={page}'), '', $this->url) . '">' . $this->text_first . '</a></li>'; |
||
| 59 | |||
| 60 | if ($page - 1 === 1) { |
||
| 61 | $output .= '<li><a href="' . str_replace(array('&page={page}', '&page={page}', '?page={page}'), '', $this->url) . '"><span uk-pagination-previous></span></a></li>'; |
||
| 62 | } else { |
||
| 63 | $output .= '<li><a href="' . str_replace('{page}', $page - 1, $this->url) . '"><span uk-pagination-previous></span></a></li>'; |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | if ($num_pages > 1) { |
||
| 68 | if ($num_pages <= $num_links) { |
||
| 69 | $start = 1; |
||
| 70 | $end = $num_pages; |
||
| 71 | } else { |
||
| 72 | $start = $page - floor($num_links / 2); |
||
| 73 | $end = $page + floor($num_links / 2); |
||
| 74 | |||
| 75 | if ($start < 1) { |
||
| 76 | $end += abs($start) + 1; |
||
| 77 | $start = 1; |
||
| 78 | } |
||
| 79 | |||
| 80 | if ($end > $num_pages) { |
||
| 81 | $start -= ($end - $num_pages); |
||
| 82 | $end = $num_pages; |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | for ($i = $start; $i <= $end; $i++) { |
||
| 87 | if ($page == $i) { |
||
| 88 | $output .= '<li class="uk-active"><span>' . $i . '</span></li>'; |
||
| 89 | } else { |
||
| 90 | if ($i === 1) { |
||
| 91 | $output .= '<li><a href="' . str_replace(array('&page={page}', '&page={page}', '?page={page}'), '', $this->url) . '">' . $i . '</a></li>'; |
||
| 92 | } else { |
||
| 93 | $output .= '<li><a href="' . str_replace('{page}', $i, $this->url) . '">' . $i . '</a></li>'; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | if ($page < $num_pages) { |
||
| 100 | $output .= '<li><a href="' . str_replace('{page}', $page + 1, $this->url) . '"><span uk-pagination-next></span></a></li>'; |
||
| 101 | $output .= '<li><a href="' . str_replace('{page}', $num_pages, $this->url) . '">' . $this->text_last . '</a></li>'; |
||
| 102 | } |
||
| 103 | |||
| 104 | $output .= '</ul>'; |
||
| 105 | |||
| 106 | if ($num_pages > 1) { |
||
| 107 | return $output; |
||
| 108 | } else { |
||
| 109 | return ''; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | } |
||
| 113 |