March 2012 school holidays support
March 2012 school holidays support

file:b/geo/trips.kml.php (new)
--- /dev/null
+++ b/geo/trips.kml.php
@@ -1,1 +1,51 @@
+<?php
+header('Content-Type: application/vnd.google-earth.kml+xml');
+include ('../include/common.inc.php');
+header('Content-Disposition: attachment; filename="trips.' . date('c') . '.kml"');
+$debugOkay = Array(); // disable debugging output even on dev server
+echo '<?xml version="1.0" encoding="UTF-8"?>
+<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom"><Document>';
+echo '
+      <Style id="ylw-pushpin">
+    <IconStyle>
+      <Icon>
+        <href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
+        
+      </Icon>
+    </IconStyle>
+    
+  </Style>
+          <Style id="bus-pushpin">
+    <IconStyle>
+      <Icon>
+        <href>http://google-maps-icons.googlecode.com/files/bus.png</href>
+        
+      </Icon>
+    </IconStyle>
+    
+  </Style>
+          <Style id="grn-pushpin">
+    <IconStyle>
+      <Icon>
+        <href>http://maps.google.com/mapfiles/kml/pushpin/grn-pushpin.png</href>
+        
+      </Icon>
+    </IconStyle>
+  </Style>';
 
+foreach (getActiveTrips() as $trip) {
+    echo "\n<Placemark>\n";
+    $link = curPageURL() . '/../trip.php?tripid=' . htmlspecialchars($trip['trip_id']);
+    $lastStop = getTripLastStop($trip['trip_id']);
+    echo "<name>" . $lastStop[0]['arrival_time'] . " @ " . htmlspecialchars($lastStop[0]['stop_name']) . "</name>";
+    echo '<atom:link rel="related" href="' . $link . '"/>';
+    echo '<description><![CDATA[ <a href="' . $link . '">' . htmlspecialchars($lastStop[0]['stop_name']) . "</a>]]> </description>";
+    echo "<styleUrl>#bus-pushpin</styleUrl>";
+    echo "<Point><coordinates>" . $lastStop[0]['stop_lon'] . "," . $lastStop[0]['stop_lat'] . "</coordinates></Point>";
+
+    echo "</Placemark>\n";
+}
+echo "</Document></kml>\n";
+?>
+
+

