Convert myway timeliness calculation to new GTFS feed format
[busui.git] / myway / myway_timeliness_reconcile.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/*
 *    Copyright 2010,2011 Alexander Sadleir 
 
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
 
  http://www.apache.org/licenses/LICENSE-2.0
 
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 */
include ('../include/common.inc.php');
auth();
foreach ($_REQUEST as $key => $value) {
    if (strstr($key, "route") && !strstr($value, "Select")) {
        $myway_route = str_replace("route", "", $key);
        $route_full_name = $value;
        $query = "update myway_routes set route_full_name = :route_full_name where myway_route = :myway_route";
        debug($query, "database");
        $query = $conn->prepare($query);
        $query->bindParam(":myway_route", $myway_route, PDO::PARAM_STR, 5);
        $query->bindParam(":route_full_name", $route_full_name, PDO::PARAM_STR, 42);
        $query->execute();
        die(print_r($conn->errorInfo(), true));
    }
    if (strstr($key, "myway_stop")) {
        $myway_stop = $value;
        $stop_code = $_REQUEST['stop_code'];
        $stop_street = $_REQUEST['stop_street'];
        $query = "update myway_stops set stop_code = :stop_code, stop_street = :stop_street where myway_stop = :myway_stop";
        debug($query, "database");
        $query = $conn->prepare($query);
        $query->bindParam(":myway_stop", $myway_stop, PDO::PARAM_STR, 25);
        $query->bindParam(":stop_code", $stop_code, PDO::PARAM_STR, 32);
        $query->bindParam(":stop_street", $stop_street);
        $query->execute();
        die(print_r($conn->errorInfo(), true));
    }
}
include_header("MyWay Data Reconcile", "mywayTimeRec");
// initialise
$count = $conn->exec("insert into myway_stops
                     select distinct myway_stop from myway_observations
                     WHERE myway_stop NOT IN
        (
        SELECT  myway_stop
        FROM    myway_stops
        )");
echo "$count new stops.<br>";
if (!$count) {
    print_r($conn->errorInfo());
}
$count = $conn->exec("insert into myway_routes select distinct myway_route from myway_observations
                     WHERE myway_route NOT IN
        (
        SELECT  myway_route
        FROM    myway_routes
        )");
echo "$count new routes.<br>";
if (!$count) {
    print_r($conn->errorInfo());
}
echo "<h2>Stops</h2>";
/* stops
  search start of name, display map and table nuimbered, two text boxes */
$query = "Select * from myway_stops where stop_code is NULL and stop_street is NUll;";
debug($query, "database");
$query = $conn->prepare($query);
$query->execute();
if (!$query) {
    databaseError($conn->errorInfo());
    return Array();
}
foreach ($query->fetchAll() as $myway_stop) {
    echo "<h3>{$myway_stop[0]}</h3>";
    $stopNameParts = explode(" ", $myway_stop[0]);
    $markers = array();
    $stopKey = 0;
    $foundStops = getStops(false, "", $stopNameParts[0] . " " . $stopNameParts[1]);
    if (sizeof($foundStops) > 0) {
        echo "<table>";
        foreach ($foundStops as $stopResult) {
            $markers[] = array(
                $stopResult['stop_lat'],
                $stopResult['stop_lon']
            );
            echo "<tr><td>" . $stopKey++ . "</td><td>" . $stopResult['stop_name'] . "</td><td>" . $stopResult['stop_code'] . "</td></tr>";
        }
        echo '</table>';
        echo "" . staticmap($markers,false,false,false,true) . "<br>\n";
    }
    echo '<form id="inputform' . md5($myway_stop[0]) . '">
        <input type="hidden" name="myway_stop" value="' . $myway_stop[0] . '">
        <div data-role="fieldcontain">
        <label for="stop_code">Stop Code</label>
        <input type="text" name="stop_code" id="stop_code" value="' . $foundStops[0]['stop_code'] . '"  />
    </div>
        <div data-role="fieldcontain">
        <label for="stop_street">Stop Street </label>
        <input type="text" name="stop_street" id="stop_street" value="' . $foundStops[0]['stop_name'] . '"  />
    </div>         <input type="button" onclick="$.post(\'myway_timeliness_reconcile.php\', $(\'#inputform' . md5($myway_stop[0]) . '\').serialize())" value="Go!"></form>
';
    echo '<hr>';
}
echo '<h2>Routes</h2>';
/* routes
  remove alpha char, search present dropdown */
$query = "Select * from myway_routes where route_full_name is NUll;";
debug($query, "database");
$query = $conn->prepare($query);
$query->execute();
if (!$query) {
    databaseError($conn->errorInfo());
    return Array();
}
foreach ($query->fetchAll() as $myway_route) {
    echo "<h3>{$myway_route[0]}</h3>";
    $query = "Select * from myway_observations where myway_route = :route order by time";
    debug($query, "database");
    $query = $conn->prepare($query);
    $query->bindParam(":route", $myway_route[0]);
    $query->execute();
    if (!$query) {
        databaseError($conn->errorInfo());
        return Array();
    }
    foreach ($query->fetchAll() as $myway_obvs) {
        echo $myway_obvs['myway_stop'] . $myway_obvs['time'] . "<br>";
    }
    $searchRouteNo = preg_replace("/[A-Z]/", "", $myway_route[0]);
    echo $searchRouteNo;
    echo '<form id="inputform' . $myway_route[0] . '">
<select name="route' . $myway_route[0] . '" onchange=\'$.post("myway_timeliness_reconcile.php", $("#inputform' . $myway_route[0] . '").serialize())\'>
<option>Select a from/to pair...</option>';
    foreach (getRoutesByNumber($searchRouteNo) as $routeResult) {
        echo "<option value=\"{$routeResult['route_short_name']}{$routeResult['route_long_name']}\"> {$routeResult['route_short_name']}{$routeResult['route_long_name']} </option>\n";
    }
    echo "</select></form>";
    echo '<hr>';
}
include_footer();
?>