S2OJ/web/app/models/Paginator.php

164 lines
5.3 KiB
PHP
Raw Normal View History

2016-07-18 16:39:37 +00:00
<?php
class Paginator {
public $n_rows;
public $n_pages;
public $page_len;
public $cur_page;
public $cur_start;
public $max_extend;
public $table;
public function __construct($config) {
if (isset($config['data'])) {
$this->n_pages = 1;
$this->cur_page = 1;
$this->cur_start = 0;
$this->table = $config['data'];
} elseif (!isset($config['echo_full'])) {
$table = $config['table_name'];
if (isset($config['pagination_table'])) {
$table = $config['pagination_table'];
2022-09-20 09:19:11 +00:00
}
$this->n_rows = DB::selectCount("select count(*) from {$table} where {$config['cond']}");
2016-07-18 16:39:37 +00:00
$this->page_len = isset($config['page_len']) ? $config['page_len'] : 10;
$this->n_pages = max((int)ceil($this->n_rows / $this->page_len), 1);
$this->cur_page = validateUInt($_GET['page']) ? (int)$_GET['page'] : 1;
if ($this->cur_page < 1) {
$this->cur_page = 1;
2020-06-25 12:41:16 +00:00
} elseif ($this->cur_page > $this->n_pages) {
2016-07-18 16:39:37 +00:00
$this->cur_page = $this->n_pages;
}
$this->cur_start = ($this->cur_page - 1) * $this->page_len;
$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}");
} else {
$this->n_pages = 1;
$this->cur_page = 1;
$this->cur_start = ($this->cur_page - 1) * $this->page_len;
$this->table = DB::selectAll("select ".join($config['col_names'], ',')." from {$config['table_name']} where {$config['cond']} {$config['tail']}");
}
$this->max_extend = isset($config['max_extend']) ? (int)$config['max_extend'] : 5;
}
public function getPageRawUri($page) {
$path = strtok($_SERVER["REQUEST_URI"], '?');
$query_string = strtok('?');
parse_str($query_string, $param);
$param['page'] = $page;
2020-06-25 12:41:16 +00:00
if ($page == 1) {
2016-07-18 16:39:37 +00:00
unset($param['page']);
2020-06-25 12:41:16 +00:00
}
2016-07-18 16:39:37 +00:00
if ($param) {
return $path . '?' . http_build_query($param);
} else {
return $path;
}
}
public function getPageUri($page) {
return HTML::escape($this->getPageRawUri($page));
}
public function get() {
$cur_idx = $this->cur_start + 1;
foreach ($this->table as $idx => $row) {
yield $cur_idx++ => $row;
}
}
public function isEmpty() {
return empty($this->table);
}
public function pagination() {
2022-09-24 00:13:39 +00:00
global $REQUIRE_LIB;
2016-07-18 16:39:37 +00:00
if ($this->n_pages == 1) {
return '';
}
$html = '<ul class="pagination top-buffer-no bot-buffer-sm justify-content-center">';
2016-07-18 16:39:37 +00:00
if ($this->cur_page > 1) {
2022-09-24 00:13:39 +00:00
$html .= '<li class="page-item">'
. '<a class="page-link" href="'.$this->getPageUri(1).'">';
if (isset($REQUIRE_LIB['bootstrap5'])) {
$html .= '<i class="bi bi-chevron-double-left"></i>';
} else {
$html .= '<span class="glyphicon glyphicon glyphicon-fast-backward"></span>';
}
$html .= '</a></li>';
$html .= '<li class="page-item">'
. '<a class="page-link" href="'.$this->getPageUri($this->cur_page - 1).'">';
if (isset($REQUIRE_LIB['bootstrap5'])) {
$html .= '<i class="bi bi-chevron-left"></i>';
} else {
$html .= '<span class="glyphicon glyphicon glyphicon-backward"></span>';
}
$html .= '</a></li>';
2016-07-18 16:39:37 +00:00
} else {
2022-09-24 00:13:39 +00:00
$html .= '<li class="page-item disabled"><a class="page-link">';
if (isset($REQUIRE_LIB['bootstrap5'])) {
$html .= '<i class="bi bi-chevron-double-left"></i>';
} else {
$html .= '<span class="glyphicon glyphicon glyphicon-fast-backward"></span>';
}
$html .= '</a></li>';
$html .= '<li class="page-item disabled"><a class="page-link">';
if (isset($REQUIRE_LIB['bootstrap5'])) {
$html .= '<i class="bi bi-chevron-left"></i>';
} else {
$html .= '<span class="glyphicon glyphicon glyphicon-backward"></span>';
}
$html .= '</a></li>';
2016-07-18 16:39:37 +00:00
}
for ($i = max($this->cur_page - $this->max_extend, 1); $i <= min($this->cur_page + $this->max_extend, $this->n_pages); $i++) {
if ($i == $this->cur_page) {
$html .= '<li class="page-item active"><a class="page-link" href="'.$this->getPageUri($i).'">'.$i.'</a></li>';
2016-07-18 16:39:37 +00:00
} else {
$html .= '<li class="page-item"><a class="page-link" href="'.$this->getPageUri($i).'">'.$i.'</a></li>';
2016-07-18 16:39:37 +00:00
}
}
if ($this->cur_page < $this->n_pages) {
2022-09-24 00:13:39 +00:00
$html .= '<li class="page-item">'
. '<a class="page-link" href="'.$this->getPageUri($this->cur_page + 1).'">';
if (isset($REQUIRE_LIB['bootstrap5'])) {
$html .= '<i class="bi bi-chevron-right"></i>';
} else {
$html .= '<span class="glyphicon glyphicon glyphicon-forward"></span>';
}
$html .= '</a></li>';
$html .= '<li class="page-item">'
. '<a class="page-link" href="'.$this->getPageUri($this->n_pages).'">';
if (isset($REQUIRE_LIB['bootstrap5'])) {
$html .= '<i class="bi bi-chevron-double-right"></i>';
} else {
$html .= '<span class="glyphicon glyphicon glyphicon-fast-forward"></span>';
}
$html .= '</a></li>';
2016-07-18 16:39:37 +00:00
} else {
2022-09-24 00:13:39 +00:00
$html .= '<li class="page-item disabled"><a class="page-link">';
if (isset($REQUIRE_LIB['bootstrap5'])) {
$html .= '<i class="bi bi-chevron-right"></i>';
} else {
$html .= '<span class="glyphicon glyphicon glyphicon-forward"></span>';
}
$html .= '</a></li>';
$html .= '<li class="page-item disabled"><a class="page-link">';
if (isset($REQUIRE_LIB['bootstrap5'])) {
$html .= '<i class="bi bi-chevron-double-right"></i>';
} else {
$html .= '<span class="glyphicon glyphicon glyphicon-fast-forward"></span>';
}
$html .= '</a></li>';
2016-07-18 16:39:37 +00:00
}
$html .= '</ul>';
return $html;
}
}