Clean up, refactor history.php

This commit is contained in:
ruan 2018-06-02 10:03:34 +02:00
parent b91def3b38
commit ae8f2c574f
No known key found for this signature in database
GPG Key ID: 0D2EC1C52E469C0B

View File

@ -18,11 +18,10 @@ class History {
$limit = $page->settings->limit_per_page; $limit = $page->settings->limit_per_page;
// $offset = History::get_offset($type);
$order = "DESC";
if ($after > 0) { if ($after > 0) {
$order = "ASC"; $order = "ASC";
} else {
$order = "DESC";
} }
$st = $page->conn->prepare("SELECT $sel FROM $table WHERE $field=:uuid AND time > :after AND time < :before ORDER BY time $order LIMIT :limit"); $st = $page->conn->prepare("SELECT $sel FROM $table WHERE $field=:uuid AND time > :after AND time < :before ORDER BY time $order LIMIT :limit");
$st->bindParam(":uuid", $uuid, PDO::PARAM_STR); $st->bindParam(":uuid", $uuid, PDO::PARAM_STR);
@ -30,8 +29,6 @@ class History {
$st->bindParam(":before", $before, PDO::PARAM_INT); $st->bindParam(":before", $before, PDO::PARAM_INT);
$st->bindParam(":after", $after, PDO::PARAM_INT); $st->bindParam(":after", $after, PDO::PARAM_INT);
// $st->bindParam(":offset", $offset, PDO::PARAM_INT);
if ($st->execute()) { if ($st->execute()) {
while ($row = $st->fetch(PDO::FETCH_ASSOC)) { while ($row = $st->fetch(PDO::FETCH_ASSOC)) {
$row['__table__'] = $type; $row['__table__'] = $type;
@ -42,12 +39,12 @@ class History {
} }
/** /**
* usort() function for rows in the database * usort() function for rows in the database, descending order (latest first)
* @param PDORow $a * @param PDORow $a
* @param PDORow $b * @param PDORow $b
* @return int * @return int
*/ */
static function cmp_row_date($a, $b) { static function cmp_row_date_desc($a, $b) {
$a = $a['time']; $a = $a['time'];
$b = $b['time']; $b = $b['time'];
if ($a === $b) { if ($a === $b) {
@ -56,7 +53,13 @@ class History {
return ($a < $b) ? 1 : -1; return ($a < $b) ? 1 : -1;
} }
static function cmp_row_date_reverse($a, $b) { /**
* usort() function for rows in the database, ascending order (oldest first)
* @param PDORow $a
* @param PDORow $b
* @return int
*/
static function cmp_row_date_asc($a, $b) {
$a = $a['time']; $a = $a['time'];
$b = $b['time']; $b = $b['time'];
if ($a === $b) { if ($a === $b) {
@ -64,16 +67,6 @@ class History {
} }
return ($a > $b) ? 1 : -1; return ($a > $b) ? 1 : -1;
} }
// static function get_offset($table) {
// $v = $table[0];
// if (isset($_GET[$v]) && is_string($_GET[$v])) {
// if (filter_var($_GET[$v], FILTER_VALIDATE_INT)) {
// return (int)$_GET[$v];
// }
// }
// return 0;
// }
} }
$page = new Page("history"); $page = new Page("history");
@ -163,38 +156,26 @@ try {
$limit = $page->settings->limit_per_page; $limit = $page->settings->limit_per_page;
if ($after > 0) { if ($after > 0) {
usort($all, array("History", "cmp_row_date_reverse")); usort($all, array("History", "cmp_row_date_asc"));
// trim all entries beyond shown, then proper sort. // trim all entries beyond shown, then proper sort.
$new_all = array(); // trim must be done in ascending order (oldest first), otherwise semantics change
$trim = array();
$i = 0; $i = 0;
foreach ($all as $row) { foreach ($all as $row) {
$i++; $i++;
if ($i > $limit) break; if ($i > $limit) break;
array_push($new_all, $row); array_push($trim, $row);
} }
$all = $new_all; $all = $trim;
} }
usort($all, array("History", "cmp_row_date")); usort($all, array("History", "cmp_row_date_desc"));
if (!empty($all)) { if (!empty($all)) {
$page->table_begin(); $page->table_begin();
/*$offset = 0;
if ($page->settings->show_pager) {
$current_page = $page->page - 1;
$offset = ($limit * $current_page);
$limit += $offset;
}*/
$i = 0; $i = 0;
foreach ($all as $row) { foreach ($all as $row) {
$i++; $i++;
/*if ($page->settings->show_pager && $i < $offset) {
continue;
}*/
if ($i > $limit) break; if ($i > $limit) break;
@ -227,6 +208,7 @@ try {
} }
$page->table_end(); $page->table_end();
// print pager // print pager
if ($page->settings->show_pager) { if ($page->settings->show_pager) {
$page->name = "history"; $page->name = "history";