diff --git a/inc/project.php b/inc/Project.php similarity index 96% rename from inc/project.php rename to inc/Project.php index e73863f..45d4132 100644 --- a/inc/project.php +++ b/inc/Project.php @@ -1,215 +1,215 @@ -self::MODE_PREVIZ, 'blog'=>self::MODE_BLOG, 'histo'=>self::MODE_HISTO); - - //DB Tables - const PROJ_TABLE = 'projects'; - - /** - * Database Handle - * @var Db - */ - private $oDb; - - private $iProjectId; - private $sMode; - private $sName; - private $sCodeName; - private $asActive; - private $asGeo; - - public function __construct(Db &$oDb, $iProjectId=0) { - parent::__construct(__CLASS__, Settings::DEBUG); - $this->oDb = &$oDb; - if($iProjectId > 0) $this->setProjectId($iProjectId); - } - - public function getProjectId() { - return $this->iProjectId; - } - - public function setProjectId($iProjectId=0) { - if($iProjectId > 0) { - $this->iProjectId = $iProjectId; - } - else { - /** - * Project 1 [-----------------] - * Project 2 [---------------------------] - * Project 3 [-----------] - * Selected Project [-------Project 1-------][------------Project 2-------------][---------------Project 3------------------ - * Mode --P--][--------B--------][--P--][-----------B---------------][---P---][-----B-----][---------H---------- - */ - $sQuery = - "SELECT MAX(id_project) ". - "FROM projects ". - "WHERE active_to = (". - "SELECT MIN(active_to) ". //Select closest project in the future - "FROM projects ". - "WHERE active_to > NOW() ". //Select Next project - "OR active_to = (". //In case there is no next project, select the last one - "SELECT MAX(active_to) ". - "FROM projects". - ")". - ")"; - $asResult = $this->oDb->getArrayQuery($sQuery, true); - $this->iProjectId = array_shift($asResult); - } - - $this->setProjectInfo(); - } - - public function createProjectId() { - $this->setProjectId($this->oDb->insertRow(self::PROJ_TABLE, array('codename'=>''))); - return $this->getProjectId(); - } - - public function getMode() { - return $this->sMode; - } - - public function getProjectName() { - return $this->sName; - } - - public function setProjectName($sName) { - return $this->updateField('name', $sName); - } - - public function getProjectCodeName() { - return $this->sCodeName; - } - - public function setProjectCodeName($sCodeName) { - return $this->updateField('codename', $sCodeName); - } - - public function getActivePeriod($sFromTo='') { - return ($sFromTo=='')?$this->asActive:$this->asActive[$sFromTo]; - } - - public function setActivePeriod($oValue, $sFromTo='') { - if($sFromTo=='') { - $this->updateField('active_from', $oValue['from']); - return $this->updateField('active_to', $oValue['to']); - } - else { - return $this->updateField('active_'.$sFromTo, $oValue); - } - } - - public function getFeedIds() { - return $this->oDb->selectColumn( - Feed::FEED_TABLE, - Db::getId(Feed::FEED_TABLE), - array(Db::getId(self::PROJ_TABLE) => $this->getProjectId()) - ); - } - - public function getProjects($iProjectId=0) { - $bSpecificProj = ($iProjectId > 0); - $asInfo = array( - 'select'=> array( - Db::getId(self::PROJ_TABLE)." AS id", - 'codename', - 'name', - 'active_from', - 'active_to', - "IF(NOW() BETWEEN active_from AND active_to, 1, IF(NOW() < active_from, 0, 2)) AS mode" - ), - 'from' => self::PROJ_TABLE - ); - if($bSpecificProj) $asInfo['constraint'] = array(Db::getId(self::PROJ_TABLE)=>$iProjectId); - - $asProjects = $this->oDb->selectRows($asInfo, 'codename'); - foreach($asProjects as $sCodeName=>&$asProject) { - switch($asProject['mode']) { - case 0: $asProject['mode'] = self::MODE_PREVIZ; break; - case 1: $asProject['mode'] = self::MODE_BLOG; break; - case 2: $asProject['mode'] = self::MODE_HISTO; break; - } - - if($sCodeName != '' && !Converter::isGeoJsonValid($sCodeName)) Converter::convertToGeoJson($sCodeName); - - $asProject['geofilepath'] = Spot::addTimestampToFilePath(Geo::getFilePath($sCodeName, GeoJson::EXT)); - $asProject['gpxfilepath'] = Spot::addTimestampToFilePath(Geo::getFilePath($sCodeName, Gpx::EXT)); - $asProject['codename'] = $sCodeName; - } - return $bSpecificProj?$asProject:$asProjects; - } - - public function getProject() { - return $this->getProjects($this->getProjectId()); - } - - public function getLastUpdate(): int { - $iLastUpdate = INF; - - $asFeedIds = $this->getFeedIds(); - foreach($asFeedIds as $iFeedId) { - $iLastUpdate = min($iLastUpdate, (new Feed($this->oDb, $iFeedId))->getLastUpdate()); - } - - return $iLastUpdate; - } - - public function getMaps() { - $sQuery = - "SELECT codename, geo_name, min_zoom, max_zoom, attribution ". - "FROM ".Spot::MAPPING_TABLE." ". - "LEFT JOIN ".Spot::MAP_TABLE." USING(".Db::getId(Spot::MAP_TABLE).") ". - "WHERE ".Db::getId(self::PROJ_TABLE)." = ".$this->getProjectId()." ". - "OR ".Db::getId(self::PROJ_TABLE)." IS NULL "; - return $this->oDb->getArrayQuery($sQuery, true); - } - - private function setProjectInfo() { - if($this->getProjectId() > 0) { - $asProject = $this->getProject(); - - $this->sMode = $asProject['mode']; - $this->asActive = array('from'=>$asProject['active_from'], 'to'=>$asProject['active_to']); - $this->sCodeName = $asProject['codename']; - $this->sName = $asProject['name']; - $this->asGeo = array('geofile'=>$asProject['geofilepath'], 'gpxfile'=>$asProject['gpxfilepath']); - } - else $this->addError('Error while setting project: no project ID'); - } - - private function updateField($sField, $oValue) { - $bResult = ($this->oDb->updateRow(self::PROJ_TABLE, $this->getProjectId(), array($sField=>$oValue)) > 0); - $this->setProjectInfo(); - - return $bResult; - } - - public function delete() { - $asResult = array(); - if($this->getProjectId() > 0) { - $asFeedIds = $this->getFeedIds(); - foreach($asFeedIds as $iFeedId) { - $asResult['feed'][] = (new Feed($this->oDb, $iFeedId))->delete(); - } - - $asResult['project'][] = array( - 'id' => $this->getProjectId(), - 'del' => $this->oDb->deleteRow(self::PROJ_TABLE, $this->getProjectId()), - 'desc' => $this->oDb->getLastError() - ); - } - else $asResult['project'][] = array('del'=>false, 'desc'=>'Error while setting project: no project ID'); - - return $asResult; - } -} +self::MODE_PREVIZ, 'blog'=>self::MODE_BLOG, 'histo'=>self::MODE_HISTO); + + //DB Tables + const PROJ_TABLE = 'projects'; + + /** + * Database Handle + * @var Db + */ + private $oDb; + + private $iProjectId; + private $sMode; + private $sName; + private $sCodeName; + private $asActive; + private $asGeo; + + public function __construct(Db &$oDb, $iProjectId=0) { + parent::__construct(__CLASS__, Settings::DEBUG); + $this->oDb = &$oDb; + if($iProjectId > 0) $this->setProjectId($iProjectId); + } + + public function getProjectId() { + return $this->iProjectId; + } + + public function setProjectId($iProjectId=0) { + if($iProjectId > 0) { + $this->iProjectId = $iProjectId; + } + else { + /** + * Project 1 [-----------------] + * Project 2 [---------------------------] + * Project 3 [-----------] + * Selected Project [-------Project 1-------][------------Project 2-------------][---------------Project 3------------------ + * Mode --P--][--------B--------][--P--][-----------B---------------][---P---][-----B-----][---------H---------- + */ + $sQuery = + "SELECT MAX(id_project) ". + "FROM projects ". + "WHERE active_to = (". + "SELECT MIN(active_to) ". //Select closest project in the future + "FROM projects ". + "WHERE active_to > NOW() ". //Select Next project + "OR active_to = (". //In case there is no next project, select the last one + "SELECT MAX(active_to) ". + "FROM projects". + ")". + ")"; + $asResult = $this->oDb->getArrayQuery($sQuery, true); + $this->iProjectId = array_shift($asResult); + } + + $this->setProjectInfo(); + } + + public function createProjectId() { + $this->setProjectId($this->oDb->insertRow(self::PROJ_TABLE, array('codename'=>''))); + return $this->getProjectId(); + } + + public function getMode() { + return $this->sMode; + } + + public function getProjectName() { + return $this->sName; + } + + public function setProjectName($sName) { + return $this->updateField('name', $sName); + } + + public function getProjectCodeName() { + return $this->sCodeName; + } + + public function setProjectCodeName($sCodeName) { + return $this->updateField('codename', $sCodeName); + } + + public function getActivePeriod($sFromTo='') { + return ($sFromTo=='')?$this->asActive:$this->asActive[$sFromTo]; + } + + public function setActivePeriod($oValue, $sFromTo='') { + if($sFromTo=='') { + $this->updateField('active_from', $oValue['from']); + return $this->updateField('active_to', $oValue['to']); + } + else { + return $this->updateField('active_'.$sFromTo, $oValue); + } + } + + public function getFeedIds() { + return $this->oDb->selectColumn( + Feed::FEED_TABLE, + Db::getId(Feed::FEED_TABLE), + array(Db::getId(self::PROJ_TABLE) => $this->getProjectId()) + ); + } + + public function getProjects($iProjectId=0) { + $bSpecificProj = ($iProjectId > 0); + $asInfo = array( + 'select'=> array( + Db::getId(self::PROJ_TABLE)." AS id", + 'codename', + 'name', + 'active_from', + 'active_to', + "IF(NOW() BETWEEN active_from AND active_to, 1, IF(NOW() < active_from, 0, 2)) AS mode" + ), + 'from' => self::PROJ_TABLE + ); + if($bSpecificProj) $asInfo['constraint'] = array(Db::getId(self::PROJ_TABLE)=>$iProjectId); + + $asProjects = $this->oDb->selectRows($asInfo, 'codename'); + foreach($asProjects as $sCodeName=>&$asProject) { + switch($asProject['mode']) { + case 0: $asProject['mode'] = self::MODE_PREVIZ; break; + case 1: $asProject['mode'] = self::MODE_BLOG; break; + case 2: $asProject['mode'] = self::MODE_HISTO; break; + } + + if($sCodeName != '' && !Converter::isGeoJsonValid($sCodeName)) Converter::convertToGeoJson($sCodeName); + + $asProject['geofilepath'] = Spot::addTimestampToFilePath(Geo::getFilePath($sCodeName, GeoJson::EXT)); + $asProject['gpxfilepath'] = Spot::addTimestampToFilePath(Geo::getFilePath($sCodeName, Gpx::EXT)); + $asProject['codename'] = $sCodeName; + } + return $bSpecificProj?$asProject:$asProjects; + } + + public function getProject() { + return $this->getProjects($this->getProjectId()); + } + + public function getLastUpdate(): int { + $iLastUpdate = INF; + + $asFeedIds = $this->getFeedIds(); + foreach($asFeedIds as $iFeedId) { + $iLastUpdate = min($iLastUpdate, (new Feed($this->oDb, $iFeedId))->getLastUpdate()); + } + + return $iLastUpdate; + } + + public function getMaps() { + $sQuery = + "SELECT codename, geo_name, min_zoom, max_zoom, attribution ". + "FROM ".Spot::MAPPING_TABLE." ". + "LEFT JOIN ".Spot::MAP_TABLE." USING(".Db::getId(Spot::MAP_TABLE).") ". + "WHERE ".Db::getId(self::PROJ_TABLE)." = ".$this->getProjectId()." ". + "OR ".Db::getId(self::PROJ_TABLE)." IS NULL "; + return $this->oDb->getArrayQuery($sQuery, true); + } + + private function setProjectInfo() { + if($this->getProjectId() > 0) { + $asProject = $this->getProject(); + + $this->sMode = $asProject['mode']; + $this->asActive = array('from'=>$asProject['active_from'], 'to'=>$asProject['active_to']); + $this->sCodeName = $asProject['codename']; + $this->sName = $asProject['name']; + $this->asGeo = array('geofile'=>$asProject['geofilepath'], 'gpxfile'=>$asProject['gpxfilepath']); + } + else $this->addError('Error while setting project: no project ID'); + } + + private function updateField($sField, $oValue) { + $bResult = ($this->oDb->updateRow(self::PROJ_TABLE, $this->getProjectId(), array($sField=>$oValue)) > 0); + $this->setProjectInfo(); + + return $bResult; + } + + public function delete() { + $asResult = array(); + if($this->getProjectId() > 0) { + $asFeedIds = $this->getFeedIds(); + foreach($asFeedIds as $iFeedId) { + $asResult['feed'][] = (new Feed($this->oDb, $iFeedId))->delete(); + } + + $asResult['project'][] = array( + 'id' => $this->getProjectId(), + 'del' => $this->oDb->deleteRow(self::PROJ_TABLE, $this->getProjectId()), + 'desc' => $this->oDb->getLastError() + ); + } + else $asResult['project'][] = array('del'=>false, 'desc'=>'Error while setting project: no project ID'); + + return $asResult; + } +}