From 0beb2a65406bbec2613683ff78a49526c0ef80e7 Mon Sep 17 00:00:00 2001 From: Tiavina Date: Wed, 30 Apr 2025 16:45:39 +0300 Subject: [PATCH 1/2] move calendar, manage ligne trajet --- gestion/lib/AppInfo/Application.php | 3 + gestion/lib/Db/Bdd.php | 51 ++++++++++++++ .../Listener/CalendarObjectMovedListener.php | 69 +++++++++++++++++++ gestion/lib/Service/GestionService.php | 38 ++++++++++ 4 files changed, 161 insertions(+) create mode 100644 gestion/lib/Listener/CalendarObjectMovedListener.php diff --git a/gestion/lib/AppInfo/Application.php b/gestion/lib/AppInfo/Application.php index 6496c86..39551de 100644 --- a/gestion/lib/AppInfo/Application.php +++ b/gestion/lib/AppInfo/Application.php @@ -5,9 +5,11 @@ namespace OCA\Gestion\AppInfo; use OCA\DAV\Events\CalendarObjectCreatedEvent; +use OCA\DAV\Events\CalendarObjectMovedEvent; use OCA\DAV\Events\CalendarObjectMovedToTrashEvent; use OCA\DAV\Events\CalendarObjectUpdatedEvent; use OCA\Gestion\Listener\CalendarObjectCreatedListener; +use OCA\Gestion\Listener\CalendarObjectMovedListener; use OCA\Gestion\Listener\CalendarObjectMovedToTrashListener; use OCA\Gestion\Listener\CalendarObjectUpdatedListener; use OCP\AppFramework\App; @@ -27,6 +29,7 @@ class Application extends App implements IBootstrap { $context->registerEventListener(CalendarObjectCreatedEvent::class, CalendarObjectCreatedListener::class); $context->registerEventListener(CalendarObjectMovedToTrashEvent::class, CalendarObjectMovedToTrashListener::class); $context->registerEventListener(CalendarObjectUpdatedEvent::class, CalendarObjectUpdatedListener::class); + $context->registerEventListener(CalendarObjectMovedEvent::class,CalendarObjectMovedListener::class); } public function boot(IBootContext $context): void { diff --git a/gestion/lib/Db/Bdd.php b/gestion/lib/Db/Bdd.php index afab43e..8329441 100644 --- a/gestion/lib/Db/Bdd.php +++ b/gestion/lib/Db/Bdd.php @@ -3694,6 +3694,57 @@ class Bdd { $sql = "SELECT * FROM {$this->tableprefix}devis WHERE MONTH(date) = ? AND YEAR(date) = ? AND id_thanato = ?;"; return $this->execSQLNoJsonReturn($sql,array( $mois , $annee, $thanato['id'] )); } + + public function reArrangeLigneTrajetyByDevisId($devisId,$idNextcloud){ + $ligne_trajet = json_decode($this->getOneTrajetdetails_byIdDevis($devisId, $idNextcloud))[0]; + if($ligne_trajet != NULL) { + $this->gestion_delete('ligne_trajet', $ligne_trajet->id, $idNextcloud); + $ligne_trajet = NULL; + } + $devisObject = json_decode($this->getOneDevis($devisId, $idNextcloud))[0]; + + $this->generate_ligneTrajet($devisObject, $idNextcloud); + $ligne_trajet = json_decode( json: $this->getOneTrajetdetails_byIdDevis($devisObject->devisid, $idNextcloud))[0]; + $this->range_ligneTrajet($ligne_trajet->id_trajet, $idNextcloud); + $this->calculer_distance_trajet(numtrajet: $ligne_trajet->id_trajet, idNextcloud: $idNextcloud); + } + + public function getCalendarPrincipalNameByCalendarId($calendarId){ + $calendar = $this->getCalendarById($calendarId); + if($calendar != null){ + $principalUri = $calendar["principaluri"]; + $organizerName = str_replace('principals/users/','',$principalUri); + $organizerName = trim($organizerName); + return $organizerName; + } + return null; + } + + public function updateDevisThanato($devisId,$requestedThanatoId){ + $sql= "UPDATE ".$this->tableprefix."devis as devis + SET devis.id_thanato = ? + WHERE devis.id = ?"; + $this->execSQLNoData($sql,[$requestedThanatoId,$devisId]); + } + + public function deleteLigneTrajetByDevisId($devisId){ + $sql = "DELETE FROM ".$this->tableprefix."ligne_trajet WHERE id_devis = ?;"; + $this->execSQLNoData($sql, array($devisId)); + } + + public function thereIsThanatoDevisRattachedToLigneTrajetForADate($date,$idNextcloud){ + $sql = "SELECT * FROM ".$this->tableprefix."ligne_trajet WHERE date = ? AND id_nextcloud = ? AND id_devis != 0 AND id_devis != NULL AND id_devis != ?;"; + $ligneTrajet = $this->execSQLNoJsonReturn($sql, array($date,$idNextcloud,"-")); + if(!empty($ligneTrajet)){ + return true; + } + return false; + } + + public function deleteThanatoLigneTrajetForADate($date,$thanatoUserUuid){ + $sql = "DELETE FROM ".$this->tableprefix."ligne_trajet WHERE date = ? AND id_nextcloud = ?;"; + $this->execSQLNoData($sql, array($date,$thanatoUserUuid)); + } } diff --git a/gestion/lib/Listener/CalendarObjectMovedListener.php b/gestion/lib/Listener/CalendarObjectMovedListener.php new file mode 100644 index 0000000..467459e --- /dev/null +++ b/gestion/lib/Listener/CalendarObjectMovedListener.php @@ -0,0 +1,69 @@ + + * + * @author 2022 Christoph Wurst + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace OCA\Gestion\Listener; + +use Exception; +use OCA\DAV\Events\CalendarObjectMovedEvent; +use OCA\Gestion\Service\GestionService; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use Psr\Log\LoggerInterface; + +class CalendarObjectMovedListener implements IEventListener +{ + + /** @var LoggerInterface */ + private $logger; + + /** @var GestionService */ + private $gestionService; + + public function __construct( + LoggerInterface $logger, + GestionService $gestionService + ) { + $this->logger = $logger; + $this->gestionService = $gestionService; + } + + public function handle(Event $event): void + { + if (!($event instanceof CalendarObjectMovedEvent)) { + return; + } + $calendarData = $event->getObjectData(); + try { + $targetCalendarId = $event->getTargetCalendarId(); + $vCalendarString = $calendarData["calendardata"]; + $this->gestionService->HandleCalendarObjectMoved($vCalendarString, $targetCalendarId); + } catch (\OC\OCS\Exception $e) { + $this->logger->debug("Error while handling calendar object moved event: " . $e->getMessage()); + } catch (\Throwable $e) { + $this->logger->debug("Error while handling calendar object moved event: " . $e->getMessage()); + } + } + +} diff --git a/gestion/lib/Service/GestionService.php b/gestion/lib/Service/GestionService.php index 130c971..05e76b8 100644 --- a/gestion/lib/Service/GestionService.php +++ b/gestion/lib/Service/GestionService.php @@ -501,6 +501,7 @@ class GestionService { } } } + $this->gestionBdd->reArrangeLigneTrajetyByDevisId($devis['id'],$userName); } else{ $this->HandleCreatedCalendarObject($vCalendarString,$cookie); @@ -617,4 +618,41 @@ class GestionService { $this->logger->debug("error sending file to talk"); } } + + public function HandleCalendarObjectMoved(string $vCalendarString, $targetCalendarId) + { + try { + $calendarUuid = $this->GetCalendarUuidFromVCalendarString($vCalendarString); + $absenceType = VCalendarHelpers::GetValueFromKeyInVCalendarString('ABSENCETYPE',$vCalendarString); + if($absenceType){ + return; + } + $devis = $this->gestionBdd->getDevisByCalendarUuid($calendarUuid); + if ($devis != null) { + $userPrincipalName = $this->gestionBdd->getCalendarPrincipalNameByCalendarId($targetCalendarId); + if ($userPrincipalName != null) { + $thanatoId = $this->gestionBdd->getThanatoIdByUserUuid($userPrincipalName); + if ($thanatoId != null) { + $thanatoHasBeenChanged = $thanatoId != $devis["id_thanato"]; + if ($thanatoHasBeenChanged) { + //delete ligne trajet from old devis + $this->gestionBdd->deleteLigneTrajetByDevisId($devis['id']); + //delete from home and to home trajet if there is no devis at all for this date + $thereIsThanatoDevisRattachedToLigneTrajetForADate = $this->gestionBdd->thereIsThanatoDevisRattachedToLigneTrajetForADate($devis['date'],$userPrincipalName); + if(!$thereIsThanatoDevisRattachedToLigneTrajetForADate){ + //delete all ligne trajet for this date + $this->gestionBdd->deleteThanatoLigneTrajetForADate($devis['date'],$userPrincipalName); + } + $this->gestionBdd->updateDevisThanato($devis['id'], $thanatoId); + $this->gestionBdd->reArrangeLigneTrajetyByDevisId($devis['id'], $userPrincipalName); + } + } + } + } + } catch (\OC\OCS\Exception $e) { + $this->logger->debug("Error while handling calendar object moved event: " . $e->getMessage()); + } catch (\Throwable $e) { + $this->logger->debug("Error while handling calendar object moved event: " . $e->getMessage()); + } + } } From 0f67a210eaf70c1087cf59a0b7491045605eb178 Mon Sep 17 00:00:00 2001 From: narindraezway Date: Wed, 30 Apr 2025 17:25:47 +0300 Subject: [PATCH 2/2] debug-delete-old-trajet-if-change-thanato --- gestion/lib/Service/GestionService.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/gestion/lib/Service/GestionService.php b/gestion/lib/Service/GestionService.php index 05e76b8..0e20d79 100644 --- a/gestion/lib/Service/GestionService.php +++ b/gestion/lib/Service/GestionService.php @@ -631,19 +631,21 @@ class GestionService { if ($devis != null) { $userPrincipalName = $this->gestionBdd->getCalendarPrincipalNameByCalendarId($targetCalendarId); if ($userPrincipalName != null) { - $thanatoId = $this->gestionBdd->getThanatoIdByUserUuid($userPrincipalName); - if ($thanatoId != null) { - $thanatoHasBeenChanged = $thanatoId != $devis["id_thanato"]; + $thanato = $this->gestionBdd->getThanatoByUserUuid($userPrincipalName); + if ($thanato != null) { + $thanatoHasBeenChanged = $thanato['id'] != $devis["id_thanato"]; if ($thanatoHasBeenChanged) { + $oldThanato = $this->gestionBdd->getThanatoByThanatoId($devis['id_thanato']); + $oldThanatoUserUuid = $oldThanato['fk_user_uuid']; //delete ligne trajet from old devis $this->gestionBdd->deleteLigneTrajetByDevisId($devis['id']); //delete from home and to home trajet if there is no devis at all for this date - $thereIsThanatoDevisRattachedToLigneTrajetForADate = $this->gestionBdd->thereIsThanatoDevisRattachedToLigneTrajetForADate($devis['date'],$userPrincipalName); + $thereIsThanatoDevisRattachedToLigneTrajetForADate = $this->gestionBdd->thereIsThanatoDevisRattachedToLigneTrajetForADate($devis['date'], $oldThanatoUserUuid ); if(!$thereIsThanatoDevisRattachedToLigneTrajetForADate){ //delete all ligne trajet for this date - $this->gestionBdd->deleteThanatoLigneTrajetForADate($devis['date'],$userPrincipalName); + $this->gestionBdd->deleteThanatoLigneTrajetForADate($devis['date'], $oldThanatoUserUuid ); } - $this->gestionBdd->updateDevisThanato($devis['id'], $thanatoId); + $this->gestionBdd->updateDevisThanato($devis['id'], $thanato['id']); $this->gestionBdd->reArrangeLigneTrajetyByDevisId($devis['id'], $userPrincipalName); } }