Nearby routes and routes by suburb
Nearby routes and routes by suburb

--- a/common.inc.php
+++ b/common.inc.php
@@ -150,5 +150,33 @@
 {
 	return str_replace(")", "</small>", str_replace("(", "<br><small>", $input));
 }
+
+function sksort(&$array, $subkey="id", $sort_ascending=false) {
+
+    if (count($array))
+        $temp_array[key($array)] = array_shift($array);
+
+    foreach($array as $key => $val){
+        $offset = 0;
+        $found = false;
+        foreach($temp_array as $tmp_key => $tmp_val)
+        {
+            if(!$found and strtolower($val[$subkey]) > strtolower($tmp_val[$subkey]))
+            {
+                $temp_array = array_merge(    (array)array_slice($temp_array,0,$offset),
+                                            array($key => $val),
+                                            array_slice($temp_array,$offset)
+                                          );
+                $found = true;
+            }
+            $offset++;
+        }
+        if(!$found) $temp_array = array_merge($temp_array, array($key => $val));
+    }
+
+    if ($sort_ascending) $array = array_reverse($temp_array);
+
+    else $array = $temp_array;
+}
 ?>
 

--- a/routeList.php
+++ b/routeList.php
@@ -1,7 +1,8 @@
 <?php
 include ('common.inc.php');
-include_header("Routes", "routeList");
-echo '
+function navbar()
+{
+	echo '
 		<div data-role="navbar"> 
 			<ul> 
 				<li><a href="routeList.php">By Final Destination...</a></li> 
@@ -11,16 +12,55 @@
 			</ul>
                 </div>
 	';
-echo '  <ul data-role="listview"  data-inset="true">';
-$url = $APIurl . "/json/routes";
-$contents = json_decode(getPage($url));
-function printRoutes($routes)
-{
+}
+if ($_REQUEST['bysuburb']) {
+	include_header("Routes by Suburb", "routeList");
+	navbar();
+	echo '  <ul data-role="listview" data-filter="true" data-inset="true" >';
+	foreach ($suburbs as $suburb) {
+		if (!isset($_REQUEST['firstLetter'])) {
+			foreach (range('A', 'Z') as $letter) {
+				echo "<li><a href=\"routeList.php?firstLetter=$letter&bysuburb=yes\">$letter...</a></li>\n";
+			}
+		}
+		else if (startsWith($suburb, $_REQUEST['firstLetter'])) {
+			echo '<li><a href="routeList.php?suburb=' . urlencode($suburb) . '">' . $suburb . '</a></li>';
+		}
+	}
+	echo '</ul>';
+}
+else if ($_REQUEST['nearby'] || $_REQUEST['suburb']) {
+	if ($_REQUEST['suburb']) {
+		$suburb = filter_var($_REQUEST['suburb'], FILTER_SANITIZE_STRING);
+		$url = $APIurl . "/json/stopzonesearch?q=" . $suburb;
+		include_header("Routes by Suburb", "routeList");
+	}
+	if ($_REQUEST['nearby']) {
+		$url = $APIurl . "/json/neareststops?lat={$_SESSION['lat']}&lon={$_SESSION['lon']}&limit=15";
+		include_header("Routes Nearby", "routeList");
+	}
+	$stops = json_decode(getPage($url));
+	$routes = Array();
+	foreach ($stops as $stop) {
+		$url = $APIurl . "/json/stoproutes?stop=" . $stop[0];
+		$stoproutes = json_decode(getPage($url));
+		foreach ($stoproutes as $route) {
+			if (!isset($routes[$route[0]])) $routes[$route[0]] = $route;
+		}
+	}
+	navbar();
+	echo '  <ul data-role="listview" data-filter="true" data-inset="true" >';
+	sksort($routes, 1, true);
 	foreach ($routes as $row) {
-		echo '<li>' . $row[1] . ' <a href="trip.php?routeid=' . $row[0] . '">' . $row[2] . " (" . ucwords($row[3]) . ")</a></li>\n";
+		echo '<li>' . $row[1] . ' <a href="trip.php?routeid=' . $row[0] . '">' . $row[2] . " (" . ucwords($row[4]) . ")</a></li>\n";
 	}
 }
-if ($_REQUEST['bynumber']) {
+else if ($_REQUEST['bynumber']) {
+	include_header("Routes by Number", "routeList");
+	navbar();
+	echo ' <ul data-role="listview"  data-inset="true">';
+	$url = $APIurl . "/json/routes";
+	$contents = json_decode(getPage($url));
 	$routeSeries = Array();
 	$seriesRange = Array();
 	foreach ($contents as $key => $row) {
@@ -54,11 +94,19 @@
 		echo '<a name="' . $series . '"></a>';
 		if ($series <= 9) echo '<li>' . $series . "<ul>\n";
 		else echo "<li>{$seriesRange[$series]['min']}-{$seriesRange[$series]['max']}<ul>\n";
-		printRoutes($routes);
+		foreach ($routes as $row) {
+			echo '<li>' . $row[1] . ' <a href="trip.php?routeid=' . $row[0] . '">' . $row[2] . " (" . ucwords($row[3]) . ")</a></li>\n";
+		}
 		echo "</ul></li>\n";
 	}
 }
 else {
+	include_header("Routes by Destination", "routeList");
+	navbar();
+	echo ' <ul data-role="listview"  data-inset="true">';
+	$url = $APIurl . "/json/routes";
+	$contents = json_decode(getPage($url));
+	// by destination!
 	foreach ($contents as $key => $row) {
 		$routeDestinations[$row[2]][] = $row;
 	}
@@ -73,7 +121,9 @@
 	foreach ($routeDestinations as $destination => $routes) {
 		echo '<a name="' . $destination . '"></a>';
 		echo '<li>' . $destination . "... <ul>\n";
-		printRoutes($routes);
+		foreach ($routes as $row) {
+			echo '<li>' . $row[1] . ' <a href="trip.php?routeid=' . $row[0] . '">' . $row[2] . " (" . ucwords($row[3]) . ")</a></li>\n";
+		}
 		echo "</ul></li>\n";
 	}
 }

--- a/schedule_viewer.py
+++ b/schedule_viewer.py
@@ -494,8 +494,8 @@
     result = {}
     for trip in trips:
       route = schedule.GetRoute(trip.route_id)
-      if not trip.route_short_name+route.route_long_name in result:
-        result[trip.route_short_name+route.route_long_name] = (route.route_id, route.route_short_name, route.route_long_name, trip.trip_id)
+      if not route.route_short_name+route.route_long_name+trip.service_id in result:
+        result[route.route_short_name+route.route_long_name+trip.service_id] = (route.route_id, route.route_short_name, route.route_long_name, trip.trip_id, trip.service_id)
     return result
     
   def handle_json_GET_stopalltrips(self, params):
@@ -526,6 +526,8 @@
       if service_period == None or trip.service_id == service_period:
         result.append((time, (trip.trip_id, trip_name, trip.service_id), tp))
     return result
+  
+
 
   def handle_json_GET_stoptrips(self, params):
     """Given a stop_id and time in seconds since midnight return the next