pagination.php (2757B)
1 <?php 2 /** 3 * @package OpenCart 4 * @author Daniel Kerr 5 * @copyright Copyright (c) 2005 - 2017, OpenCart, Ltd. (https://www.opencart.com/) 6 * @license https://opensource.org/licenses/GPL-3.0 7 * @link https://www.opencart.com 8 */ 9 10 /** 11 * Pagination class 12 */ 13 class Pagination { 14 public $total = 0; 15 public $page = 1; 16 public $limit = 20; 17 public $num_links = 8; 18 public $url = ''; 19 public $text_first = '|<'; 20 public $text_last = '>|'; 21 public $text_next = '>'; 22 public $text_prev = '<'; 23 24 /** 25 * 26 * 27 * @return text 28 */ 29 public function render() { 30 $total = $this->total; 31 32 if ($this->page < 1) { 33 $page = 1; 34 } else { 35 $page = $this->page; 36 } 37 38 if (!(int)$this->limit) { 39 $limit = 10; 40 } else { 41 $limit = $this->limit; 42 } 43 44 $num_links = $this->num_links; 45 $num_pages = ceil($total / $limit); 46 47 $this->url = str_replace('%7Bpage%7D', '{page}', $this->url); 48 49 $output = '<ul class="pagination">'; 50 51 if ($page > 1) { 52 $output .= '<li><a href="' . str_replace(array('&page={page}', '?page={page}', '&page={page}'), '', $this->url) . '">' . $this->text_first . '</a></li>'; 53 54 if ($page - 1 === 1) { 55 $output .= '<li><a href="' . str_replace(array('&page={page}', '?page={page}', '&page={page}'), '', $this->url) . '">' . $this->text_prev . '</a></li>'; 56 } else { 57 $output .= '<li><a href="' . str_replace('{page}', $page - 1, $this->url) . '">' . $this->text_prev . '</a></li>'; 58 } 59 } 60 61 if ($num_pages > 1) { 62 if ($num_pages <= $num_links) { 63 $start = 1; 64 $end = $num_pages; 65 } else { 66 $start = $page - floor($num_links / 2); 67 $end = $page + floor($num_links / 2); 68 69 if ($start < 1) { 70 $end += abs($start) + 1; 71 $start = 1; 72 } 73 74 if ($end > $num_pages) { 75 $start -= ($end - $num_pages); 76 $end = $num_pages; 77 } 78 } 79 80 for ($i = $start; $i <= $end; $i++) { 81 if ($page == $i) { 82 $output .= '<li class="active"><span>' . $i . '</span></li>'; 83 } else { 84 if ($i === 1) { 85 $output .= '<li><a href="' . str_replace(array('&page={page}', '?page={page}', '&page={page}'), '', $this->url) . '">' . $i . '</a></li>'; 86 } else { 87 $output .= '<li><a href="' . str_replace('{page}', $i, $this->url) . '">' . $i . '</a></li>'; 88 } 89 } 90 } 91 } 92 93 if ($page < $num_pages) { 94 $output .= '<li><a href="' . str_replace('{page}', $page + 1, $this->url) . '">' . $this->text_next . '</a></li>'; 95 $output .= '<li><a href="' . str_replace('{page}', $num_pages, $this->url) . '">' . $this->text_last . '</a></li>'; 96 } 97 98 $output .= '</ul>'; 99 100 if ($num_pages > 1) { 101 return $output; 102 } else { 103 return ''; 104 } 105 } 106 }