diff options
Diffstat (limited to 'src/model/entities/Node.php')
-rw-r--r-- | src/model/entities/Node.php | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/src/model/entities/Node.php b/src/model/entities/Node.php index eb73116..c5df8d1 100644 --- a/src/model/entities/Node.php +++ b/src/model/entities/Node.php | |||
@@ -171,7 +171,31 @@ class Node | |||
171 | public function addChild(self $child): void | 171 | public function addChild(self $child): void |
172 | { | 172 | { |
173 | $this->children[] = $child; | 173 | $this->children[] = $child; |
174 | $this->sortChildren(false); | 174 | |
175 | // cas particulier des news: utilise les dates au lieu des positions (les positions existent mais sont ignorées) | ||
176 | if($this->getName() === 'news_block'){ | ||
177 | $this->sortNews($this->getNodeData()->getData()['chrono'] ?? false); // faux = ordre chronologique | ||
178 | } | ||
179 | else{ | ||
180 | $this->sortChildren(false); | ||
181 | } | ||
182 | } | ||
183 | |||
184 | private function sortNews(bool $chrono = false) // affichage du plus récent au plus ancien par défaut | ||
185 | { | ||
186 | // tri par insertion similaire à Position::sortChildren | ||
187 | for($i = 1; $i < count($this->children); $i++){ | ||
188 | $tmp = $this->children[$i]; | ||
189 | $j = $i - 1; | ||
190 | |||
191 | $compare = $chrono ? fn($a, $b) => $a > $b : fn($a, $b) => $a < $b; | ||
192 | |||
193 | while($j >= 0 && $compare($this->children[$j]->getArticle()->getDateTime(), $tmp->getArticle()->getDateTime())){ | ||
194 | $this->children[$j + 1] = $this->children[$j]; | ||
195 | $j--; | ||
196 | } | ||
197 | $this->children[$j + 1] = $tmp; | ||
198 | } | ||
175 | } | 199 | } |
176 | 200 | ||
177 | public function removeChild(self $child): void | 201 | public function removeChild(self $child): void |