--- a/include/common-geo.inc.php
+++ b/include/common-geo.inc.php
@@ -27,13 +27,13 @@
     if (sizeof($mapPoints) < 1)
         return 'map error';
     if (sizeof($mapPoints) === 1) {
-        $markers = 'markers={$mapPoints[0][0]},{$mapPoints[0][1]}';
+        $markers = 'markers='.$mapPoints[0][0].','.$mapPoints[0][1];
     } else {
         if (!$numbered) {
             $markers = 'markers=';
         }
         if ($path) {
-            $markers.= 'markers={$mapPoints[0][0]},{$mapPoints[0][1]}&amp;path=';
+            $markers.= 'markers='.$mapPoints[0][0].','.$mapPoints[0][1].'&amp;path=';
         }
         foreach ($mapPoints as $index => $mapPoint) {
             if ($twotone && $index == 0) {

--- a/include/common-transit.inc.php
+++ b/include/common-transit.inc.php
@@ -55,7 +55,7 @@
 // school holidays
             $ymd = date('Ymd', $date);
             $dow = date('w', $date);
-            if (intval($ymd) < '20120203' && $dow != 0 && $dow != 6) {
+            if (intval($ymd) < '20120427' && $dow != 0 && $dow != 6) {
                 return Array('Weekday-SchoolVacation', 'Weekday-SchoolVacation');
             } else {
                 return Array('Weekday', 'Weekday');

--- a/include/common.inc.php
+++ b/include/common.inc.php
@@ -93,7 +93,7 @@
 
 function debug($msg, $debugReason = 'other') {
     if (isDebug($debugReason))
-        echo PHP_EOL.'<!-- ' . date(DATE_RFC822) . PHP_EOL.' $msg -->'.PHP_EOL;
+        echo PHP_EOL.'<!-- ' . date(DATE_RFC822) . PHP_EOL.$msg.PHP_EOL.' -->'.PHP_EOL;
 }
 function isIOSDevice() {
    return strstr($_SERVER['HTTP_USER_AGENT'], 'iPhone') || strstr($_SERVER['HTTP_USER_AGENT'], 'iPod') || strstr($_SERVER['HTTP_USER_AGENT'], 'iPad');

--- a/include/db/route-dao.inc.php
+++ b/include/db/route-dao.inc.php
@@ -292,7 +292,7 @@
 WHERE (service_id=:service_periodA OR service_id=:service_periodB)
 AND ST_DWithin(position, ST_GeographyFromText(\'SRID=4326;POINT($lng $lat)\'), :distance, FALSE)
         group by service_id,trips.route_id,trips.direction_id,route_short_name,route_long_name
-        order by distance $limitSQL';
+        order by distance '.$limitSQL;
     debug($query, 'database');
     $query = $conn->prepare($query);
     $query->bindParam(':service_periodA', $sidA);

--- a/include/db/stop-dao.inc.php
+++ b/include/db/stop-dao.inc.php
@@ -69,9 +69,9 @@
     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;';
+    $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);
@@ -186,7 +186,7 @@
 AND stop_times.trip_id = end_times.trip_id
 AND (service_id=:service_periodA OR service_id=:service_periodB) ' . ($route_short_name != '' ? ' AND route_short_name = :route_short_name ' : '') . ' 
 AND end_times.arrival_time > :afterTime
-ORDER BY end_time $limitSQL';
+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
 FROM stop_times
@@ -195,7 +195,7 @@
 join routes on trips.route_id = routes.route_id
 WHERE stop_times.stop_id = :stopID
 AND (service_id=:service_periodA OR service_id=:service_periodB) ' . ($route_short_name != '' ? ' AND route_short_name = :route_short_name ' : '') . ' 
-ORDER BY arrival_time $limitSQL';
+ORDER BY arrival_time '.$limitSQL;
     }
     debug($query, 'database');
     $query = $conn->prepare($query);

--- a/include/db/trip-dao.inc.php
+++ b/include/db/trip-dao.inc.php
@@ -72,7 +72,6 @@
 }
 
 function getTripShape($tripID) {
-    // todo, use shapes table if shape_id specified
     global $conn;
     $query = 'SELECT ST_AsKML(ST_MakeLine(geometry(a.shape_pt))) as the_route
 FROM (SELECT shapes.shape_id,shape_pt from shapes
@@ -188,16 +187,34 @@
     return $r;
 }
 
-function getActiveTrips($time) {
-    global $conn;
-    if ($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
 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_id) as end_times
 WHERE start_times.trip_id = end_times.trip_id AND stop_times.trip_id = end_times.trip_id AND :time > start_times.arrival_time  AND :time < end_times.arrival_time';
     debug($query, 'database');
     $query = $conn->prepare($query);
     $query->bindParam(':time', $time);
+    $query->execute();
+    if (!$query) {
+        databaseError($conn->errorInfo());
+        return Array();
+    }
+    return $query->fetchAll();
+}
+function getTripLastStop($tripid, $time='') {
+    global $conn;
+    if ($time == '') {
+        $time = current_time();
+    }
+    $query = 'Select trip_id,stops.stop_id,stop_sequence,stop_code,stop_name,stop_lat,stop_lon,arrival_time,(arrival_time - :time::time) as time_diff  from stop_times inner join stops on stop_times.stop_id = stops.stop_id WHERE trip_id = :tripid  and arrival_time >= :time::time order by stop_sequence limit 2';
+    debug($query, 'database');
+    $query = $conn->prepare($query);
+    $query->bindParam(':time', $time);
+    $query->bindParam(':tripid', $tripid);
     $query->execute();
     if (!$query) {
         databaseError($conn->errorInfo());