<?php |
<?php |
include('common.inc.php'); |
include('common.inc.php'); |
if ($_REQUEST['time']) { |
include_header("Trip Planner", true, true); |
$toPlace = geocode($_REQUEST['to'],false); |
$from = (isset($_REQUEST['from']) ? filter_var($_REQUEST['from'],FILTER_SANITIZE_STRING) : "Brigalow"); |
$fromPlace = geocode($_REQUEST['from'],false); |
$to = (isset($_REQUEST['to']) ? filter_var($_REQUEST['to'],FILTER_SANITIZE_STRING) : "Barry"); |
$url = "http://localhost:8080/opentripplanner-api-webapp/ws/plan?_dc=1290254798856&arriveBy=false&date=11%2F17%2F2010&time=1%3A06%20pm&mode=TRANSIT%2CWALK&optimize=QUICK&maxWalkDistance=840&wheelchair=false&toPlace=$toPlace&fromPlace=$fromPlace&intermediatePlaces="; |
$date = (isset($_REQUEST['date']) ? filter_var($_REQUEST['date'],FILTER_SANITIZE_STRING) : date("m/d/Y")); |
$ch = curl_init($url); |
$time = (isset($_REQUEST['time']) ? filter_var($_REQUEST['time'],FILTER_SANITIZE_STRING) : date("H:m")); |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); |
# todo: convert date from form to h:ia? |
curl_setopt( $ch, CURLOPT_HEADER, 0 ); |
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json")); |
function tripPlanForm($errorMessage = "") |
$page = curl_exec($ch); |
{ |
curl_close($ch); |
global $date,$time,$from,$to; |
$tripplan = json_decode($page); |
echo "<font color=red>$errorMessage</font>"; |
echo "<h1> From: {$tripplan->plan->from->name} </h1>"; |
echo '<form action="tripPlanner.php" method="post"> |
echo "<h1> To: {$tripplan->plan->to->name} </h1>"; |
<div data-role="fieldcontain"> |
echo "<h1> At: {$tripplan->plan->date} </h1>"; |
<label for="from">I would like to go from</label> |
foreach ($tripplan->plan->itineraries->itinerary as $itineraryNumber => $itinerary ){ |
<input type="text" name="from" id="from" value="' . $from . '" /> |
echo "<hr> <h2> Option #".($itineraryNumber+1)."<h2>"; |
<a href="#" style="display:none" name="fromHere" id="fromHere"/>Here?</a> |
echo "Duration: ".floor($itinerary->duration/60000)." minutes ({$itinerary->startTime} to {$itinerary->endTime})<br>\n"; |
</div> |
echo "Walking time: ".floor($itinerary->walkTime/60000)." minutes ({$itinerary->walkDistance} meters)<br>\n"; |
<div data-role="fieldcontain"> |
echo "Transit time: ".floor($itinerary->transitTime/60000)." minutes<br>\n"; |
<label for="to"> to </label> |
echo "Waiting time: ".floor($itinerary->waitingTime/60000)." minutes<br>\n"; |
<input type="text" name="to" id="to" value="' . $to . '" /> |
$legMarkers = Array(); |
<a href="#" style="display:none" name="toHere" id="toHere"/>Here?</a> |
foreach ($itinerary->legs->leg as $legNumber => $leg ){ |
</div> |
$legMarkers[] = Array($leg->from->lat,$leg->from->lon); |
<div data-role="fieldcontain"> |
} |
<label for="date"> on </label> |
echo staticmap($legMarkers)."<br>\n"; |
<input type="date" name="date" id="date" value="' . $date . '" /> |
foreach ($itinerary->legs->leg as $legNumber => $leg ){ |
</div> |
echo "Leg #".($legNumber+1)." (from: {$leg->from->name} to {$leg->to->name}, ".floor($leg->duration/60000)." minutes) <br>\n"; |
<div data-role="fieldcontain"> |
$legArray = object2array($leg); |
<label for="time"> at </label> |
if ($legArray["@mode"] === "BUS") { |
<input type="time" name="time" id="time" value="' . $time . '" /> |
echo "Take bus {$legArray['@route']} ".str_replace("To","towards",$legArray['@headsign'])."<br>"; |
</div> |
} |
<input type="submit" value="Go!"></form>'; |
else { |
echo "<script> |
foreach ($leg->steps->walkSteps as $stepNumber => $step ){ |
$('#toHere').click(function(event) { $('#to').val(getCookie('geolocate')); return false;}); |
echo "Walking step $stepNumber $step->absoluteDirection / $step->relativeDirection on $step->streetName for $step->distance meters / ".floor($step->duration/60000)." minutes<br>\n"; |
$('#toHere').show(); |
} |
$('#fromHere').click(function(event) { $('#from').val(getCookie('geolocate')); return false;}); |
} |
$('#fromHere').show(); |
} |
|
} |
</script>"; |
} else { |
} |
echo '<form> I would like to go from <input type="text" name="from" value="Brigalow"></input> to |
|
<input type="text" name="to" value="Barry"></input> on <input type="text" name="date" value="1:00pm"> at <input type="text" name="time" value="1:00pm"></input> |
function processItinerary($itineraryNumber, $itinerary) |
<input type="submit"></form>'; |
{ |
|
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 "Transit time: " . floor($itinerary->transitTime / 60000) . " minutes<br>\n"; |
|
echo "Waiting time: " . floor($itinerary->waitingTime / 60000) . " minutes<br>\n"; |
|
|
|
|
} |
|
include_footer(); |
if (is_array($itinerary->legs->leg)) { |
|
$legMarkers = array(); |
|
foreach ($itinerary->legs->leg as $legNumber => $leg) { |
|
$legMarkers[] = array($leg->from->lat, $leg->from->lon); |
|
} |
|
echo '' . staticmap($legMarkers) . "<br>\n"; |
|
echo '<ul>'; |
|
foreach ($itinerary->legs->leg as $legNumber => $leg) { |
|
echo '<li>'; |
|
processLeg($legNumber, $leg); |
|
echo "</li>"; |
|
} |
|
echo "</ul>"; |
|
} else { |
|
echo '' . staticmap(array(array($itinerary->legs->leg->from->lat, $itinerary->legs->leg->from->lon))) . "<br>\n"; |
|
processLeg(0, $itinerary->legs->leg); |
|
} |
|
|
|
echo "</p></div>"; |
|
} |
|
|
|
function processLeg($legNumber, $leg) { |
|
$legArray = object2array($leg); |
|
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") { |
|
echo "Take bus {$legArray['@route']} " . str_replace("To", "towards", $legArray['@headsign']) . "<br>"; |
|
} else { |
|
$walkStepMarkers = array(); |
|
foreach ($leg->steps->walkSteps as $stepNumber => $step) { |
|
$walkStepMarkers[] = array($step->lat, $step->lon); |
|
} |
|
echo "" . staticmap($walkStepMarkers, "icong") . "<br>\n"; |
|
foreach ($leg->steps->walkSteps as $stepNumber => $step) { |
|
echo "Walking step " . ($stepNumber + 1) . " $step->absoluteDirection / $step->relativeDirection on $step->streetName for " . floor($step->distance) . " meters<br>\n"; |
|
} |
|
} |
|
} |
|
|
|
if ($_REQUEST['time']) { |
|
$toPlace = (startsWith($to, "-") ? $to : geocode($to, false)); |
|
$fromPlace = (startsWith($from, "-") ? $from : geocode($from, false)); |
|
if ($toPlace == "" || $fromPlace == "") { |
|
$errorMessage = ""; |
|
if ($toPlace === "") |
|
$errorMessage .= urlencode($to) . " not found.<br>\n"; |
|
if ($fromPlace === "") |
|
$errorMessage .= urlencode($from) . " not found.<br>\n"; |
|
tripPlanForm($errorMessage); |
|
} else { |
|
$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="; |
|
$ch = curl_init($url); |
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
curl_setopt($ch, CURLOPT_HEADER, 0); |
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json")); |
|
curl_setopt($ch,CURLOPT_TIMEOUT,5); |
|
$page = curl_exec($ch); |
|
|
|
if(curl_errno($ch)) { tripPlanForm("Trip planner temporarily unavailable: ".curl_errno($ch)." ".curl_error($ch));} |
|
else { |
|
$tripplan = json_decode($page); |
|
debug(print_r($triplan,true)); |
|
echo "<h1> From: {$tripplan->plan->from->name} To: {$tripplan->plan->to->name} </h1>"; |
|
echo "<h1> At: {$tripplan->plan->date} </h1>"; |
|
|
|
if (is_array($tripplan->plan->itineraries->itinerary)) { |
|
echo '<div data-role="collapsible-set">'; |
|
foreach ($tripplan->plan->itineraries->itinerary as $itineraryNumber => $itinerary) { |
|
processItinerary($itineraryNumber, $itinerary); |
|
} |
|
echo "</div>"; |
|
} else { |
|
processItinerary(0, $tripplan->plan->itineraries->itinerary); |
|
} |
|
} |
|
curl_close($ch); |
|
} |
|
} else { |
|
tripPlanForm(); |
|
} |
|
include_footer(); |
?> |
?> |
|
|