From 760f38374f82d6be1c7c09e68cbc91c6b740339c Mon Sep 17 00:00:00 2001 From: Franzz Date: Sat, 19 Jul 2025 16:21:44 +0200 Subject: [PATCH] Add email trigger to manual positioning --- lib/Converter.php | 4 +-- lib/Spot.php | 70 +++++++++++++++++++++++++++-------------------- lib/index.php | 3 -- 3 files changed, 41 insertions(+), 36 deletions(-) diff --git a/lib/Converter.php b/lib/Converter.php index 00ef055..13ec19d 100644 --- a/lib/Converter.php +++ b/lib/Converter.php @@ -33,12 +33,10 @@ class Converter extends PhpObject { } public static function isGeoJsonValid($sCodeName) { - $bResult = false; $sGpxFilePath = Gpx::getFilePath($sCodeName); $sGeoJsonFilePath = GeoJson::getFilePath($sCodeName); //No need to generate if gpx is missing - if(!file_exists($sGpxFilePath) || file_exists($sGeoJsonFilePath) && filemtime($sGeoJsonFilePath) > filemtime(Gpx::getFilePath($sCodeName))) $bResult = true; - return $bResult; + return !file_exists($sGpxFilePath) || file_exists($sGeoJsonFilePath) && filemtime($sGeoJsonFilePath) >= filemtime($sGpxFilePath); } } \ No newline at end of file diff --git a/lib/Spot.php b/lib/Spot.php index d7845d5..6311d5b 100755 --- a/lib/Spot.php +++ b/lib/Spot.php @@ -223,34 +223,7 @@ class Spot extends Main //Send Update Email if($bNewMsg) { - $oEmail = new Email($this->asContext['serv_name'], 'email_update'); - $oEmail->setDestInfo($this->oUser->getActiveUsersInfo()); - - //Add Position - $asLastMessage = array_shift($this->getSpotMessages(array($this->oProject->getLastMessageId($this->getFeedConstraints(Feed::MSG_TABLE))))); - $oEmail->oTemplate->setTags($asLastMessage); - $oEmail->oTemplate->setTag('date_time', 'time:'.$asLastMessage['unix_time'], 'd/m/Y, H:i'); - - //Add latest news feed - $asNews = $this->getNextFeed(0, true); - $iPostCount = 0; - foreach($asNews as $asPost) { - if($asPost['type'] != 'message') { - $oEmail->oTemplate->newInstance('news'); - $oEmail->oTemplate->setInstanceTags('news', array( - 'local_server' => $this->asContext['serv_name'], - 'project' => $this->oProject->getProjectCodeName(), - 'type' => $asPost['type'], - 'id' => $asPost['id_'.$asPost['type']]) - ); - $oEmail->oTemplate->addInstance($asPost['type'], $asPost); - $oEmail->oTemplate->setInstanceTag($asPost['type'], 'local_server', $this->asContext['serv_name']); - $iPostCount++; - } - if($iPostCount == self::MAIL_CHUNK_SIZE) break; - } - - $bSuccess = $oEmail->send(); + $bSuccess = $this->sendEmail(); $sDesc = $bSuccess?'mail_sent':'mail_failure'; } else $sDesc = 'no_new_msg'; @@ -258,6 +231,37 @@ class Spot extends Main return self::getJsonResult($bSuccess, $sDesc); } + private function sendEmail() { + $oEmail = new Email($this->asContext['serv_name'], 'email_update'); + $oEmail->setDestInfo($this->oUser->getActiveUsersInfo()); + + //Add Position + $asLastMessage = array_shift($this->getSpotMessages(array($this->oProject->getLastMessageId($this->getFeedConstraints(Feed::MSG_TABLE))))); + $oEmail->oTemplate->setTags($asLastMessage); + $oEmail->oTemplate->setTag('date_time', 'time:'.$asLastMessage['unix_time'], 'd/m/Y, H:i'); + + //Add latest news feed + $asNews = $this->getNextFeed(0, true); + $iPostCount = 0; + foreach($asNews as $asPost) { + if($asPost['type'] != 'message') { + $oEmail->oTemplate->newInstance('news'); + $oEmail->oTemplate->setInstanceTags('news', array( + 'local_server' => $this->asContext['serv_name'], + 'project' => $this->oProject->getProjectCodeName(), + 'type' => $asPost['type'], + 'id' => $asPost['id_'.$asPost['type']]) + ); + $oEmail->oTemplate->addInstance($asPost['type'], $asPost); + $oEmail->oTemplate->setInstanceTag($asPost['type'], 'local_server', $this->asContext['serv_name']); + $iPostCount++; + } + if($iPostCount == self::MAIL_CHUNK_SIZE) break; + } + + return $oEmail->send(); + } + public function genCronFile() { //$bSuccess = (file_put_contents('spot_cron.sh', '#!/bin/bash'."\n".'cd '.dirname($_SERVER['SCRIPT_FILENAME'])."\n".'php -f index.php a=update_feed')!==false); $sFileName = 'spot_cron.sh'; @@ -634,9 +638,15 @@ class Spot extends Main public function addPosition($sLat, $sLng, $iTimestamp) { $oFeed = new Feed($this->oDb, $this->oProject->getFeedIds()[0]); - $bResult = ($oFeed->addManualPosition($sLat, $sLng, $iTimestamp) > 0); + $bSuccess = ($oFeed->addManualPosition($sLat, $sLng, $iTimestamp) > 0); - return self::getJsonResult($bResult, $bResult?'':$this->oDb->getLastError()); + if($bSuccess) { + $bSuccess = $this->sendEmail(); + $sDesc = $bSuccess?'mail_sent':'mail_failure'; + } + else $sDesc = 'error_commit_db'; + + return self::getJsonResult($bSuccess, $sDesc); } public function getAdminSettings($sType='') { diff --git a/lib/index.php b/lib/index.php index f24045a..df67ef2 100755 --- a/lib/index.php +++ b/lib/index.php @@ -80,9 +80,6 @@ if($sAction!='') case 'add_position': $sResult = $oSpot->addPosition($sLat, $sLng, $iTimestamp); break; - case 'admin_new': - $sResult = $oSpot->createProject(); - break; case 'admin_get': $sResult = $oSpot->getAdminSettings(); break;