forked from 54wang17/INLS690_VA_BikeShare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.php
executable file
·156 lines (137 loc) · 4.97 KB
/
controller.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
<?php
date_default_timezone_set("America/New_York");
require_once("./orm/Station.php");
require_once("./orm/Trip.php");
//if(isset($_SERVER['PATH_INFO'])) {
$path_components = explode('/', $_SERVER['PATH_INFO']);
//}
if($_SERVER['REQUEST_METHOD'] == 'GET') {
// query
if (count($path_components) >= 2 && $path_components[1]!='') {
if ($path_components[1] == 'station') {
if (count($path_components) == 2){
//url: ~/station
$all_stations = Station::getAllStations();// return all station
if ($all_stations == null) {
// Stations info not found
header("HTTP/1.1 404 Not Found");
print("Data for all stations are null");
exit();
}
header("Content-type: application/json");
print(json_encode($all_stations));
exit();
}if (count($path_components) >= 4) {
$date1 = '';
$date2 = '';
if (count($path_components) == 7){
$date1 = $path_components[5];
$date2 = $path_components[6];
}
//url: ~/station/top/N/in or ~/station/top/N/out or ~/station/top/N [in & out]
//url: ~/station/bottom/N/in or ~/station/bottom/N/out or ~/station/bottom/N [in & out]
if (($path_components[2] == 'top' || $path_components[2] == 'bottom')
&& $path_components[3] !=''){
$order = $path_components[2]; // top or bottom
$num_of_station = intval($path_components[3]); // N station
//incoming or ourcoming or all
if (count($path_components) == 4){
$weight = 'all';
}else{
$weight = $path_components[4];
}
$all_stations = Station::getNStations($num_of_station,$order,$weight,$date1,$date2);
if ($all_stations == null) {
//Station info not found
header("HTTP/1.1 404 Not Found");
print("Data for ".$order." ".$weight." ".$num_of_station." stations are null;");
exit();
}
header("Content-type: application/json");
print(json_encode($all_stations));
exit();
}
}
}else if ($path_components[1] == 'trip') {
if (count($path_components) == 2){
//url: ~/trip
//Default return: top 50 trips without datetime limit
$all_trips = Trip::getNTrips(50,'top','',''); // return all trips
if ($all_trips == null) {
// Trip info not found
header("HTTP/1.1 404 Not Found");
print("Data for all trips are null;");
exit();
}
header("Content-type: application/json");
print(json_encode($all_trips));
exit();
}elseif (count($path_components) > 3) {
//url: ~/trip/top/N[/date1/date2] or ~/trip/bottom/N[/date1/date2]
if (($path_components[2] == 'top' || $path_components[2] == 'bottom')
&& $path_components[3] != ''){
$date1 = '';
$date2 = '';
if (count($path_components) == 6){
$date1 = $path_components[4];
$date2 = $path_components[5];
}
$order = $path_components[2]; // top or bottom
$num_of_trip = intval($path_components[3]); // N trip
$all_trips = Trip::getNTrips($num_of_trip,$order,$date1,$date2);
if ($all_trips == null) {
// Trip info not found
header("HTTP/1.1 404 Not Found");
print("Data for ".$order." ".$num_of_trip." trips are null;");
exit();
}
header("Content-type: application/json");
print(json_encode($all_trips));
exit();
}else if (($path_components[2] == 'from' || $path_components[2] == 'to')
&& $path_components[3]!=''){
//url: ~/trip/from/sid[/date1/date2] or ~/trip/to/sid[/date1/date2]
$dir = $path_components[2];
$sid = intval($path_components[3]);
$date1 = '';
$date2 = '';
if (count($path_components) == 6){
$date1 = $path_components[4];
$date2 = $path_components[5];
}
$all_trips_of_sid = Trip::getTripsBySid($dir,$sid,$date1,$date2);
if ($all_trips_of_sid == null) {
//trip from/to station with id 'sid' not found
header("HTTP/1.1 404 Not Found");
print("Trip data for station ".$sid." from date: ".$date1." to date :".$date2." are null");
exit();
}
header("Content-type: application/json");
print(json_encode($all_trips_of_sid));
exit();
}else{
//url: ~/trip/date1/date2
$date1 = $path_components[2];
$date2 = $path_components[3];
$all_trips_within_range = Trip::getTripsByDateRange($date1,$date2);
if ($all_trips_within_range == null) {
// trip from date 1 to date 2 not found
header("HTTP/1.1 404 Not Found");
print("Trip data from date: ".$date1." to date :".$date2." are null.");
exit();
}
header("Content-type: application/json");
print(json_encode($all_trips_within_range));
exit();
}
}
}
}
}
// If here, none of the above applied and URL could
// not be interpreted with respect to RESTful conventions.
header("HTTP/1.1 400 Bad Request!");
print '<h1 class="title">Page Not Found<h1>';
print '<h4>We\'re sorry but the page you requested is not available on the website. </h4>';
// print '<h4>Please Click <span><a href="'.$base_url.'">Here</a></span> to return to Homepage.</h4>';
?>