Paginator.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. class Paginator {
  3. public $n_rows;
  4. public $n_pages;
  5. public $page_len;
  6. public $cur_page;
  7. public $cur_start;
  8. public $max_extend;
  9. public $table;
  10. public function __construct($config) {
  11. if (isset($config['data'])) {
  12. $this->n_pages = 1;
  13. $this->cur_page = 1;
  14. $this->cur_start = 0;
  15. $this->table = $config['data'];
  16. } elseif (!isset($config['echo_full'])) {
  17. $this->n_rows = DB::selectCount("select count(*) from {$config['table_name']} where {$config['cond']}");
  18. $this->page_len = isset($config['page_len']) ? $config['page_len'] : 10;
  19. $this->n_pages = max((int)ceil($this->n_rows / $this->page_len), 1);
  20. $this->cur_page = validateUInt($_GET['page']) ? (int)$_GET['page'] : 1;
  21. if ($this->cur_page < 1) {
  22. $this->cur_page = 1;
  23. } elseif ($this->cur_page > $this->n_pages) {
  24. $this->cur_page = $this->n_pages;
  25. }
  26. $this->cur_start = ($this->cur_page - 1) * $this->page_len;
  27. $this->table = DB::selectAll("select ".join($config['col_names'], ',')." from {$config['table_name']} where {$config['cond']} {$config['tail']} limit {$this->cur_start}, {$this->page_len}");
  28. } else {
  29. $this->n_pages = 1;
  30. $this->cur_page = 1;
  31. $this->cur_start = ($this->cur_page - 1) * $this->page_len;
  32. $this->table = DB::selectAll("select ".join($config['col_names'], ',')." from {$config['table_name']} where {$config['cond']} {$config['tail']}");
  33. }
  34. $this->max_extend = isset($config['max_extend']) ? (int)$config['max_extend'] : 5;
  35. }
  36. public function getPageRawUri($page) {
  37. $path = strtok($_SERVER["REQUEST_URI"], '?');
  38. $query_string = strtok('?');
  39. parse_str($query_string, $param);
  40. $param['page'] = $page;
  41. if ($page == 1) {
  42. unset($param['page']);
  43. }
  44. if ($param) {
  45. return $path . '?' . http_build_query($param);
  46. } else {
  47. return $path;
  48. }
  49. }
  50. public function getPageUri($page) {
  51. return HTML::escape($this->getPageRawUri($page));
  52. }
  53. public function get() {
  54. $cur_idx = $this->cur_start + 1;
  55. foreach ($this->table as $idx => $row) {
  56. yield $cur_idx++ => $row;
  57. }
  58. }
  59. public function isEmpty() {
  60. return empty($this->table);
  61. }
  62. public function pagination() {
  63. if ($this->n_pages == 1) {
  64. return '';
  65. }
  66. $html = '<ul class="pagination top-buffer-no bot-buffer-sm justify-content-center">';
  67. if ($this->cur_page > 1) {
  68. $html .= '<li class="page-item"><a class="page-link" href="'.$this->getPageUri($this->cur_page - 1).'"><span class="glyphicon glyphicon glyphicon-backward"></span></a></li>';
  69. } else {
  70. $html .= '<li class="page-item disabled"><a class="page-link"><span class="glyphicon glyphicon glyphicon-backward"></span></a></li>';
  71. }
  72. for ($i = max($this->cur_page - $this->max_extend, 1); $i <= min($this->cur_page + $this->max_extend, $this->n_pages); $i++) {
  73. if ($i == $this->cur_page) {
  74. $html .= '<li class="page-item active"><a class="page-link" href="'.$this->getPageUri($i).'">'.$i.'</a></li>';
  75. } else {
  76. $html .= '<li class="page-item"><a class="page-link" href="'.$this->getPageUri($i).'">'.$i.'</a></li>';
  77. }
  78. }
  79. if ($this->cur_page < $this->n_pages) {
  80. $html .= '<li class="page-item"><a class="page-link" href="'.$this->getPageUri($this->cur_page + 1).'"><span class="glyphicon glyphicon glyphicon-forward"></span></a></li>';
  81. } else {
  82. $html .= '<li class="page-item disabled"><a class="page-link"><span class="glyphicon glyphicon glyphicon-forward"></span></a></li>';
  83. }
  84. $html .= '</ul>';
  85. return $html;
  86. }
  87. }