diff options
Diffstat (limited to 'src/model')
| -rw-r--r-- | src/model/Position.php | 63 | ||||
| -rw-r--r-- | src/model/entities/Node.php | 33 | ||||
| -rw-r--r-- | src/model/entities/Page.php | 38 |
3 files changed, 87 insertions, 47 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 | ||
diff --git a/src/model/entities/Node.php b/src/model/entities/Node.php index a52a7e6..103163b 100644 --- a/src/model/entities/Node.php +++ b/src/model/entities/Node.php | |||
| @@ -12,6 +12,8 @@ use Doctrine\ORM\Mapping as ORM; | |||
| 12 | #[ORM\Table(name: TABLE_PREFIX . "node")] | 12 | #[ORM\Table(name: TABLE_PREFIX . "node")] |
| 13 | class Node | 13 | class Node |
| 14 | { | 14 | { |
| 15 | use \Position; | ||
| 16 | |||
| 15 | #[ORM\Id] | 17 | #[ORM\Id] |
| 16 | #[ORM\GeneratedValue] | 18 | #[ORM\GeneratedValue] |
| 17 | #[ORM\Column(type: "integer")] | 19 | #[ORM\Column(type: "integer")] |
| @@ -135,36 +137,7 @@ class Node | |||
| 135 | $this->children[] = $child; | 137 | $this->children[] = $child; |
| 136 | $this->sortChildren(false); | 138 | $this->sortChildren(false); |
| 137 | } | 139 | } |
| 138 | // utiliser $position pour afficher les éléments dans l'ordre | 140 | |
| 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 | 141 | public function removeChild(self $child): void |
| 169 | { | 142 | { |
| 170 | foreach($this->children as $key => $object){ | 143 | foreach($this->children as $key => $object){ |
diff --git a/src/model/entities/Page.php b/src/model/entities/Page.php index c40a297..c30305c 100644 --- a/src/model/entities/Page.php +++ b/src/model/entities/Page.php | |||
| @@ -13,6 +13,8 @@ use Doctrine\Common\Collections\ArrayCollection; | |||
| 13 | #[ORM\Table(name: TABLE_PREFIX . "page")] | 13 | #[ORM\Table(name: TABLE_PREFIX . "page")] |
| 14 | class Page | 14 | class Page |
| 15 | { | 15 | { |
| 16 | use \Position; | ||
| 17 | |||
| 16 | #[ORM\Id] | 18 | #[ORM\Id] |
| 17 | #[ORM\GeneratedValue] | 19 | #[ORM\GeneratedValue] |
| 18 | #[ORM\Column(type: "integer")] | 20 | #[ORM\Column(type: "integer")] |
| @@ -56,11 +58,11 @@ class Page | |||
| 56 | $this->children = new ArrayCollection(); | 58 | $this->children = new ArrayCollection(); |
| 57 | } | 59 | } |
| 58 | 60 | ||
| 59 | // getters | 61 | // getters/setters |
| 60 | /*public function getId(): int | 62 | public function getId(): int |
| 61 | { | 63 | { |
| 62 | return $this->id_page; | 64 | return $this->id_page; |
| 63 | }*/ | 65 | } |
| 64 | public function getPageName(): string | 66 | public function getPageName(): string |
| 65 | { | 67 | { |
| 66 | return $this->name_page; | 68 | return $this->name_page; |
| @@ -85,6 +87,10 @@ class Page | |||
| 85 | { | 87 | { |
| 86 | return $this->position; | 88 | return $this->position; |
| 87 | } | 89 | } |
| 90 | public function setPosition(int $position): void | ||
| 91 | { | ||
| 92 | $this->position = $position; | ||
| 93 | } | ||
| 88 | public function getParent(): ?Page | 94 | public function getParent(): ?Page |
| 89 | { | 95 | { |
| 90 | return $this->parent; | 96 | return $this->parent; |
| @@ -105,25 +111,23 @@ class Page | |||
| 105 | public function addChild(self $child): void | 111 | public function addChild(self $child): void |
| 106 | { | 112 | { |
| 107 | $this->children[] = $child; | 113 | $this->children[] = $child; |
| 108 | $this->sortChildren(); | 114 | $this->sortChildren(false); |
| 109 | } | 115 | } |
| 110 | 116 | ||
| 111 | // utiliser $position pour afficher les éléments dans l'ordre | 117 | public function findPageById(int $id): ?Page |
| 112 | private function sortChildren(): void | ||
| 113 | { | 118 | { |
| 114 | $iteration = count($this->children); | 119 | $target = null; |
| 115 | while($iteration > 1) | 120 | foreach($this->children as $page){ |
| 116 | { | 121 | if($page->getId() === $id){ |
| 117 | for($i = 0; $i < $iteration - 1; $i++) | 122 | return $page; |
| 118 | { | 123 | } |
| 119 | if($this->children[$i]->getPosition() > $this->children[$i + 1]->getPosition()) | 124 | if(count($page->getChildren()) > 0){ |
| 120 | { | 125 | $target = $page->findPageById($id); |
| 121 | $tmp = $this->children[$i]; | 126 | if($target !== null){ |
| 122 | $this->children[$i] = $this->children[$i + 1]; | 127 | return $target; |
| 123 | $this->children[$i + 1] = $tmp; | ||
| 124 | } | 128 | } |
| 125 | } | 129 | } |
| 126 | $iteration--; | ||
| 127 | } | 130 | } |
| 131 | return $target; | ||
| 128 | } | 132 | } |
| 129 | } | 133 | } |
