diff options
| author | polo <ordipolo@gmx.fr> | 2025-04-06 12:18:49 +0200 |
|---|---|---|
| committer | polo <ordipolo@gmx.fr> | 2025-04-06 12:18:49 +0200 |
| commit | 68b6058e2a27fc251c117c4efeb141392a0c9736 (patch) | |
| tree | 5c029b2c147bd14f777765d41bc623582c81daa2 /src/model/entities/Node.php | |
| parent | e4a325c9d5c07f09bc18b7e366ffb82b82c43502 (diff) | |
| download | cms-68b6058e2a27fc251c117c4efeb141392a0c9736.tar.gz cms-68b6058e2a27fc251c117c4efeb141392a0c9736.tar.bz2 cms-68b6058e2a27fc251c117c4efeb141392a0c9736.zip | |
nouvel article, boutons dans les builders, makeArticleNode, JS MAJ page, tri quand déplacement ou suppression
Diffstat (limited to 'src/model/entities/Node.php')
| -rw-r--r-- | src/model/entities/Node.php | 71 |
1 files changed, 45 insertions, 26 deletions
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 | |||
| 65 | $this->article = $article; | 65 | $this->article = $article; |
| 66 | } | 66 | } |
| 67 | 67 | ||
| 68 | public function addChild(self $child): void | ||
| 69 | { | ||
| 70 | $this->children[] = $child; | ||
| 71 | $this->sortChildren(); | ||
| 72 | } | ||
| 73 | |||
| 74 | // utiliser $position pour afficher les éléments dans l'ordre | ||
| 75 | private function sortChildren(): void | ||
| 76 | { | ||
| 77 | $iteration = count($this->children); | ||
| 78 | while($iteration > 1) | ||
| 79 | { | ||
| 80 | for($i = 0; $i < $iteration - 1; $i++) | ||
| 81 | { | ||
| 82 | //echo '<br>' . $this->children[$i]->getPosition() . ' - ' . $this->children[$i + 1]->getPosition(); | ||
| 83 | if($this->children[$i]->getPosition() > $this->children[$i + 1]->getPosition()) | ||
| 84 | { | ||
| 85 | $tmp = $this->children[$i]; | ||
| 86 | $this->children[$i] = $this->children[$i + 1]; | ||
| 87 | $this->children[$i + 1] = $tmp; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | $iteration--; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | // pfff... | 68 | // pfff... |
| 95 | public function getId(): int | 69 | public function getId(): int |
| 96 | { | 70 | { |
| @@ -156,6 +130,51 @@ class Node | |||
| 156 | { | 130 | { |
| 157 | return $this->children; | 131 | return $this->children; |
| 158 | } | 132 | } |
| 133 | public function addChild(self $child): void | ||
| 134 | { | ||
| 135 | $this->children[] = $child; | ||
| 136 | $this->sortChildren(false); | ||
| 137 | } | ||
| 138 | // utiliser $position pour afficher les éléments dans l'ordre | ||
| 139 | public function sortChildren(bool $reposition = false): void | ||
| 140 | { | ||
| 141 | // ordre du tableau des enfants | ||
| 142 | // inefficace quand des noeuds ont la même position | ||
| 143 | |||
| 144 | // tri par insertion | ||
| 145 | for($i = 1; $i < count($this->children); $i++) | ||
| 146 | { | ||
| 147 | $tmp = $this->children[$i]; | ||
| 148 | $j = $i - 1; | ||
| 149 | |||
| 150 | // Déplacez les éléments du tableau qui sont plus grands que la clé | ||
| 151 | // à une position devant leur position actuelle | ||
| 152 | while ($j >= 0 && $this->children[$j]->getPosition() > $tmp->getPosition()) { | ||
| 153 | $this->children[$j + 1] = $this->children[$j]; | ||
| 154 | $j = $j - 1; | ||
| 155 | } | ||
| 156 | $this->children[$j + 1] = $tmp; | ||
| 157 | } | ||
| 158 | |||
| 159 | // nouvelles positions | ||
| 160 | if($reposition){ | ||
| 161 | $i = 1; | ||
| 162 | foreach($this->children as $child){ | ||
| 163 | $child->setPosition($i); | ||
| 164 | $i++; | ||
| 165 | } | ||
| 166 | } | ||
| 167 | } | ||
| 168 | public function removeChild(self $child): void | ||
| 169 | { | ||
| 170 | foreach($this->children as $key => $object){ | ||
| 171 | if($object->getId() === $child->getId()){ | ||
| 172 | unset($this->children[$key]); | ||
| 173 | } | ||
| 174 | break; | ||
| 175 | } | ||
| 176 | $this->children = array_values($this->children); // réindexer pour supprimer la case vide | ||
| 177 | } | ||
| 159 | 178 | ||
| 160 | public function getTempChild(): ?self // peut renvoyer null | 179 | public function getTempChild(): ?self // peut renvoyer null |
| 161 | { | 180 | { |
