summaryrefslogtreecommitdiff
path: root/src/model/Position.php
blob: b5040dfd6704f3723f4c24ac1fefd00e130dd74c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
// src/modele/Position.php
//
// pour Node et Page

declare(strict_types=1);

trait Position
{
    // tri par insertion du tableau des enfants
	public function sortChildren(bool $reindexation = false): void
    {
        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--;
            }
            $this->children[$j + 1] = $tmp;
        }
        
        foreach($this->children as $child) {
            if(count($child->children) > 0) {
                $child->sortChildren($reindexation);
            }
        }

        if($reindexation){
            $this->reindex();
        }
    }

    // nouvelles positions (tableau $children => BDD)
    public function reindex(): void
    {
        $i = 1;
        foreach($this->children as $child){
            $child->setPosition($i);
            $i++;
        }
    }
}