From 68b6058e2a27fc251c117c4efeb141392a0c9736 Mon Sep 17 00:00:00 2001 From: polo Date: Sun, 6 Apr 2025 12:18:49 +0200 Subject: =?UTF-8?q?nouvel=20article,=20boutons=20dans=20les=20builders,=20?= =?UTF-8?q?makeArticleNode,=20JS=20MAJ=20page,=20tri=20quand=20d=C3=A9plac?= =?UTF-8?q?ement=20ou=20suppression?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/model/entities/Article.php | 8 +++-- src/model/entities/Node.php | 71 ++++++++++++++++++++++++++---------------- 2 files changed, 51 insertions(+), 28 deletions(-) (limited to 'src/model') diff --git a/src/model/entities/Article.php b/src/model/entities/Article.php index 601e573..dc2d78b 100644 --- a/src/model/entities/Article.php +++ b/src/model/entities/Article.php @@ -20,7 +20,7 @@ class Article // datetime_immutable permet à la base de toujours gérer cette clé primaire correctement #[ORM\Column(type: 'datetime', options: ['default' => 'CURRENT_TIMESTAMP'], unique: true)] - private \DateTime $date_time; // le type datetime de doctrine convertit en type \DateTime de PHP + private ?\DateTime $date_time; // le type datetime de doctrine convertit en type \DateTime de PHP #[ORM\Column(type: "string")] private string $title; @@ -40,8 +40,12 @@ class Article )] private Collection $images; - public function __construct() + public function __construct(string $content, \DateTime $date_time = null, string $title = '', string $preview = '') { + $this->date_time = $date_time; + $this->title = $title; + $this->preview = $preview; + $this->content = $content; $this->images = new ArrayCollection(); // initialisation nécessaire } diff --git a/src/model/entities/Node.php b/src/model/entities/Node.php index 9240413..c9b310a 100644 --- a/src/model/entities/Node.php +++ b/src/model/entities/Node.php @@ -65,32 +65,6 @@ class Node $this->article = $article; } - public function addChild(self $child): void - { - $this->children[] = $child; - $this->sortChildren(); - } - - // utiliser $position pour afficher les éléments dans l'ordre - private function sortChildren(): void - { - $iteration = count($this->children); - while($iteration > 1) - { - for($i = 0; $i < $iteration - 1; $i++) - { - //echo '
' . $this->children[$i]->getPosition() . ' - ' . $this->children[$i + 1]->getPosition(); - 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; - } - } - $iteration--; - } - } - // pfff... public function getId(): int { @@ -156,6 +130,51 @@ class Node { return $this->children; } + public function addChild(self $child): void + { + $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){ + if($object->getId() === $child->getId()){ + unset($this->children[$key]); + } + break; + } + $this->children = array_values($this->children); // réindexer pour supprimer la case vide + } public function getTempChild(): ?self // peut renvoyer null { -- cgit v1.2.3