Stop using session for time/day
[busui.git] / stop.php
blob:a/stop.php -> blob:b/stop.php
--- a/stop.php
+++ b/stop.php
@@ -16,8 +16,9 @@
   limitations under the License.
  */
 include ('include/common.inc.php');
-if ($stopid)
+if (isset($stopid)) {
     $stop = getStop($stopid);
+}
 /* if ($stopcode != "" && $stop[5] != $stopcode) {
   $url = $APIurl . "/json/stopcodesearch?q=" . $stopcode;
   $stopsearch = json_decode(getPage($url));
@@ -73,15 +74,19 @@
         //}
     }
 }
-include_header($stop['stop_name'], "stop");
-
+if (sizeof($stops) > 0) {
+    $stopDescParts = explode("<br>", $stop['stop_desc']);
+    include_header(trim(str_replace("Street: ", "", $stopDescParts[0])), "stop");
+} else {
+    include_header($stop['stop_name'], "stop");
+}
 /* $serviceAlerts = json_decode(getPage(curPageURL() . "/servicealerts_api.php?filter_class=stop&filter_id=".$stopid) , true);
 
   foreach($serviceAlerts['entities'] as $serviceAlert) {
   echo '<div id="servicewarning">'.$serviceAlert['alert']['description']['translation'].'</div>';
   } */
 
-echo '<span class="content-secondary">';
+echo '<div class="content-secondary">';
 echo $stopLinks;
 if (sizeof($stops) > 0) {
     trackEvent("View Stops", "View Combined Stops", $stop["stop_name"], $stop["stop_id"]);
@@ -96,56 +101,39 @@
     ));
 }
 
-// time settings
-echo '<div id="settings" data-role="collapsible" data-collapsed="true">
-<h3>Change Time (' . (isset($_REQUEST['time']) ? $_REQUEST['time'] : "Current Time,") . ' ' . ucwords(service_period()) . ')...</h3>
-        <form action="' . basename($_SERVER['PHP_SELF']) . '" method="GET">
-            <input type="hidden" name="stopid" id="stopid" value="' . (isset($_REQUEST['stopid']) ? $_REQUEST['stopid'] : "") . '"/>
-                 <input type="hidden" name="stopcode" id="stopcode" value="' . (isset($_REQUEST['stopcode']) ? $_REQUEST['stopcode'] : "") . '"/>
-        <div class="ui-body"> 
-    		<div data-role="fieldcontain">
-		        <label for="time"> Time: </label>
-		    	<input type="time" name="time" id="time" value="' . (isset($_REQUEST['time']) ? $_REQUEST['time'] : date("H:i")) . '"/>
-			<a href="#" name="currentTime" id="currentTime" onClick="var d = new Date();' . "$('#time').val(d.getHours() +':'+ (d.getMinutes().toString().length == 1 ? '0'+ d.getMinutes():  d.getMinutes()));" . '">Current Time?</a>
-	        </div>
-		<div data-role="fieldcontain">
-		    <label for="service_period"> Service Period:  </label>
-			<select name="service_period" id="service_period">';
-foreach ($service_periods as $service_period) {
-    echo "<option value=\"$service_period\"" . (service_period() === $service_period ? " SELECTED" : "") . '>' . ucwords($service_period) . '</option>';
-}
-echo '</select>
-			<a href="#" style="display:none" name="currentPeriod" id="currentPeriod">Current Period?</a>
-		</div>
-		
-		<input type="submit" value="Update"/>
-                </div></form>
-            </div>';
-
-echo '</span><span class="content-primary">';
+timeSettings();
+
+echo '</div><div class="content-primary">';
 echo '  <ul data-role="listview"  data-inset="true">';
 if (sizeof($allStopsTrips) > 0) {
     sktimesort($allStopsTrips, "arrival_time", true);
     $trips = $allStopsTrips;
 } else {
-    $trips = getStopTripsWithTimes($stopid);
-}
+    $trips = getStopTripsWithTimes($stopid, "", "", "", (isset($filterIncludeRoutes) || isset($filterHasStop) ? "75" : ""));
+}
+
 echo "<div class='ui-header' style='overflow: visible; height: 2.5em'>";
+// if we have too many trips, cut down to size.
+if (!isset($filterIncludeRoutes) && !isset($filterHasStop) && sizeof($trips) > 10) {
+    $trips = array_splice($trips, 0, 10);
+}
+
 // later/earlier button setup
 if (sizeof($trips) == 0) {
     $time = isset($_REQUEST['time']) ? strtotime($_REQUEST['time']) : time();
     $earlierTime = $time - (90 * 60);
     $laterTime = $time + (90 * 60);
 } else {
-    $earlierTime = strtotime($trips[0]['arrival_time']) - (90 * 60);
-    $laterTime = strtotime($trips[sizeof($trips) - 1]['arrival_time']) - 60;
-}
-if (sizeof($stopids) > 0) {
+    $tripsKeys = array_keys($trips);
+    $earlierTime = strtotime($trips[$tripsKeys[0]]['arrival_time']) - (90 * 60);
+    $laterTime = strtotime($trips[$tripsKeys[sizeof($trips) - 1]]['arrival_time']) - 60;
+}
+if (isset($stopids) && sizeof($stopids) > 0) {
     $stopidurl = "stopids=" . implode(",", $stopids);
 } else {
     $stopidurl = "stopid=$stopid";
 }
