Refactoring of route list page
[busui.git] / stopList.php
blob:a/stopList.php -> blob:b/stopList.php
<?php <?php
include ('include/common.inc.php'); include ('include/common.inc.php');
$stops = Array(); $stops = Array();
function filterByFirstLetter($var) function filterByFirstLetter($var)
{ {
return $var[1][0] == $_REQUEST['firstLetter']; return $var[1][0] == $_REQUEST['firstLetter'];
} }
function navbar() function navbar()
{ {
echo ' echo '
<div data-role="navbar"> <div data-role="navbar">
<ul> <ul>
<li><a href="stopList.php">Timing Points</a></li> <li><a href="stopList.php">Timing Points</a></li>
<li><a href="stopList.php?suburbs=yes">By Suburb</a></li> <li><a href="stopList.php?suburbs=yes">By Suburb</a></li>
<li><a href="stopList.php?nearby=yes">Nearby Stops</a></li> <li><a href="stopList.php?nearby=yes">Nearby Stops</a></li>
<li><a href="stopList.php?allstops=yes">All Stops</a></li> <li><a href="stopList.php?allstops=yes">All Stops</a></li>
</ul> </ul>
</div> </div>
'; ';
} }
// By suburb // By suburb
if (isset($_REQUEST['suburbs'])) { if (isset($_REQUEST['suburbs'])) {
include_header("Stops by Suburb", "stopList"); include_header("Stops by Suburb", "stopList");
navbar(); navbar();
echo ' <ul data-role="listview" data-filter="true" data-inset="true" >'; echo ' <ul data-role="listview" data-filter="true" data-inset="true" >';
if (!isset($_REQUEST['firstLetter'])) { if (!isset($_REQUEST['firstLetter'])) {
foreach (range('A', 'Z') as $letter) { foreach (range('A', 'Z') as $letter) {
echo "<li><a href=\"stopList.php?firstLetter=$letter&suburbs=yes\">$letter...</a></li>\n"; echo "<li><a href=\"stopList.php?firstLetter=$letter&suburbs=yes\">$letter...</a></li>\n";
} }
} }
else { else {
foreach ($suburbs as $suburb) { foreach ($suburbs as $suburb) {
if (startsWith($suburb, $_REQUEST['firstLetter'])) { if (startsWith($suburb, $_REQUEST['firstLetter'])) {
echo '<li><a href="stopList.php?suburb=' . urlencode($suburb) . '">' . $suburb . '</a></li>'; echo '<li><a href="stopList.php?suburb=' . urlencode($suburb) . '">' . $suburb . '</a></li>';
} }
} }
} }
echo '</ul>'; echo '</ul>';
} }
else { else {
// Timing Points / All stops // Timing Points / All stops
if ($_REQUEST['allstops']) { if ($_REQUEST['allstops']) {
$listType = 'allstops=yes'; $listType = 'allstops=yes';
$stops = getStops(); $stops = getStops();
include_header("All Stops", "stopList"); include_header("All Stops", "stopList");
navbar(); navbar();
timePlaceSettings(); timePlaceSettings();
} }
else if ($_REQUEST['nearby']) { else if ($_REQUEST['nearby']) {
$listType = 'nearby=yes'; $listType = 'nearby=yes';
$stops = getNearbyStops($_SESSION['lat'],$_SESSION['lon'],15);  
include_header("Nearby Stops", "stopList", true, true); include_header("Nearby Stops", "stopList", true, true);
  trackEvent("Stop Lists","Stops Nearby", $_SESSION['lat'].",".$_SESSION['lon']);
navbar(); navbar();
timePlaceSettings(true); timePlaceSettings(true);
if (!isset($_SESSION['lat']) || !isset($_SESSION['lat']) || $_SESSION['lat'] == "" || $_SESSION['lon'] == "") { if (!isset($_SESSION['lat']) || !isset($_SESSION['lat']) || $_SESSION['lat'] == "" || $_SESSION['lon'] == "") {
include_footer(); include_footer();
die(); die();
} }
   
  $stops = getNearbyStops($_SESSION['lat'],$_SESSION['lon'],15);
} }
else if ($_REQUEST['suburb']) { else if ($_REQUEST['suburb']) {
$suburb = filter_var($_REQUEST['suburb'], FILTER_SANITIZE_STRING); $suburb = filter_var($_REQUEST['suburb'], FILTER_SANITIZE_STRING);
$stops = getStopsBySuburb($suburb); $stops = getStopsBySuburb($suburb);
include_header("Stops in " . ucwords($suburb) , "stopList"); include_header("Stops in " . ucwords($suburb) , "stopList");
navbar(); navbar();
trackEvent("Stop Lists","Stops By Suburb", $suburb); trackEvent("Stop Lists","Stops By Suburb", $suburb);
} }
else { else {
$stops = getStops(true,$_REQUEST['firstLetter']); $stops = getStops(true,$_REQUEST['firstLetter']);
include_header("Timing Points / Major Stops", "stopList"); include_header("Timing Points / Major Stops", "stopList");
navbar(); navbar();
timePlaceSettings(); timePlaceSettings();
} }
echo ' <ul data-role="listview" data-filter="true" data-inset="true" >'; echo ' <ul data-role="listview" data-filter="true" data-inset="true" >';
if (!isset($_REQUEST['firstLetter']) && !$_REQUEST['suburb'] && !$_REQUEST['nearby']) { if (!isset($_REQUEST['firstLetter']) && !$_REQUEST['suburb'] && !$_REQUEST['nearby']) {
foreach (range('A', 'Z') as $letter) { foreach (range('A', 'Z') as $letter) {
echo "<li><a href=\"stopList.php?firstLetter=$letter&$listType\">$letter...</a></li>\n"; echo "<li><a href=\"stopList.php?firstLetter=$letter&$listType\">$letter...</a></li>\n";
} }
} }
else { else {
//var_dump($stops); //var_dump($stops);
$stopsGrouped = Array(); $stopsGrouped = Array();
foreach ($stops as $key => $stop) { foreach ($stops as $key => $stop) {
if ((trim(preg_replace("/\(Platform.*/", "", $stops[$key]["stop_name"])) != trim(preg_replace("/\(Platform.*/", "", $stops[$key + 1]["stop_name"]))) || $key + 1 >= sizeof($stops)) { if ((trim(preg_replace("/\(Platform.*/", "", $stops[$key]["stop_name"])) != trim(preg_replace("/\(Platform.*/", "", $stops[$key + 1]["stop_name"]))) || $key + 1 >= sizeof($stops)) {
if (sizeof($stopsGrouped) > 0) { if (sizeof($stopsGrouped) > 0) {
// print and empty grouped stops // print and empty grouped stops
// subsequent duplicates // subsequent duplicates
$stopsGrouped["stop_ids"][] = $stop['stop_id']; $stopsGrouped["stop_ids"][] = $stop['stop_id'];
echo '<li>'; echo '<li>';
if (!startsWith($stopsGrouped['stop_codes'][0], "Wj")) echo '<img src="css/images/time.png" alt="Timing Point: " class="ui-li-icon">'; if (!startsWith($stopsGrouped['stop_codes'][0], "Wj")) echo '<img src="css/images/time.png" alt="Timing Point: " class="ui-li-icon">';
echo '<a href="stop.php?stopids=' . implode(",", $stopsGrouped['stop_ids']) . '">'; echo '<a href="stop.php?stopids=' . implode(",", $stopsGrouped['stop_ids']) . '">';
if (isset($_SESSION['lat']) && isset($_SESSION['lon'])) { if (isset($_SESSION['lat']) && isset($_SESSION['lon'])) {
echo '<span class="ui-li-count">' . distance($stop['stop_lat'],$stop['stop_lon'], $_SESSION['lat'], $_SESSION['lon'], true) . 'm away</span>'; echo '<span class="ui-li-count">' . distance($stop['stop_lat'],$stop['stop_lon'], $_SESSION['lat'], $_SESSION['lon'], true) . 'm away</span>';
} }
echo bracketsMeanNewLine(trim(preg_replace("/\(Platform.*/", "", $stop['stop_name'])) . '(' . sizeof($stopsGrouped["stop_ids"]) . ' stops)'); echo bracketsMeanNewLine(trim(preg_replace("/\(Platform.*/", "", $stop['stop_name'])) . '(' . sizeof($stopsGrouped["stop_ids"]) . ' stops)');
echo "</a></li>\n"; echo "</a></li>\n";
flush(); @ob_flush(); flush(); @ob_flush();
$stopsGrouped = Array(); $stopsGrouped = Array();
} }
else { else {
// just a normal stop // just a normal stop
echo '<li>'; echo '<li>';
if (!startsWith($stop['stop_code'], "Wj")) echo '<img src="css/images/time.png" alt="Timing Point" class="ui-li-icon">'; if (!startsWith($stop['stop_code'], "Wj")) echo '<img src="css/images/time.png" alt="Timing Point" class="ui-li-icon">';
echo '<a href="stop.php?stopid=' . $stop['stop_id'] . (startsWith($stop['stop_code'], "Wj") ? '&stopcode=' . $stop['stop_code'] : "") . '">'; echo '<a href="stop.php?stopid=' . $stop['stop_id'] . (startsWith($stop['stop_code'], "Wj") ? '&stopcode=' . $stop['stop_code'] : "") . '">';
if (isset($_SESSION['lat']) && isset($_SESSION['lon'])) { if (isset($_SESSION['lat']) && isset($_SESSION['lon'])) {
echo '<span class="ui-li-count">' . distance($stop['stop_lat'],$stop['stop_lon'], $_SESSION['lat'], $_SESSION['lon'], true) . 'm away</span>'; echo '<span class="ui-li-count">' . distance($stop['stop_lat'],$stop['stop_lon'], $_SESSION['lat'], $_SESSION['lon'], true) . 'm away</span>';
} }
echo bracketsMeanNewLine($stop['stop_name']); echo bracketsMeanNewLine($stop['stop_name']);
echo "</a></li>\n"; echo "</a></li>\n";
flush(); @ob_flush(); flush(); @ob_flush();
} }
} }
else { else {
// this is a duplicated line item // this is a duplicated line item
if ($key - 1 <= 0 || (trim(preg_replace("/\(Platform.*/", "", $stops[$key]['stop_name'])) != trim(preg_replace("/\(Platform.*/", "", $stops[$key - 1]['stop_name'])))) { if ($key - 1 <= 0 || (trim(preg_replace("/\(Platform.*/", "", $stops[$key]['stop_name'])) != trim(preg_replace("/\(Platform.*/", "", $stops[$key - 1]['stop_name'])))) {
// first duplicate // first duplicate
$stopsGrouped = Array( $stopsGrouped = Array(
"name" => trim(preg_replace("/\(Platform.*/", "", $stop['stop_name'])) , "name" => trim(preg_replace("/\(Platform.*/", "", $stop['stop_name'])) ,
"stop_ids" => Array( "stop_ids" => Array(
$stop['stop_id'] $stop['stop_id']
) , ) ,
"stop_codes" => Array( "stop_codes" => Array(
$stop['stop_code'] $stop['stop_code']
) )
); );
} }
else { else {
// subsequent duplicates // subsequent duplicates
$stopsGrouped["stop_ids"][] = $stop['stop_id'];; $stopsGrouped["stop_ids"][] = $stop['stop_id'];;
} }
} }
} }
} }
echo '</ul>'; echo '</ul>';
} }
include_footer(); include_footer();
?> ?>