--- a/tripPlanner.php +++ b/tripPlanner.php @@ -1,20 +1,36 @@ $errorMessage"; - echo '
+ +function tripPlanForm($errorMessage = "") { + global $date, $time, $from, $to; + echo "
$errorMessage"; + echo '
@@ -34,150 +50,139 @@
'; +} +function processItinerary($itineraryNumber, $itinerary) { + echo '
0 ? 'data-collapsed="true"' : "") . '>

Option #' . ($itineraryNumber + 1) . ": " . floor($itinerary->duration / 60000) . " minutes (" . formatTime($itinerary->startTime) . " to " . formatTime($itinerary->endTime) . ")

"; + echo "Walking time: " . floor($itinerary->walkTime / 60000) . " minutes (" . floor($itinerary->walkDistance) . " meters)
\n"; + echo "Transit time: " . floor($itinerary->transitTime / 60000) . " minutes
\n"; + echo "Waiting time: " . floor($itinerary->waitingTime / 60000) . " minutes
\n"; + 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, 0, "iconb", false) . "
\n"; + echo '

    '; + foreach ($itinerary->legs->leg as $legNumber => $leg) { + echo '
  • '; + processLeg($legNumber, $leg); + echo "
  • "; + flush(); + @ob_flush(); + } + echo "
"; + } else { + echo '' . staticmap(array( + array( + $itinerary->legs->leg->from->lat, + $itinerary->legs->leg->from->lon + ) + ), 0, "iconb", false) . "
\n"; + processLeg(0, $itinerary->legs->leg); + } + echo "

"; } -function processItinerary($itineraryNumber, $itinerary) -{ - echo '
0 ? 'data-collapsed="true"' : "") . '>

Option #' . ($itineraryNumber + 1) . ": " . floor($itinerary->duration / 60000) . " minutes (" . formatTime($itinerary->startTime) . " to " . formatTime($itinerary->endTime) . ")

"; - echo "Walking time: " . floor($itinerary->walkTime / 60000) . " minutes (" . floor($itinerary->walkDistance) . " meters)
\n"; - echo "Transit time: " . floor($itinerary->transitTime / 60000) . " minutes
\n"; - echo "Waiting time: " . floor($itinerary->waitingTime / 60000) . " minutes
\n"; - 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, 0, "iconb", false) . "
\n"; - echo '

    '; - foreach ($itinerary->legs->leg as $legNumber => $leg) { - echo '
  • '; - processLeg($legNumber, $leg); - echo "
  • "; - flush(); - @ob_flush(); - } - echo "
"; - } - else { - echo '' . staticmap(array( - array( - $itinerary->legs->leg->from->lat, - $itinerary->legs->leg->from->lon - ) - ) , 0, "iconb", false) . "
\n"; - processLeg(0, $itinerary->legs->leg); - } - echo "

"; + +function processLeg($legNumber, $leg) { + $legArray = object2array($leg); + echo '

Leg #' . ($legNumber + 1) . " ( {$legArray['@mode']} from: {$leg->from->name} to {$leg->to->name}, " . floor($leg->duration / 60000) . " minutes)

\n"; + if ($legArray["@mode"] === "BUS") { + echo "Take bus {$legArray['@route']} " . str_replace("To", "towards", $legArray['@headsign']) . " departing at " . formatTime($leg->startTime) . "
"; + } else { + $walkStepMarkers = array(); + foreach ($leg->steps->walkSteps as $stepNumber => $step) { + $walkStepMarkers[] = array( + $step->lat, + $step->lon + ); + } + echo "" . staticmap($walkStepMarkers, 0, "icong", false) . "
\n"; + foreach ($leg->steps->walkSteps as $stepNumber => $step) { + echo "Walking step " . ($stepNumber + 1) . ": "; + if ($step->relativeDirection == "CONTINUE") { + echo "Continue, "; + } else if ($step->relativeDirection) + echo "Turn " . ucwords(strtolower(str_replace("_", " ", $step->relativeDirection))) . ", "; + echo "Go " . ucwords(strtolower($step->absoluteDirection)) . " on "; + if (strpos($step->streetName, "from") !== false && strpos($step->streetName, "way") !== false) { + echo "footpath"; + } else { + echo $step->streetName; + } + echo " for " . floor($step->distance) . " meters
\n"; + } + } } -function processLeg($legNumber, $leg) -{ - $legArray = object2array($leg); - echo '

Leg #' . ($legNumber + 1) . " ( {$legArray['@mode']} from: {$leg->from->name} to {$leg->to->name}, " . floor($leg->duration / 60000) . " minutes)

