diff options
| author | polo <ordipolo@gmx.fr> | 2025-04-06 12:18:49 +0200 |
|---|---|---|
| committer | polo <ordipolo@gmx.fr> | 2025-04-06 12:18:49 +0200 |
| commit | 68b6058e2a27fc251c117c4efeb141392a0c9736 (patch) | |
| tree | 5c029b2c147bd14f777765d41bc623582c81daa2 /src/model/entities | |
| parent | e4a325c9d5c07f09bc18b7e366ffb82b82c43502 (diff) | |
| download | cms-68b6058e2a27fc251c117c4efeb141392a0c9736.tar.gz cms-68b6058e2a27fc251c117c4efeb141392a0c9736.tar.bz2 cms-68b6058e2a27fc251c117c4efeb141392a0c9736.zip | |
nouvel article, boutons dans les builders, makeArticleNode, JS MAJ page, tri quand déplacement ou suppression
Diffstat (limited to 'src/model/entities')
| -rw-r--r-- | src/model/entities/Article.php | 8 | ||||
| -rw-r--r-- | src/model/entities/Node.php | 71 |
2 files changed, 51 insertions, 28 deletions
diff --git a/src/model/entities/Article.php b/src/model/entities/Article.php index 601e573..dc2d78b 100644 --- a/src/model/entities/Article.php +++ b/src/model/entities/Article.php | |||
| @@ -20,7 +20,7 @@ class Article | |||
| 20 | 20 | ||
| 21 | // datetime_immutable permet à la base de toujours gérer cette clé primaire correctement | 21 | // datetime_immutable permet à la base de toujours gérer cette clé primaire correctement |
| 22 | #[ORM\Column(type: 'datetime', options: ['default' => 'CURRENT_TIMESTAMP'], unique: true)] | 22 | #[ORM\Column(type: 'datetime', options: ['default' => 'CURRENT_TIMESTAMP'], unique: true)] |
| 23 | private \DateTime $date_time; // le type datetime de doctrine convertit en type \DateTime de PHP | 23 | private ?\DateTime $date_time; // le type datetime de doctrine convertit en type \DateTime de PHP |
| 24 | 24 | ||
| 25 | #[ORM\Column(type: "string")] | 25 | #[ORM\Column(type: "string")] |
| 26 | private string $title; | 26 | private string $title; |
| @@ -40,8 +40,12 @@ class Article | |||
| 40 | )] | 40 | )] |
| 41 | private Collection $images; | 41 | private Collection $images; |
| 42 | 42 | ||
| 43 | public function __construct() | 43 | public function __construct(string $content, \DateTime $date_time = null, string $title = '', string $preview = '') |
| 44 | { | 44 | { |
| 45 | $this->date_time = $date_time; | ||
| 46 | $this->title = $title; | ||
| 47 | $this->preview = $preview; | ||
| 48 | $this->content = $content; | ||
| 45 | $this->images = new ArrayCollection(); // initialisation nécessaire | 49 | $this->images = new ArrayCollection(); // initialisation nécessaire |
| 46 | } | 50 | } |
| 47 | 51 | ||
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 | { |
