Inheritance: extends Phinx\Migration\AbstractMigration
 public static function getStops(Agency $agency, $version = 0)
 {
     /**
      * @var DB
      */
     $dbObj = DBPool::getInstance();
     $version = $version == 0 ? TableUpdate::getVersion() : $version;
     $dbObj->bindParams(array($agency->getId(), TableUpdate::getVersion()));
     $stops = $dbObj->get_results("SELECT * FROM stop WHERE agency_id=?\n            AND version = ?");
     if ($dbObj->num_rows > 0) {
         $stopArray = array();
         foreach ($stops as $s) {
             $stopObj = new Stop();
             $stopObj->setId($s->id);
             $stopObj->setAgency($agency);
             $stopObj->setFlipStopTag($s->flip_stop_tag);
             $stopObj->setTag($s->tag);
             $stopObj->setTitle($s->title);
             $stopObj->setPrettyTitle($s->pretty_title);
             $stopObj->setLatitude($s->latitude);
             $stopObj->setLongitude($s->longitude);
             $stopArray[$s->tag] = $stopObj;
         }
         return $stopArray;
     } else {
         //TODO: Don't use a generic exception
         throw new Exception("No data available - Stop::getStops");
     }
 }
 public static function getBaseDirectoryPath($type)
 {
     $basePath = Configuration::getBasePath();
     $baseDirPath = $basePath . "www/data/" . $type . "/" . TableUpdate::getVersion() . "/";
     Util::createDir($baseDirPath);
     return $baseDirPath;
 }
 public static function getDirections(Route $route, $version = 0)
 {
     /**
      * @var DB
      */
     $dbObj = DBPool::getInstance();
     $version = $version == 0 ? TableUpdate::getVersion() : $version;
     $dbObj->bindParams(array($route->getId(), $version));
     $directions = $dbObj->get_results("SELECT * FROM direction WHERE route_id=?\n            AND version = ?");
     if ($dbObj->num_rows > 0) {
         $directionArray = array();
         foreach ($directions as $d) {
             $dirObj = new Direction();
             $dirObj->setId($d->id);
             $dirObj->setName($d->name);
             $dirObj->setPrettyName($d->pretty_name);
             $dirObj->setRoute($route);
             $dirObj->setTag($d->tag);
             $dirObj->setTitle($d->title);
             $dirObj->setPrettyTitle($d->pretty_title);
             $dirObj->setUseForUi($d->use_for_ui);
             $dirObj->setShow($d->show);
             $directionArray[$d->tag] = $dirObj;
         }
         return $directionArray;
     } else {
         //TODO: Don't use a generic exception
         throw new Exception("No data available - Direction::getDirections");
     }
 }
 /**
  *
  * @return Boolean - true if changes are present in the current version
  */
 public static function changesPresentInCurrentVersion()
 {
     $dbObj = DBPool::getInstance();
     $query = "SELECT changes_present FROM version WHERE id = " . TableUpdate::getVersion();
     if (true == $dbObj->get_var($query)) {
         return true;
     } else {
         return false;
     }
 }
 /**
  * Add the routes to the database
  * @param array $routes - Array of Route objects
  */
 protected function add(array $routes)
 {
     $insertCnt = 0;
     $version = TableUpdate::getVersion();
     foreach ($routes as $r) {
         //Add the mandatory columns
         $columns = array('agency_id', 'tag', 'color', 'title', 'short_title', 'vehicle_type', 'version', 'position');
         $boundParams = array($this->agency->getId(), $r->getTag(), $r->getColor(), $r->getTitle(), $r->getShortTitle(), $r->getVehicleType(), $version, $r->getPosition());
         $columnCnt = count($columns);
         //Check and add the optional columns
         if ($r->getLatMin()) {
             $columns[] = 'lat_min';
             $boundParams[] = $r->getLatMin();
             $columnCnt++;
         }
         if ($r->getLatMax()) {
             $columns[] = 'lat_max';
             $boundParams[] = $r->getLatMax();
             $columnCnt++;
         }
         if ($r->getLonMin()) {
             $columns[] = 'lon_min';
             $boundParams[] = $r->getLonMin();
             $columnCnt++;
         }
         if ($r->getLonMax()) {
             $columns[] = 'lon_max';
             $boundParams[] = $r->getLonMax();
             $columnCnt++;
         }
         $this->dbObj->bindParams($boundParams);
         //var_dump($boundParams);
         $query = "INSERT INTO route (" . implode(",", $columns) . ", created_date)\n                VALUES (" . implode(",", array_fill(0, $columnCnt, "?")) . ", NOW())";
         $this->dbObj->query($query);
         if ($this->dbObj->rows_affected != 1) {
             //$this->dbObj->debug();exit;
             throw new DBException("Addition of route failed [agency:" . $this->agency->getId() . "] [route tag:" . $r->getTag() . "] [route title:" . $r->getTitle() . "]");
         }
         $insertCnt++;
     }
     //TODO: Add a log for the total number of rows added
     //Write the changes to the change log
     $this->saveChangesToFile();
 }
 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
 }
 protected function add(array $stops)
 {
     //var_dump($stops);exit;
     $insertCnt = 0;
     $version = TableUpdate::getVersion();
     foreach ($stops as $s) {
         $boundParams = array($this->agency->getId(), $s->getTag(), $s->getLatitude(), $s->getLongitude(), $s->getTitle(), $this->getPrettyTitle($s->getTitle()), $s->getFlipStopTag(), $version);
         $this->dbObj->bindParams($boundParams);
         $query = "INSERT INTO stop (agency_id, tag, latitude, longitude, \n                title, pretty_title, flip_stop_tag, version, created_date)\n                VALUES (?, ?, ?, ?, ?, ?, ?, ?, NOW())";
         $this->dbObj->query($query);
         if ($this->dbObj->rows_affected != 1) {
             //$this->dbObj->debug();exit;
             throw new DBException("Addition of stop failed [agency:" . $this->agency->getId() . "] [stop tag:" . $s->getTag() . "]");
         }
         $insertCnt++;
     }
     //TODO: Add a log for the total number of stops added
     //Write the changes to the change log
     $this->saveChangesToFile();
 }
 protected function add(array $directions)
 {
     $insertCnt = 0;
     $version = TableUpdate::getVersion();
     foreach ($directions as $d) {
         $boundParams = array($this->route->getId(), $d->getTitle(), $this->getPrettyTitle($d->getTitle()), $d->getName(), $this->getPrettyName($d->getName()), $d->getTag(), $d->getUseForUi() ? 1 : 0, $d->getShow() ? 1 : 0, $version);
         //var_dump($boundParams);
         $this->dbObj->bindParams($boundParams);
         $query = "INSERT INTO direction (route_id, title, pretty_title,\n                name, pretty_name, tag, use_for_ui, show, version, created_date)\n                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())";
         $this->dbObj->query($query);
         //$this->dbObj->debug();
         //print $query;
         if ($this->dbObj->rows_affected != 1) {
             throw new DBException("Addition of direction failed [agency:" . $this->route->getAgency()->getId() . "] [route tag:" . $this->route->getTag() . "] [route title:" . $this->route->getTitle() . "]");
         }
         $insertCnt++;
     }
     //TODO: Add a log for the total number of directions added
     //Write the changes to the change log
     $this->saveChangesToFile();
 }
 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
                 }
             }
         }
     }
 }
                $bartStopDirMap->updateStopDirMap();
                //print "Stop-Dir map updated";
            }
        }
        /******************** GENERATE THE XML FILE ******************/
        generateXMLFile($agencyObj);
        //Add filename to zip file contents array
        $zipFileContents[] = Util::getBaseDirectoryPath(Util::XML_FILE) . $agencyObj->getShortTitle() . ".xml";
    }
    //Create the zip file
    $zipFileContents[] = Util::getBaseDirectoryPath(Util::XML_FILE) . "bart-platforms.xml";
    $destination = Util::getBaseDirectoryPath(Util::XML_FILE) . "data.zip";
    Util::createZip($zipFileContents, $destination, true);
    //Delete old versions
    Version::deleteOldVersions();
    $mailMessage = "Version: " . TableUpdate::getVersion();
    $mailMessage .= "\n\nThe job was executed successfully without any errors.";
    if (Version::changesPresentInCurrentVersion()) {
        $mailMessage .= "\nChanges were detected when compared with the live version.\n            Please login to the admin console for the changelog.";
    } else {
        $mailMessage .= "\nNo changes were detected when compared with the live version.";
    }
    print "1";
} catch (Exception $ex) {
    $logger->log($ex->getTraceAsString(), Logger::WARN, "UPDATE");
    $mailMessage = "Version: " . TableUpdate::getVersion();
    $mailMessage = "\n\nThe job could not be executed successfully. Please check the log for errors.";
    print "-1";
}
$subject = "Kronos Server update for " . date("Y-m-d");
Util::mail($config['email ids'], $subject, $mailMessage);
Beispiel #11
0
 /**
  * Create or update the database tables for the plugin.
  *
  * @since 0.1.0
  * @return Boolean
  */
 public function updateTables() : bool
 {
     $update = new TableUpdate($this->pluginVersion);
     return $update->run();
 }
            //We absolutely need the last stop
            foreach ($tmpPathArray as $key => $xy) {
                if ($key < $stepCnt || $key % $stepCnt == 0) {
                    $tmpArray[] = $xy;
                }
            }
            $pathStr = implode("|", array_reverse($tmpArray));
            $stepCnt++;
        } else {
            break;
        }
    }
    //var_dump($stepCnt);
    return $pathStr;
}
$version = $_GET['version'];
TableUpdate::setVersion($version);
if (!ctype_digit($version)) {
    print '0';
} else {
    try {
        generateMapImages($version);
        $dbObj = DBPool::getInstance();
        $dbObj->bindParams(array($version));
        $dbObj->query("UPDATE version SET images_generated=true WHERE id=?");
        print '1';
    } catch (Exception $ex) {
        //var_dump($ex->getTraceAsString());
        print '-2';
    }
}
 /**
  * Given an Agency object, return an array of Route objects for the live
  * version
  * @param Agency $agency
  * @return Array Key is route tag and value is Route object
  */
 public static function getLiveRoutes(Agency $agency)
 {
     //TODO: This gets called for every direction. Needs to be cached
     return self::getRoutes($agency, TableUpdate::getLiveVersion());
 }
 protected function saveChangesToFile()
 {
     if (count($this->addedTags) == 0 && count($this->removedTags) == 0 && count($this->changedTags) == 0) {
         return false;
     }
     $fileName = Util::getChangeLogFile();
     $data = "*** " . $this->table . " ***\n";
     $data .= $this->getChangesAsString();
     file_put_contents($fileName, $data, FILE_APPEND);
     $this->dbObj->bindParams(array(TableUpdate::getVersion()));
     $this->dbObj->query("UPDATE version SET changes_present=true WHERE id=?");
 }