\n"; - if ($legArray["@mode"] === "BUS") { - echo "Take bus {$legArray['@route']} " . str_replace("To", "towards", $legArray['@headsign']) . " departing at ". formatTime($leg->startTime)."
"; - } - else { - $walkStepMarkers = array(); - foreach ($leg->steps->walkSteps as $stepNumber => $step) { - $walkStepMarkers[] = array( - $step->lat, - $step->lon - ); - } - echo "" . staticmap($walkStepMarkers, 0, "icong", false) . "
\n"; - foreach ($leg->steps->walkSteps as $stepNumber => $step) { - echo "Walking step " . ($stepNumber + 1) . ": "; - if ($step->relativeDirection == "CONTINUE") { - echo "Continue, "; - } - else if ($step->relativeDirection) echo "Turn " . ucwords(strtolower(str_replace("_", " ", $step->relativeDirection))) . ", "; - echo "Go " . ucwords(strtolower($step->absoluteDirection)) . " on "; - if (strpos($step->streetName, "from") !== false && strpos($step->streetName, "way") !== false) { - echo "footpath"; - } - else { - echo $step->streetName; - } - echo " for " . floor($step->distance) . " meters
\n"; - } - } -} + if ($_REQUEST['time']) { - if (startsWith($to, "-")) { - $toPlace = $to; - } - else if (strpos($to, "(") !== false) { - $toParts = explode("(", $to); - $toPlace = str_replace( ")", "", $toParts[1]); - } - else { - $toPlace = geocode($to, false); - } - - if (startsWith($from, "-")) { - $fromPlace = $from; - } - else if (strpos($from, "(") !== false) { - $fromParts = explode("(", urldecode($from)); - $fromPlace = str_replace(")", "", $fromParts[1]); - } - else { - $fromPlace = geocode($from, false); - } - - if ($toPlace == "" || $fromPlace == "") { - $errorMessage = ""; - if ($toPlace == "") { - $errorMessage.= urlencode($to) . " not found.
\n"; - trackEvent("Trip Planner", "Geocoder Failed", $to); - } - if ($fromPlace == "") { - $errorMessage.= urlencode($from) . " not found.
\n"; - trackEvent("Trip Planner", "Geocoder Failed", $from); - } - 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="; - debug($url); - $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, 10); - $page = curl_exec($ch); - if (curl_errno($ch) || curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200) { - tripPlanForm("Trip planner temporarily unavailable: " . curl_errno($ch) . " " . curl_error($ch) . " " . curl_getinfo($ch, CURLINFO_HTTP_CODE) . (isDebug() ? "
" . $url : "")); - trackEvent("Trip Planner", "Trip Planner Failed", $url); - } - else { - trackEvent("Trip Planner", "Plan Trip From", $from); - trackEvent("Trip Planner", "Plan Trip To", $to); - $tripplan = json_decode($page); - debug(print_r($tripplan, true)); - echo "

From: {$tripplan->plan->from->name} To: {$tripplan->plan->to->name}

"; - echo "

At: " . formatTime($tripplan->plan->date) . "

"; - if (is_array($tripplan->plan->itineraries->itinerary)) { - echo '
'; - foreach ($tripplan->plan->itineraries->itinerary as $itineraryNumber => $itinerary) { - processItinerary($itineraryNumber, $itinerary); - } - echo "
"; - } - else { - processItinerary(0, $tripplan->plan->itineraries->itinerary); - } - } - curl_close($ch); - } -} -else { - tripPlanForm(); + if (startsWith($to, "-")) { + $toPlace = $to; + } else if (strpos($to, "(") !== false) { + $toParts = explode("(", $to); + $toPlace = str_replace(")", "", $toParts[1]); + } else { + $toPlace = geocode($to, false); + } + + if (startsWith($from, "-")) { + $fromPlace = $from; + } else if (strpos($from, "(") !== false) { + $fromParts = explode("(", urldecode($from)); + $fromPlace = str_replace(")", "", $fromParts[1]); + } else { + $fromPlace = geocode($from, false); + } + + if ($toPlace == "" || $fromPlace == "") { + $errorMessage = ""; + if ($toPlace == "") { + $errorMessage.= urlencode($to) . " not found.
\n"; + trackEvent("Trip Planner", "Geocoder Failed", $to); + } + if ($fromPlace == "") { + $errorMessage.= urlencode($from) . " not found.
\n"; + trackEvent("Trip Planner", "Geocoder Failed", $from); + } + 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="; + debug($url); + $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, 10); + $page = curl_exec($ch); + if (curl_errno($ch) || curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200) { + tripPlanForm("Trip planner temporarily unavailable: " . curl_errno($ch) . " " . curl_error($ch) . " " . curl_getinfo($ch, CURLINFO_HTTP_CODE) . (isDebug() ? "
" . $url : "")); + trackEvent("Trip Planner", "Trip Planner Failed", $url); + } else { + trackEvent("Trip Planner", "Plan Trip From", $from); + trackEvent("Trip Planner", "Plan Trip To", $to); + $tripplan = json_decode($page); + debug(print_r($tripplan, true)); + echo "

From: {$tripplan->plan->from->name} To: {$tripplan->plan->to->name}

"; + echo "

At: " . formatTime($tripplan->plan->date) . "

"; + if (is_array($tripplan->plan->itineraries->itinerary)) { + echo '
'; + foreach ($tripplan->plan->itineraries->itinerary as $itineraryNumber => $itinerary) { + processItinerary($itineraryNumber, $itinerary); + } + echo "
"; + } else { + processItinerary(0, $tripplan->plan->itineraries->itinerary); + } + } + curl_close($ch); + } +} else { + tripPlanForm(); } include_footer(); ?>