From d81b4a242855984aacf9fefd8426948e8c83db36 Mon Sep 17 00:00:00 2001 From: Franzz Date: Sat, 8 Feb 2020 21:13:52 +0100 Subject: [PATCH] Improve admin page (add new projects) --- inc/feed.php | 73 ++++++++++++++++++---- inc/project.php | 18 +++++- inc/spot.php | 73 +++++++++++++++++----- index.php | 12 ++-- languages/en.lang | 9 +++ languages/fr.lang | 9 +++ masks/admin.html | 134 ++++++++++++++++++++++++++++++----------- readme.md | 8 ++- script/spot.js | 9 ++- style/_mask_admin.scss | 42 +++++++++++-- style/spot.css | 2 +- style/spot.css.map | 2 +- 12 files changed, 312 insertions(+), 79 deletions(-) diff --git a/inc/feed.php b/inc/feed.php index 90a614a..bb37b24 100644 --- a/inc/feed.php +++ b/inc/feed.php @@ -2,10 +2,7 @@ /** * Feed Class - * Also takes care of spot (device) & messages - * - * To add a new feed, create a new records in the feed table: - * INSERT INTO feeds (ref_feed_id, id_spot, id_project) VALUES ('', '', ''); + * Also manages spots (devices) & messages */ class Feed extends PhpObject { @@ -29,24 +26,68 @@ class Feed extends PhpObject { private $sRefFeedId; private $iLastUpdate; - public function __construct(Db &$oDb, $iFeedId) { + public function __construct(Db &$oDb, $iFeedId=0) { parent::__construct(__CLASS__, Settings::DEBUG); $this->oDb = &$oDb; - $this->setFeedId($iFeedId); + if($iFeedId > 0) $this->setFeedId($iFeedId); + } + + public function getFeedId() { + return $this->iFeedId; } public function setFeedId($iFeedId) { $this->iFeedId = $iFeedId; - $asFeed = $this->oDb->selectRow(self::FEED_TABLE, $this->iFeedId); + $asFeed = $this->getFeed(); $this->sRefFeedId = $asFeed['ref_feed_id']; $this->iLastUpdate = strtotime($asFeed['last_update']); } + public function createFeedId($oProjectId) { + $this->setFeedId($this->oDb->insertRow(self::FEED_TABLE, array( + Db::getId(Project::PROJ_TABLE) => $oProjectId, + 'status' => 'INACTIVE' + ))); + return $this->getFeedId(); + } + + public function setRefFeedId($sRefFeedId) { + return $this->updateField('ref_feed_id', $sRefFeedId); + } + + public function setSpotId($iSpotId) { + return $this->updateField(Db::getId(self::SPOT_TABLE), $iSpotId); + } + + public function setProjectId($iProjectId) { + return $this->updateField(Db::getId(Project::PROJ_TABLE), $iProjectId); + } + + public function getSpots() { + $asSpots = $this->oDb->selectRows(array('from'=>self::SPOT_TABLE)); + foreach($asSpots as &$asSpot) $asSpot['id'] = $asSpot[Db::getId(self::SPOT_TABLE)]; + return $asSpots; + } + + public function getFeeds($iFeedId=0) { + $asInfo = array('from'=>self::FEED_TABLE); + if($iFeedId > 0) $asInfo['constraint'] = array(Db::getId(self::FEED_TABLE)=>$iFeedId); + $asFeeds = $this->oDb->selectRows($asInfo); + + foreach($asFeeds as &$asFeed) $asFeed['id'] = $asFeed[Db::getId(self::FEED_TABLE)]; + return $asFeeds; + } + + public function getFeed() { + $asFeeds = $this->getFeeds($this->getFeedId()); + return array_shift($asFeeds); + } + public function getMessages($asActivePeriod = array()) { $asInfo = array( 'select' => array('id_message', 'ref_msg_id', 'type', 'latitude', 'longitude', 'site_time', 'unix_time'), 'from' => self::MSG_TABLE, - 'constraint'=> array(Db::getId(self::FEED_TABLE) => $this->iFeedId), + 'constraint'=> array(Db::getId(self::FEED_TABLE) => $this->getFeedId()), 'constOpe' => array(Db::getId(self::FEED_TABLE) => "="), 'orderBy' => array('site_time'=>'ASC') ); @@ -114,12 +155,22 @@ class Feed extends PhpObject { } } } - else $this->oDb->updateRow(self::FEED_TABLE, $this->iFeedId, array('last_update'=>$sLastUpdate)); + else $this->oDb->updateRow(self::FEED_TABLE, $this->getFeedId(), array('last_update'=>$sLastUpdate)); } private function retrieveFeed() { - $sUrl = self::FEED_HOOK.$this->sRefFeedId.self::FEED_TYPE_JSON; - $sContent = file_get_contents($sUrl); + $sContent = '[]'; + if($this->sRefFeedId !='') { + $sUrl = self::FEED_HOOK.$this->sRefFeedId.self::FEED_TYPE_JSON; + $sContent = file_get_contents($sUrl); + } return json_decode($sContent, true); } + + private function updateField($sField, $oValue) { + $bResult = ($this->oDb->updateRow(self::FEED_TABLE, $this->getFeedId(), array($sField=>$oValue)) > 0); + $this->setFeedId($this->getFeedId()); + + return $bResult; + } } diff --git a/inc/project.php b/inc/project.php index eb9d276..e251d86 100644 --- a/inc/project.php +++ b/inc/project.php @@ -24,9 +24,10 @@ class Project extends PhpObject { private $asActive; private $asGeo; - public function __construct(Db &$oDb) { + public function __construct(Db &$oDb, $iProjectId=0) { parent::__construct(__CLASS__, Settings::DEBUG); $this->oDb = &$oDb; + if($iProjectId > 0) $this->setProjectId($iProjectId); } public function getProjectId() { @@ -64,10 +65,23 @@ class Project extends PhpObject { $this->setProjectInfo(); } + public function createProjectId() { + $this->setProjectId($this->oDb->insertRow(self::PROJ_TABLE, array('timezone'=>Settings::TIMEZONE))); + 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; } @@ -130,7 +144,7 @@ class Project extends PhpObject { case 2: $asProject['mode'] = self::MODE_HISTO; break; } - if(!Converter::isGeoJsonValid($sCodeName)) Converter::convertToGeoJson($sCodeName); + 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)); diff --git a/inc/spot.php b/inc/spot.php index 6907cab..cb36600 100755 --- a/inc/spot.php +++ b/inc/spot.php @@ -338,30 +338,73 @@ class Spot extends Main } public function getAdminSettings() { - return self::getJsonResult(true, '', array('projects'=>$this->oProject->getProjects())); + $oFeed = new Feed($this->oDb); + return self::getJsonResult(true, '', array( + 'project' => $this->oProject->getProjects(), + 'feed' => $oFeed->getFeeds(), + 'spot' => $oFeed->getSpots() + )); } - public function setAdminSettings($sField, $sValue) { + public function setAdminSettings($sType, $iId, $sField, $sValue) { $bSuccess = false; $sDesc = ''; - switch($sField) { - case 'codename': - $bSuccess = $this->oProject->setProjectCodeName($sValue); + switch($sType) { + case 'project': + $oProject = new Project($this->oDb, $iId); + switch($sField) { + case 'name': + $bSuccess = $oProject->setProjectName($sValue); + break; + case 'codename': + $bSuccess = $oProject->setProjectCodeName($sValue); + break; + case 'active_from': + $bSuccess = $oProject->setActivePeriod($sValue.' 00:00:00', 'from'); + break; + case 'active_to': + $bSuccess = $oProject->setActivePeriod($sValue.' 23:59:59', 'to'); + break; + case 'timezone': + $bSuccess = $oProject->setTimeZone($sValue); + break; + } + $asResult = $oProject->getProject(); break; - case 'active_from': - $bSuccess = $this->oProject->setActivePeriod($sValue.' 00:00:00', 'from'); - break; - case 'active_to': - $bSuccess = $this->oProject->setActivePeriod($sValue.' 23:59:59', 'to'); - break; - case 'timezone': - $bSuccess = $this->oProject->setTimeZone($sValue); + case 'feed': + case 'spot': + $oFeed = new Feed($this->oDb, $iId); + switch($sField) { + case 'ref_feed_id': + $bSuccess = $oFeed->setRefFeedId($sValue); + break; + case 'spot_id': + $bSuccess = $oFeed->setSpotId($sValue); + break; + case 'project_id': + $bSuccess = $oFeed->setProjectId($sValue); + break; + } + $asResult = $oFeed->getFeed(); break; } if(!$bSuccess) $sDesc = Mask::LANG_PREFIX.'error_commit_db'; - return self::getJsonResult($bSuccess, $sDesc, array('values'=>$this->oProject->getProject())); + return self::getJsonResult($bSuccess, $sDesc, array($sType=>array($asResult))); + } + + public function createProject() { + $oProject = new Project($this->oDb); + $iNewProjectId = $oProject->createProjectId(); + + $oFeed = new Feed($this->oDb); + $oFeed->createFeedId($iNewProjectId); + + return self::getJsonResult($iNewProjectId>0, '', array( + 'project' => array($oProject->getProject()), + 'feed' => array($oFeed->getFeed()) + )); } public function convertGpxToGeojson($sGeoFileName) { @@ -385,7 +428,7 @@ class Spot extends Main //Seconds $fSecond = round($dLeft * 3600, 1); - return $iDegree.'°'.$iMinute.'\''.$fSecond.'"'.$sDirection; + return $iDegree.'°'.$iMinute."'".$fSecond.'"'.$sDirection; } public function getTimeFormat($iTime) { diff --git a/index.php b/index.php index b54fb15..77c23eb 100755 --- a/index.php +++ b/index.php @@ -20,7 +20,8 @@ $iChunk = isset($_GET['chunk'])?$_GET['chunk']:0; $iProjectId = isset($_REQUEST['project_id'])?$_REQUEST['project_id']:0; $sField = isset($_REQUEST['field'])?$_REQUEST['field']:''; $oValue = isset($_REQUEST['value'])?$_REQUEST['value']:''; -$iId = isset($_REQUEST['id'])?$_REQUEST['id']:''; +$iId = isset($_REQUEST['id'])?$_REQUEST['id']:0; +$sType = isset($_REQUEST['type'])?$_REQUEST['type']:''; //Initiate class $oSpot = new Spot($oClassManagement, __FILE__, $sTimezone); @@ -52,11 +53,14 @@ if($sAction!='') case 'sync_pics': $sResult = $oSpot->syncPics(); break; - case 'get_admin': + case 'admin_get': $sResult = $oSpot->getAdminSettings(); break; - case 'set_admin': - $sResult = $oSpot->setAdminSettings($sField, $oValue); + case 'admin_set': + $sResult = $oSpot->setAdminSettings($sType, $iId, $sField, $oValue); + break; + case 'admin_new': + $sResult = $oSpot->createProject(); break; case 'build_geojson': $sResult = $oSpot->convertGpxToGeojson($sName); diff --git a/languages/en.lang b/languages/en.lang index 224c396..35e6e38 100644 --- a/languages/en.lang +++ b/languages/en.lang @@ -41,11 +41,20 @@ media_comment_update= Comment of media "$0" updated city_time = $0 Time +project_id = Project ID project = Project mode = Mode code_name = Code name start = Start end = End +feed_id = Feed ID +ref_feed_id = Ref. Feed ID +spot_id = Spot ID +name = Name +status = Status +last_update = Last Update +ref_spot_id = Ref. Spot ID +model = Model date_time = $0 at $1 time_zone = Time zone diff --git a/languages/fr.lang b/languages/fr.lang index c0e0217..5f47a56 100644 --- a/languages/fr.lang +++ b/languages/fr.lang @@ -41,11 +41,20 @@ media_comment_update= Commentaire du media "$0" mis-à-jour city_time = heure de $0 +project_id = ID projet project = Projet mode = Mode code_name = Nom de code start = Départ end = Arrivée +feed_id = ID Feed +ref_feed_id = ID Feed ref. +spot_id = ID Spot +name = Description +status = Statut +last_update = Dernière maj +ref_spot_id = ID Spot ref. +model = Modèle date_time = $0 à $1 time_zone = Fuseau horaire diff --git a/masks/admin.html b/masks/admin.html index 040c0d4..1035341 100644 --- a/masks/admin.html +++ b/masks/admin.html @@ -1,26 +1,59 @@
+

Projects

- - - - - - - - + + + + + + + + + + + + +
[#]lang:project[#][#]lang:mode[#][#]lang:code_name[#][#]lang:start[#][#]lang:end[#][#]lang:time_zone[#]
[#]lang:project_id[#][#]lang:project[#][#]lang:mode[#][#]lang:code_name[#][#]lang:start[#][#]lang:end[#][#]lang:time_zone[#]
+
+
+

Feeds

+
+ + + + + + + + + + + + + +
[#]lang:feed_id[#][#]lang:ref_feed_id[#][#]lang:spot_id[#][#]lang:project_id[#][#]lang:name[#][#]lang:status[#][#]lang:last_update[#]
+
+

Spots

+
+ + + + + + + + + +
[#]lang:spot_id[#][#]lang:ref_spot_id[#][#]lang:name[#][#]lang:model[#]