-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetAllLocations.php
More file actions
122 lines (112 loc) · 4.15 KB
/
getAllLocations.php
File metadata and controls
122 lines (112 loc) · 4.15 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
<?php
/**
* getAllLocations.php - Find It Now
*
* Created March 17, 2011 by Spencer Thomas <srt4@uw.edu>
*
* This file gets all items at a given latitude, longitude,
* and search radius. This will return JSON strings in plaintext
* format to the browser/client.
*/
$time_start = microtime(true);
include "FINsert/config.php"; // get db credentials ...
include "FINsert/opendb.php"; // ... which are used to open the db
$cat; $long; $lat; $rad;
/**
* $cat should be passed as a name-value pair using GET or POST.
* The value should be an array, condensed into the following form:
* original array: [1,2,3,4,5]
* $cat array : "1 2 3 4 5"
* Essentially, it is storing the array as a string
*/
$cat = html_entity_decode($_REQUEST["cat"]);
$cat_array = explode(" ", $cat);
// Radius isn't a required parameter, so it won't always be passed
if(isset($_REQUEST["rad"]))
$rad = html_entity_decode($_REQUEST["rad"]);
else
$rad = 0;
$building_id = isset($_REQUEST["building_id"]);
// Assume lat and long were submitted, otherwise this won't work
$lat = html_entity_decode($_REQUEST["lat"]);
$long = html_entity_decode($_REQUEST["long"]);
// Call and print the function if all parameters are passed
if ( isset($_REQUEST["lat"]) && isset($_REQUEST["long"])
&& isset($_REQUEST["cat"]) ) {
echo (get_object($lat, $long, $cat_array, $rad));
}
/**
* get_object function accepts a latitude, longitude, and
* returns all items at and around that area
* @lat: latitude in SQL format
* @long: longitude in SQL format
* @cat: (optional) item categories in array form: ["coffee", "atms"]
* @radius: (optional) search radius (m)
* @returns: JSON string
*/
function get_object($lat, $long, $cat, $rad) {
// base case: a single element in the array
if (is_array($cat) && count($cat) > 0) {
$return_string = "";
foreach ($cat as $cat_item) {
$piece = get_single_category($lat, $long, $cat_item, $rad);
if ($piece != "[]") // hack - don't return empty results
$return_string .= $piece;
}
return $return_string;
}
}
/**
* get_single_category function accepts a single category and outputs
* in JSON the items associated with a given category, lat, lon, and radius
* All variables except $cat are used in the same fashion as get_object.
* @returns: JSON string
*/
function get_single_category($lat, $long, $cat, $rad) {
$rad = $rad == 0 ? 1 : $rad; // Search the current building with radius = 0
switch ($cat) {
case "school_supplies": // special case
$supplies_array = array ( 'blue_book', 'scantron', 'printing' );
if ( $_REQUEST['item'] != null && $_REQUEST['item'] != "" ) {
$supplies_array = array ( $_REQUEST['item'] );
}
$return_string;
foreach ($supplies_array as $supply) {
$return_string .= get_single_category($lat, $long, $supply, $rad);
}
return $return_string;
break;
default: // non-special cases
$query = "SELECT *, i.item_id AS id, f.name AS floor FROM testTable2 i "
. "JOIN categories c ON i.cat_id = c.cat_id "
. "JOIN floors f ON i.fid = f.fid "
. "WHERE c.name = '$cat' LIMIT 0, 100";
$result = mysql_query($query);
if (!$result) {
echo mysql_error();
die ("MySQL encountered an error: " + mysql_error());
}
$json_array = array();
$is_school_supply = ($cat == "blue_book" || $cat == "scantron" || $cat == "printing" );
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$item = $is_school_supply ? $cat : "";
$cat2 = $is_school_supply ? "school_supplies" : $cat;
$row_array = array( 'id' => (int)$row['id'],
'lat' => (int)$row['latitude'],
'long' => (int)$row['longitude'],
'info' => $row['special_info'],
'floor_names' => $row['floor'] == null ? array() : array( $row['floor'] ),
'cat' => $cat2,
'item' => $item );
// This is probably a good thing to have with an item.
if (building_id){
$row_array['bid'] = (int)$row['bid'];
}
array_push($json_array, $row_array);
}
return json_encode($json_array);
break; // probably unnecessary with return?
}
}
include "FINsert/closedb.php"; // close the db connection
?>