From: Maxious Date: Sun, 04 Sep 2011 15:33:12 +0000 Subject: Merge branch 'master' of https://github.com/maxious/ACTBus-ui X-Git-Url: https://maxious.lambdacomplex.org/git/?p=busui.git&a=commitdiff&h=a956bd0cd793766df08deed2efa33f4e642e103e --- Merge branch 'master' of https://github.com/maxious/ACTBus-ui --- --- /dev/null +++ b/.gitignore @@ -1,1 +1,8 @@ +/labs/tiles/12 +/labs/tiles/13 +/labs/tiles/14 +/labs/tiles/15 +/labs/tiles/16 +/labs/tiles/17 +/labs/tiles/19 --- a/about.php +++ b/about.php @@ -21,6 +21,10 @@ , ACT Buses by David Sullivan) and Android (MyBus 2.0 by Imagine Team)
+GTFS-realtime API; +Alerts and Trip Updates (but only Cancelled or Stop Skipped) +Default format binary but can get JSON by adding ?ascii=yes +

Disclaimer: The content of this website is of a general and informative nature. Please check with printed timetables or those available on http://action.act.gov.au before your trip. Whilst every effort has been made to ensure the high quality and accuracy of the Site, the Author makes no warranty, --- a/aws/busuiphp.sh +++ b/aws/busuiphp.sh @@ -2,9 +2,15 @@ mkdir /var/www/lib/staticmaplite/cache chcon -h system_u:object_r:httpd_sys_content_t /var/www chcon -R -h root:object_r:httpd_sys_content_t /var/www/* + chcon -R -t httpd_sys_content_rw_t /var/www/lib/staticmaplite/cache chmod -R 777 /var/www/lib/staticmaplite/cache + chcon -R -t httpd_sys_content_rw_t /var/www/labs/tiles chmod -R 777 /var/www/labs/tiles + +chcon -R -t httpd_sys_content_rw_t /var/www/lib/openid-php/oid_store +chmod -R 777 /var/www/lib/openid-php/oid_store + wget http://s3-ap-southeast-1.amazonaws.com/busresources/cbrfeed.zip \ -O /var/www/cbrfeed.zip --- /dev/null +++ b/include/common-auth.inc.php @@ -1,1 +1,91 @@ + begin($oid_identifier); + + // Create attribute request object + // See http://code.google.com/apis/accounts/docs/OpenID.html#Parameters for parameters + // Usage: make($type_uri, $count=1, $required=false, $alias=null) + $attribute[] = Auth_OpenID_AX_AttrInfo :: make('http://axschema.org/contact/email', 2, 1, 'email'); + $attribute[] = Auth_OpenID_AX_AttrInfo :: make('http://axschema.org/namePerson/first', 1, 1, 'firstname'); + $attribute[] = Auth_OpenID_AX_AttrInfo :: make('http://axschema.org/namePerson/last', 1, 1, 'lastname'); + + // Create AX fetch request + $ax = new Auth_OpenID_AX_FetchRequest; + + // Add attributes to AX fetch request + foreach($attribute as $attr) { + $ax -> add($attr); + } + + // Add AX fetch request to authentication request + $auth -> addExtension($ax); + $_SESSION['returnURL'] = curPageURL(); + // Redirect to OpenID provider for authentication + $url = $auth -> redirectURL(getTrustRoot(), $_SESSION['returnURL']); + header('Location: ' . $url); + } + + +function auth() + +{ + if ($_SESSION['authed'] == true) return true; + + // Create file storage area for OpenID data + $store = new Auth_OpenID_FileStore('lib/openid-php/oid_store'); + // Create OpenID consumer + $consumer = new Auth_OpenID_Consumer($store); + // Create an authentication request to the OpenID provider + $response = $consumer -> complete($_SESSION['returnURL']); + + if ($response -> status == Auth_OpenID_SUCCESS) { + // Get registration informations + $ax = new Auth_OpenID_AX_FetchResponse(); + $obj = $ax -> fromSuccessResponse($response); + $email = $obj -> data['http://axschema.org/contact/email'][0]; + var_dump($email); + if ($email != "maxious@gmail.com") { + die("Access Denied"); + } else { + $_SESSION['authed'] = true; + } + } else { + login(); + } + } + if ($_REQUEST['janrain_nonce']) auth(); +?> --- a/include/common-template.inc.php +++ b/include/common-template.inc.php @@ -25,14 +25,14 @@ } function include_header($pageTitle, $pageType, $opendiv = true, $geolocate = false, $datepicker = false) { - global $labsPath; + global $labsPath,$serviceAlertsEnabled; echo ' -' . $pageTitle . ' +' . $pageTitle . ' - Canberra Bus Timetable @@ -156,6 +156,13 @@ echo '
Buses are running on an altered timetable today due to industrial action/public holiday. See http://www.action.act.gov.au for details.
'; } } + if ($GTFSREnabled) { + $serviceAlerts = getServiceAlertsAsArray("agency","0"); + foreach ($serviceAlerts['entity'] as $entity) { + echo "
".date("F j, g:i a",strtotime($entity['alert']['active_period'][0]['start']))." to ". date("F j, g:i a", strtotime($entity['alert']['active_period'][0]['end']))."{$entity['alert']['header_text']['translation'][0]['text']}
Warning: {$entity['alert']['description_text']['translation'][0]['text']} +
Source
"; + } + } } } function include_footer() --- a/include/common-transit.inc.php +++ b/include/common-transit.inc.php @@ -45,5 +45,216 @@ return ""; } } +if ($GTFSREnabled) { +$serviceAlertCause = Array( +"UNKNOWN_CAUSE" => "Unknown cause", +"OTHER_CAUSE" => "Other cause", +"TECHNICAL_PROBLEM" => "Technical problem", +"STRIKE" => "Strike", +"DEMONSTRATION" => "Demonstration", +"ACCIDENT" => "Accident", +"HOLIDAY" => "Holiday", +"WEATHER" => "Weather", +"MAINTENANCE" => "Maintenance", +"CONSTRUCTION" => "Construction", +"POLICE_ACTIVITY" => "Police activity", +"MEDICAL_EMERGENCY" => "Medical emergency" +); +$serviceAlertEffect = Array( +"NO_SERVICE" => "No service", +"REDUCED_SERVICE" => "Reduced service", +"SIGNIFICANT_DELAYS" => "Significant delays", +"DETOUR" => "Detour", +"ADDITIONAL_SERVICE" => "Additional service", +"MODIFIED_SERVICE" => "Modified service", +"OTHER_EFFECT" => "Other effect", +"UNKNOWN_EFFECT" => "Unknown effect", +"STOP_MOVED" => "Stop moved"); + +set_include_path(get_include_path() . PATH_SEPARATOR . $labsPath."lib/Protobuf-PHP/library/DrSlump/"); + +include_once("Protobuf.php"); +include_once("Protobuf/Message.php"); +include_once("Protobuf/Registry.php"); +include_once("Protobuf/Descriptor.php"); +include_once("Protobuf/Field.php"); + +include_once($labsPath."lib/Protobuf-PHP/gtfs-realtime.php"); +include_once("Protobuf/CodecInterface.php"); +include_once("Protobuf/Codec/PhpArray.php"); +include_once("Protobuf/Codec/Binary.php"); +include_once("Protobuf/Codec/Binary/Writer.php"); +include_once("Protobuf/Codec/Json.php"); + +function getServiceAlerts($filter_class = "", $filter_id = "") { +/* + + also need last modified epoch of client gtfs + + - add,remove,patch,inform (null) + - stop + - trip + - network + - classes (WHERE=) + - route (short_name or route_id) + - street + - stop + - trip + Currently support: + network inform + trip patch: stop remove + street inform: route inform, trip inform, stop inform + route patch: trip remove + */ + $fm = new transit_realtime\FeedMessage(); +$fh = new transit_realtime\FeedHeader(); +$fh->setGtfsRealtimeVersion(1); +$fh->setTimestamp(time()); +$fm->setHeader($fh); +foreach(getCurrentAlerts() as $alert) { +$fe = new transit_realtime\FeedEntity(); + $fe->setId($alert['id']); + $fe->setIsDeleted(false); + $alert = new transit_realtime\Alert(); + $tr = new transit_realtime\TimeRange(); + $tr->setStart($alert['start']); + $tr->setEnd($alert['end']); + $alert-> addActivePeriod($tr); + $informedEntities = getInformedAlerts($alert['id'],$_REQUEST['filter_class'],$_REQUEST['filter_id']); + if (sizeof($informedEntities) >0) { + $informed = Array(); + $es = new transit_realtime\EntitySelector(); + if ($informedEntity['informed_class'] == "agency") { + $es->setAgencyId($informedEntity['informed_id']); + } + if ($informedEntity['informed_class'] == "stop") { + $es->setStopId($informedEntity['informed_id']); + } + if ($informedEntity['informed_class'] == "route") { + $es->setRouteId($informedEntity['informed_id']); + } + if ($informedEntity['informed_class'] == "trip") { + $td = new transit_realtime\TripDescriptor(); + $td->setTripId($informedEntity['informed_id']); + $es->setTrip($td); + } + $alert-> addInformedEntity($es); + } + $alert->setCause(constant("transit_realtime\Alert\Cause::".$alert['cause'])); + $alert->setEffect(constant("transit_realtime\Alert\Effect::".$alert['effect'])); + $tsUrl = new transit_realtime\TranslatedString(); + $tUrl = new transit_realtime\TranslatedString\Translation(); + $tUrl->setText($alert['url']); + $tUrl->setLanguage("en"); + $tsUrl->addTranslation($tUrl); + $alert->setUrl($tsUrl); + $tsHeaderText= new transit_realtime\TranslatedString(); + $tHeaderText = new transit_realtime\TranslatedString\Translation(); + $tHeaderText->setText($alert['header']); + $tHeaderText->setLanguage("en"); + $tsHeaderText->addTranslation($tHeaderText); + $alert->setHeaderText($tsHeaderText); + $tsDescriptionText= new transit_realtime\TranslatedString(); + $tDescriptionText = new transit_realtime\TranslatedString\Translation(); + $tDescriptionText->setText($alert['description']); + $tDescriptionText->setLanguage("en"); + $tsDescriptionText->addTranslation($tDescriptionText); + $alert->setDescriptionText($tsDescriptionText); + $fe->setAlert($alert); +$fm->addEntity($fe); +} +return $fm; +} +function getServiceAlertsAsArray($filter_class = "", $filter_id = "") { + $codec = new DrSlump\Protobuf\Codec\PhpArray(); + return $codec->encode(getServiceAlerts($filter_class, $filter_id)); +} + +function getServiceAlertsAsBinary($filter_class = "", $filter_id = "") { + $codec = new DrSlump\Protobuf\Codec\Binary(); + return $codec->encode(getServiceAlerts($filter_class, $filter_id)); +} + +function getServiceAlertsAsJSON($filter_class = "", $filter_id = "") { + $codec = new DrSlump\Protobuf\Codec\Json(); + return $codec->encode(getServiceAlerts($filter_class, $filter_id)); +} +function getServiceAlertsByClass() { + $return = Array(); + $alerts = getServiceAlertsAsArray("",""); + foreach ($alerts['entities'] as $entity) { + foreach ($entity['informed'] as $informed) { + foreach($informed as $key => $value){ + if (strpos("_id",$key) > 0) { + $parts = explode($key); + $class = $parts[0]; + $id = $value; + } + } + $return[$class][$id][] = $entity; + } + } +} + +function getTripUpdates($filter_class = "", $filter_id = "") { + $fm = new transit_realtime\FeedMessage(); +$fh = new transit_realtime\FeedHeader(); +$fh->setGtfsRealtimeVersion(1); +$fh->setTimestamp(time()); +$fm->setHeader($fh); +foreach(getCurrentAlerts() as $alert) { + $informedEntities = getInformedAlerts($alert['id'],$_REQUEST['filter_class'],$_REQUEST['filter_id']); + $stops = Array(); + $routestrips = Array(); + if (sizeof($informedEntities) >0) { + if ($informedEntity['informed_class'] == "stop" && $informed["x-action"] == "remove") { + $stops[] = $informedEntity['informed_id']; + } + if (($informedEntity['informed_class'] == "route" || $informedEntity['informed_class'] == "trip") && $informed["x-action"] == "patch" ) { + $routestrips[] = Array( "id" => $informedEntity['informed_id'], + "type"=>$informedEntity['informed_class']); + } + } +foreach ($routestrips as $routetrip) { +$fe = new transit_realtime\FeedEntity(); + $fe->setId($alert['id'].$routetrip['id']); + $fe->setIsDeleted(false); + $tu = new transit_realtime\TripUpdate(); + $td = new transit_realtime\TripDescriptor(); + if ($routetrip['type'] == "route") { + $td->setRouteId($routetrip['id']); + } else if ($routetrip['type'] == "trip") { + $td->setTripId($routetrip['id']); + } + $tu->setTrip($td); + foreach ($stops as $stop) { + $stu = new transit_realtime\TripUpdate\StopTimeUpdate(); + $stu->setStopId($stop); + $stu->setScheduleRelationship(transit_realtime\TripUpdate\StopTimeUpdate\ScheduleRelationship::SKIPPED); + $tu->addStopTimeUpdate($stu); + } + $fe->setTripUpdate($tu); +$fm->addEntity($fe); +} + +} +return $fm; + +} +function getTripUpdatesAsArray($filter_class = "", $filter_id = "") { + $codec = new DrSlump\Protobuf\Codec\PhpArray(); + return $codec->encode(getTripUpdates($filter_class, $filter_id)); +} + +function getTripUpdatesAsBinary($filter_class = "", $filter_id = "") { + $codec = new DrSlump\Protobuf\Codec\Binary(); + return $codec->encode(getTripUpdates($filter_class, $filter_id)); +} + +function getTripUpdatesAsJSON($filter_class = "", $filter_id = "") { + $codec = new DrSlump\Protobuf\Codec\Json(); + return $codec->encode(getTripUpdates($filter_class, $filter_id)); +} +} ?> --- a/include/common.inc.php +++ b/include/common.inc.php @@ -10,6 +10,7 @@ "database", "other" ); +$GTFSREnabled = true; $cloudmadeAPIkey = "daa03470bb8740298d4b10e3f03d63e6"; $googleMapsAPIkey = "ABQIAAAA95XYXN0cki3Yj_Sb71CFvBTPaLd08ONybQDjcH_VdYtHHLgZvRTw2INzI_m17_IoOUqH3RNNmlTk1Q"; $otpAPIurl = 'http://localhost:8080/opentripplanner-api-webapp/'; @@ -31,7 +32,7 @@ function isDebugServer() { - return php_sapi_name() == "cli" || isset($_SERVER['SERVER_NAME']) && ( $_SERVER['SERVER_NAME'] == "10.0.1.154" || $_SERVER['SERVER_NAME'] == "10.1.0.4" || $_SERVER['SERVER_NAME'] == + return php_sapi_name() == "cli" || isset($_SERVER['SERVER_NAME']) && ( $_SERVER['SERVER_NAME'] == "azusa" || $_SERVER['SERVER_NAME'] == "vanille" || $_SERVER['SERVER_NAME'] == "localhost" || $_SERVER['SERVER_NAME'] == "127.0.0.1") ; } @@ -42,18 +43,22 @@ include_once ("common-request.inc.php"); include_once ("common-session.inc.php"); +include_once ("common-auth.inc.php"); include_once ("common-template.inc.php"); function isAnalyticsOn() { - return !isDebugServer(); + $user_agent = $_SERVER['HTTP_USER_AGENT']; + return !isDebugServer() && !preg_match('/cloudkick/i', $user_agent) && !preg_match('/googlebot/i', $user_agent) && +!preg_match('/baidu/i', $user_agent); } function isDebug($debugReason = "other") { global $debugOkay; return in_array($debugReason, $debugOkay, false) && isDebugServer(); } + function debug($msg, $debugReason = "other") { if (isDebug($debugReason)) echo "\n\n"; @@ -186,5 +191,6 @@ } return implode( $glue, $retVal ); } + ?> --- a/include/db/route-dao.inc.php +++ b/include/db/route-dao.inc.php @@ -1,211 +1,222 @@ prepare($query); - $query->bindParam(":routeID", $routeID); - $query->execute(); - if (!$query) { - databaseError($conn->errorInfo()); - return Array(); - } - return $query->fetch(PDO::FETCH_ASSOC); -} + +{ + global $conn; + $query = "Select * from routes where route_id = :routeID LIMIT 1"; + debug($query, "database"); + $query = $conn -> prepare($query); + $query -> bindParam(":routeID", $routeID); + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + return $query -> fetch(PDO :: FETCH_ASSOC); + } function getRouteByFullName($routeFullName) -{ - global $conn; - $query = "Select * from routes where route_short_name||route_long_name = :routeFullName LIMIT 1"; - debug($query, "database"); - $query = $conn->prepare($query); - $query->bindParam(":routeFullName", $routeFullName); - $query->execute(); - if (!$query) { - databaseError($conn->errorInfo()); - return Array(); - } - return $query->fetch(PDO::FETCH_ASSOC); -} + +{ + global $conn; + $query = "Select * from routes where route_short_name||route_long_name = :routeFullName LIMIT 1"; + debug($query, "database"); + $query = $conn -> prepare($query); + $query -> bindParam(":routeFullName", $routeFullName); + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + return $query -> fetch(PDO :: FETCH_ASSOC); + } function getRoutes() -{ - global $conn; - $query = "Select * from routes order by route_short_name;"; - debug($query, "database"); - $query = $conn->prepare($query); - $query->execute(); - if (!$query) { - databaseError($conn->errorInfo()); - return Array(); - } - return $query->fetchAll(); -} + +{ + global $conn; + $query = "Select * from routes order by route_short_name;"; + debug($query, "database"); + $query = $conn -> prepare($query); + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + return $query -> fetchAll(); + } function getRoutesByNumber($routeNumber = "") -{ - global $conn; - 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 = + +{ + global $conn; + 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 = routes.route_id join stop_times on stop_times.trip_id = trips.trip_id where route_short_name = :routeNumber OR route_short_name LIKE :routeNumber2 order by route_short_name;"; - } - else { - $query = "SELECT DISTINCT route_short_name from routes order by route_short_name"; - } - debug($query, "database"); - $query = $conn->prepare($query); - if ($routeNumber != "") { - $query->bindParam(":routeNumber", $routeNumber); - $routeNumber2 = "% ".$routeNumber; - $query->bindParam(":routeNumber2", $routeNumber2); - } - $query->execute(); - if (!$query) { - databaseError($conn->errorInfo()); - return Array(); - } - return $query->fetchAll(); -} + } + else { + $query = "SELECT DISTINCT route_short_name from routes order by route_short_name"; + } + debug($query, "database"); + $query = $conn -> prepare($query); + if ($routeNumber != "") { + $query -> bindParam(":routeNumber", $routeNumber); + $routeNumber2 = "% " . $routeNumber; + $query -> bindParam(":routeNumber2", $routeNumber2); + } + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + return $query -> fetchAll(); + } function getRoutesByNumberSeries($routeNumberSeries = "") -{ - global $conn; - if (strlen($routeNumberSeries) == 1) { - return getRoutesByNumber($routeNumberSeries); - } - $seriesMin = substr($routeNumberSeries, 0, -1) . "0"; - $seriesMax = substr($routeNumberSeries, 0, -1) . "9"; - $query = "Select distinct routes.route_id,routes.route_short_name,routes.route_long_name,service_id from routes join trips on trips.route_id = + +{ + global $conn; + if (strlen($routeNumberSeries) == 1) { + return getRoutesByNumber($routeNumberSeries); + } + $seriesMin = substr($routeNumberSeries, 0, -1) . "0"; + $seriesMax = substr($routeNumberSeries, 0, -1) . "9"; + $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 to_number(route_short_name, 'FM999') between :seriesMin and :seriesMax OR route_short_name LIKE :routeNumberSeries order by route_short_name;"; - debug($query, "database"); - $query = $conn->prepare($query); - $query->bindParam(":seriesMin", $seriesMin); - $query->bindParam(":seriesMax", $seriesMax); - $routeNumberSeries = "% ".substr($routeNumberSeries, 0, -1)."%"; - $query->bindParam(":routeNumberSeries", $routeNumberSeries); - $query->execute(); - if (!$query) { - databaseError($conn->errorInfo()); - return Array(); - } - return $query->fetchAll(); -} + debug($query, "database"); + $query = $conn -> prepare($query); + $query -> bindParam(":seriesMin", $seriesMin); + $query -> bindParam(":seriesMax", $seriesMax); + $routeNumberSeries = "% " . substr($routeNumberSeries, 0, -1) . "%"; + $query -> bindParam(":routeNumberSeries", $routeNumberSeries); + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + return $query -> fetchAll(); + } function getRouteNextTrip($routeID) -{ - global $conn; - $query = "select * from routes join trips on trips.route_id = routes.route_id + +{ + global $conn; + $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 arrival_time > :currentTime and routes.route_id = :routeID order by arrival_time limit 1"; - debug($query, "database"); - $query = $conn->prepare($query); - $query->bindParam(":currentTime", current_time()); - $query->bindParam(":routeID", $routeID); - $query->execute(); - if (!$query) { - databaseError($conn->errorInfo()); - return Array(); - } - $r = $query->fetch(PDO::FETCH_ASSOC); - - // past last trip of the day special case - if (sizeof($r) < 16) { - $query = "select * from routes join trips on trips.route_id = routes.route_id + debug($query, "database"); + $query = $conn -> prepare($query); + $query -> bindParam(":currentTime", current_time()); + $query -> bindParam(":routeID", $routeID); + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + $r = $query -> fetch(PDO :: FETCH_ASSOC); + + // past last trip of the day special case + if (sizeof($r) < 16) { + $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 arrival_time DESC limit 1"; - debug($query, "database"); - $query = $conn->prepare($query); - $query->bindParam(":routeID", $routeID); - $query->execute(); - if (!$query) { - databaseError($conn->errorInfo()); - return Array(); - } - - $r = $query->fetch(PDO::FETCH_ASSOC); - } - return $r; -} + debug($query, "database"); + $query = $conn -> prepare($query); + $query -> bindParam(":routeID", $routeID); + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + + $r = $query -> fetch(PDO :: FETCH_ASSOC); + } + return $r; + } 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(); -} + +{ + $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) -{ - global $conn; - $query = "select routes.route_id,trips.trip_id,service_id,arrival_time, stop_id, stop_sequence from routes join trips on trips.route_id = routes.route_id + +{ + global $conn; + $query = "select routes.route_id,trips.trip_id,service_id,arrival_time, stop_id, stop_sequence 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 and stop_sequence = '1' order by arrival_time "; - debug($query, "database"); - $query = $conn->prepare($query); - $query->bindParam(":routeID", $routeID); - $query->execute(); - if (!$query) { - databaseError($conn->errorInfo()); - return Array(); - } - return $query->fetchAll(); -} + debug($query, "database"); + $query = $conn -> prepare($query); + $query -> bindParam(":routeID", $routeID); + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + return $query -> fetchAll(); + } function getRoutesByDestination($destination = "", $service_period = "") -{ - global $conn; - if ($service_period == "") $service_period = service_period(); - if ($destination != "") { - $query = "SELECT DISTINCT trips.route_id,route_short_name,route_long_name, service_id + +{ + global $conn; + if ($service_period == "") $service_period = service_period(); + if ($destination != "") { + $query = "SELECT DISTINCT trips.route_id,route_short_name,route_long_name, service_id FROM stop_times join trips on trips.trip_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"; - } - else { - $query = "SELECT DISTINCT route_long_name + } + else { + $query = "SELECT DISTINCT route_long_name FROM stop_times join trips on trips.trip_id = stop_times.trip_id join routes on trips.route_id = routes.route_id WHERE service_id= :service_period order by route_long_name"; - } - debug($query, "database"); - $query = $conn->prepare($query); - $query->bindParam(":service_period", $service_period); - if ($destination != "") $query->bindParam(":destination", $destination); - $query->execute(); - if (!$query) { - databaseError($conn->errorInfo()); - return Array(); - } - return $query->fetchAll(); -} + } + debug($query, "database"); + $query = $conn -> prepare($query); + $query -> bindParam(":service_period", $service_period); + if ($destination != "") $query -> bindParam(":destination", $destination); + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + return $query -> fetchAll(); + } function getRoutesBySuburb($suburb, $service_period = "") -{ - if ($service_period == "") $service_period = service_period(); - global $conn; - $query = "SELECT DISTINCT service_id,trips.route_id,route_short_name,route_long_name + +{ + if ($service_period == "") $service_period = service_period(); + global $conn; + $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 join routes on trips.route_id = routes.route_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"; - debug($query, "database"); - $query = $conn->prepare($query); - $query->bindParam(":service_period", $service_period); - $suburb = "%" . $suburb . ";%"; - $query->bindParam(":suburb", $suburb); - $query->execute(); - if (!$query) { - databaseError($conn->errorInfo()); - return Array(); - } - return $query->fetchAll(); -} + debug($query, "database"); + $query = $conn -> prepare($query); + $query -> bindParam(":service_period", $service_period); + $suburb = "%" . $suburb . ";%"; + $query -> bindParam(":suburb", $suburb); + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + return $query -> fetchAll(); + } function getRoutesNearby($lat, $lng, $limit = "", $distance = 500) -{ - if ($service_period == "") $service_period = service_period(); - if ($limit != "") $limitSQL = " LIMIT :limit "; - global $conn; - $query = "SELECT service_id,trips.route_id,route_short_name,route_long_name,min(stops.stop_id) as stop_id, + +{ + if ($service_period == "") $service_period = service_period(); + if ($limit != "") $limitSQL = " LIMIT :limit "; + global $conn; + $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 FROM stop_times join trips on trips.trip_id = stop_times.trip_id @@ -215,16 +226,16 @@ 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 order by distance $limitSQL"; - debug($query, "database"); - $query = $conn->prepare($query); - $query->bindParam(":service_period", $service_period); - $query->bindParam(":distance", $distance); - if ($limit != "") $query->bindParam(":limit", $limit); - $query->execute(); - if (!$query) { - databaseError($conn->errorInfo()); - return Array(); - } - return $query->fetchAll(); -} + debug($query, "database"); + $query = $conn -> prepare($query); + $query -> bindParam(":service_period", $service_period); + $query -> bindParam(":distance", $distance); + if ($limit != "") $query -> bindParam(":limit", $limit); + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + return $query -> fetchAll(); + } ?> --- a/include/db/servicealert-dao.inc.php +++ b/include/db/servicealert-dao.inc.php @@ -1,53 +1,169 @@ prepare($query); // Create a prepared statement - $query->bindParam(":date", date("Ymd",($date != "" ? $date : time()))); - $query->execute(); - if (!$query) { - databaseError($conn->errorInfo()); - return Array(); - } - return $query->fetch(PDO::FETCH_ASSOC); -} +function getServiceOverride($date = "") +{ + global $conn; + $query = "Select * from calendar_dates where date = :date and exception_type = '1' LIMIT 1"; + // debug($query,"database"); + $query = $conn -> prepare($query); // Create a prepared statement + $query -> bindParam(":date", date("Ymd", ($date != "" ? $date : time()))); + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + return $query -> fetch(PDO :: FETCH_ASSOC); + } -function getCurrentAlerts() { - global $conn; - $query = "SELECT * from servicealerts_alerts"; - //debug($query, "database"); - $query = $conn->prepare($query); - //if ($stop_sequence != "") $query->bindParam(":stop_sequence", $stop_sequence); - $query->execute(); - if (!$query) { - databaseError($conn->errorInfo()); - return Array(); - } - return $query->fetchAll(); -} -function getInformedAlerts($id,$filter_class,$filter_id) { - - global $conn; - $query = "SELECT * from servicealerts_informed where servicealert_id = :servicealert_id"; - - if ($filter_class != "" && $filter_id != "") { - $query .= " AND (informed_class = :informed_class OR informed_class = 'network') AND informed_id = :informed_id"; - - } - //debug($query, "database"); - $query = $conn->prepare($query); - if ($filter_class != "" && $filter_id != "") { - $query->bindParam(":informed_class", $filter_class); - $query->bindParam(":informed_id", $filter_id); - } - $query->bindParam(":servicealert_id", $id); - $query->execute(); - if (!$query) { - databaseError($conn->errorInfo()); - return Array(); - } - return $query->fetchAll(); -} +function getServiceAlert($alertID) +{ + global $conn; + $query = "SELECT id,extract('epoch', start) as start, extract('epoch', end) as end,cause,effect,header,description,url from servicealerts_alerts where id = :servicealert_id"; + debug($query, "database"); + $query = $conn -> prepare($query); + $query -> bindParam(":servicealert_id", $alertID); + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + return $query -> fetch(PDO :: FETCH_ASSOC); + } + +function updateServiceAlert($alertID, $start, $end, $header, $description, $url) +{ + global $conn; + $query = 'update servicealerts_alerts set start=:start, "end"=:end, header=:header, description=:description, url=:url where id = :servicealert_id'; + debug($query, "database"); + $query = $conn -> prepare($query); + $query -> bindParam(":servicealert_id", $alertID); + $query -> bindParam(":start", $start); + $query -> bindParam(":end", $end); + $query -> bindParam(":header", $header); + $query -> bindParam(":description", $description); + $query -> bindParam(":url", $url); + $query -> execute(); + + print_r($conn -> errorInfo()); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + return $query -> fetch(PDO :: FETCH_ASSOC); + } + + function addServiceAlert($start, $end, $header, $description, $url) +{ + global $conn; + $query = 'INSERT INTO servicealerts_alerts (start, "end", header, description, url) VALUES (:start, :end, :header, :description, :url) '; + debug($query, "database"); + $query = $conn -> prepare($query); + $query -> bindParam(":start", $start); + $query -> bindParam(":end", $end); + $query -> bindParam(":header", $header); + $query -> bindParam(":description", $description); + $query -> bindParam(":url", $url); + $query -> execute(); + + print_r($conn -> errorInfo()); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + return $query -> fetch(PDO :: FETCH_ASSOC); + } + +function getCurrentAlerts() +{ + global $conn; + $query = "SELECT id,extract('epoch', start) as start, extract('epoch', end) as end,cause,effect,header,description,url from servicealerts_alerts where NOW() > start and NOW() < \"end\""; + // debug($query, "database"); + $query = $conn -> prepare($query); + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + return $query -> fetchAll(); + } + +function getFutureAlerts() +{ + global $conn; + $query = "SELECT id,extract('epoch', start) as start, extract('epoch', end) as end,cause,effect,header,description,url from servicealerts_alerts where NOW() > start or NOW() < \"end\""; + // debug($query, "database"); + $query = $conn -> prepare($query); + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + return $query -> fetchAll(); + } +function getInformedAlerts($id, $filter_class, $filter_id) +{ + + global $conn; + $query = "SELECT * from servicealerts_informed where servicealert_id = :servicealert_id"; + + if ($filter_class != "") { + $query .= " AND informed_class = :informed_class "; + + } + if ($filter_id != "") { + $query .= " AND informed_id = :informed_id "; + + } + // debug($query, "database"); + $query = $conn -> prepare($query); + if ($filter_class != "") { + $query -> bindParam(":informed_class", $filter_class); + } + if ($filter_id != "") { + $query -> bindParam(":informed_id", $filter_id); + } + $query -> bindParam(":servicealert_id", $id); + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + return $query -> fetchAll(); + } +function deleteInformedAlert($serviceAlertID, $class, $id) +{ + global $conn; + $query = 'DELETE from servicealerts_informed where servicealert_id = :servicealert_id and informed_class = :informed_class AND informed_id = :informed_id'; + debug($query, "database"); + $query = $conn -> prepare($query); + $query -> bindParam(":servicealert_id", $serviceAlertID); + $query -> bindParam(":informed_class", $class); + $query -> bindParam(":informed_id", $id); + $query -> execute(); + print_r($conn -> errorInfo()); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + return null; + } +function addInformedAlert($serviceAlertID, $class, $id, $action) +{ + global $conn; + $query = 'INSERT INTO servicealerts_informed (servicealert_id , informed_class , informed_id) VALUES(:servicealert_id ,:informed_class, :informed_id)'; + debug($query, "database"); + $query = $conn -> prepare($query); + $query -> bindParam(":servicealert_id", $serviceAlertID); + $query -> bindParam(":informed_class", $class); + $query -> bindParam(":informed_id", $id); + $query -> execute(); + + print_r($conn -> errorInfo()); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + return null; + + } ?> --- a/include/db/stop-dao.inc.php +++ b/include/db/stop-dao.inc.php @@ -1,131 +1,154 @@ prepare($query); - $query->bindParam(":stopID", $stopID); - $query->execute(); - if (!$query) { - databaseError($conn->errorInfo()); - return Array(); - } - return $query->fetch(PDO::FETCH_ASSOC); -} + +{ + global $conn; + $query = "Select * from stops where stop_id = :stopID LIMIT 1"; + debug($query, "database"); + $query = $conn -> prepare($query); + $query -> bindParam(":stopID", $stopID); + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + return $query -> fetch(PDO :: FETCH_ASSOC); + } function getStops($timingPointsOnly = false, $firstLetter = "", $startsWith = "") -{ - global $conn; - $conditions = Array(); - if ($timingPointsOnly) $conditions[] = "substr(stop_code,1,2) != 'Wj'"; - if ($firstLetter != "") $conditions[] = "substr(stop_name,1,1) = :firstLetter"; - if ($startsWith != "") $conditions[] = "stop_name like :startsWith"; - $query = "Select * from stops"; - if (sizeof($conditions) > 0) { - if (sizeof($conditions) > 1) { - $query.= " Where " . implode(" AND ", $conditions) . " "; - } - else { - $query.= " Where " . $conditions[0] . " "; - } - } - $query.= " order by stop_name;"; - $query = $conn->prepare($query); - if ($firstLetter != "") $query->bindParam(":firstLetter", $firstLetter); - - if ($startsWith != "") { - $startsWith = $startsWith."%"; - $query->bindParam(":startsWith", $startsWith); - } - $query->execute(); - if (!$query) { - databaseError($conn->errorInfo()); - return Array(); - } - return $query->fetchAll(); -} + +{ + global $conn; + $conditions = Array(); + if ($timingPointsOnly) $conditions[] = "substr(stop_code,1,2) != 'Wj'"; + if ($firstLetter != "") $conditions[] = "substr(stop_name,1,1) = :firstLetter"; + if ($startsWith != "") $conditions[] = "stop_name like :startsWith"; + $query = "Select * from stops"; + if (sizeof($conditions) > 0) { + if (sizeof($conditions) > 1) { + $query .= " Where " . implode(" AND ", $conditions) . " "; + } + else { + $query .= " Where " . $conditions[0] . " "; + } + } + $query .= " order by stop_name;"; + $query = $conn -> prepare($query); + if ($firstLetter != "") $query -> bindParam(":firstLetter", $firstLetter); + + if ($startsWith != "") { + $startsWith = $startsWith . "%"; + $query -> bindParam(":startsWith", $startsWith); + } + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + return $query -> fetchAll(); + } function getNearbyStops($lat, $lng, $limit = "", $distance = 1000) -{ - if ($lat == null || $lng == null) return Array(); - if ($limit != "") $limitSQL = " LIMIT :limit "; - global $conn; - $query = "Select *, ST_Distance(position, ST_GeographyFromText('SRID=4326;POINT($lng $lat)'), FALSE) as distance + +{ + if ($lat == null || $lng == null) return Array(); + if ($limit != "") $limitSQL = " LIMIT :limit "; + global $conn; + $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) order by distance $limitSQL;"; - debug($query, "database"); - $query = $conn->prepare($query); - $query->bindParam(":distance", $distance); - $query->bindParam(":limit", $limit); - $query->execute(); - if (!$query) { - databaseError($conn->errorInfo()); - return Array(); - } - return $query->fetchAll(); -} + debug($query, "database"); + $query = $conn -> prepare($query); + $query -> bindParam(":distance", $distance); + $query -> bindParam(":limit", $limit); + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + return $query -> fetchAll(); + } +function getStopsByName($name) + +{ + global $conn; + $query = "Select * from stops where stop_name LIKE :name;"; + debug($query, "database"); + $query = $conn -> prepare($query); + $name = "%" . $name . ";%"; + $query -> bindParam(":name", $name); + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + return $query -> fetchAll(); + } function getStopsBySuburb($suburb) -{ - global $conn; - $query = "Select * from stops where zone_id LIKE :suburb order by stop_name;"; - debug($query, "database"); - $query = $conn->prepare($query); - $suburb = "%" . $suburb . ";%"; - $query->bindParam(":suburb", $suburb); - $query->execute(); - if (!$query) { - databaseError($conn->errorInfo()); - return Array(); - } - return $query->fetchAll(); -} -function getStopsByStopCode($stop_code,$startsWith = "") -{ - global $conn; - $query = "Select * from stops where (stop_code = :stop_code OR stop_code LIKE :stop_code2)"; - if ($startsWith != "") $query .= " AND stop_name like :startsWith"; - - debug($query, "database"); - $query = $conn->prepare($query); - - $query->bindParam(":stop_code", $stop_code); - $stop_code2 = $stop_code . "%"; - $query->bindParam(":stop_code2", $stop_code2); - if ($startsWith != "") { - $startsWith = $startsWith."%"; - $query->bindParam(":startsWith", $startsWith); - } - $query->execute(); - if (!$query) { - databaseError($conn->errorInfo()); - return Array(); - } - return $query->fetchAll(); -} + +{ + global $conn; + $query = "Select * from stops where zone_id LIKE :suburb order by stop_name;"; + debug($query, "database"); + $query = $conn -> prepare($query); + $suburb = "%" . $suburb . ";%"; + $query -> bindParam(":suburb", $suburb); + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + return $query -> fetchAll(); + } +function getStopsByStopCode($stop_code, $startsWith = "") + +{ + global $conn; + $query = "Select * from stops where (stop_code = :stop_code OR stop_code LIKE :stop_code2)"; + if ($startsWith != "") $query .= " AND stop_name like :startsWith"; + + debug($query, "database"); + $query = $conn -> prepare($query); + + $query -> bindParam(":stop_code", $stop_code); + $stop_code2 = $stop_code . "%"; + $query -> bindParam(":stop_code2", $stop_code2); + if ($startsWith != "") { + $startsWith = $startsWith . "%"; + $query -> bindParam(":startsWith", $startsWith); + } + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + return $query -> fetchAll(); + } function getStopRoutes($stopID, $service_period) -{ - if ($service_period == "") $service_period = service_period(); - global $conn; - $query = "SELECT distinct service_id,trips.route_id,route_short_name,route_long_name + +{ + if ($service_period == "") $service_period = service_period(); + global $conn; + $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 join routes on trips.route_id = routes.route_id WHERE stop_id = :stopID AND service_id=:service_period"; - debug($query, "database"); - $query = $conn->prepare($query); - $query->bindParam(":service_period", $service_period); - $query->bindParam(":stopID", $stopID); - $query->execute(); - if (!$query) { - databaseError($conn->errorInfo()); - return Array(); - } - return $query->fetchAll(); -} + debug($query, "database"); + $query = $conn -> prepare($query); + $query -> bindParam(":service_period", $service_period); + $query -> bindParam(":stopID", $stopID); + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + return $query -> fetchAll(); + } function getStopTrips($stopID, $service_period = "", $afterTime = "", $limit = "") -{ - if ($service_period == "") $service_period = service_period(); - if ($limit != "") $limitSQL = " LIMIT :limit "; - global $conn; - 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, end_times.arrival_time as end_time + +{ + if ($service_period == "") $service_period = service_period(); + if ($limit != "") $limitSQL = " LIMIT :limit "; + global $conn; + 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, end_times.arrival_time as end_time FROM stop_times join trips on trips.trip_id = stop_times.trip_id @@ -136,9 +159,9 @@ AND service_id=:service_period AND end_times.arrival_time > :afterTime ORDER BY end_time $limitSQL"; - } - 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 + } + 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 FROM stop_times join trips on trips.trip_id = stop_times.trip_id @@ -146,45 +169,46 @@ WHERE stop_times.stop_id = :stopID AND service_id=:service_period ORDER BY arrival_time $limitSQL"; - } - debug($query, "database"); - $query = $conn->prepare($query); - $query->bindParam(":service_period", $service_period); - $query->bindParam(":stopID", $stopID); - if ($limit != "") $query->bindParam(":limit", $limit); - if ($afterTime != "") $query->bindParam(":afterTime", $afterTime); - $query->execute(); - if (!$query) { - databaseError($conn->errorInfo()); - return Array(); - } - return $query->fetchAll(); -} + } + debug($query, "database"); + $query = $conn -> prepare($query); + $query -> bindParam(":service_period", $service_period); + $query -> bindParam(":stopID", $stopID); + if ($limit != "") $query -> bindParam(":limit", $limit); + if ($afterTime != "") $query -> bindParam(":afterTime", $afterTime); + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + return $query -> fetchAll(); + } function getStopTripsWithTimes($stopID, $time = "", $service_period = "", $time_range = "", $limit = "") -{ - if ($service_period == "") $service_period = service_period(); - if ($time_range == "") $time_range = (24 * 60 * 60); - if ($time == "") $time = current_time(); - if ($limit == "") $limit = 10; - $trips = getStopTrips($stopID, $service_period, $time); - $timedTrips = Array(); - if ($trips && sizeof($trips) > 0) { - foreach ($trips as $trip) { - if ($trip['arrival_time'] != "") { - if (strtotime($trip['arrival_time']) > strtotime($time) and strtotime($trip['arrival_time']) < (strtotime($time) + $time_range)) { - $timedTrips[] = $trip; - } - } - else { - $timedTrip = getTimeInterpolatedTripAtStop($trip['trip_id'], $trip['stop_sequence']); - if ($timedTrip['arrival_time'] > $time and strtotime($timedTrip['arrival_time']) < (strtotime($time) + $time_range)) { - $timedTrips[] = $timedTrip; - } - } - if (sizeof($timedTrips) > $limit) break; - } - sktimesort($timedTrips, "arrival_time", true); - } - return $timedTrips; -} + +{ + if ($service_period == "") $service_period = service_period(); + if ($time_range == "") $time_range = (24 * 60 * 60); + if ($time == "") $time = current_time(); + if ($limit == "") $limit = 10; + $trips = getStopTrips($stopID, $service_period, $time); + $timedTrips = Array(); + if ($trips && sizeof($trips) > 0) { + foreach ($trips as $trip) { + if ($trip['arrival_time'] != "") { + if (strtotime($trip['arrival_time']) > strtotime($time) and strtotime($trip['arrival_time']) < (strtotime($time) + $time_range)) { + $timedTrips[] = $trip; + } + } + else { + $timedTrip = getTimeInterpolatedTripAtStop($trip['trip_id'], $trip['stop_sequence']); + if ($timedTrip['arrival_time'] > $time and strtotime($timedTrip['arrival_time']) < (strtotime($time) + $time_range)) { + $timedTrips[] = $timedTrip; + } + } + if (sizeof($timedTrips) > $limit) break; + } + sktimesort($timedTrips, "arrival_time", true); + } + return $timedTrips; + } ?> --- a/include/db/trip-dao.inc.php +++ b/include/db/trip-dao.inc.php @@ -1,240 +1,251 @@ prepare($query); - $query->bindParam(":tripID", $tripID); - $query->execute(); - if (!$query) { - databaseError($conn->errorInfo()); - return Array(); - } - return $query->fetch(PDO::FETCH_ASSOC); -} + debug($query, "database"); + $query = $conn -> prepare($query); + $query -> bindParam(":tripID", $tripID); + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + return $query -> fetch(PDO :: FETCH_ASSOC); + } function getTripShape($tripID) -{ - global $conn; - $query = "SELECT ST_AsKML(ST_MakeLine(geometry(a.position))) as the_route + +{ + global $conn; + $query = "SELECT ST_AsKML(ST_MakeLine(geometry(a.position))) as the_route FROM (SELECT position, stop_sequence, trips.trip_id FROM stop_times join trips on trips.trip_id = stop_times.trip_id join stops on stops.stop_id = stop_times.stop_id WHERE trips.trip_id = :tripID ORDER BY stop_sequence) as a group by a.trip_id"; - debug($query, "database"); - $query = $conn->prepare($query); - $query->bindParam(":tripID", $tripID); - $query->execute(); - if (!$query) { - databaseError($conn->errorInfo()); - return Array(); - } - return $query->fetchColumn(0); -} + debug($query, "database"); + $query = $conn -> prepare($query); + $query -> bindParam(":tripID", $tripID); + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + return $query -> fetchColumn(0); + } function getTimeInterpolatedTrip($tripID, $range = "") -{ - global $conn; - $query = "SELECT stop_times.trip_id,arrival_time,stop_times.stop_id,stop_lat,stop_lon,stop_name,stop_code, + +{ + global $conn; + $query = "SELECT stop_times.trip_id,arrival_time,stop_times.stop_id,stop_lat,stop_lon,stop_name,stop_code, stop_sequence,service_id,trips.route_id,route_short_name,route_long_name FROM stop_times join trips on trips.trip_id = stop_times.trip_id join routes on trips.route_id = routes.route_id join stops on stops.stop_id = stop_times.stop_id WHERE trips.trip_id = :tripID $range ORDER BY stop_sequence"; - debug($query, "database"); - $query = $conn->prepare($query); - $query->bindParam(":tripID", $tripID); - $query->execute(); - if (!$query) { - databaseError($conn->errorInfo()); - return Array(); - } - $stopTimes = $query->fetchAll(); - $cur_timepoint = Array(); - $next_timepoint = Array(); - $distance_between_timepoints = 0.0; - $distance_traveled_between_timepoints = 0.0; - $rv = Array(); - foreach ($stopTimes as $i => $stopTime) { - if ($stopTime['arrival_time'] != "") { - // is timepoint - $cur_timepoint = $stopTime; - $distance_between_timepoints = 0.0; - $distance_traveled_between_timepoints = 0.0; - if ($i + 1 < sizeof($stopTimes)) { - $k = $i + 1; - $distance_between_timepoints+= distance($stopTimes[$k - 1]["stop_lat"], $stopTimes[$k - 1]["stop_lon"], $stopTimes[$k]["stop_lat"], $stopTimes[$k]["stop_lon"]); - while ($stopTimes[$k]["arrival_time"] == "" && $k + 1 < sizeof($stopTimes)) { - $k+= 1; - //echo "k".$k; - $distance_between_timepoints+= distance($stopTimes[$k - 1]["stop_lat"], $stopTimes[$k - 1]["stop_lon"], $stopTimes[$k]["stop_lat"], $stopTimes[$k]["stop_lon"]); - } - $next_timepoint = $stopTimes[$k]; - - } - $rv[] = $stopTime; - } - else { - // is untimed point - //echo "i".$i; - $distance_traveled_between_timepoints+= distance($stopTimes[$i - 1]["stop_lat"], $stopTimes[$i - 1]["stop_lon"], $stopTimes[$i]["stop_lat"], $stopTimes[$i]["stop_lon"]); - //echo "$distance_traveled_between_timepoints / $distance_between_timepoints
"; - $distance_percent = $distance_traveled_between_timepoints / $distance_between_timepoints; - if ($next_timepoint["arrival_time"] != "") { - $total_time = strtotime($next_timepoint["arrival_time"]) - strtotime($cur_timepoint["arrival_time"]); - //echo strtotime($next_timepoint["arrival_time"])." - ".strtotime($cur_timepoint["arrival_time"])."
"; - $time_estimate = ($distance_percent * $total_time) + strtotime($cur_timepoint["arrival_time"]); - $stopTime["arrival_time"] = date("H:i:s", $time_estimate); - } - else { - $stopTime["arrival_time"] = $cur_timepoint["arrival_time"]; - } - $rv[] = $stopTime; - - - } - } - //var_dump($rv); - return $rv; -} + debug($query, "database"); + $query = $conn -> prepare($query); + $query -> bindParam(":tripID", $tripID); + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + $stopTimes = $query -> fetchAll(); + $cur_timepoint = Array(); + $next_timepoint = Array(); + $distance_between_timepoints = 0.0; + $distance_traveled_between_timepoints = 0.0; + $rv = Array(); + foreach ($stopTimes as $i => $stopTime) { + if ($stopTime['arrival_time'] != "") { + // is timepoint + $cur_timepoint = $stopTime; + $distance_between_timepoints = 0.0; + $distance_traveled_between_timepoints = 0.0; + if ($i + 1 < sizeof($stopTimes)) { + $k = $i + 1; + $distance_between_timepoints += distance($stopTimes[$k - 1]["stop_lat"], $stopTimes[$k - 1]["stop_lon"], $stopTimes[$k]["stop_lat"], $stopTimes[$k]["stop_lon"]); + while ($stopTimes[$k]["arrival_time"] == "" && $k + 1 < sizeof($stopTimes)) { + $k += 1; + // echo "k".$k; + $distance_between_timepoints += distance($stopTimes[$k - 1]["stop_lat"], $stopTimes[$k - 1]["stop_lon"], $stopTimes[$k]["stop_lat"], $stopTimes[$k]["stop_lon"]); + } + $next_timepoint = $stopTimes[$k]; + + } + $rv[] = $stopTime; + } + else { + // is untimed point + // echo "i".$i; + $distance_traveled_between_timepoints += distance($stopTimes[$i - 1]["stop_lat"], $stopTimes[$i - 1]["stop_lon"], $stopTimes[$i]["stop_lat"], $stopTimes[$i]["stop_lon"]); + // echo "$distance_traveled_between_timepoints / $distance_between_timepoints
"; + $distance_percent = $distance_traveled_between_timepoints / $distance_between_timepoints; + if ($next_timepoint["arrival_time"] != "") { + $total_time = strtotime($next_timepoint["arrival_time"]) - strtotime($cur_timepoint["arrival_time"]); + // echo strtotime($next_timepoint["arrival_time"])." - ".strtotime($cur_timepoint["arrival_time"])."
"; + $time_estimate = ($distance_percent * $total_time) + strtotime($cur_timepoint["arrival_time"]); + $stopTime["arrival_time"] = date("H:i:s", $time_estimate); + } + else { + $stopTime["arrival_time"] = $cur_timepoint["arrival_time"]; + } + $rv[] = $stopTime; + + + } + } + // var_dump($rv); + return $rv; + } function getTripPreviousTimePoint($tripID, $stop_sequence) -{ - global $conn; - $query = " SELECT trip_id,stop_id, + +{ + global $conn; + $query = " SELECT trip_id,stop_id, stop_sequence FROM stop_times WHERE trip_id = :tripID and stop_sequence < :stop_sequence and stop_times.arrival_time IS NOT NULL ORDER BY stop_sequence DESC LIMIT 1"; - debug($query, "database"); - $query = $conn->prepare($query); - $query->bindParam(":tripID", $tripID); - $query->bindParam(":stop_sequence", $stop_sequence); - $query->execute(); - if (!$query) { - databaseError($conn->errorInfo()); - return Array(); - } - return $query->fetch(PDO::FETCH_ASSOC); -} + debug($query, "database"); + $query = $conn -> prepare($query); + $query -> bindParam(":tripID", $tripID); + $query -> bindParam(":stop_sequence", $stop_sequence); + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + return $query -> fetch(PDO :: FETCH_ASSOC); + } function getTripNextTimePoint($tripID, $stop_sequence) -{ - global $conn; - $query = " SELECT trip_id,stop_id, + +{ + global $conn; + $query = " SELECT trip_id,stop_id, stop_sequence FROM stop_times WHERE trip_id = :tripID and stop_sequence > :stop_sequence and stop_times.arrival_time IS NOT NULL ORDER BY stop_sequence LIMIT 1"; - debug($query, "database"); - $query = $conn->prepare($query); - $query->bindParam(":tripID", $tripID); - $query->bindParam(":stop_sequence", $stop_sequence); - $query->execute(); - if (!$query) { - databaseError($conn->errorInfo()); - return Array(); - } - return $query->fetch(PDO::FETCH_ASSOC); -} + debug($query, "database"); + $query = $conn -> prepare($query); + $query -> bindParam(":tripID", $tripID); + $query -> bindParam(":stop_sequence", $stop_sequence); + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + return $query -> fetch(PDO :: FETCH_ASSOC); + } function getTimeInterpolatedTripAtStop($tripID, $stop_sequence) -{ - global $conn; - // limit interpolation to between nearest actual points. - $prevTimePoint = getTripPreviousTimePoint($tripID, $stop_sequence); - $nextTimePoint = getTripNextTimePoint($tripID, $stop_sequence); - //echo " prev {$lowestDelta['stop_sequence']} next {$nextTimePoint['stop_sequence']} "; - $range = ""; - if ($prevTimePoint != "") $range .= " AND stop_sequence >= '{$prevTimePoint['stop_sequence']}'"; - if ($nextTimePoint != "") $range .= " AND stop_sequence <= '{$nextTimePoint['stop_sequence']}'"; - foreach (getTimeInterpolatedTrip($tripID, $range) as $tripStop) { - if ($tripStop['stop_sequence'] == $stop_sequence) return $tripStop; - } - return Array(); -} + +{ + global $conn; + // limit interpolation to between nearest actual points. + $prevTimePoint = getTripPreviousTimePoint($tripID, $stop_sequence); + $nextTimePoint = getTripNextTimePoint($tripID, $stop_sequence); + // echo " prev {$lowestDelta['stop_sequence']} next {$nextTimePoint['stop_sequence']} "; + $range = ""; + if ($prevTimePoint != "") $range .= " AND stop_sequence >= '{$prevTimePoint['stop_sequence']}'"; + if ($nextTimePoint != "") $range .= " AND stop_sequence <= '{$nextTimePoint['stop_sequence']}'"; + foreach (getTimeInterpolatedTrip($tripID, $range) as $tripStop) { + if ($tripStop['stop_sequence'] == $stop_sequence) return $tripStop; + } + return Array(); + } function getTripStartTime($tripID) -{ - global $conn; - $query = "Select * from stop_times + +{ + global $conn; + $query = "Select * from stop_times where trip_id = :tripID AND arrival_time IS NOT NULL AND stop_sequence = '1'"; - debug($query, "database"); - $query = $conn->prepare($query); - $query->bindParam(":tripID", $tripID); - $query->execute(); - if (!$query) { - databaseError($conn->errorInfo()); - return Array(); - } - $r = $query->fetch(PDO::FETCH_ASSOC); - return $r['arrival_time']; -} + debug($query, "database"); + $query = $conn -> prepare($query); + $query -> bindParam(":tripID", $tripID); + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + $r = $query -> fetch(PDO :: FETCH_ASSOC); + return $r['arrival_time']; + } function getTripEndTime($tripID) -{ - global $conn; - $query = "SELECT trip_id,max(arrival_time) as arrival_time from stop_times + +{ + global $conn; + $query = "SELECT trip_id,max(arrival_time) as arrival_time from stop_times WHERE stop_times.arrival_time IS NOT NULL and trip_id = :tripID group by trip_id"; - debug($query, "database"); - $query = $conn->prepare($query); - $query->bindParam(":tripID", $tripID); - $query->execute(); - if (!$query) { - databaseError($conn->errorInfo()); - return Array(); - } - $r = $query->fetch(PDO::FETCH_ASSOC); - return $r['arrival_time']; -} + debug($query, "database"); + $query = $conn -> prepare($query); + $query -> bindParam(":tripID", $tripID); + $query -> execute(); + if (!$query) { + databaseError($conn -> errorInfo()); + return Array(); + } + $r = $query -> fetch(PDO :: FETCH_ASSOC); + return $r['arrival_time']; + } function getActiveTrips($time) -{ - global $conn; - if ($time == "") $time = current_time(); - $query = "Select distinct stop_times.trip_id, start_times.arrival_time as start_time, end_times.arrival_time as end_time from stop_times, (SELECT trip_id,arrival_time from stop_times WHERE stop_times.arrival_time IS NOT NULL + +{ + global $conn; + if ($time == "") $time = current_time(); + $query = "Select distinct stop_times.trip_id, start_times.arrival_time as start_time, end_times.arrival_time as end_time from stop_times, (SELECT trip_id,arrival_time from stop_times WHERE stop_times.arrival_time IS NOT NULL AND stop_sequence = '1') as start_times, (SELECT trip_id,max(arrival_time) as arrival_time from stop_times WHERE stop_times.arrival_time IS NOT NULL group by trip_i