/
history.php
159 lines (124 loc) · 3.97 KB
/
history.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
header("Access-Control-Allow-Origin: *");
include "databasefunctions.php";
include_once "bintypes.php";
include "bin.php";
if(isset($_POST["newStamp"])) //Imporant: Keep same variable names as set in 'function makeUser()' so users know what to set.
{
$data = $_POST["newStamp"];
$newId = isset($data["BinId"])? $data["BinId"] : "";
$newWeight = isset($data["Weight"])? $data["Weight"] : "";
$newTimestamp = isset($data["UnixTimestamp"])? $data["UnixTimestamp"] : "";
$success = true;
$errors = [];
if($newId == "")
{
array_push($errors, "Bin ID not set");
$success = false;
}
if($newWeight == "")
{
array_push($errors, "Weight not set");
$success = false;
}
if($newTimestamp == "")
{
array_push($errors, "Timestamp not set");
$success = false;
}
if($success)
{
RegisterNewHistory($newId, $newWeight, $newTimestamp);
echo '{"success":"Stamp successfully registered"}';
}
else
{
echo '{"errors":' . json_encode($errors) . '}';
}
}
else
{
if(isset($_GET["id"]) && !is_array($_GET["id"]))
{
$histRaw = GetBinHistory($_GET["id"], isset($_GET["from"])? $_GET["from"] : 0, isset($_GET["to"])? $_GET["to"] : 0);
$history = [];
$returnObject = new Collection();
for($i = 0; $i < count($histRaw); $i++)
{
$newHist = new Collection();
$newHist->BinId = (int)$histRaw[$i][1];
$newHist->Weight = (int)$histRaw[$i][2];
$newHist->UnixTimestamp = (int)$histRaw[$i][3];
$newHist->Date = date("Y-m-d", $histRaw[$i][3]);
$newHist->Time = date("H:i:s", $histRaw[$i][3]);
array_push($history, $newHist);
}
$returnObject->BinId = (int)$_GET["id"];
$returnObject->History = $history;
echo json_encode($returnObject);
}
else
{
$history = [];
$returnObject = new Collection();
$returnObject->UnixFrom = isset($_GET["from"])? $_GET["from"] : 0;
$returnObject->UnixTo = isset($_GET["to"])? $_GET["to"] : 0;
$returnObject->BinHistories = array();
if(isset($_GET["id"]) && is_array($_GET["id"]))
{
$histRaw = GetBundledBinHistory($_GET["id"], isset($_GET["from"])? $_GET["from"] : 0, isset($_GET["to"])? $_GET["to"] : 0);
$noDuplicates = array();
for($i = 0; $i < count($_GET["id"]); $i++)
{
$add = true;
for($i2=0; $i2 < count($noDuplicates); $i2++)
{
if($noDuplicates[$i2] == $_GET["id"][$i])
$ad = false;
}
if($add)
{
$newHistContainer = new Collection();
$newHistContainer->BinId = (int)$_GET["id"][$i];
$newHistContainer->History = array();
array_push($noDuplicates, $_GET["id"][$i]);
array_push($returnObject->BinHistories, $newHistContainer);
}
}
}
else
$histRaw = GetAllBinHistory(isset($_GET["from"])? $_GET["from"] : 0, isset($_GET["to"])? $_GET["to"] : 0);
for($i = 0; $i < count($histRaw); $i++)
{
$newHist = new Collection();
$newHist->BinId = (int)$histRaw[$i][1];
$newHist->Weight = (int)$histRaw[$i][2];
$newHist->UnixTimestamp = (int)$histRaw[$i][3];
$newHist->Date = date("Y-m-d", $histRaw[$i][3]);
$newHist->Time = date("H:i:s", $histRaw[$i][3]);
$makeNew = true;
for($h = 0; $h < count($returnObject->BinHistories); $h++)
{
if($returnObject->BinHistories[$h]->BinId == (int)$histRaw[$i][1])
{
$makeNew = false;
array_push($returnObject->BinHistories[$h]->History, $newHist);
}
}
if($makeNew)
{
$newHistContainer = new Collection();
$newHistContainer->BinId = (int)$histRaw[$i][1];
$newHistContainer->History = [$newHist];
array_push($returnObject->BinHistories, $newHistContainer);
}
}
if(isset($_GET["id"]) && is_array($_GET["id"]))
{
$returnObject->IdsRequested = array();
for($i = 0; $i < count($_GET["id"]); $i++)
array_push($returnObject->IdsRequested, (int)$_GET["id"][$i]);
}
echo json_encode($returnObject);
}
}