From f9c247854f008f97ad392ef662157f380d3ddbed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20Sajdl?= Date: Fri, 13 Apr 2018 22:38:10 +0200 Subject: [PATCH] Fix #11 - add second part of api --- api/incidents.php | 41 +++++++++++++++++++++++++++++++++++++-- classes/constellation.php | 2 +- classes/incident.php | 8 ++++++-- 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/api/incidents.php b/api/incidents.php index 569e41c..b597062 100644 --- a/api/incidents.php +++ b/api/incidents.php @@ -5,7 +5,44 @@ if (!file_exists("../config.php")) header("Location: ../"); } else{ - require_once("../config.php"); - require_once("../classes/constellation.php"); + require_once("../config.php"); + require_once("../classes/constellation.php"); + if (isset($_GET['future']) && $_GET['future'] == true) + { + $result = get_incidents(true); + }else{ + $result = get_incidents(); + } + + echo json_encode($result); +} + +function get_incidents($future = false){ + global $mysqli; + $c = ($future)?">=":"<="; + $limit = (isset($_GET['limit'])?$_GET['limit']:5); + $offset = (isset($_GET['offset'])?$_GET['offset']:0); + $timestamp = (isset($_GET['timestamp']))?$_GET['timestamp']:time(); + $limit++; + $sql = $mysqli->prepare("SELECT *, status.id as status_id FROM status INNER JOIN users ON user_id=users.id WHERE `time` $c ? AND `end_time` $c ? OR (`time`<=? AND `end_time` $c ? ) ORDER BY `time` DESC LIMIT ? OFFSET ?"); + $sql->bind_param("iiiiii",$timestamp, $timestamp, $timestamp, $timestamp, $limit, $offset); + $sql->execute(); + $query = $sql->get_result(); + $array = []; + $limit--; + $more = false; + if ($query->num_rows>$limit){ + $more = true; + } + if ($query->num_rows){ + while(($result = $query->fetch_assoc()) && $limit-- > 0) + { + $array[] = new Incident($result); + } + } + return [ + "more" => $more, + "incidents" => $array + ]; } \ No newline at end of file diff --git a/classes/constellation.php b/classes/constellation.php index 6f01fa4..3682f61 100644 --- a/classes/constellation.php +++ b/classes/constellation.php @@ -52,7 +52,7 @@ class Constellation $offset += $limit; if ($query->num_rows){ - while(($result = $query->fetch_assoc()) && $limit-->0) + while(($result = $query->fetch_assoc()) && $limit-- > 0) { $incident = new Incident($result); $incident->render($admin); diff --git a/classes/incident.php b/classes/incident.php index b1b6344..c145db0 100644 --- a/classes/incident.php +++ b/classes/incident.php @@ -7,6 +7,8 @@ class Incident implements JsonSerializable private $id; private $date; private $end_date; + private $timestamp; + private $end_timestamp; private $text; private $type; private $title; @@ -20,6 +22,8 @@ class Incident implements JsonSerializable { //TODO: Maybe get data from id? $this->id = $data['status_id']; + $this->timestamp = $data['time']; + $this->end_timestamp = $data['end_time']; $this->date = new DateTime("@".$data['time']); $this->date = $this->date->format('Y-m-d H:i:sP'); if ($data['end_time']>0){ @@ -187,8 +191,8 @@ class Incident implements JsonSerializable public function jsonSerialize() { return [ "id" => $this->id, - "date" => $this->date, - "end_date" => $this->end_date, + "date" => $this->timestamp, + "end_date" => $this->end_timestamp, "text" => $this->text, "type" => $this->type, "title" => $this->title,