summaryrefslogtreecommitdiff
path: root/index.php
blob: 90a58f3f9cbfc48ef09e40d13087cf972ee9a9b7 (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<!DOCTYPE HTML>
<html>

<head>
<title>bus.of.je</title>
</head>

<style>
table {
    border-collapse: collapse;
}
tr:nth-child(even) {background-color: #f2f2f2;}
</style>

<body>
<h2>bus.of.je | Liberty Bus Timetables</h2>

<?php

// TODO: tidy up HTML creation
// TODO: make it pretty

ini_set('display_errors', 1);
error_reporting(~0);

function getData($type, $route, $direction, $day) {
  $timetables = json_decode(file_get_contents("timetable_full.json"), true);
  switch ($type){
    case "routes":
      return $timetables['timetables'];
      break;
    case "trips":
      return $timetables['timetables'][$route]['timetable'][$direction][$day]['trips'];
      break;
    case "stoplist":
      return $timetables['timetables'][$route]['timetable'][$direction]['stops_list'];
      break;
    case "schedule":
      return $timetables['timetables'][$route];
      break;
    case "meta":
      return $timetables['meta'];
      break;
    case "service":
      return json_decode(file_get_contents("service_updates.json"), true);
    default:
      return False;
      break;
    }
}

$servUpdates = getData('service', false, false, false);

if (isset($_GET['route'])) {
  $route = $_GET['route'];
  $days = array('mon_fri', 'sat', 'sun');
  $directions = array('outbound', 'inbound');
  $schedule = getData('schedule', $route, false, false);
  $routeUpdates = array();
  echo '<h3>'. $schedule['service_number'] . ' - ' . $schedule['service_name'] . '</h3>';

  // print service updates for current timetable
  foreach ($servUpdates as $update) {
    if(in_array($route, $update['services'])) {
      array_push($routeUpdates, $update['content']);
    }
  }
  if (!empty($routeUpdates)) {
    echo '<h4> Service Updates:</h4>';
    foreach ($routeUpdates as $update) {
      echo $update . '<br>';
    }
  }
  // print timetables
  foreach ($days as $day) {
    if ($schedule['schedule'][$day] == 'true') {
      // set display friendly variables for days
      switch ($day){
        case "mon_fri":
          $displayDay = "Mon - Fri";
          break;
        case "sat":
          $displayDay = "Saturday";
          break;
        case "sun":
          $displayDay = "Sunday";
          break;
        default:
          break;
      }
      // function to $stopList by $stopList['order'] using usort
      function cmp($a, $b) {
        return $a['order'] - $b['order'];
      }
      foreach ($directions as $direction) {
        if ($schedule['directions'][$direction] == 'true') {
          $stopList = getData('stoplist', $route, $direction, false);
          // usort to sort stoplist
          usort($stopList,"cmp");
          // print timetable info while setting display friendly case for directions
          // TODO: add linebreaks between end of table and next table title
          echo '<p><br><b>' . ucfirst($direction) . ' | ' . $displayDay . '</b>';
          echo '<table align="left" border="1" cellpadding="3" width="100%">';
          $trips = getData('trips', $route, $direction, $day);
          // TODO: must be a more efficient way to search for $rowStop['stop_name'] in $trip?
          // search for $stopList['stop_name'] in each trip and print time in row on table
          foreach ($stopList as $rowStop) {
            // TODO: don't use $rowStop if it doesn't appear in $trip
            echo '<tr><td><b>' . $rowStop['stop_name'] . '</b></td>';
            foreach ($trips as $trip) {
              $found=False;
              foreach ($trip['stops'] as $stop) {
                if ($stop['stop_name'] == $rowStop['stop_name']) {
                  echo '<td>' . $stop['departure_time'] . '</td>';
                  $found=True;
                }
              }
              // print something to show bus doesn't use this stop on this trip e.g. route variation
              if ($found != True) {
                echo '<td>----</td>';
              }
            }
            echo '</tr>';
          }
          echo '</table><br><br></p>';
        }
      }
    }
  }
}
// print landing page if no route is selected
else {
  $routes = getData('routes', false, false, false);
  $meta = getData('meta', false, false, false);
  if ($meta['from'] = "-- --- ----") {
      echo '<h3>' . $meta['name']. '</h3>';
  } else {
      echo '<h3>' . $meta['name']. ' | ' . $meta['from'] . ' - ' . $meta['to'] . '</h3>';
  }
  echo '<h4>Service Updates:</h4><p>';
  foreach ($servUpdates as $update) {
    echo $update['content'] . '- <b>Affected Routes: </b>' . implode(", ",$update['services']) . '<br><br>';
  }
  // TODO: sort routes to print in (alpha)numerical order
  foreach ($routes as $route) {
    echo '<h4><a href="index.php?route=' . $route['service_number'] . '">' . $route['service_number'] . ' - ' . $route['service_name'] . '</a></h4>';
  }
  echo '</p>';
}
?>

<a href="https://www.blatech.co.uk/ars/bus.of.je">source?</a>
</p>
</body>

</html>