protected function getAllExisting()
 {
     try {
         $liveRouteArray = Route::getLiveRoutes($this->route->getAgency());
         if (array_key_exists($this->route->getTag(), $liveRouteArray)) {
             $liveRoute = $liveRouteArray[$this->route->getTag()];
             return Direction::getDirections($liveRoute, TableUpdate::getLiveVersion());
         }
     } catch (Exception $ex) {
         return false;
     }
     return false;
 }
function generateXMLFile(Agency $agencyObj)
{
    $dbObj = DBPool::getInstance();
    $routeArray = Route::getRoutes($agencyObj);
    $stopArray = Stop::getStops($agencyObj);
    $xmlStr = '<?xml version="1.0" encoding="UTF-8" ?><agency title="' . $agencyObj->getTitle() . '" shortTitle="' . $agencyObj->getShortTitle() . '"></agency>';
    $xml = new SimpleXMLElement($xmlStr);
    foreach ($stopArray as $s) {
        $stop = $xml->addChild("stop");
        $stop->addAttribute("tag", $s->getTag());
        $stop->addAttribute("title", $s->getPrettyTitle());
        $stop->addAttribute("lat", $s->getLatitude());
        $stop->addAttribute("lon", $s->getLongitude());
        $stop->addAttribute("oppositeStopTag", $s->getFlipStopTag());
    }
    foreach ($routeArray as $r) {
        $route = $xml->addChild("route");
        $route->addAttribute("tag", $r->getTag());
        $route->addAttribute("title", $r->getTitle());
        $route->addAttribute("shortTitle", $r->getShortTitle());
        $route->addAttribute("color", $r->getColor());
        $route->addAttribute("latMin", $r->getLatMin());
        $route->addAttribute("latMax", $r->getLatMax());
        $route->addAttribute("lonMin", $r->getLonMin());
        $route->addAttribute("lonMax", $r->getLonMax());
        $route->addAttribute("vehicle", $r->getVehicleType());
        $route->addAttribute("sortOrder", $r->getPosition());
        $directionsArray = Direction::getDirections($r);
        foreach ($directionsArray as $d) {
            $dir = $route->addChild("direction");
            $dir->addAttribute("tag", $d->getTag());
            $dir->addAttribute("title", $d->getPrettyTitle());
            $dir->addAttribute("name", $d->getPrettyName());
            $dir->addAttribute("show", $d->getShow() ? "true" : "false");
            $dir->addAttribute("useForUI", $d->getUseForUi() ? "true" : "false");
            $dbObj->bindParams(array($d->getId()));
            $stopDirMap = $dbObj->get_results("SELECT a.tag, b.position\n                                                FROM stop as a, stop_direction_map as b\n                                                WHERE a.id=b.stop_id AND b.direction_id=?\n                                                ORDER BY b.position");
            if ($dbObj->num_rows > 0) {
                foreach ($stopDirMap as $m) {
                    $map = $dir->addChild("stop");
                    $map->addAttribute("tag", $m->tag);
                }
            }
        }
    }
    $fileName = Util::getBaseDirectoryPath(Util::XML_FILE) . $agencyObj->getShortTitle() . ".xml";
    Util::prettyPrintXml($xml, $fileName);
}
 public function updateStopDirMap()
 {
     /**
      * @var DB
      */
     $dbObj = DBPool::getInstance();
     $routeArray = Route::getRoutes($this->agency);
     $stopArray = Stop::getStops($this->agency);
     $bartApiKey = $this->appConfig['BART_API_KEY'];
     foreach ($routeArray as $routeTag => $routeObj) {
         $pos = 0;
         $directionArray = Direction::getDirections($routeObj);
         foreach ($directionArray as $dirTag => $dirObj) {
             //We're only interested in the directions we're showing
             if (!$dirObj->getShow()) {
                 continue;
             }
             //Fetch the direction details
             $apiURL = str_replace(BartApiEndPoints::DIRECTION, $dirObj->getTag(), BartApiEndPoints::ROUTE_INFO . $bartApiKey);
             $dirInfoXmlBuilder = new XmlObjBuilder($apiURL);
             $dirInfoXml = $dirInfoXmlBuilder->getXmlObj();
             foreach ($dirInfoXml->routes->route->config as $c) {
                 foreach ($c as $station) {
                     $pos++;
                     $stopTag = (string) $station;
                     $tempStopObj = $stopArray[$stopTag];
                     $stopId = $tempStopObj->getId();
                     $tempDirObj = $directionArray[$dirTag];
                     $dirId = $tempDirObj->getId();
                     $dbObj->bindParams(array($stopId, $dirId, $pos, TableUpdate::getVersion()));
                     $dbObj->query("INSERT INTO stop_direction_map\n                            (stop_id, direction_id, position, version, created_date)\n                            VALUES (?, ?, ?, ?, NOW())");
                     if ($dbObj->rows_affected != 1) {
                         //TODO: Log it
                     }
                 }
                 //Stations
             }
         }
         //Directions
     }
     //Routes
 }
 public function updateStopDirMap()
 {
     /**
      * @var DB
      */
     $dbObj = DBPool::getInstance();
     $routeArray = Route::getRoutes($this->agency);
     $stopArray = Stop::getStops($this->agency);
     foreach ($this->xml->route as $r) {
         $routeTag = (string) $r['tag'];
         $routeObj = $routeArray[$routeTag];
         foreach ($r->direction as $d) {
             $pos = 0;
             $dirTag = (string) $d['tag'];
             $directionArray = Direction::getDirections($routeObj);
             $dirObj = $directionArray[$dirTag];
             //We're only interested in the directions we're showing
             //if (!$dirObj->getShow()) { continue; }
             foreach ($d->stop as $s) {
                 $pos++;
                 $stopTag = (string) $s['tag'];
                 $tempStop = $stopArray[$stopTag];
                 if (empty($tempStop)) {
                     var_dump("{$routeTag} {$stopTag}");
                 }
                 $stopId = $tempStop->getId();
                 $tempDir = $directionArray[$dirTag];
                 $dirId = $tempDir->getId();
                 $dbObj->bindParams(array($stopId, $dirId, $pos, TableUpdate::getVersion()));
                 $dbObj->query("INSERT INTO stop_direction_map\n                        (stop_id, direction_id, position, version, created_date)\n                        VALUES (?, ?, ?, ?, NOW())");
                 if ($dbObj->rows_affected != 1) {
                     //TODO: Log it
                 }
             }
         }
     }
 }
 public function updateStops()
 {
     $routeArray = Route::getRoutes($this->agency);
     $flipStopMap = $this->getFlipStopOverrides();
     $stopTitleMap = array();
     $stopLatLonMap = array();
     $uniqueStopsAcrossRoutes = array();
     //Generate maps of stopTag => StopTitle and stopTag => [lat, lon] for
     //later look up
     foreach ($this->xml->route as $r) {
         foreach ($r->stop as $s) {
             $stopTag = (string) $s['tag'];
             $stopTitle = html_entity_decode((string) $s['title']);
             $lat = (string) $s['lat'];
             $lon = (string) $s['lon'];
             $stopTitleMap[$stopTag] = $stopTitle;
             $stopLatLonMap[$stopTag] = array("lat" => $lat, "lon" => $lon);
             $uniqueStopsAcrossRoutes[] = $stopTag;
         }
         //Stops
     }
     //Routes
     //We'll only consider the stops in the directions with show=true
     foreach ($this->xml->route as $r) {
         $routeTag = (string) $r['tag'];
         //if($routeTag != "25") {continue;}
         $routeObj = $routeArray[$routeTag];
         $dirArray = Direction::getDirections($routeObj);
         $agencyName = $routeObj->getAgency()->getShortTitle();
         $trackDuplicatesInDir = array();
         /**
          * @var Array $routeDirDetails - Array representation of the
          * directions and stops in a route
          */
         $routeDirDetails = array();
         foreach ($r->direction as $d) {
             $dirTag = (string) $d['tag'];
             $dirObj = $dirArray[$dirTag];
             $dirStops = array();
             //if ($dirObj->getShow()) {
             foreach ($d->stop as $s) {
                 $stopTag = (string) $s['tag'];
                 $stopTitle = $stopTitleMap[$stopTag];
                 $dirStops[$stopTag] = $stopTitle;
             }
             //Stops
             $routeDirDetails[$dirObj->getTag()] = $dirStops;
             //} //Check for show=true
         }
         //Directions
         //Now that we have the details of the directions for this group,
         //let's find the flip stops
         foreach ($routeDirDetails as $fDirTag => $fDirStops) {
             foreach ($routeDirDetails as $fDirTagDiffDir => $fDirStopsDiffDir) {
                 if ($fDirTag == $fDirTagDiffDir) {
                     continue;
                 }
                 foreach ($fDirStops as $fStopTag => $fStopTitle) {
                     //Check if we have one or more matching flip stops in the
                     //direction we are checking against
                     $fFlipStopInOppDir = $this->getFlipStopsInOppDir($fStopTag, $fStopTitle, $fDirStopsDiffDir);
                     //If we don't have any flip stops continue to the next stop
                     if (count($fFlipStopInOppDir) == 0) {
                         continue;
                     }
                     if (count($fFlipStopInOppDir) > 1) {
                         //We have encountered more than one stop at the
                         //same intersection in a different direction
                         //TODO: This has to go in the e-mail report
                         //Generate the (stopTag, lat, lon) string
                         $fstopLatLonMap = array();
                         $fstopLatLonMap[] = "{$fStopTag}," . implode(",", $stopLatLonMap[$fStopTag]);
                         foreach ($fFlipStopInOppDir as $tempStopForOppDir) {
                             $fstopLatLonMap[] = "{$tempStopForOppDir}," . implode(",", $stopLatLonMap[$tempStopForOppDir]);
                         }
                         $logStr = "More than one stop in diff. direction [agency: " . $agencyName . "] [route: " . $routeTag . "] [current stop|dir: {$fStopTag}|{$fDirTag}] [other stops|dir: " . implode(",", $fFlipStopInOppDir) . "|{$fDirTagDiffDir}] [stop title: " . $fStopTitle . "] [" . implode("|", $fstopLatLonMap) . "]";
                         //$this->logger->log($logStr, Logger::WARN, NxtbusStop::PACKAGE);
                     } else {
                         if (!array_key_exists($fStopTag, $flipStopMap)) {
                             $tempFlipStopTag = $fFlipStopInOppDir[0];
                             $flipStopMap[$fStopTag] = $tempFlipStopTag;
                             $flipStopMap[$tempFlipStopTag] = $fStopTag;
                         }
                     }
                 }
             }
             // Inner loop
         }
         //Find flip stops
     }
     //Routes
     //Check if any of the stops don't have a flip stop
     $uniqueStopsAcrossRoutes = array_unique($uniqueStopsAcrossRoutes);
     foreach ($uniqueStopsAcrossRoutes as $finalCheckStopTag) {
         if (!array_key_exists($finalCheckStopTag, $flipStopMap)) {
             $flipStopMap[$finalCheckStopTag] = "";
         }
     }
     //Create the Stop objects
     $stopArray = array();
     foreach ($this->xml->route as $r) {
         foreach ($r->stop as $s) {
             $stopTag = (string) $s['tag'];
             $stopTitle = html_entity_decode($s['title']);
             if (array_key_exists($stopTag, $flipStopMap) && !array_key_exists($stopTag, $stopArray)) {
                 $stopObj = new Stop();
                 $stopObj->setAgency($this->agency);
                 $stopObj->setTag($stopTag);
                 $stopObj->setTitle((string) $stopTitle);
                 $stopObj->setFlipStopTag($flipStopMap[$stopTag]);
                 $stopObj->setLatitude((string) $s['lat']);
                 $stopObj->setLongitude((string) $s['lon']);
                 $stopArray[$stopTag] = $stopObj;
             }
         }
         //Stops
     }
     //Routes
     try {
         //Pass it to the base RouteUpdate class
         $stopUpdate = new StopUpdate($this->agency, $stopArray);
         $stopUpdate->updateDB();
     } catch (Exception $ex) {
         throw new Exception($ex->getMessage());
     }
 }