Fix analytics to only happen in production mode
[busui.git] / tripPlanner.php
blob:a/tripPlanner.php -> blob:b/tripPlanner.php
<?php <?php
include('common.inc.php'); include ('include/common.inc.php');
include_header("Trip Planner","tripPlanner", true, true); include_header("Trip Planner", "tripPlanner", true, true, true);
$from = (isset($_REQUEST['from']) ? filter_var($_REQUEST['from'],FILTER_SANITIZE_STRING) : "Brigalow"); $from = (isset($_REQUEST['from']) ? filter_var($_REQUEST['from'], FILTER_SANITIZE_STRING) : "Brigalow");
$to = (isset($_REQUEST['to']) ? filter_var($_REQUEST['to'],FILTER_SANITIZE_STRING) : "Barry"); $to = (isset($_REQUEST['to']) ? filter_var($_REQUEST['to'], FILTER_SANITIZE_STRING) : "Barry");
$date = (isset($_REQUEST['date']) ? filter_var($_REQUEST['date'],FILTER_SANITIZE_STRING) : date("m/d/Y")); $date = (isset($_REQUEST['date']) ? filter_var($_REQUEST['date'], FILTER_SANITIZE_STRING) : date("m/d/Y"));
$time = (isset($_REQUEST['time']) ? filter_var($_REQUEST['time'],FILTER_SANITIZE_STRING) : date("H:i")); $time = (isset($_REQUEST['time']) ? filter_var($_REQUEST['time'], FILTER_SANITIZE_STRING) : date("H:i"));
# todo: convert date from form to h:ia? function formatTime($timeString) {
  $timeParts = explode("T",$timeString);
  return str_replace("Z","",$timeParts[1]);
  }
  function tripPlanForm($errorMessage = "")
  {
   
function tripPlanForm($errorMessage = "") global $date, $time, $from, $to;
{ echo "<font color=red>$errorMessage</font>";
global $date,$time,$from,$to; echo '<form action="tripPlanner.php" method="post">
echo "<font color=red>$errorMessage</font>";  
echo '<form action="tripPlanner.php" method="post">  
<div data-role="fieldcontain"> <div data-role="fieldcontain">
<label for="from">I would like to go from</label> <label for="from">I would like to go from</label>
<input type="text" name="from" id="from" value="' . $from . '" /> <input type="text" name="from" id="from" value="' . $from . '" />
<a href="#" style="display:none" name="fromHere" id="fromHere"/>Here?</a> <a href="#" style="display:none" name="fromHere" id="fromHere"/>Here?</a>
</div> </div>
<div data-role="fieldcontain"> <div data-role="fieldcontain">
<label for="to"> to </label> <label for="to"> to </label>
<input type="text" name="to" id="to" value="' . $to . '" /> <input type="text" name="to" id="to" value="' . $to . '" />
<a href="#" style="display:none" name="toHere" id="toHere"/>Here?</a> <a href="#" style="display:none" name="toHere" id="toHere"/>Here?</a>
</div> </div>
<div data-role="fieldcontain"> <div data-role="fieldcontain">
<label for="date"> on </label> <label for="date"> on </label>
<input type="text" name="date" id="date" value="' . $date . '" /> <input type="text" name="date" id="date" value="' . $date . '" />
</div> </div>
<div data-role="fieldcontain"> <div data-role="fieldcontain">
<label for="time"> at </label> <label for="time"> at </label>
<input type="time" name="time" id="time" value="' . $time . '" /> <input type="time" name="time" id="time" value="' . $time . '" />
</div> </div>
<input type="submit" value="Go!"></form>'; <input type="submit" value="Go!"></form>';
echo "<script> echo "<script>
$('#toHere').click(function(event) { $('#to').val(getCookie('geolocate')); return false;}); $('#toHere').click(function(event) { $('#to').val(getCookie('geolocate')); return false;});
$('#toHere').show(); $('#toHere').show();
$('#fromHere').click(function(event) { $('#from').val(getCookie('geolocate')); return false;}); $('#fromHere').click(function(event) { $('#from').val(getCookie('geolocate')); return false;});
$('#fromHere').show(); $('#fromHere').show();
   
</script>"; </script>";
} }
  function processItinerary($itineraryNumber, $itinerary)
