From 06c74a049bf9f2cc2868a6aa5dbe2e4a35bf26bd Mon Sep 17 00:00:00 2001 From: Tiavina Date: Wed, 1 Jan 2025 20:38:06 +0300 Subject: [PATCH] on defunt list : show only the defunts related to the thanato if the user connected is thanato only --- gestion/lib/Controller/PageController.php | 15 ++-- gestion/lib/Db/Bdd.php | 83 +++++++++++++++++++++-- 2 files changed, 87 insertions(+), 11 deletions(-) diff --git a/gestion/lib/Controller/PageController.php b/gestion/lib/Controller/PageController.php index ce2495e..1551ef8 100644 --- a/gestion/lib/Controller/PageController.php +++ b/gestion/lib/Controller/PageController.php @@ -2004,9 +2004,13 @@ class PageController extends Controller { * @NoCSRFRequired */ public function getStats(){ + + $isThanato = in_array('Thanatos',$this->groups); + $isAdmin = in_array('admin',$this->groups); + $isUserThanatoOnly = $isThanato && !$isAdmin; $res = array(); $res['client'] = json_decode($this->myDb->numberClient($this->idNextcloud))[0]->c; - $res['defunt'] = json_decode($this->myDb->numberDefunt($this->idNextcloud))[0]->c; + $res['defunt'] = $this->myDb->numberDefunt($this->idNextcloud,$isUserThanatoOnly); $res['thanato'] = json_decode($this->myDb->numberThanato($this->idNextcloud))[0]->c; $res['devis'] = json_decode($this->myDb->numberDevis($this->idNextcloud))[0]->c; $res['lieu'] = json_decode($this->myDb->numberLieu($this->idNextcloud))[0]->c; @@ -2060,9 +2064,12 @@ class PageController extends Controller { * @NoAdminRequired * @NoCSRFRequired */ - public function getDefunts() { - return $this->myDb->getDefunts($this->idNextcloud); - } + public function getDefunts() { + $isThanato = in_array('Thanatos',$this->groups); + $isAdmin = in_array('admin',$this->groups); + $isUserThanatoOnly = $isThanato && !$isAdmin; + return $this->myDb->getDefunts($this->idNextcloud,$isUserThanatoOnly); + } /** * @NoAdminRequired diff --git a/gestion/lib/Db/Bdd.php b/gestion/lib/Db/Bdd.php index 759efc0..8353434 100644 --- a/gestion/lib/Db/Bdd.php +++ b/gestion/lib/Db/Bdd.php @@ -404,7 +404,47 @@ class Bdd { return $this->execSQL($sql, array()); } - public function getDefunts($idNextcloud) { + private function getThanatoByIdNextcloud($idNextcloud){ + $sql = "SELECT * + FROM ".$this->tableprefix."thanato as thanato + WHERE LOWER(thanato.nom) = LOWER(?) + ORDER BY thanato.id ASC;"; + + $thanato = $this->execSQLNoJsonReturn($sql, array($idNextcloud)); + if(!empty($thanato)){ + return $thanato[0]; + } + return null; + } + + private function getDefuntIdsRelatedToThanato($thanatoId){ + $devis = $this->getDevisMadeByAThanato($thanatoId); + $defuntIds = $this->getDefuntIdsFromDevisList($devis); + return $defuntIds; + } + + private function getDefuntIdsFromDevisList($devisList){ + $defuntIds = []; + foreach($devisList as $devis){ + $defuntIds[] = $devis["id_defunt"]; + } + return $defuntIds; + } + + private function getDevisMadeByAThanato($thanatoId){ + $sql = "SELECT + devis.id, + devis.id_thanato, + devis.id_defunt + FROM ".$this->tableprefix."devis as devis + WHERE devis.id_thanato = ? ;"; + + $devisList = $this->execSQLNoJsonReturn($sql, array($thanatoId)); + return $devisList; + } + + public function getDefunts($idNextcloud, $isUserThanatoOnly = false) + { $sql = "SELECT ".$this->tableprefix."defunt.id, ".$this->tableprefix."defunt.nom, ".$this->tableprefix."defunt.date_naissance, ".$this->tableprefix."defunt.ref_pacemaker, ".$this->tableprefix."defunt.sexe, " .$this->tableprefix."client.nom as nom_client, ".$this->tableprefix."client.id as id_client, " .$this->tableprefix."devis.num as numero_devis, ".$this->tableprefix."devis.id as id_devis, ".$this->tableprefix."devis.user_id as user_id, " @@ -412,9 +452,23 @@ class Bdd { FROM ".$this->tableprefix."defunt LEFT JOIN ".$this->tableprefix."devis ON ".$this->tableprefix."devis.id_defunt = ".$this->tableprefix."defunt.id LEFT JOIN ".$this->tableprefix."client ON ".$this->tableprefix."devis.id_client = ".$this->tableprefix."client.id - LEFT JOIN ".$this->tableprefix."lieu ON ".$this->tableprefix."devis.id_lieu = ".$this->tableprefix."lieu.id - ORDER BY ".$this->tableprefix."defunt.id DESC;"; - return $this->execSQL($sql, array()); + LEFT JOIN ".$this->tableprefix."lieu ON ".$this->tableprefix."devis.id_lieu = ".$this->tableprefix."lieu.id"; + + $conditions = []; + if($isUserThanatoOnly){ + $thanato = $this->getThanatoByIdNextcloud($idNextcloud); + if($thanato != null){ + $thanatoId = $thanato["id"]; + $defuntsIdRelatedToThanato = $this->getDefuntIdsRelatedToThanato($thanatoId); + if(!empty($defuntsIdRelatedToThanato)){ + $defuntListConditionPlaceholder = implode(',', array_fill(0, count($defuntsIdRelatedToThanato), '?')); + $conditions = $defuntsIdRelatedToThanato; + $sql .= " WHERE ".$this->tableprefix."defunt.id IN ($defuntListConditionPlaceholder)"; + } + } + } + $sql .= " ORDER BY ".$this->tableprefix."defunt.id DESC;"; + return $this->execSQL($sql, $conditions); } public function getUnusedDefunts($idNextcloud) { @@ -1658,9 +1712,24 @@ class Bdd { /** * Number défunt */ - public function numberDefunt($idNextcloud){ - $sql = "SELECT count(*) as c from ".$this->tableprefix."defunt;"; - return $this->execSQL($sql, array()); + public function numberDefunt($idNextcloud, $isUserThanatoOnly = false){ + $defuntCount = 0; + if($isUserThanatoOnly == false){ + $sql = "SELECT count(*) as c from ".$this->tableprefix."defunt;"; + $defuntCountBythanato = $this->execSQLNoJsonReturn($sql, array()); + if(!empty($defuntCountBythanato)){ + $defuntCount = $defuntCountBythanato[0]['c']; + } + } + else{ + $thanato = $this->getThanatoByIdNextcloud($idNextcloud); + if($thanato != null){ + $thanatoId = $thanato["id"]; + $defuntsIdRelatedToThanato = $this->getDefuntIdsRelatedToThanato($thanatoId); + $defuntCount = count($defuntsIdRelatedToThanato); + } + } + return $defuntCount; } /**