summaryrefslogtreecommitdiff
path: root/src/model/entities/Node.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/model/entities/Node.php')
-rw-r--r--src/model/entities/Node.php71
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 {