From eb3e1eb8c8365d3b3d1d39f24314ba420255afc2 Mon Sep 17 00:00:00 2001 From: polo Date: Mon, 21 Apr 2025 20:36:10 +0200 Subject: page menu et chemin, partie1 --- src/model/entities/Node.php | 33 +++------------------------------ src/model/entities/Page.php | 38 +++++++++++++++++++++----------------- 2 files changed, 24 insertions(+), 47 deletions(-) (limited to 'src/model/entities') diff --git a/src/model/entities/Node.php b/src/model/entities/Node.php index a52a7e6..103163b 100644 --- a/src/model/entities/Node.php +++ b/src/model/entities/Node.php @@ -12,6 +12,8 @@ use Doctrine\ORM\Mapping as ORM; #[ORM\Table(name: TABLE_PREFIX . "node")] class Node { + use \Position; + #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(type: "integer")] @@ -135,36 +137,7 @@ class Node $this->children[] = $child; $this->sortChildren(false); } - // utiliser $position pour afficher les éléments dans l'ordre - public function sortChildren(bool $reposition = false): void - { - // ordre du tableau des enfants - // inefficace quand des noeuds ont la même position - - // tri par insertion - for($i = 1; $i < count($this->children); $i++) - { - $tmp = $this->children[$i]; - $j = $i - 1; - - // Déplacez les éléments du tableau qui sont plus grands que la clé - // à une position devant leur position actuelle - while ($j >= 0 && $this->children[$j]->getPosition() > $tmp->getPosition()) { - $this->children[$j + 1] = $this->children[$j]; - $j = $j - 1; - } - $this->children[$j + 1] = $tmp; - } - - // nouvelles positions - if($reposition){ - $i = 1; - foreach($this->children as $child){ - $child->setPosition($i); - $i++; - } - } - } + public function removeChild(self $child): void { foreach($this->children as $key => $object){ diff --git a/src/model/entities/Page.php b/src/model/entities/Page.php index c40a297..c30305c 100644 --- a/src/model/entities/Page.php +++ b/src/model/entities/Page.php @@ -13,6 +13,8 @@ use Doctrine\Common\Collections\ArrayCollection; #[ORM\Table(name: TABLE_PREFIX . "page")] class Page { + use \Position; + #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(type: "integer")] @@ -56,11 +58,11 @@ class Page $this->children = new ArrayCollection(); } - // getters - /*public function getId(): int + // getters/setters + public function getId(): int { return $this->id_page; - }*/ + } public function getPageName(): string { return $this->name_page; @@ -85,6 +87,10 @@ class Page { return $this->position; } + public function setPosition(int $position): void + { + $this->position = $position; + } public function getParent(): ?Page { return $this->parent; @@ -105,25 +111,23 @@ class Page public function addChild(self $child): void { $this->children[] = $child; - $this->sortChildren(); + $this->sortChildren(false); } - // utiliser $position pour afficher les éléments dans l'ordre - private function sortChildren(): void + public function findPageById(int $id): ?Page { - $iteration = count($this->children); - while($iteration > 1) - { - for($i = 0; $i < $iteration - 1; $i++) - { - if($this->children[$i]->getPosition() > $this->children[$i + 1]->getPosition()) - { - $tmp = $this->children[$i]; - $this->children[$i] = $this->children[$i + 1]; - $this->children[$i + 1] = $tmp; + $target = null; + foreach($this->children as $page){ + if($page->getId() === $id){ + return $page; + } + if(count($page->getChildren()) > 0){ + $target = $page->findPageById($id); + if($target !== null){ + return $target; } } - $iteration--; } + return $target; } } -- cgit v1.2.3