diff options
Diffstat (limited to 'src/model/Position.php')
| -rw-r--r-- | src/model/Position.php | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/model/Position.php b/src/model/Position.php new file mode 100644 index 0000000..74d173a --- /dev/null +++ b/src/model/Position.php | |||
| @@ -0,0 +1,63 @@ | |||
| 1 | <?php | ||
| 2 | // src/modele/Position.php | ||
| 3 | // | ||
| 4 | // pour Node et Page | ||
| 5 | |||
| 6 | declare(strict_types=1); | ||
| 7 | |||
| 8 | trait Position | ||
| 9 | { | ||
| 10 | public function sortChildren(bool $reposition = false): void | ||
| 11 | { | ||
| 12 | // ordre du tableau des enfants | ||
| 13 | // inefficace quand des noeuds ont la même position | ||
| 14 | |||
| 15 | // tri par insertion avant affichage | ||
| 16 | for($i = 1; $i < count($this->children); $i++) | ||
| 17 | { | ||
| 18 | $tmp = $this->children[$i]; | ||
| 19 | $j = $i - 1; | ||
| 20 | |||
| 21 | // Déplacez les éléments du tableau qui sont plus grands que la clé | ||
| 22 | // à une position devant leur position actuelle | ||
| 23 | while ($j >= 0 && $this->children[$j]->getPosition() > $tmp->getPosition()) { | ||
| 24 | $this->children[$j + 1] = $this->children[$j]; | ||
| 25 | $j = $j - 1; | ||
| 26 | } | ||
| 27 | $this->children[$j + 1] = $tmp; | ||
| 28 | } | ||
| 29 | |||
| 30 | foreach ($this->children as $child) { | ||
| 31 | if (count($child->children) > 0) { | ||
| 32 | $child->sortChildren($reposition); | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | // nouvelles positions (tableau $children => BDD) | ||
| 37 | if($reposition){ | ||
| 38 | $i = 1; | ||
| 39 | foreach($this->children as $child){ | ||
| 40 | $child->setPosition($i); | ||
| 41 | $i++; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | /*private function sortChildren(): void | ||
| 47 | { | ||
| 48 | $iteration = count($this->children); | ||
| 49 | while($iteration > 1) | ||
| 50 | { | ||
| 51 | for($i = 0; $i < $iteration - 1; $i++) | ||
| 52 | { | ||
| 53 | if($this->children[$i]->getPosition() > $this->children[$i + 1]->getPosition()) | ||
| 54 | { | ||
| 55 | $tmp = $this->children[$i]; | ||
| 56 | $this->children[$i] = $this->children[$i + 1]; | ||
| 57 | $this->children[$i + 1] = $tmp; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | $iteration--; | ||
| 61 | } | ||
| 62 | }*/ | ||
| 63 | } \ No newline at end of file | ||