function processItinerary($itineraryNumber, $itinerary) {
{ echo '<div data-role="collapsible" ' . ($itineraryNumber > 0 ? 'data-collapsed="true"' : "") . '> <h3> Option #' . ($itineraryNumber + 1) . ": " . floor($itinerary->duration / 60000) . " minutes (".formatTime($itinerary->startTime)." to ".formatTime($itinerary->endTime).")</h3><p>";
echo '<div data-role="collapsible" ' . ($itineraryNumber > 0 ? 'data-collapsed="true"' : "") . '> <h3> Option #' . ($itineraryNumber + 1) . ": " . floor($itinerary->duration / 60000) . " minutes ({$itinerary->startTime} to {$itinerary->endTime})</h3><p>"; echo "Walking time: " . floor($itinerary->walkTime / 60000) . " minutes (" . floor($itinerary->walkDistance) . " meters)<br>\n";
echo "Walking time: " . floor($itinerary->walkTime / 60000) . " minutes (" . floor($itinerary->walkDistance) . " meters)<br>\n"; echo "Transit time: " . floor($itinerary->transitTime / 60000) . " minutes<br>\n";
echo "Transit time: " . floor($itinerary->transitTime / 60000) . " minutes<br>\n"; echo "Waiting time: " . floor($itinerary->waitingTime / 60000) . " minutes<br>\n";
echo "Waiting time: " . floor($itinerary->waitingTime / 60000) . " minutes<br>\n"; if (is_array($itinerary->legs->leg)) {
  $legMarkers = array();
  foreach ($itinerary->legs->leg as $legNumber => $leg) {
  $legMarkers[] = array(
if (is_array($itinerary->legs->leg)) { $leg->from->lat,
$legMarkers = array(); $leg->from->lon
foreach ($itinerary->legs->leg as $legNumber => $leg) { );
$legMarkers[] = array($leg->from->lat, $leg->from->lon); }
} echo '' . staticmap($legMarkers, 0, "iconb", false) . "<br>\n";
echo '' . staticmap($legMarkers) . "<br>\n"; echo '<ul>';
echo '<ul>'; foreach ($itinerary->legs->leg as $legNumber => $leg) {
foreach ($itinerary->legs->leg as $legNumber => $leg) { echo '<li>';
echo '<li>'; processLeg($legNumber, $leg);
processLeg($legNumber, $leg); echo "</li>";
echo "</li>"; }
} echo "</ul>";
echo "</ul>"; }
} else { else {
echo '' . staticmap(array(array($itinerary->legs->leg->from->lat, $itinerary->legs->leg->from->lon))) . "<br>\n"; echo '' . staticmap(array(
processLeg(0, $itinerary->legs->leg); array(
} $itinerary->legs->leg->from->lat,
  $itinerary->legs->leg->from->lon
echo "</p></div>"; )
} ) , 0, "iconb", false) . "<br>\n";
  processLeg(0, $itinerary->legs->leg);
function processLeg($legNumber, $leg) { }
$legArray = object2array($leg); echo "</p></div>";
echo '<h3>Leg #' . ($legNumber + 1) . " ( {$legArray['@mode']} from: {$leg->from->name} to {$leg->to->name}, " . floor($leg->duration / 60000) . " minutes) </h3>\n"; }
if ($legArray["@mode"] === "BUS") { function processLeg($legNumber, $leg)
echo "Take bus {$legArray['@route']} " . str_replace("To", "towards", $legArray['@headsign']) . "<br>"; {
} else { $legArray = object2array($leg);
$walkStepMarkers = array(); echo '<h3>Leg #' . ($legNumber + 1) . " ( {$legArray['@mode']} from: {$leg->from->name} to {$leg->to->name}, " . floor($leg->duration / 60000) . " minutes) </h3>\n";
foreach ($leg->steps->walkSteps as $stepNumber => $step) { if ($legArray["@mode"] === "BUS") {
$walkStepMarkers[] = array($step->lat, $step->lon); echo "Take bus {$legArray['@route']} " . str_replace("To", "towards", $legArray['@headsign']) . "<br>";
} }
echo "" . staticmap($walkStepMarkers, "icong") . "<br>\n"; else {
foreach ($leg->steps->walkSteps as $stepNumber => $step) { $walkStepMarkers = array();
echo "Walking step " . ($stepNumber + 1) . " $step->absoluteDirection / $step->relativeDirection on $step->streetName for " . floor($step->distance) . " meters<br>\n"; foreach ($leg->steps->walkSteps as $stepNumber => $step) {
} $walkStepMarkers[] = array(
} $step->lat,
} $step->lon
  );
if ($_REQUEST['time']) { }
$toPlace = (startsWith($to, "-") ? $to : geocode($to, false)); echo "" . staticmap($walkStepMarkers, 0, "icong", false) . "<br>\n";
$fromPlace = (startsWith($from, "-") ? $from : geocode($from, false)); foreach ($leg->steps->walkSteps as $stepNumber => $step) {
if ($toPlace == "" || $fromPlace == "") { echo "Walking step " . ($stepNumber + 1) . ": ";
$errorMessage = ""; if ($step->relativeDirection == "CONTINUE") {
if ($toPlace === "") echo "Continue, ";
$errorMessage .= urlencode($to) . " not found.<br>\n"; } else if ($step->relativeDirection) echo "Turn ".ucwords(strtolower(str_replace("_"," ",$step->relativeDirection))).", ";
if ($fromPlace === "") echo "Go ".ucwords(strtolower($step->absoluteDirection))." on ";
$errorMessage .= urlencode($from) . " not found.<br>\n"; if (strpos($step->streetName,"from") !== false && strpos($step->streetName,"way") !== false) {
tripPlanForm($errorMessage); echo "footpath";
} else { } else {
if (isMetricsOn()) { echo $step->streetName;
// Create a new Instance of the tracker }
$owa = new owa_php(); echo " for " . floor($step->distance) . " meters<br>\n";
// Set the ID of the site being tracked }
$owa->setSiteId($owaSiteID); }
// Create a new event object }
$event = $owa->makeEvent(); if ($_REQUEST['time']) {
// Set the Event Type, in this case a "video_play" $toPlace = (startsWith($to, "-") ? $to : geocode($to, false));
$event->setEventType('view_trip_plan'); $fromPlace = (startsWith($from, "-") ? $from : geocode($from, false));
// Set a property if ($toPlace == "" || $fromPlace == "") {
$event->set('from',$from); $errorMessage = "";
$event->set('to',$to); if ($toPlace === "") {
$event->set('time',$time); $errorMessage.= urlencode($to) . " not found.<br>\n";
$event->set('date',$date); trackEvent("Trip Planner","Geocoder Failed", $to);
// Track the event }
$owa->trackEvent($event); if ($fromPlace === "") {
} $errorMessage.= urlencode($from) . " not found.<br>\n";
$url = $otpAPIurl."ws/plan?date=" . urlencode($_REQUEST['date']) . "&time=" . urlencode($_REQUEST['time']) . "&mode=TRANSIT%2CWALK&optimize=QUICK&maxWalkDistance=840&wheelchair=false&toPlace=$toPlace&fromPlace=$fromPlace&intermediatePlaces="; trackEvent("Trip Planner","Geocoder Failed", $from);
$ch = curl_init($url); }
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); tripPlanForm($errorMessage);
curl_setopt($ch, CURLOPT_HEADER, 0); }
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json")); else {
curl_setopt($ch,CURLOPT_TIMEOUT,5); $url = $otpAPIurl . "ws/plan?date=" . urlencode($_REQUEST['date']) . "&time=" . urlencode($_REQUEST['time']) . "&mode=TRANSIT%2CWALK&optimize=QUICK&maxWalkDistance=840&wheelchair=false&toPlace=$toPlace&fromPlace=$fromPlace&intermediatePlaces=";
$page = curl_exec($ch); $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if(curl_errno($ch)) { tripPlanForm("Trip planner temporarily unavailable: ".curl_errno($ch)." ".curl_error($ch));} curl_setopt($ch, CURLOPT_HEADER, 0);
else { curl_setopt($ch, CURLOPT_HTTPHEADER, array(
$tripplan = json_decode($page); "Accept: application/json"
debug(print_r($triplan,true)); ));
echo "<h1> From: {$tripplan->plan->from->name} To: {$tripplan->plan->to->name} </h1>"; curl_setopt($ch, CURLOPT_TIMEOUT, 5);
echo "<h1> At: {$tripplan->plan->date} </h1>"; $page = curl_exec($ch);
  if (curl_errno($ch)) {
if (is_array($tripplan->plan->itineraries->itinerary)) { tripPlanForm("Trip planner temporarily unavailable: " . curl_errno($ch) . " " . curl_error($ch) .(isDebug() ? $url : ""));
echo '<div data-role="collapsible-set">'; trackEvent("Trip Planner","Trip Planner Failed", $url);
foreach ($tripplan->plan->itineraries->itinerary as $itineraryNumber => $itinerary) { }
processItinerary($itineraryNumber, $itinerary); else {
} trackEvent("Trip Planner","Plan Trip From", $from);
echo "</div>"; trackEvent("Trip Planner","Plan Trip To", $to);
} else { $tripplan = json_decode($page);
processItinerary(0, $tripplan->plan->itineraries->itinerary); debug(print_r($triplan, true));
} echo "<h1> From: {$tripplan->plan->from->name} To: {$tripplan->plan->to->name} </h1>";
} echo "<h1> At: ".formatTime($tripplan->plan->date)." </h1>";
curl_close($ch); if (is_array($tripplan->plan->itineraries->itinerary)) {
} echo '<div data-role="collapsible-set">';
} else { foreach ($tripplan->plan->itineraries->itinerary as $itineraryNumber => $itinerary) {
tripPlanForm(); processItinerary($itineraryNumber, $itinerary);
} }
include_footer(); echo "</div>";
  }
  else {
  processItinerary(0, $tripplan->plan->itineraries->itinerary);
  }
  }
  curl_close($ch);
  }
  }
  else {
  tripPlanForm();
  }
  include_footer();
?> ?>