Paginator.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. if (!isset($config['raw_sql'])) {
  18. $this->n_rows = DB::selectCount("select count(*) from {$config['table_name']} where {$config['cond']}");
  19. }
  20. else {
  21. $this->table = DB::selectAll($config['raw_sql']);
  22. $this->n_rows = count($this->table);
  23. }
  24. # 这里有一个潜在 bug,晚点修。
  25. $this->page_len = isset($config['page_len']) ? $config['page_len'] : 10;
  26. $this->n_pages = max((int)ceil($this->n_rows / $this->page_len), 1);
  27. $this->cur_page = validateUInt($_GET['page']) ? (int)$_GET['page'] : 1;
  28. if ($this->cur_page < 1) {
  29. $this->cur_page = 1;
  30. } elseif ($this->cur_page > $this->n_pages) {
  31. $this->cur_page = $this->n_pages;
  32. }
  33. $this->cur_start = ($this->cur_page - 1) * $this->page_len;
  34. if (!isset($config['raw_sql'])) {
  35. $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}");
  36. }
  37. else {
  38. $this->table = DB::selectAll($config['raw_sql']);
  39. $this->n_rows = count($this->table);
  40. }
  41. } else {
  42. $this->n_pages = 1;
  43. $this->cur_page = 1;
  44. $this->cur_start = ($this->cur_page - 1) * $this->page_len;
  45. if (isset($config['raw_sql']))
  46. $this->table = DB::selectAll("select ".join($config['col_names'], ',')." from {$config['table_name']} where {$config['cond']} {$config['tail']}");
  47. else
  48. $this->table = DB::selectAll($config['raw_sql']);
  49. }
  50. $this->max_extend = isset($config['max_extend']) ? (int)$config['max_extend'] : 5;
  51. }
  52. public function getPageRawUri($page) {
  53. $path = strtok($_SERVER["REQUEST_URI"], '?');
  54. $query_string = strtok('?');
  55. parse_str($query_string, $param);
  56. $param['page'] = $page;
  57. if ($page == 1) {
  58. unset($param['page']);
  59. }
  60. if ($param) {
  61. return $path . '?' . http_build_query($param);
  62. } else {
  63. return $path;
  64. }
  65. }
  66. public function getPageUri($page) {
  67. return HTML::escape($this->getPageRawUri($page));
  68. }
  69. public function get() {
  70. $cur_idx = $this->cur_start + 1;
  71. foreach ($this->table as $idx => $row) {
  72. yield $cur_idx++ => $row;
  73. }
  74. }
  75. public function isEmpty() {
  76. return empty($this->table);
  77. }
  78. public function pagination() {
  79. if ($this->n_pages == 1) {
  80. return '';
  81. }
  82. $html = '<ul class="pagination top-buffer-no bot-buffer-sm justify-content-center">';
  83. if ($this->cur_page > 1) {
  84. $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>';
  85. } else {
  86. $html .= '<li class="page-item disabled"><a class="page-link"><span class="glyphicon glyphicon glyphicon-backward"></span></a></li>';
  87. }
  88. for ($i = max($this->cur_page - $this->max_extend, 1); $i <= min($this->cur_page + $this->max_extend, $this->n_pages); $i++) {
  89. if ($i == $this->cur_page) {
  90. $html .= '<li class="page-item active"><a class="page-link" href="'.$this->getPageUri($i).'">'.$i.'</a></li>';
  91. } else {
  92. $html .= '<li class="page-item"><a class="page-link" href="'.$this->getPageUri($i).'">'.$i.'</a></li>';
  93. }
  94. }
  95. if ($this->cur_page < $this->n_pages) {
  96. $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>';
  97. } else {
  98. $html .= '<li class="page-item disabled"><a class="page-link"><span class="glyphicon glyphicon glyphicon-forward"></span></a></li>';
  99. }
  100. $html .= '</ul>';
  101. return $html;
  102. }
  103. }