Tidy up unused source
[bus.git] / busui / tripPlanner.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
  include('common.inc.php');
  include_header("Trip Planner", true, true);
  function tripPlanForm($errorMessage = "")
  {
      $from = (isset($_REQUEST['from']) ? $_REQUEST['from'] : "Brigalow");
      $to = (isset($_REQUEST['to']) ? $_REQUEST['to'] : "Barry");
      $date = (isset($_REQUEST['date']) ? $_REQUEST['date'] : date("m/d/Y"));
      $time = (isset($_REQUEST['time']) ? $_REQUEST['time'] : date("H:m"));
      # todo: convert date from form to h:ia?
      echo "<font color=red>$errorMessage</font>";
      echo '<form action="tripPlanner.php" method="post">
    <div data-role="fieldcontain">
        <label for="from">I would like to go from</label>
        <input type="text" name="from" id="from" value="' . $from . '"  />
        <a href="#" style="display:none" name="fromHere" id="fromHere"/>Here?</a>
    </div>
        <div data-role="fieldcontain">
        <label for="to"> to </label>
        <input type="text" name="to" id="to" value="' . $to . '"  />
        <a href="#" style="display:none" name="toHere" id="toHere"/>Here?</a>
    </div>
    <div data-role="fieldcontain">
        <label for="date"> on </label>
        <input type="date" name="date" id="date" value="' . $date . '"  />
    </div>
        <div data-role="fieldcontain">
        <label for="time"> at </label>
        <input type="time" name="time" id="time" value="' . $time . '"  />
    </div>
        <input type="submit" value="Go!"></form>';
        echo "<script>
$('#toHere').click(function(event) { $('#to').val(getCookie('geolocate')); return false;});
$('#toHere').show();
$('#fromHere').click(function(event) { $('#from').val(getCookie('geolocate')); return false;});
$('#fromHere').show();
 
        </script>";
  }
  
  function processItinerary($itineraryNumber, $itinerary)
  {
      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";
 
     
      
      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($_REQUEST['to'], "-") ? $_REQUEST['to'] : geocode(urlencode($_REQUEST['to']), false));
      $fromPlace = (startsWith($_REQUEST['from'], "-") ? $_REQUEST['from'] : geocode(urlencode($_REQUEST['from']), false));
      if ($toPlace == "" || $fromPlace == "") {
          $errorMessage = "";
          if ($toPlace === "")
              $errorMessage .= urlencode($_REQUEST['to']) . " not found.<br>\n";
          if ($fromPlace === "")
              $errorMessage .= urlencode($_REQUEST['from']) . " not found.<br>\n";
          tripPlanForm($errorMessage);
      } else {
          $url = "http://10.1.0.243:5080/opentripplanner-api-webapp/ws/plan?_dc=1290254798856&arriveBy=false&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();
?>