summaryrefslogtreecommitdiff
path: root/index.php
blob: d0103da0a06feb417495e02da8f8dc75355bd910 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<!DOCTYPE HTML>
<html>

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

<style>
table {
    border-collapse: collapse;
}

tr:nth-child(even) {background-color: #f2f2f2;}

.frionly {
  background-color: #039DFF;
}

</style>

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

<?php

function debug_to_console($data) {
    $output = $data;
    if (is_array($output))
        $output = implode(',', $output);

    echo "<script>console.log('Debug Objects: " . $output . "' );</script>";
}

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;
    }
}

// function to $stopList by $stopList['order'] using usort
function cmp($a, $b) {
  return $a['order'] - $b['order'];
}

$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 friday only gmp_legend
  echo '<br><table align="left" border="1" cellpadding="3" style="background-color: #039DFF;"><tr><td>Friday Only</td><tr><table><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;
      }

      foreach ($directions as $direction) {
        if ($schedule['directions'][$direction] == 'true') {
          $stopList = getData('stoplist', $route, $direction, false);
          // usort to sort stoplist
          usort($stopList,"cmp");
          $trips = getData('trips', $route, $direction, $day);
          // print timetable info while setting display friendly case for directions
          // TODO: add linebreaks between end of table and next table title
          echo '<h3>' . ucfirst($direction) . ' | ' . $displayDay . '</h3>';
          echo '<table align="left" border="1" cellpadding="3" width="100%">';
          // search for $stopList['stop_name'] in each trip and print time in row on table
          foreach ($stopList as $rowStop) {
            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']) {
                  // check if bus runs on friday only by checking another day (in this case thursday) and apply custom color to cells
                    if (($day == 'mon_fri') && ($trip['days_of_operation']['mon'] == False)) {
                        echo '<td class="frionly">' . $stop['departure_time'] . '</td>';
                    } else {
                      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>';
        }
      }
    }
  }
}
// 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 alphanumerical 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>