-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetLocations.php
More file actions
159 lines (136 loc) · 5.94 KB
/
getLocations.php
File metadata and controls
159 lines (136 loc) · 5.94 KB
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
# This getLocations file accepts parameters from an HTTP request and
# returns information from the FIN database in JSON array or throws a MYSQL error
# @param: cat = category which matches a table name in DB
# @param: lat = latituude around which to search
# @param: long = longitude around which to search
# @param(optional): item = required when category is "supplies"
# author: Jillyn J.
include 'FINsert/config.php';
include 'FINsert/opendb.php';
$cat = "";
$lat = 0;
$long = 0;
# retrieve and save parameters sent in HTTP request
$cat = html_entity_decode($_REQUEST["cat"]);
$lat = html_entity_decode($_REQUEST["lat"]);
$long = html_entity_decode($_REQUEST["long"]);
$output = "";
# For any category other than special-case "supplies"
if ($cat != "school_supplies") {
#query the database for locations associated with buildings
$results = mysql_query("SELECT DISTINCT t.latitude, t.longitude, t.id, t.special_info, fnum, f.name as \"floor\"\n"
. "FROM $cat t, floors f, regions\n"
. "WHERE t.fid = f.fid AND t.rid IN (SELECT rid FROM regions WHERE $lat > regions.latitude - 6020 AND $lat ".
"< regions.latitude + 6020 AND $long > (regions.longitude - 6020) AND $long < (regions.longitude + 6020))\n"
. "ORDER BY fnum DESC, t.special_info;");
if (!$results) {
die("Error, could not query the db! " . mysql_error());
}
$output = "[";
$row = mysql_fetch_assoc($results);
$hasEntry = false; #have we encoded a row into output yet? used for commas in output
$id = "";
if ($row) {
if ($hasEntry) {
$output = $output . ",";
}
$output = $output."{\"lat\":".$row["latitude"].",\"long\":".$row["longitude"].",\"id\":".$row["id"].",\"info\":\"".$row["special_info"]."\",\"floor_names\":[\"".$row["floor"]."\"";
$id = $row["id"];
$row = mysql_fetch_assoc($results);
$hasEntry = true;
while ($row) {
#if output has already been generated for this location, just add floor
if ($row["id"] == $id) {
$output = $output . ",\"" . $row["floor"] . "\"";
} else {
$output = $output."]}, {\"lat\":".$row["latitude"].",\"long\":".$row["longitude"].",\"id\":".$row["id"].
",\"info\":\"".$row["special_info"]."\",\"floor_names\":[\"".$row["floor"]."\"";
}
$id = $row["id"];
$row = mysql_fetch_assoc($results);
}
$output = $output . "]}";
}
#query the database for locations not related to buildings
$results = mysql_query("SELECT DISTINCT t.latitude, t.longitude, t.id, t.special_info\n"
. "FROM $cat t, regions\n"
. "WHERE t.fid = 0 AND t.rid IN (SELECT rid FROM regions WHERE $lat > regions.latitude - 6020 AND $lat ".
"< regions.latitude + 6020 AND $long > (regions.longitude - 6020) AND $long < (regions.longitude + 6020))\n"
. "ORDER BY t.special_info;");
if (!$results) {
die("Error, could not query the db for objects outside buildings! " . mysql_error());
}
$row = mysql_fetch_assoc($results);
if ($row) {
while ($row) {
if ($hasEntry) {
$output = $output . ",";
}
$output = $output."{\"lat\":".$row["latitude"].",\"long\":".$row["longitude"].",\"id\":".$row["id"].",\"info\":\"".$row["special_info"]."\",\"floor_names\":[]}";
$row = mysql_fetch_assoc($results);
$hasEntry = true;
}
}
$output = $output . "]";
} else { #must be "suplies" category
$item = html_entity_decode($_REQUEST["item"]);
#query for supplies locations related to buildings
$results = mysql_query("SELECT DISTINCT t.latitude, t.longitude, t.id ,t.special_info, fnum, f.name as \"floor\"\n"
. "FROM school_supplies t, floors f, regions\n"
. "WHERE $item > 0 AND t.fid = f.fid AND t.rid IN (SELECT rid FROM regions WHERE $lat > regions.latitude - 6020 AND $lat ".
"< regions.latitude + 6020 AND $long > (regions.longitude - 6020) AND $long < (regions.longitude + 6020))\n"
. "ORDER BY fnum DESC, t.special_info;");
if (!$results) {
die("Error, could not query the db for supplies! " . mysql_error());
}
$output = "[";
$row = mysql_fetch_assoc($results);
$hasEntry = false;
$id = "";
if ($row) {
if ($hasEntry) {
$output = $output . ",";
}
$output = $output."{\"lat\":".$row["latitude"].",\"long\":".$row["longitude"].",\"id\":".$row["id"].",\"info\":\"".$row["special_info"]."\",\"floor_names\":[\"".$row["floor"]."\"";
$id = $row["id"];
$row = mysql_fetch_assoc($results);
$hasEntry = true;
while ($row) {
#if output has already been generated for this location, just add floor
if ($row["id"] == $id) {
$output = $output . ",\"" . $row["floor"] . "\"";
} else {
$output = $output."]}, {\"lat\":".$row["latitude"].",\"long\":".$row["longitude"].",\"id\":".$row["id"].
",\"info\":\"".$row["special_info"]."\",\"floor_names\":[\"".$row["floor"]."\"";
}
$id = $row["id"];
$row = mysql_fetch_assoc($results);
}
$output = $output . "]}";
}
#query database for locations not associated with a building
$results = mysql_query("SELECT DISTINCT t.latitude, t.longitude, t.id ,t.special_info\n"
. "FROM school_supplies t, regions\n"
. "WHERE $item > 0 AND t.fid = 0\n"
. "ORDER BY t.special_info;");
if (!$results) {
die("Error, could not query the db for objects outside buildings! " . mysql_error());
}
$row = mysql_fetch_assoc($results);
if ($row) {
while ($row) {
if ($hasEntry) {
$output = $output . ",";
}
$output = $output."{\"lat\":".$row["latitude"].",\"long\":".$row["longitude"].",\"id\":".$row["id"].",\"info\":\"".$row["special_info"]."\",\"floor_names\":[]}";
$row = mysql_fetch_assoc($results);
$hasEntry = true;
}
}
$output = $output . "]";
}
#set output as response text
print($output);
include 'FINsert/closedb.php';
?>