diff --git a/ajax.php b/ajax.php index ded0002f6cae7d08d87a09484d09ad82e6d78a9f..aa1ebd6583e76945f3413c057d5ba67bc3516fdf 100644 --- a/ajax.php +++ b/ajax.php @@ -3,8 +3,13 @@ This file serves up data for asynchronous requests (usually as JSON). */ -require_once('config.php'); -require_once('functions.php'); +use Doctrine\Common\Collections\Criteria; + +require_once 'config.php'; +require_once 'functions.php'; +require_once 'doctrine.php'; +require_once 'src/BusMap/VehicleLocation.php'; +require_once 'src/BusMap/Prediction.php'; if (!isset($_GET['command'])) die(); @@ -13,45 +18,51 @@ $agency = $cfg['agency']['shortname']; if ($_GET['command'] == "vehicleLocations") { // return a JSON array of vehicles and their locations. - $db = dbc(); - - // MAX age of a bus location to return + // Maximum age of a bus location to return $maxAge = $cfg['agency']['staleBusTime']; - $time = 0; - if (isset($_GET['t'])) $time = $db->real_escape_string((int)$_GET['t']); + if (isset($_GET['t'])) $time = (int)$_GET['t']; if ($time < $maxAge) $time = time() - $maxAge; - $sql = <<< V_LOC - SELECT - `vehicle`, `title` AS `route`, `routeTag`, `dirTag`, `lat`, `lon`, `heading`, `speedKmHr` - FROM `vehicleLocations` - LEFT JOIN `routes` ON `vehicleLocations`.`routeTag` = `routes`.`tag` - WHERE `time` > '{$time}' - AND `vehicleLocations`.`agency` = '{$agency}' -V_LOC; - $result = dbq($db, $sql); - $locations = array(); - while($row = $result->fetch_assoc()) $locations[$row['vehicle']] = $row; + // Get all vehicles for this agency that aren't stale. + $vehicles = $em->getRepository('BusMap\VehicleLocation')->matching( + Criteria::create() + ->where(Criteria::expr()->eq('agency', $agency)) + ->where(Criteria::expr()->gt("time", $time)) + )->toArray(); - // predictions - $sql = <<< V_LOC - SELECT - `predictions`.`vehicle`, `stops`.`title`, `predictions`.`epochTime` - FROM `predictions` - LEFT JOIN `stops` ON `predictions`.`stopTag` = `stops`.`tag` - WHERE `predictions`.`agency` = '{$agency}' - ORDER BY `epochTime` ASC -V_LOC; - $result = dbq($db, $sql); + // Get all predictions for this agency. + $vehicles = $em->getRepository('BusMap\Prediction')->matching( + Criteria::create() + ->where(Criteria::expr()->eq('agency', $agency)) + )->toArray(); - while($row = $result->fetch_assoc()) if (isset($locations[$row['vehicle']])) $locations[$row['vehicle']]['stops'][] = $row; + $vehicleLocations = array(); - foreach ($locations as $key => $location) { - $locations[$key]['route'] = cleanRouteName($location['route']); - $locations[$key]['stops'] = array_slice($location['stops'], 0, 8); + foreach ($vehicles as $v) { + } + + // predictions +// $sql = <<< V_LOC +// SELECT +// `predictions`.`vehicle`, `stops`.`title`, `predictions`.`epochTime` +// FROM `predictions` +// LEFT JOIN `stops` ON `predictions`.`stopTag` = `stops`.`tag` +// WHERE `predictions`.`agency` = '{$agency}' +// ORDER BY `epochTime` ASC +//V_LOC; +// $result = dbq($db, $sql); +// +// while($row = $result->fetch_assoc()) if (isset($locations[$row['vehicle']])) $locations[$row['vehicle']]['stops'][] = $row; +// +// foreach ($locations as $key => $location) { +// $locations[$key]['route'] = cleanRouteName($location['route']); +// $locations[$key]['stops'] = array_slice($location['stops'], 0, 8); +// } + + $db = dbc(); $lastFetched = 0; $result = dbq($db, "SELECT `time` FROM `lastChecked` WHERE `selector` = 'vehicleLocations' LIMIT 1", true); if ($result->num_rows == 1) { @@ -62,7 +73,7 @@ V_LOC; // also return lastTime! header("Content-type: application/json"); echo json_encode(array( - "vehicles" => $locations, + "vehicles" => $vehicles, "lastTime" => $lastFetched, )); diff --git a/cli-config.php b/cli-config.php index f3f0db0a9b76110cf509d65981f150880b054132..789e669671e8e74b651e323548d7bbab0cda6ba1 100644 --- a/cli-config.php +++ b/cli-config.php @@ -7,4 +7,4 @@ use Doctrine\ORM\Tools\Console\ConsoleRunner; require 'doctrine.php'; -return ConsoleRunner::createHelperSet($entityManager); +return ConsoleRunner::createHelperSet($em); diff --git a/doctrine.php b/doctrine.php index 335f37e30b852582c04183b193d002eebaa9f01c..112892e73332cd62fb16ab93ff959748b0003793 100644 --- a/doctrine.php +++ b/doctrine.php @@ -13,7 +13,7 @@ use Doctrine\ORM\Mapping\Driver\AnnotationDriver; require_once "vendor/autoload.php"; require_once "config.php"; -$paths = array( realpath(__DIR__."/src/BusMap") ); +$paths = array( realpath(__DIR__."/src") ); $isDevMode = true; $cache = new ArrayCache(); @@ -25,4 +25,4 @@ $config->setMetadataCacheImpl($cache); $config->setQueryCacheImpl($cache); $config->setMetadataDriverImpl($driver); -$entityManager = EntityManager::create($cfg['db'], $config); +$em= EntityManager::create($cfg['db'], $config); diff --git a/js/script.js b/js/script.js index d1e9e8714ac4059a17b144de1208bc19ccc69ea4..7559449ebe4e416a584ac96a712cc9da517fe208 100644 --- a/js/script.js +++ b/js/script.js @@ -284,8 +284,8 @@ function updateRouteButtons(vehicles) { function updateMapVehicles(routes, vehicles, busIconUrl) { if (window.map != undefined) { for (i in vehicles) { - if (window.busMarkers[vehicles[i]['routeTag']] == undefined) { - window.busMarkers[vehicles[i]['routeTag']] = {}; + if (window.busMarkers[vehicles[i]['route_tag']] == undefined) { + window.busMarkers[vehicles[i]['route_tag']] = {}; } var stops = []; @@ -300,11 +300,11 @@ function updateMapVehicles(routes, vehicles, busIconUrl) { popup = ""; popup += "<div class='title'>" + label + "</div>"; popup += stopList; - popup += "<div class='footer'>Bus #" + vehicles[i]['id'] + "</div>"; + popup += "<div class='footer'>Bus #" + vehicles[i]['vehicle'] + "</div>"; popup += ""; - if (window.busMarkers[vehicles[i]['routeTag']][i] == undefined) { + if (window.busMarkers[vehicles[i]['route_tag']][i] == undefined) { - window.busMarkers[vehicles[i]['routeTag']][i] = L.marker([vehicles[i]['lat'], vehicles[i]['lon']], { + window.busMarkers[vehicles[i]['route_tag']][i] = L.marker([vehicles[i]['lat'], vehicles[i]['lon']], { icon: L.icon({ iconUrl: busIconUrl, iconSize: [21,25], @@ -317,9 +317,9 @@ function updateMapVehicles(routes, vehicles, busIconUrl) { className: 'busLabel', }).bindPopup(popup).addTo(window.map); } else { - window.busMarkers[vehicles[i]['routeTag']][i].setLatLng([vehicles[i]['lat'], vehicles[i]['lon']]).setPopupContent(popup).setIconAngle(vehicles[i]['heading']); + window.busMarkers[vehicles[i]['route_tag']][i].setLatLng([vehicles[i]['lat'], vehicles[i]['lon']]).setPopupContent(popup).setIconAngle(vehicles[i]['heading']); } - window.busMarkers[vehicles[i]['routeTag']][i].lastUpdated = (new Date).getTime(); + window.busMarkers[vehicles[i]['route_tag']][i].lastUpdated = (new Date).getTime(); } } } diff --git a/src/BusMap/Agency.php b/src/BusMap/Agency.php index 621bc03de306f796e483b80f5f49026b7ac8280f..a99dbd4d1c736d1aca1441a792133217733fd426 100644 --- a/src/BusMap/Agency.php +++ b/src/BusMap/Agency.php @@ -2,6 +2,7 @@ namespace BusMap; +use JsonSerializable; use Doctrine\ORM\Mapping as ORM; /** @@ -10,7 +11,7 @@ use Doctrine\ORM\Mapping as ORM; * @ORM\Table(name="agencies") * @ORM\Entity */ -class Agency +class Agency implements JsonSerializable { /** @@ -19,7 +20,7 @@ class Agency * @ORM\Column(name="tag", type="string", length=50, nullable=false) * @ORM\Id */ - private $tag; + protected $tag; /** * @var string @@ -33,15 +34,40 @@ class Agency * * @ORM\Column(name="shortTitle", type="string", length=100, nullable=true) */ - private $shorttitle; + private $short_title; /** * @var string * * @ORM\Column(name="regionTitle", type="string", length=100, nullable=true) */ - private $regiontitle; + private $region_title; + + public function getTag() { + return $this->tag; + } + + public function getTitle() { + return $this->title; + } + + public function getShort_title() { + return $this->short_title; + } + + public function getRegion_title() { + return $this->region_title; + } + + public function jsonSerialize() { + return array( + 'tag' => $this->tag, + 'title' => $this->title, + 'short_title' => $this->short_title, + 'region_title' => $this->region_title, + ); + } } diff --git a/src/BusMap/Prediction.php b/src/BusMap/Prediction.php index a8004c529317a36c920f653424efbf5d7b3bbebc..f4f0b840a2edc015b64eec271371f9800de5023f 100644 --- a/src/BusMap/Prediction.php +++ b/src/BusMap/Prediction.php @@ -2,6 +2,10 @@ namespace BusMap; +require_once 'src/BusMap/Agency.php'; +require_once 'src/BusMap/Route.php'; + +use JsonSerializable; use Doctrine\ORM\Mapping as ORM; /** @@ -10,7 +14,7 @@ use Doctrine\ORM\Mapping as ORM; * @ORM\Table(name="predictions") * @ORM\Entity */ -class Prediction +class Prediction implements JsonSerializable { /** * @var integer @@ -22,32 +26,30 @@ class Prediction private $id; /** - * @var string - * - * @ORM\Column(name="routeTag", type="string", length=25, nullable=true) + * @ORM\ManyToOne(targetEntity="Route") + * @ORM\JoinColumn(name="routeTag", referencedColumnName="tag") */ - private $routetag; + protected $route; /** - * @var string - * - * @ORM\Column(name="agency", type="string", length=50, nullable=true) + * @ORM\ManyToOne(targetEntity="Agency") + * @ORM\JoinColumn(name="agency", referencedColumnName="tag") */ - private $agency; + protected $agency; /** * @var string * * @ORM\Column(name="stopTag", type="string", length=25, nullable=true) */ - private $stoptag; + private $stop_tag; /** * @var integer * * @ORM\Column(name="epochTime", type="bigint", nullable=true) */ - private $epochtime; + private $epoch_time; /** * @var integer @@ -68,21 +70,21 @@ class Prediction * * @ORM\Column(name="isDeparture", type="boolean", nullable=true) */ - private $isdeparture; + private $is_departure; /** * @var boolean * * @ORM\Column(name="affectedByLayover", type="boolean", nullable=true) */ - private $affectedbylayover; + private $affected_by_layover; /** * @var string * * @ORM\Column(name="dirTag", type="string", length=25, nullable=true) */ - private $dirtag; + private $dir_tag; /** * @var integer @@ -98,6 +100,21 @@ class Prediction */ private $block; + public function jsonSerialize() { + return array( + 'route' => $this->route, + 'agency' => $this->agency, + 'stop_tag' => $this->stop_tag, + 'epoch_time'=> $this->epoch_time, + 'seconds' => $this->seconds, + 'minutes' => $this->minutes, + 'is_departure' => $this->is_departure, + 'affected_by_layover' => $this->affected_by_layover, + 'dir_tag' => $this->dir_tag, + 'vehicle' => $this->vehicle, + 'block' => $this->block, + ); + } } diff --git a/src/BusMap/Route.php b/src/BusMap/Route.php index ad674946f0d7025eb7601602fdcc5b0fbfab76a6..9ca620f939bd3f05f636f0243eb6ae716cf53d5e 100644 --- a/src/BusMap/Route.php +++ b/src/BusMap/Route.php @@ -2,6 +2,7 @@ namespace BusMap; +use JsonSerializable; use Doctrine\ORM\Mapping as ORM; /** @@ -10,38 +11,48 @@ use Doctrine\ORM\Mapping as ORM; * @ORM\Table(name="routes", uniqueConstraints={@ORM\UniqueConstraint(name="agency_tag", columns={"agency", "tag"})}) * @ORM\Entity */ -class Route +class Route implements JsonSerializable { - /** - * @var integer - * - * @ORM\Column(name="id", type="integer", nullable=false) - * @ORM\Id - * @ORM\GeneratedValue(strategy="IDENTITY") - */ - private $id; /** - * @var string - * - * @ORM\Column(name="agency", type="string", length=50, nullable=true) + * @ORM\ManyToOne(targetEntity="Agency") + * @ORM\JoinColumn(name="agency", referencedColumnName="tag") */ - private $agency; + protected $agency; /** * @var string * * @ORM\Column(name="tag", type="string", length=25, nullable=false) + * @ORM\Id */ - private $tag; + protected $tag; /** * @var string * * @ORM\Column(name="title", type="string", length=25, nullable=true) */ - private $title; + protected $title; + public function getAgency() { + return $this->agency; + } -} + public function getTag() { + return $this->tag; + } + + public function getTitle() { + return $this->title; + } + public function jsonSerialize() { + return array( + 'agency' => $this->agency, + 'tag' => $this->tag, + 'title' => $this->title, + ); + } + +} diff --git a/src/BusMap/VehicleLocation.php b/src/BusMap/VehicleLocation.php index 825440266d47dc2aad7c9c2e1e9c137f12c1a24d..5961add629039270fd9691632944129a02d51fb4 100644 --- a/src/BusMap/VehicleLocation.php +++ b/src/BusMap/VehicleLocation.php @@ -2,6 +2,10 @@ namespace BusMap; +require_once 'src/BusMap/Agency.php'; +require_once 'src/BusMap/Route.php'; + +use JsonSerializable; use Doctrine\ORM\Mapping as ORM; /** @@ -10,7 +14,7 @@ use Doctrine\ORM\Mapping as ORM; * @ORM\Table(name="vehicleLocations", uniqueConstraints={@ORM\UniqueConstraint(name="agency_route_vehID", columns={"agency", "routeTag", "vehicle"})}) * @ORM\Entity */ -class VehicleLocation +class VehicleLocation implements JsonSerializable { /** * @var integer @@ -22,11 +26,10 @@ class VehicleLocation private $id; /** - * @var string - * - * @ORM\Column(name="agency", type="string", length=50, nullable=true) + * @ORM\ManyToOne(targetEntity="Agency") + * @ORM\JoinColumn(name="agency", referencedColumnName="tag") */ - private $agency; + protected $agency; /** * @var integer @@ -36,18 +39,17 @@ class VehicleLocation private $vehicle; /** - * @var string - * - * @ORM\Column(name="routeTag", type="string", length=20, nullable=false) + * @ORM\ManyToOne(targetEntity="Route") + * @ORM\JoinColumn(name="routeTag", referencedColumnName="tag") */ - private $routetag; + protected $route; /** * @var string * * @ORM\Column(name="dirTag", type="string", length=20, nullable=true) */ - private $dirtag; + private $dir_tag; /** * @var string @@ -75,7 +77,7 @@ class VehicleLocation * * @ORM\Column(name="speedKmHr", type="integer", nullable=true) */ - private $speedkmhr; + private $speed_kmhr; /** * @var integer @@ -84,6 +86,19 @@ class VehicleLocation */ private $time; + public function jsonSerialize() { + return array( + 'agency' => $this->agency, + 'vehicle' => $this->vehicle, + 'route_tag' => $this->route, + 'dir_tag' => $this->dir_tag, + 'lat' => $this->lat, + 'lon' => $this->lon, + 'heading' => $this->heading, + 'speed_kmhr'=> $this->speed_kmhr, + 'time' => $this->time, + ); + } }