Better time management, times on nearby routes
Better time management, times on nearby routes

<?php <?php
// you have to open the session to be able to modify or remove it // you have to open the session to be able to modify or remove it
session_start(); session_start();
if (isset($_REQUEST['service_period'])) { if (isset($_REQUEST['service_period'])) {
$_SESSION['service_period'] = filter_var($_REQUEST['service_period'], FILTER_SANITIZE_STRING); $_SESSION['service_period'] = filter_var($_REQUEST['service_period'], FILTER_SANITIZE_STRING);
sessionUpdated(); sessionUpdated();
} }
if (isset($_REQUEST['time'])) { if (isset($_REQUEST['time'])) {
$_SESSION['time'] = filter_var($_REQUEST['time'], FILTER_SANITIZE_STRING); $_SESSION['time'] = filter_var($_REQUEST['time'], FILTER_SANITIZE_STRING);
sessionUpdated(); sessionUpdated();
} }
if (isset($_REQUEST['geolocate']) && $_REQUEST['geolocate'] != "Enter co-ordinates or address here") { if (isset($_REQUEST['geolocate']) && $_REQUEST['geolocate'] != "Enter co-ordinates or address here") {
$geocoded = false; $geocoded = false;
if (isset($_REQUEST['lat']) && isset($_REQUEST['lon'])) { if (isset($_REQUEST['lat']) && isset($_REQUEST['lon'])) {
$_SESSION['lat'] = trim(filter_var($_REQUEST['lat'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION)); $_SESSION['lat'] = trim(filter_var($_REQUEST['lat'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION));
$_SESSION['lon'] = trim(filter_var($_REQUEST['lon'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION)); $_SESSION['lon'] = trim(filter_var($_REQUEST['lon'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION));
} }
else { else {
$geolocate = filter_var($_REQUEST['geolocate'], FILTER_SANITIZE_URL); $geolocate = filter_var($_REQUEST['geolocate'], FILTER_SANITIZE_URL);
if (startsWith($geolocate, "-")) { if (startsWith($geolocate, "-")) {
$locateparts = explode(",", $geolocate); $locateparts = explode(",", $geolocate);
$_SESSION['lat'] = $locateparts[0]; $_SESSION['lat'] = $locateparts[0];
$_SESSION['lon'] = $locateparts[1]; $_SESSION['lon'] = $locateparts[1];
} }
else { else {
$contents = geocode($geolocate, true); $contents = geocode($geolocate, true);
print_r($contents); print_r($contents);
if (isset($contents[0]->centroid)) { if (isset($contents[0]->centroid)) {
$geocoded = true; $geocoded = true;
$_SESSION['lat'] = $contents[0]->centroid->coordinates[0]; $_SESSION['lat'] = $contents[0]->centroid->coordinates[0];
$_SESSION['lon'] = $contents[0]->centroid->coordinates[1]; $_SESSION['lon'] = $contents[0]->centroid->coordinates[1];
} }
else { else {
$_SESSION['lat'] = ""; $_SESSION['lat'] = "";
$_SESSION['lon'] = ""; $_SESSION['lon'] = "";
} }
} }
} }
if ($_SESSION['lat'] != "" && isAnalyticsOn()) { if ($_SESSION['lat'] != "" && isAnalyticsOn()) {
trackEvent("Geolocation","Updated Location", "Geocoded - ".($geocoded ? "Yes" : "No")); trackEvent("Geolocation","Updated Location", "Geocoded - ".($geocoded ? "Yes" : "No"));
} }
sessionUpdated(); sessionUpdated();
} }
function sessionUpdated() { function sessionUpdated() {
$_SESSION['lastUpdated'] = time(); $_SESSION['lastUpdated'] = time();
} }
// timeoutSession // timeoutSession
$TIMEOUT_LIMIT = 60*5; // 5 minutes $TIMEOUT_LIMIT = 60*5; // 5 minutes
if (isset($_SESSION['lastUpdated']) && $_SESSION['lastUpdated']+$TIMEOUT_LIMIT < time()) { if (isset($_SESSION['lastUpdated']) && $_SESSION['lastUpdated']+$TIMEOUT_LIMIT < time()) {
debug ("Session timeout ".($_SESSION['lastUpdated']+$TIMEOUT_LIMIT).">".time(),"session"); debug ("Session timeout ".($_SESSION['lastUpdated']+$TIMEOUT_LIMIT).">".time(),"session");
session_destroy(); session_destroy();
session_start(); session_start();
} }
debug(print_r($_SESSION, true) , "session"); debug(print_r($_SESSION, true) , "session");
   
  function current_time() {
  return ($_SESSION['time']? $_SESSION['time'] : date("H:i:s"));
  }
?> ?>
<?php <?php
   
function getRoute($routeID) { function getRoute($routeID) {
$query = "Select * from routes where route_id = '$routeID' LIMIT 1"; $query = "Select * from routes where route_id = '$routeID' LIMIT 1";
debug($query,"database"); debug($query,"database");
$result = pg_query($conn, $query); $result = pg_query($conn, $query);
if (!$result) { if (!$result) {
databaseError(pg_result_error($result)); databaseError(pg_result_error($result));
return Array(); return Array();
} }
return pg_fetch_assoc($result); return pg_fetch_assoc($result);
} }
function getRoutes() { function getRoutes() {
global $conn; global $conn;
$query = "Select * from routes order by route_short_name;"; $query = "Select * from routes order by route_short_name;";
debug($query,"database"); debug($query,"database");
$result = pg_query($conn, $query); $result = pg_query($conn, $query);
if (!$result) { if (!$result) {
databaseError(pg_result_error($result)); databaseError(pg_result_error($result));
return Array(); return Array();
} }
return pg_fetch_all($result); return pg_fetch_all($result);
} }
   
function getRoutesByNumber($routeNumber = "") { function getRoutesByNumber($routeNumber = "") {
global $conn; global $conn;
if ($routeNumber != "") { if ($routeNumber != "") {
$query = "Select distinct routes.route_id,routes.route_short_name,routes.route_long_name,service_id from routes join trips on trips.route_id = $query = "Select distinct routes.route_id,routes.route_short_name,routes.route_long_name,service_id from routes join trips on trips.route_id =
routes.route_id join stop_times on stop_times.trip_id = trips.trip_id where route_short_name = '$routeNumber' order by route_short_name;"; routes.route_id join stop_times on stop_times.trip_id = trips.trip_id where route_short_name = '$routeNumber' order by route_short_name;";
} else { } else {
$query = "SELECT DISTINCT route_short_name from routes order by route_short_name"; $query = "SELECT DISTINCT route_short_name from routes order by route_short_name";
} }
debug($query,"database"); debug($query,"database");
$result = pg_query($conn, $query); $result = pg_query($conn, $query);
if (!$result) { if (!$result) {
databaseError(pg_result_error($result)); databaseError(pg_result_error($result));
return Array(); return Array();
} }
return pg_fetch_all($result); return pg_fetch_all($result);
} }
   
function getRouteNextTrip($routeID) { function getRouteNextTrip($routeID) {
global $conn; global $conn;
$query = "select * from routes join trips on trips.route_id = routes.route_id $query = "select * from routes join trips on trips.route_id = routes.route_id
join stop_times on stop_times.trip_id = trips.trip_id where join stop_times on stop_times.trip_id = trips.trip_id where
arrival_time > CURRENT_TIME and routes.route_id = '$routeID' order by arrival_time > '".current_time()."' and routes.route_id = '$routeID' order by
arrival_time limit 1"; arrival_time limit 1";
debug($query,"database"); debug($query,"database");
$result = pg_query($conn, $query); $result = pg_query($conn, $query);
if (!$result) { if (!$result) {
databaseError(pg_result_error($result)); databaseError(pg_result_error($result));
return Array(); return Array();
} }
return pg_fetch_assoc($result); return pg_fetch_assoc($result);
} }
   
  function getTimeInterpolatedRouteAtStop($routeID, $stop_id)
  {
  $nextTrip = getRouteNextTrip($routeID);
  if ($nextTrip['trip_id']){
  foreach (getTimeInterpolatedTrip($nextTrip['trip_id']) as $tripStop) {
  if ($tripStop['stop_id'] == $stop_id) return $tripStop;
  }
  }
  return Array();
  }
   
function getRouteTrips($routeID) { function getRouteTrips($routeID) {
global $conn; global $conn;
$query = "select * from routes join trips on trips.route_id = routes.route_id $query = "select * from routes join trips on trips.route_id = routes.route_id
join stop_times on stop_times.trip_id = trips.trip_id where routes.route_id = '$routeID' order by join stop_times on stop_times.trip_id = trips.trip_id where routes.route_id = '$routeID' order by
arrival_time "; arrival_time ";
debug($query,"database"); debug($query,"database");
$result = pg_query($conn, $query); $result = pg_query($conn, $query);
if (!$result) { if (!$result) {
databaseError(pg_result_error($result)); databaseError(pg_result_error($result));
return Array(); return Array();
} }
return pg_fetch_all($result); return pg_fetch_all($result);
} }
function getRoutesByDestination($destination = "", $service_period = "") { function getRoutesByDestination($destination = "", $service_period = "") {
global $conn; global $conn;
if ($service_period == "") $service_period = service_period(); if ($service_period == "") $service_period = service_period();
if ($destination != "") { if ($destination != "") {
$query = "SELECT DISTINCT trips.route_id,route_short_name,route_long_name, service_id $query = "SELECT DISTINCT trips.route_id,route_short_name,route_long_name, service_id
FROM stop_times join trips on trips.trip_id = FROM stop_times join trips on trips.trip_id =
stop_times.trip_id join routes on trips.route_id = routes.route_id stop_times.trip_id join routes on trips.route_id = routes.route_id
WHERE route_long_name = '$destination' AND service_id='$service_period' order by route_short_name"; WHERE route_long_name = '$destination' AND service_id='$service_period' order by route_short_name";
} else { } else {
$query = "SELECT DISTINCT route_long_name $query = "SELECT DISTINCT route_long_name
FROM stop_times join trips on trips.trip_id = FROM stop_times join trips on trips.trip_id =
stop_times.trip_id join routes on trips.route_id = routes.route_id stop_times.trip_id join routes on trips.route_id = routes.route_id
WHERE service_id='$service_period' order by route_long_name"; WHERE service_id='$service_period' order by route_long_name";
} }
debug($query,"database"); debug($query,"database");
$result = pg_query($conn, $query); $result = pg_query($conn, $query);
if (!$result) { if (!$result) {
databaseError(pg_result_error($result)); databaseError(pg_result_error($result));
return Array(); return Array();
} }
return pg_fetch_all($result); return pg_fetch_all($result);
} }
   
function getRoutesBySuburb($suburb, $service_period = "") { function getRoutesBySuburb($suburb, $service_period = "") {
if ($service_period == "") $service_period = service_period(); if ($service_period == "") $service_period = service_period();
global $conn; global $conn;
$query = "SELECT DISTINCT service_id,trips.route_id,route_short_name,route_long_name $query = "SELECT DISTINCT service_id,trips.route_id,route_short_name,route_long_name
FROM stop_times join trips on trips.trip_id = stop_times.trip_id FROM stop_times join trips on trips.trip_id = stop_times.trip_id
join routes on trips.route_id = routes.route_id join routes on trips.route_id = routes.route_id
join stops on stops.stop_id = stop_times.stop_id join stops on stops.stop_id = stop_times.stop_id
WHERE zone_id LIKE '%$suburb;%' AND service_id='$service_period' ORDER BY route_short_name"; WHERE zone_id LIKE '%$suburb;%' AND service_id='$service_period' ORDER BY route_short_name";
debug($query,"database"); debug($query,"database");
$result = pg_query($conn, $query); $result = pg_query($conn, $query);
if (!$result) { if (!$result) {
databaseError(pg_result_error($result)); databaseError(pg_result_error($result));
return Array(); return Array();
} }
return pg_fetch_all($result); return pg_fetch_all($result);
} }
   
function getRoutesNearby($lat, $lng, $limit = "", $distance = 500) { function getRoutesNearby($lat, $lng, $limit = "", $distance = 500) {
   
if ($service_period == "") $service_period = service_period(); if ($service_period == "") $service_period = service_period();
if ($limit != "") $limit = " LIMIT $limit "; if ($limit != "") $limit = " LIMIT $limit ";
global $conn; global $conn;
$query = "SELECT service_id,trips.route_id,route_short_name,route_long_name, $query = "SELECT service_id,trips.route_id,route_short_name,route_long_name,min(stops.stop_id) as stop_id,
min(ST_Distance(position, ST_GeographyFromText('SRID=4326;POINT($lng $lat)'), FALSE)) as distance min(ST_Distance(position, ST_GeographyFromText('SRID=4326;POINT($lng $lat)'), FALSE)) as distance
FROM stop_times FROM stop_times
join trips on trips.trip_id = stop_times.trip_id join trips on trips.trip_id = stop_times.trip_id
join routes on trips.route_id = routes.route_id join routes on trips.route_id = routes.route_id
join stops on stops.stop_id = stop_times.stop_id join stops on stops.stop_id = stop_times.stop_id
WHERE service_id='$service_period' WHERE service_id='$service_period'
AND ST_DWithin(position, ST_GeographyFromText('SRID=4326;POINT($lng $lat)'), $distance, FALSE) AND ST_DWithin(position, ST_GeographyFromText('SRID=4326;POINT($lng $lat)'), $distance, FALSE)
group by service_id,trips.route_id,route_short_name,route_long_name group by service_id,trips.route_id,route_short_name,route_long_name
order by distance $limit"; order by distance $limit";
debug($query,"database"); debug($query,"database");
$result = pg_query($conn, $query); $result = pg_query($conn, $query);
if (!$result) { if (!$result) {
databaseError(pg_result_error($result)); databaseError(pg_result_error($result));
return Array(); return Array();
} }
return pg_fetch_all($result); return pg_fetch_all($result);
} }
?> ?>
<?php <?php
function getStop($stopID) function getStop($stopID)
{ {
global $conn; global $conn;
$query = "Select * from stops where stop_id = '$stopID' LIMIT 1"; $query = "Select * from stops where stop_id = '$stopID' LIMIT 1";
debug($query, "database"); debug($query, "database");
$result = pg_query($conn, $query); $result = pg_query($conn, $query);
if (!$result) { if (!$result) {
databaseError(pg_result_error($result)); databaseError(pg_result_error($result));
return Array(); return Array();
} }
return pg_fetch_assoc($result); return pg_fetch_assoc($result);
} }
function getStops($timingPointsOnly = false, $firstLetter = "") function getStops($timingPointsOnly = false, $firstLetter = "")
{ {
global $conn; global $conn;
$conditions = Array(); $conditions = Array();
if ($timingPointsOnly) $conditions[] = "substr(stop_code,1,2) != 'Wj'"; if ($timingPointsOnly) $conditions[] = "substr(stop_code,1,2) != 'Wj'";
if ($firstLetter != "") $conditions[] = "substr(stop_name,1,1) = '$firstLetter'"; if ($firstLetter != "") $conditions[] = "substr(stop_name,1,1) = '$firstLetter'";
$query = "Select * from stops"; $query = "Select * from stops";
if (sizeof($conditions) > 0) { if (sizeof($conditions) > 0) {
if (sizeof($conditions) > 1) { if (sizeof($conditions) > 1) {
$query.= " Where " . implode(" AND ", $conditions) . " "; $query.= " Where " . implode(" AND ", $conditions) . " ";
} }
else { else {
$query.= " Where " . $conditions[0] . " "; $query.= " Where " . $conditions[0] . " ";
} }
} }
$query.= " order by stop_name;"; $query.= " order by stop_name;";
debug($query, "database"); debug($query, "database");
$result = pg_query($conn, $query); $result = pg_query($conn, $query);
if (!$result) { if (!$result) {
databaseError(pg_result_error($result)); databaseError(pg_result_error($result));
return Array(); return Array();
} }
return pg_fetch_all($result); return pg_fetch_all($result);
} }
function getNearbyStops($lat, $lng, $limit = "", $distance = 1000) function getNearbyStops($lat, $lng, $limit = "", $distance = 1000)
{ {
if ($lat == null || $lng == null) return Array(); if ($lat == null || $lng == null) return Array();
if ($limit != "") $limit = " LIMIT $limit "; if ($limit != "") $limit = " LIMIT $limit ";
global $conn; global $conn;
$query = "Select *, ST_Distance(position, ST_GeographyFromText('SRID=4326;POINT($lng $lat)'), FALSE) as distance $query = "Select *, ST_Distance(position, ST_GeographyFromText('SRID=4326;POINT($lng $lat)'), FALSE) as distance
from stops WHERE ST_DWithin(position, ST_GeographyFromText('SRID=4326;POINT($lng $lat)'), $distance, FALSE) from stops WHERE ST_DWithin(position, ST_GeographyFromText('SRID=4326;POINT($lng $lat)'), $distance, FALSE)
order by distance $limit;"; order by distance $limit;";
debug($query, "database"); debug($query, "database");
$result = pg_query($conn, $query); $result = pg_query($conn, $query);
if (!$result) { if (!$result) {
databaseError(pg_result_error($result)); databaseError(pg_result_error($result));
return Array(); return Array();
} }
return pg_fetch_all($result); return pg_fetch_all($result);
} }
function getStopsBySuburb($suburb) function getStopsBySuburb($suburb)
{ {
global $conn; global $conn;
$query = "Select * from stops where zone_id LIKE '%$suburb;%' order by stop_name;"; $query = "Select * from stops where zone_id LIKE '%$suburb;%' order by stop_name;";
debug($query, "database"); debug($query, "database");
$result = pg_query($conn, $query); $result = pg_query($conn, $query);
if (!$result) { if (!$result) {
databaseError(pg_result_error($result)); databaseError(pg_result_error($result));
return Array(); return Array();
} }
return pg_fetch_all($result); return pg_fetch_all($result);
} }
function getStopRoutes($stopID, $service_period) function getStopRoutes($stopID, $service_period)
{ {
if ($service_period == "") $service_period = service_period(); if ($service_period == "") $service_period = service_period();
global $conn; global $conn;
$query = "SELECT service_id,trips.route_id,route_short_name,route_long_name $query = "SELECT service_id,trips.route_id,route_short_name,route_long_name
FROM stop_times join trips on trips.trip_id = FROM stop_times join trips on trips.trip_id =
stop_times.trip_id join routes on trips.route_id = routes.route_id WHERE stop_id = '$stopID' AND service_id='$service_period'"; stop_times.trip_id join routes on trips.route_id = routes.route_id WHERE stop_id = '$stopID' AND service_id='$service_period'";
debug($query, "database"); debug($query, "database");
$result = pg_query($conn, $query); $result = pg_query($conn, $query);
if (!$result) { if (!$result) {
databaseError(pg_result_error($result)); databaseError(pg_result_error($result));
return Array(); return Array();
} }
return pg_fetch_all($result); return pg_fetch_all($result);
} }
function getStopTrips($stopID, $service_period = "", $afterTime = "") function getStopTrips($stopID, $service_period = "", $afterTime = "")
{ {
if ($service_period == "") $service_period = service_period(); if ($service_period == "") $service_period = service_period();
$afterCondition = "AND arrival_time > '$afterTime'"; $afterCondition = "AND arrival_time > '$afterTime'";
global $conn; global $conn;
if ($afterTime != "") { if ($afterTime != "") {
$query = " SELECT stop_times.trip_id,stop_times.arrival_time,stop_times.stop_id,stop_sequence,service_id,trips.route_id,route_short_name,route_long_name, start_times.arrival_time as start_time $query = " SELECT stop_times.trip_id,stop_times.arrival_time,stop_times.stop_id,stop_sequence,service_id,trips.route_id,route_short_name,route_long_name, start_times.arrival_time as start_time
FROM stop_times FROM stop_times
join trips on trips.trip_id = join trips on trips.trip_id =
stop_times.trip_id stop_times.trip_id
join routes on trips.route_id = routes.route_id , (SELECT trip_id,arrival_time from stop_times join routes on trips.route_id = routes.route_id , (SELECT trip_id,arrival_time from stop_times
WHERE stop_times.arrival_time IS NOT NULL WHERE stop_times.arrival_time IS NOT NULL
AND stop_sequence = '1') as start_times AND stop_sequence = '1') as start_times
WHERE stop_times.stop_id = '$stopID' WHERE stop_times.stop_id = '$stopID'
AND stop_times.trip_id = start_times.trip_id AND stop_times.trip_id = start_times.trip_id
AND service_id='$service_period' AND service_id='$service_period'
AND start_times.arrival_time > '$afterTime' AND start_times.arrival_time > '$afterTime'
ORDER BY start_time"; ORDER BY start_time";
} }
else { else {
$query = "SELECT stop_times.trip_id,arrival_time,stop_times.stop_id,stop_sequence,service_id,trips.route_id,route_short_name,route_long_name $query = "SELECT stop_times.trip_id,arrival_time,stop_times.stop_id,stop_sequence,service_id,trips.route_id,route_short_name,route_long_name
FROM stop_times FROM stop_times
join trips on trips.trip_id = join trips on trips.trip_id =
stop_times.trip_id stop_times.trip_id
join routes on trips.route_id = routes.route_id join routes on trips.route_id = routes.route_id
WHERE stop_times.stop_id = '$stopID' WHERE stop_times.stop_id = '$stopID'
AND service_id='$service_period' AND service_id='$service_period'
ORDER BY arrival_time"; ORDER BY arrival_time";
} }
debug($query, "database"); debug($query, "database");
$result = pg_query($conn, $query); $result = pg_query($conn, $query);
if (!$result) { if (!$result) {
databaseError(pg_result_error($result)); databaseError(pg_result_error($result));
return Array(); return Array();
} }
return pg_fetch_all($result); return pg_fetch_all($result);
} }
function getStopTripsWithTimes($stopID, $time = "", $service_period = "", $time_range = "", $limit = "") function getStopTripsWithTimes($stopID, $time = "", $service_period = "", $time_range = "", $limit = "")
{ {
if ($service_period == "") $service_period = service_period(); if ($service_period == "") $service_period = service_period();
if ($time_range == "") $time_range = (24 * 60 * 60); if ($time_range == "") $time_range = (24 * 60 * 60);
if ($time == "") $time = ($_SESSION['time'] ? $_SESSION['time'] : date("H:i:s")); if ($time == "") $time = current_time();
if ($limit == "") $limit = 10; if ($limit == "") $limit = 10;
$trips = getStopTrips($stopID, $service_period, $time); $trips = getStopTrips($stopID, $service_period, $time);
$timedTrips = Array(); $timedTrips = Array();
if ($trips && sizeof($trips) > 0) { if ($trips && sizeof($trips) > 0) {
foreach ($trips as $trip) { foreach ($trips as $trip) {
if ($trip['arrival_time'] != "") { if ($trip['arrival_time'] != "") {