-if (sizeof($trips) > 10) {
+if (sizeof($trips) >= 10) {
     echo '<a href="stop.php?' . $stopidurl . '&service_period=' . service_period() . '&time=' . date("H:i", $laterTime) . '" data-icon="arrow-r" class="ui-btn-right">Later Trips</a>';
 }
 echo '<a href="stop.php?' . $stopidurl . '&service_period=' . service_period() . '&time=' . date("H:i", $earlierTime) . '" data-icon="arrow-l" class="ui-btn-left">Earlier Trips</a>';
@@ -153,34 +141,67 @@
 if (sizeof($trips) == 0) {
     echo "<li style='text-align: center;'>No trips in the near future.</li>";
 } else {
+            if (isset($labs)) {
+// ETA calculation
+                
+                $tripETA = Array();
+                // max/min delay instead of stddev?
+                $query = $query = "select 'lol', avg(timing_delta), stddev(timing_delta), count(*) from myway_timingdeltas where extract(hour from time) between ".date("H", $earlierTime)." and ".date("H", $laterTime);
+       //select 'lol', stop_id,extract(hour from time), avg(timing_delta), stddev(timing_delta), count(*) from myway_timingdeltas where stop_id = '5501' group by stop_id, extract(hour from time) order by extract(hour from time)
+                $query = $conn->prepare($query);
+    $query->execute();
+    if (!$query) {
+        databaseError($conn->errorInfo());
+        return Array();
+    }
+    $ETAparams = Array();
+    foreach ($query->fetchAll() as $row) {
+        $ETAparams[$row[0]] = Array("avg"=> $row[1], "stddev"=>floor($row[2]),"count"=>$row[3]);
+    };
+    //print_r($ETAparams);
     foreach ($trips as $trip) {
-        echo '<li>';
-
-        $destination = getTripDestination($trip['trip_id']);
-        echo '<a href="trip.php?stopid=' . $stopid . '&amp;tripid=' . $trip['trip_id'] . '"><h3>' . $trip['route_short_name'] . " towards " . $destination['stop_name'] . "</h3><p>";
-        $viaPoints = viaPointNames($trip['trip_id'], $trip['stop_sequence']);
-        if ($viaPoints != "")
-            echo '<br><span class="viaPoints">Via: ' . $viaPoints . '</span>';
-        if (sizeof($tripStopNumbers) > 0) {
-            echo '<br><small>Boarding At: ';
-            if (sizeof($tripStopNumbers[$trip['trip_id']]) == sizeof($stopids)) {
-                echo "All Stops";
-            } else {
-                foreach ($tripStopNumbers[$trip['trip_id']] as $key) {
-                    echo $stopNames[$key] . ', ';
+        $tripETA[$trip['trip_id']] = date("H:i",strtotime($trip['arrival_time']." - ".(floor($ETAparams['lol']['stddev']))." seconds"))." to ".
+        date("H:i",strtotime($trip['arrival_time']." + ".(floor($ETAparams['lol']['stddev']))." seconds"));
+    }
+    //print_r($tripETA);
+}
+    foreach ($trips as $trip) {
+        if (
+                isset($filterHasStop) && (getTripHasStop($trip['trip_id'], $filterHasStop) == 1)
+                || (isset($filterIncludeRoutes) && in_array($trip["route_short_name"], $filterIncludeRoutes))
+                || (!isset($filterIncludeRoutes) && !isset($filterHasStop))
+        ) {
+            echo '<li>';
+
+            $destination = getTripDestination($trip['trip_id']);
+            echo '<a href="trip.php?stopid=' . $stopid . '&amp;tripid=' . $trip['trip_id'] . '"><h3>' . $trip['route_short_name'] . " towards " . $destination['stop_name'] . "</h3><p>";
+            $viaPoints = viaPointNames($trip['trip_id'], $trip['stop_sequence']);
+if (isset($labs)) {
+                echo '<br><span class="eta">ETA: ' . $tripETA[$trip['trip_id']] . '</span>';
+            }
+            if ($viaPoints != "")
+                echo '<br><span class="viaPoints">Via: ' . $viaPoints . '</span>';
+            if (sizeof($tripStopNumbers) > 0) {
+                echo '<br><small>Boarding At: ';
+                if (sizeof($tripStopNumbers[$trip['trip_id']]) == sizeof($stopids)) {
+                    echo "All Stops";
+                } else {
+                    foreach ($tripStopNumbers[$trip['trip_id']] as $key) {
+                        echo $stopNames[$key] . ', ';
+                    }
                 }
+                echo '</small>';
             }
-            echo '</small>';
+            echo '</p>';
+            echo '<p class="ui-li-aside"><strong>' . $trip['arrival_time'] . '</strong></p>';
+            echo '</a></li>';
+            flush();
+            @ob_flush();
         }
-        echo '</p>';
-        echo '<p class="ui-li-aside"><strong>' . $trip['arrival_time'] . '</strong></p>';
-        echo '</a></li>';
-        flush();
-        @ob_flush();
     }
 }
 echo '</ul>';
-echo '</span>';
+echo '</div>';
 include_footer();
 ?>