diff options
author | polo <ordipolo@gmx.fr> | 2025-10-10 17:29:30 +0200 |
---|---|---|
committer | polo <ordipolo@gmx.fr> | 2025-10-10 17:30:47 +0200 |
commit | 596ce6f78e37c901d9b4ed464918b0294ba8dd2a (patch) | |
tree | a11c55145cd0479a416891f5ee0587b536d24261 /src/controller | |
parent | a0b94008b26ef20f1164e0c5302d5d11c313b1ad (diff) | |
download | cms-596ce6f78e37c901d9b4ed464918b0294ba8dd2a.zip |
src/controller/Director.php devient /src/model/Model.php, message d'erreur type de bloc dans modify_block.php
Diffstat (limited to 'src/controller')
-rw-r--r-- | src/controller/ArticleController.php | 42 | ||||
-rw-r--r-- | src/controller/Director.php | 256 | ||||
-rw-r--r-- | src/controller/MenuAndPathsController.php | 30 | ||||
-rw-r--r-- | src/controller/PageManagementController.php | 90 | ||||
-rw-r--r-- | src/controller/ViewController.php | 8 |
5 files changed, 85 insertions, 341 deletions
diff --git a/src/controller/ArticleController.php b/src/controller/ArticleController.php index fabd997..06562e7 100644 --- a/src/controller/ArticleController.php +++ b/src/controller/ArticleController.php | |||
@@ -16,12 +16,12 @@ class ArticleController | |||
16 | if($request->query->has('id') && !empty($request->query->get('id')) && $request->query->has('last_article')){ | 16 | if($request->query->has('id') && !empty($request->query->get('id')) && $request->query->has('last_article')){ |
17 | //var_dump($request->query->get('last_article')); | 17 | //var_dump($request->query->get('last_article')); |
18 | $id = (int)$request->get('id'); // type et nettoie | 18 | $id = (int)$request->get('id'); // type et nettoie |
19 | $director = new Director($entityManager); | 19 | $model = new Model($entityManager); |
20 | $director->findNodeById($id); | 20 | $model->findNodeById($id); |
21 | $parent_block = $director->getNode(); | 21 | $parent_block = $model->getNode(); |
22 | 22 | ||
23 | if(Blocks::hasPresentation($parent_block->getName())){ | 23 | if(Blocks::hasPresentation($parent_block->getName())){ |
24 | $get_articles_return = $director->getNextArticles($parent_block, $request); | 24 | $get_articles_return = $model->getNextArticles($parent_block, $request); |
25 | $bulk_data = $get_articles_return[0]; | 25 | $bulk_data = $get_articles_return[0]; |
26 | 26 | ||
27 | if($parent_block->getName() === 'post_block'){ | 27 | if($parent_block->getName() === 'post_block'){ |
@@ -60,7 +60,7 @@ class ArticleController | |||
60 | $id = substr($id, 1); | 60 | $id = substr($id, 1); |
61 | } | 61 | } |
62 | 62 | ||
63 | $director = new Director($entityManager); | 63 | $model = new Model($entityManager); |
64 | $content = $json['content']; | 64 | $content = $json['content']; |
65 | 65 | ||
66 | // nettoyage | 66 | // nettoyage |
@@ -77,12 +77,12 @@ class ArticleController | |||
77 | if($json['id'][0] === 'n') // ici $id est un bloc | 77 | if($json['id'][0] === 'n') // ici $id est un bloc |
78 | { | 78 | { |
79 | $section_id = (int)substr($id, 1); // id du bloc <section> | 79 | $section_id = (int)substr($id, 1); // id du bloc <section> |
80 | if(!$director->findNodeById($section_id)){ // erreur mauvais id | 80 | if(!$model->findNodeById($section_id)){ // erreur mauvais id |
81 | echo json_encode(['success' => false, 'error' => 'article_not_saved, bad id']); | 81 | echo json_encode(['success' => false, 'error' => 'article_not_saved, bad id']); |
82 | die; | 82 | die; |
83 | } | 83 | } |
84 | $director->makeSectionNode(); | 84 | $model->makeSectionNode(); |
85 | $node = $director->getNode(); // = <section> | 85 | $node = $model->getNode(); // = <section> |
86 | 86 | ||
87 | if(is_array($content)){ // cas d'une nouvelle "news" | 87 | if(is_array($content)){ // cas d'une nouvelle "news" |
88 | if($node->getPage()->getEndOfPath() !== $json['from']){ // erreur mauvais from | 88 | if($node->getPage()->getEndOfPath() !== $json['from']){ // erreur mauvais from |
@@ -118,9 +118,9 @@ class ArticleController | |||
118 | // modification article | 118 | // modification article |
119 | //else{} | 119 | //else{} |
120 | 120 | ||
121 | if($director->makeArticleNode($id)) // une entrée est trouvée | 121 | if($model->makeArticleNode($id)) // une entrée est trouvée |
122 | { | 122 | { |
123 | $node = $director->getArticleNode(); // article | 123 | $node = $model->getArticleNode(); // article |
124 | switch($json['id'][0]){ | 124 | switch($json['id'][0]){ |
125 | case 'i': | 125 | case 'i': |
126 | $node->getArticle()->setContent($content); | 126 | $node->getArticle()->setContent($content); |
@@ -153,14 +153,14 @@ class ArticleController | |||
153 | 153 | ||
154 | static public function deleteArticle(EntityManager $entityManager, array $data): Response // $data peut être un $_GET ou du JSON | 154 | static public function deleteArticle(EntityManager $entityManager, array $data): Response // $data peut être un $_GET ou du JSON |
155 | { | 155 | { |
156 | $director = new Director($entityManager); | 156 | $model = new Model($entityManager); |
157 | if(!$director->makeArticleNode($data['id'], true)){ | 157 | if(!$model->makeArticleNode($data['id'], true)){ |
158 | return new Response( | 158 | return new Response( |
159 | '{"success": false, "message": "Erreur: pas d\'article à supprimer"}', | 159 | '{"success": false, "message": "Erreur: pas d\'article à supprimer"}', |
160 | Response::HTTP_INTERNAL_SERVER_ERROR); // 500 | 160 | Response::HTTP_INTERNAL_SERVER_ERROR); // 500 |
161 | } | 161 | } |
162 | $article = $director->getArticleNode(); | 162 | $article = $model->getArticleNode(); |
163 | $section = $director->getNode(); | 163 | $section = $model->getNode(); |
164 | 164 | ||
165 | $entityManager->remove($article); | 165 | $entityManager->remove($article); |
166 | $section->removeChild($article); | 166 | $section->removeChild($article); |
@@ -181,10 +181,10 @@ class ArticleController | |||
181 | 181 | ||
182 | static public function switchPositions(EntityManager $entityManager, array $json): void | 182 | static public function switchPositions(EntityManager $entityManager, array $json): void |
183 | { | 183 | { |
184 | $director = new Director($entityManager); | 184 | $model = new Model($entityManager); |
185 | $director->makeArticleNode($json['id1'], true); | 185 | $model->makeArticleNode($json['id1'], true); |
186 | $article1 = $director->getArticleNode(); | 186 | $article1 = $model->getArticleNode(); |
187 | $section = $director->getNode(); | 187 | $section = $model->getNode(); |
188 | 188 | ||
189 | $section->sortChildren(true); // régénère les positions avant inversion | 189 | $section->sortChildren(true); // régénère les positions avant inversion |
190 | $article2 = null; | 190 | $article2 = null; |
@@ -211,9 +211,9 @@ class ArticleController | |||
211 | $id = substr($json['id'], 1); | 211 | $id = substr($json['id'], 1); |
212 | $date = new DateTime($json['date']); | 212 | $date = new DateTime($json['date']); |
213 | 213 | ||
214 | $director = new Director($entityManager); | 214 | $model = new Model($entityManager); |
215 | $director->makeArticleNode($id); | 215 | $model->makeArticleNode($id); |
216 | $node = $director->getArticleNode(); | 216 | $node = $model->getArticleNode(); |
217 | $node->getArticle()->setDateTime($date); | 217 | $node->getArticle()->setDateTime($date); |
218 | $entityManager->flush(); | 218 | $entityManager->flush(); |
219 | 219 | ||
diff --git a/src/controller/Director.php b/src/controller/Director.php deleted file mode 100644 index 9c1c6e3..0000000 --- a/src/controller/Director.php +++ /dev/null | |||
@@ -1,256 +0,0 @@ | |||
1 | <?php | ||
2 | // src/controller/Director.php | ||
3 | |||
4 | // => src/model/Model.php serait mieux | ||
5 | |||
6 | declare(strict_types=1); | ||
7 | |||
8 | use Doctrine\ORM\EntityManager; | ||
9 | use App\Entity\Page; | ||
10 | use App\Entity\Node; | ||
11 | use Doctrine\ORM\QueryBuilder; | ||
12 | use Symfony\Component\HttpFoundation\Request; | ||
13 | |||
14 | class Director | ||
15 | { | ||
16 | private EntityManager $entityManager; | ||
17 | static public Menu $menu_data; // pour NavBuilder | ||
18 | static public ?Path $page_path = null; // pour $current dans NavBuilder et pour BreadcrumbBuilder | ||
19 | private Page $page; | ||
20 | private ?Node $node; | ||
21 | private Node $article; | ||
22 | |||
23 | public function __construct(EntityManager $entityManager) | ||
24 | { | ||
25 | $this->entityManager = $entityManager; | ||
26 | $this->node = new Node; // instance mère "vide" ne possédant rien d'autre que des enfants | ||
27 | } | ||
28 | |||
29 | // à déplacer dans Path ou un truc comme ça? | ||
30 | // couper Director en deux classes NodeModel et PageModel? | ||
31 | public function makeMenuAndPaths(): void // lit la table "page" | ||
32 | { | ||
33 | self::$menu_data = new Menu($this->entityManager); | ||
34 | self::$page_path = new Path(); | ||
35 | $this->page = self::$page_path->getLast(); | ||
36 | } | ||
37 | |||
38 | public function getNode(): Node | ||
39 | { | ||
40 | return $this->node; | ||
41 | } | ||
42 | public function getArticleNode(): Node | ||
43 | { | ||
44 | return $this->article; | ||
45 | } | ||
46 | |||
47 | // affichage d'une page ordinaire | ||
48 | public function getWholePageData(Request $request): void // lit la table "node" + jointures | ||
49 | { | ||
50 | $id = CURRENT_PAGE === 'article' ? htmlspecialchars($request->query->get('id')) : ''; | ||
51 | |||
52 | if($id === ''){ // page "normale" | ||
53 | // récupérer tous les noeuds sauf les articles | ||
54 | $dql = "SELECT n FROM App\Entity\Node n WHERE n.name_node != 'new' AND n.name_node != 'post' AND (n.page = :page OR n.page IS null)"; | ||
55 | $bulk_data = $this->entityManager | ||
56 | ->createQuery($dql) | ||
57 | ->setParameter('page', $this->page) | ||
58 | ->getResult(); | ||
59 | |||
60 | // groupes d'articles triés par bloc, permet de paginer par bloc | ||
61 | foreach($bulk_data as $parent_block){ | ||
62 | if(Blocks::hasPresentation($parent_block->getName())){ // = post_block ou news_block | ||
63 | $bulk_data = array_merge($bulk_data, $this->getNextArticles($parent_block, $request)[0]); | ||
64 | } | ||
65 | } | ||
66 | } | ||
67 | else{ // page "article" | ||
68 | $dql = 'SELECT n FROM App\Entity\Node n WHERE n.page = :page OR n.page IS null OR n.id_node = :id'; | ||
69 | $bulk_data = $this->entityManager | ||
70 | ->createQuery($dql) | ||
71 | ->setParameter('page', $this->page) | ||
72 | ->setParameter('id', $id) | ||
73 | ->getResult(); | ||
74 | } | ||
75 | |||
76 | $this->makeNodeTree($bulk_data); | ||
77 | } | ||
78 | |||
79 | // récupération d'articles | ||
80 | public function getNextArticles(Node $parent_block, Request $request): array | ||
81 | { | ||
82 | $qb = $this->entityManager->createQueryBuilder(); | ||
83 | $qb->select('n') | ||
84 | ->from('App\Entity\Node', 'n') | ||
85 | ->where('n.parent = :parent') | ||
86 | ->setParameter('parent', $parent_block); | ||
87 | |||
88 | if($parent_block->getName() === 'post_block'){ | ||
89 | $qb->orderBy('n.position'); | ||
90 | } | ||
91 | elseif($parent_block->getName() === 'news_block'){ | ||
92 | $qb->join('n.article', 'a'); | ||
93 | if($parent_block->getNodeData()->getChronoOrder() ?? false){ // ordre antichrono par défaut | ||
94 | $qb->orderBy('a.date_time', 'ASC'); | ||
95 | } | ||
96 | else{ | ||
97 | $qb->orderBy('a.date_time', 'DESC'); | ||
98 | } | ||
99 | } | ||
100 | |||
101 | // pagination | ||
102 | $limit = $parent_block->getNodeData()->getPaginationLimit(); // = 12 par défaut si = null en BDD | ||
103 | $this->paginateWithCursor($qb, $parent_block, $request->query->get('last_article')); | ||
104 | $result = $qb->getQuery()->getResult(); | ||
105 | |||
106 | // il reste des articles à récupérer SI on vient d'en récupérer trop | ||
107 | // ET on gère le cas particulier de $limit <= 0 | ||
108 | $truncated = false; | ||
109 | if(count($result) > $limit && $limit > 0){ // si nb résultat > limit > 0 | ||
110 | $truncated = true; | ||
111 | array_pop($result); // compenser le $limit + 1 dans paginateWithCursor | ||
112 | } | ||
113 | |||
114 | return [$result, $truncated]; // besoin exceptionnel de retourner deux valeurs | ||
115 | } | ||
116 | |||
117 | private function paginateWithCursor(QueryBuilder $qb, Node $parent_block, ?string $last_article): void | ||
118 | { | ||
119 | //var_dump($last_article); | ||
120 | $limit = $parent_block->getNodeData()->getPaginationLimit(); // = 12 par défaut si = null en BDD | ||
121 | |||
122 | if($limit > 0){ // si 0 ou moins pas de pagination | ||
123 | // nombres de "pages" d'articles | ||
124 | $nb_pages = $this->getNumberOfPages($parent_block, $limit); | ||
125 | $parent_block->getNodeData()->setNumberOfPages($nb_pages > 1 ? $nb_pages : 1); | ||
126 | |||
127 | // adaptation de la requête | ||
128 | if($parent_block->getName() === 'post_block'){ | ||
129 | $qb->andWhere('n.position > :last_position') | ||
130 | ->setParameter('last_position', empty($last_article) ? 0 : $last_article) | ||
131 | ->setMaxResults($limit + 1); | ||
132 | } | ||
133 | elseif($parent_block->getName() === 'news_block'){ | ||
134 | $cursor_start = $parent_block->getNodeData()->getChronoOrder() ? '1970-01-01' : '9999-12-31'; | ||
135 | $qb->andWhere($parent_block->getNodeData()->getChronoOrder() ? 'a.date_time > :date_time' : 'a.date_time < :date_time') | ||
136 | ->setParameter('date_time', empty($last_article) ? $cursor_start : $last_article) | ||
137 | ->setMaxResults($limit + 1); | ||
138 | } | ||
139 | } | ||
140 | } | ||
141 | |||
142 | // le Paginator de doctrine le fait aussi si on décidait de s'en servir | ||
143 | private function getNumberOfPages(Node $parent_block, int $limit): int | ||
144 | { | ||
145 | $dql = 'SELECT COUNT(n.id_node) FROM App\Entity\Node n WHERE n.parent = :parent'; | ||
146 | $nb_articles = $this->entityManager | ||
147 | ->createQuery($dql) | ||
148 | ->setParameter('parent', $parent_block) | ||
149 | ->getSingleScalarResult(); | ||
150 | return (int)ceil($nb_articles / $limit); // que PHP fasse une division non euclidienne (pas comme en C) nous arrange ici | ||
151 | } | ||
152 | |||
153 | private function makeNodeTree(array $bulk_data): void // $bulk_data = tableau de Node | ||
154 | { | ||
155 | // puis on les range | ||
156 | // (attention, risque de disfonctionnement si les noeuds de 1er niveau ne sont pas récupérés en 1er dans la BDD) | ||
157 | foreach($bulk_data as $node){ | ||
158 | // premier niveau | ||
159 | if($node->getParent() == null){ | ||
160 | $this->node->addChild($node); | ||
161 | |||
162 | // spécifique page article | ||
163 | if($node->getName() === 'main' && $this->page->getEndOfPath() == 'article'){ | ||
164 | $main = $node; | ||
165 | } | ||
166 | } | ||
167 | // autres niveaux | ||
168 | else{ | ||
169 | $node->getParent()->addChild($node); | ||
170 | |||
171 | // spécifique page article | ||
172 | if($this->page->getEndOfPath() == 'article'){ | ||
173 | if($node->getName() === 'new'){ | ||
174 | $new = $node; | ||
175 | } | ||
176 | } | ||
177 | } | ||
178 | } | ||
179 | if(isset($new)){ | ||
180 | $main->setAdoptedChild($new); | ||
181 | } | ||
182 | } | ||
183 | |||
184 | // le basique | ||
185 | public function findNodeById(int $id): bool | ||
186 | { | ||
187 | $this->node = $this->entityManager->find('App\Entity\Node', $id); | ||
188 | return $this->node === null ? false : true; | ||
189 | } | ||
190 | |||
191 | // récupération d'un article pour modification | ||
192 | public function makeArticleNode(string $id = '', bool $get_section = false): bool | ||
193 | { | ||
194 | if($get_section){ | ||
195 | $dql = 'SELECT n, p FROM App\Entity\Node n LEFT JOIN n.parent p WHERE n.id_node = :id'; | ||
196 | } | ||
197 | else{ | ||
198 | $dql = 'SELECT n FROM App\Entity\Node n WHERE n.id_node = :id'; | ||
199 | } | ||
200 | // n est l'article et p son $parent | ||
201 | $bulk_data = $this->entityManager | ||
202 | ->createQuery($dql) | ||
203 | ->setParameter('id', $id) | ||
204 | ->getResult(); | ||
205 | |||
206 | if(count($bulk_data) === 0){ | ||
207 | return false; | ||
208 | } | ||
209 | |||
210 | if($get_section){ | ||
211 | $this->article = $bulk_data[0]; | ||
212 | $this->findNodeById($bulk_data[0]->getParent()->getId()); | ||
213 | $this->makeSectionNode(); | ||
214 | } | ||
215 | else{ | ||
216 | $this->article = $bulk_data[0]; | ||
217 | } | ||
218 | |||
219 | return true; | ||
220 | } | ||
221 | |||
222 | // récupération des articles d'un bloc <section> à la création d'un article | ||
223 | public function makeSectionNode(): bool | ||
224 | { | ||
225 | $bulk_data = $this->entityManager | ||
226 | ->createQuery('SELECT n FROM App\Entity\Node n WHERE n.parent = :parent') | ||
227 | ->setParameter('parent', $this->node) | ||
228 | ->getResult(); | ||
229 | |||
230 | foreach($bulk_data as $article){ | ||
231 | $this->node->addChild($article); // pas de flush, on ne va pas écrire dans la BDD à chaque nouvelle page | ||
232 | } | ||
233 | return true; | ||
234 | } | ||
235 | |||
236 | public function findUniqueNodeByName(string $name): void // = unique en BDD, donc sans "page" associée | ||
237 | { | ||
238 | $bulk_data = $this->entityManager | ||
239 | ->createQuery('SELECT n FROM App\Entity\Node n WHERE n.name_node = :name') | ||
240 | ->setParameter('name', $name) | ||
241 | ->getResult(); | ||
242 | $this->node = $bulk_data[0]; | ||
243 | } | ||
244 | |||
245 | public function findItsChildren(): void | ||
246 | { | ||
247 | $bulk_data = $this->entityManager | ||
248 | ->createQuery('SELECT n FROM App\Entity\Node n WHERE n.parent = :parent AND n.page = :page') | ||
249 | ->setParameter('parent', $this->node) | ||
250 | ->setParameter('page', $this->page) | ||
251 | ->getResult(); | ||
252 | foreach($bulk_data as $child){ | ||
253 | $this->node->addChild($child); | ||
254 | } | ||
255 | } | ||
256 | } | ||
diff --git a/src/controller/MenuAndPathsController.php b/src/controller/MenuAndPathsController.php index 3ff7d54..f0553ad 100644 --- a/src/controller/MenuAndPathsController.php +++ b/src/controller/MenuAndPathsController.php | |||
@@ -10,8 +10,8 @@ class MenuAndPathsController | |||
10 | { | 10 | { |
11 | static public function newUrlMenuEntry(EntityManager $entityManager): void | 11 | static public function newUrlMenuEntry(EntityManager $entityManager): void |
12 | { | 12 | { |
13 | Director::$menu_data = new Menu($entityManager); | 13 | Model::$menu_data = new Menu($entityManager); |
14 | $previous_page = Director::$menu_data->findPageById((int)$_POST["location"]); // (int) à cause de declare(strict_types=1); | 14 | $previous_page = Model::$menu_data->findPageById((int)$_POST["location"]); // (int) à cause de declare(strict_types=1); |
15 | $parent = $previous_page->getParent(); | 15 | $parent = $previous_page->getParent(); |
16 | 16 | ||
17 | $url_input = trim($_POST["url_input"]); // faire htmlspecialchars à l'affichage | 17 | $url_input = trim($_POST["url_input"]); // faire htmlspecialchars à l'affichage |
@@ -31,7 +31,7 @@ class MenuAndPathsController | |||
31 | // addChild l'ajoute à la fin du tableau "children" puis on trie | 31 | // addChild l'ajoute à la fin du tableau "children" puis on trie |
32 | // exemple avec 2 comme position demandée: 1 2 3 4 2 devient 1 2 3 4 5 et la nouvelle entrée sera en 3è position | 32 | // exemple avec 2 comme position demandée: 1 2 3 4 2 devient 1 2 3 4 5 et la nouvelle entrée sera en 3è position |
33 | if(!$parent){ | 33 | if(!$parent){ |
34 | $parent = Director::$menu_data; | 34 | $parent = Model::$menu_data; |
35 | } | 35 | } |
36 | $parent->addChild($page); // true pour réindexer les positions en BDD | 36 | $parent->addChild($page); // true pour réindexer les positions en BDD |
37 | $parent->reindexPositions(); | 37 | $parent->reindexPositions(); |
@@ -63,11 +63,11 @@ class MenuAndPathsController | |||
63 | 63 | ||
64 | static public function deleteUrlMenuEntry(EntityManager $entityManager): void | 64 | static public function deleteUrlMenuEntry(EntityManager $entityManager): void |
65 | { | 65 | { |
66 | Director::$menu_data = new Menu($entityManager); | 66 | Model::$menu_data = new Menu($entityManager); |
67 | $page = Director::$menu_data->findPageById((int)$_POST["delete"]); | 67 | $page = Model::$menu_data->findPageById((int)$_POST["delete"]); |
68 | $parent = $page->getParent(); | 68 | $parent = $page->getParent(); |
69 | if($parent == null){ | 69 | if($parent == null){ |
70 | $parent = Director::$menu_data; | 70 | $parent = Model::$menu_data; |
71 | } | 71 | } |
72 | 72 | ||
73 | $parent->removeChild($page); // suppression de $children avant de trier | 73 | $parent->removeChild($page); // suppression de $children avant de trier |
@@ -82,7 +82,7 @@ class MenuAndPathsController | |||
82 | static public function MoveOneLevelUp(EntityManager $entityManager, array $json): void | 82 | static public function MoveOneLevelUp(EntityManager $entityManager, array $json): void |
83 | { | 83 | { |
84 | $id = $json['id']; | 84 | $id = $json['id']; |
85 | $page = Director::$menu_data->findPageById((int)$id); | 85 | $page = Model::$menu_data->findPageById((int)$id); |
86 | 86 | ||
87 | $parent = $page->getParent(); // peut être null | 87 | $parent = $page->getParent(); // peut être null |
88 | if($parent === null){ | 88 | if($parent === null){ |
@@ -97,9 +97,9 @@ class MenuAndPathsController | |||
97 | // 2ème niveau: le parent devient $menu_data, puis null après tri | 97 | // 2ème niveau: le parent devient $menu_data, puis null après tri |
98 | if($parent->getParent() === null){ | 98 | if($parent->getParent() === null){ |
99 | // connexion dans les deux sens | 99 | // connexion dans les deux sens |
100 | $page->setParent(Director::$menu_data); // => pour la persistance | 100 | $page->setParent(Model::$menu_data); // => pour la persistance |
101 | 101 | ||
102 | //Director::$menu_data->addChild($page); // => pour sortChildren | 102 | //Model::$menu_data->addChild($page); // => pour sortChildren |
103 | $page->getParent()->addChild($page); // => pour sortChildren | 103 | $page->getParent()->addChild($page); // => pour sortChildren |
104 | $page->getParent()->sortChildren(true); // positions décaléees des nouveaux petits frères | 104 | $page->getParent()->sortChildren(true); // positions décaléees des nouveaux petits frères |
105 | $page->setParent(null); | 105 | $page->setParent(null); |
@@ -129,11 +129,11 @@ class MenuAndPathsController | |||
129 | static public function MoveOneLevelDown(EntityManager $entityManager, array $json): void | 129 | static public function MoveOneLevelDown(EntityManager $entityManager, array $json): void |
130 | { | 130 | { |
131 | $id = $json['id']; | 131 | $id = $json['id']; |
132 | $page = Director::$menu_data->findPageById((int)$id); | 132 | $page = Model::$menu_data->findPageById((int)$id); |
133 | 133 | ||
134 | $parent = $page->getParent(); // peut être null | 134 | $parent = $page->getParent(); // peut être null |
135 | if($parent == null){ | 135 | if($parent == null){ |
136 | $parent = Director::$menu_data; | 136 | $parent = Model::$menu_data; |
137 | } | 137 | } |
138 | 138 | ||
139 | // BDD | 139 | // BDD |
@@ -166,8 +166,8 @@ class MenuAndPathsController | |||
166 | $id2 = $json['id2']; | 166 | $id2 = $json['id2']; |
167 | 167 | ||
168 | // vérifier qu'ils ont le même parent | 168 | // vérifier qu'ils ont le même parent |
169 | $page1 = Director::$menu_data->findPageById((int)$id1); | 169 | $page1 = Model::$menu_data->findPageById((int)$id1); |
170 | $page2 = Director::$menu_data->findPageById((int)$id2); | 170 | $page2 = Model::$menu_data->findPageById((int)$id2); |
171 | 171 | ||
172 | // double le contrôle fait en JS | 172 | // double le contrôle fait en JS |
173 | if($page1->getParent() === $page2->getParent()) // comparaison stricte d'objet (même instance du parent?) | 173 | if($page1->getParent() === $page2->getParent()) // comparaison stricte d'objet (même instance du parent?) |
@@ -176,7 +176,7 @@ class MenuAndPathsController | |||
176 | $tmp = $page1->getPosition(); | 176 | $tmp = $page1->getPosition(); |
177 | $page1->setPosition($page2->getPosition()); | 177 | $page1->setPosition($page2->getPosition()); |
178 | $page2->setPosition($tmp); | 178 | $page2->setPosition($tmp); |
179 | Director::$menu_data->sortChildren(true); // modifie tableau children | 179 | Model::$menu_data->sortChildren(true); // modifie tableau children |
180 | $entityManager->flush(); | 180 | $entityManager->flush(); |
181 | 181 | ||
182 | // nouveau menu | 182 | // nouveau menu |
@@ -194,7 +194,7 @@ class MenuAndPathsController | |||
194 | $id = $json['id']; | 194 | $id = $json['id']; |
195 | $checked = $json['checked']; | 195 | $checked = $json['checked']; |
196 | 196 | ||
197 | $page = Director::$menu_data->findPageById((int)$id); | 197 | $page = Model::$menu_data->findPageById((int)$id); |
198 | if($page->isHidden() === $checked){ | 198 | if($page->isHidden() === $checked){ |
199 | $page->setHidden(!$checked); | 199 | $page->setHidden(!$checked); |
200 | $entityManager->flush(); | 200 | $entityManager->flush(); |
diff --git a/src/controller/PageManagementController.php b/src/controller/PageManagementController.php index e6886b1..9dce952 100644 --- a/src/controller/PageManagementController.php +++ b/src/controller/PageManagementController.php | |||
@@ -25,16 +25,16 @@ class PageManagementController | |||
25 | 25 | ||
26 | static public function updatePageMenuPath(EntityManager $entityManager): void | 26 | static public function updatePageMenuPath(EntityManager $entityManager): void |
27 | { | 27 | { |
28 | Director::$menu_data = new Menu($entityManager); | 28 | Model::$menu_data = new Menu($entityManager); |
29 | Director::$page_path = new Path(); | 29 | Model::$page_path = new Path(); |
30 | $page = Director::$page_path->getLast(); | 30 | $page = Model::$page_path->getLast(); |
31 | $path = htmlspecialchars($_POST['page_menu_path']); | 31 | $path = htmlspecialchars($_POST['page_menu_path']); |
32 | 32 | ||
33 | // mise en snake_case: filtre caractères non-alphanumériques, minuscule, doublons d'underscore, trim des underscores | 33 | // mise en snake_case: filtre caractères non-alphanumériques, minuscule, doublons d'underscore, trim des underscores |
34 | $path = trim(preg_replace('/_+/', '_', strtolower(preg_replace('/[^a-zA-Z0-9]/', '_', $path))), '_'); | 34 | $path = trim(preg_replace('/_+/', '_', strtolower(preg_replace('/[^a-zA-Z0-9]/', '_', $path))), '_'); |
35 | $page->setEndOfPath($path); | 35 | $page->setEndOfPath($path); |
36 | foreach(Director::$menu_data->getChildren() as $child){ | 36 | foreach(Model::$menu_data->getChildren() as $child){ |
37 | if($child->getEndOfPath() === Director::$page_path->getArray()[0]->getEndOfPath()){ | 37 | if($child->getEndOfPath() === Model::$page_path->getArray()[0]->getEndOfPath()){ |
38 | $child->fillChildrenPagePath(); // MAJ de $page_path | 38 | $child->fillChildrenPagePath(); // MAJ de $page_path |
39 | } | 39 | } |
40 | } | 40 | } |
@@ -55,10 +55,10 @@ class PageManagementController | |||
55 | static public function newPage(EntityManager $entityManager, array $post): void | 55 | static public function newPage(EntityManager $entityManager, array $post): void |
56 | { | 56 | { |
57 | // titre et chemin | 57 | // titre et chemin |
58 | $director = new Director($entityManager); | 58 | $model = new Model($entityManager); |
59 | $director->makeMenuAndPaths(); | 59 | $model->makeMenuAndPaths(); |
60 | //Director::$menu_data = new Menu($entityManager); | 60 | //Model::$menu_data = new Menu($entityManager); |
61 | $previous_page = Director::$menu_data->findPageById((int)$post["page_location"]); // (int) à cause de declare(strict_types=1); | 61 | $previous_page = Model::$menu_data->findPageById((int)$post["page_location"]); // (int) à cause de declare(strict_types=1); |
62 | $parent = $previous_page->getParent(); | 62 | $parent = $previous_page->getParent(); |
63 | 63 | ||
64 | $page = new Page( | 64 | $page = new Page( |
@@ -73,7 +73,7 @@ class PageManagementController | |||
73 | // addChild l'ajoute à la fin du tableau "children" puis on trie | 73 | // addChild l'ajoute à la fin du tableau "children" puis on trie |
74 | // exemple avec 2 comme position demandée: 1 2 3 4 2 devient 1 2 3 4 5 et la nouvelle entrée sera en 3è position | 74 | // exemple avec 2 comme position demandée: 1 2 3 4 2 devient 1 2 3 4 5 et la nouvelle entrée sera en 3è position |
75 | if($parent == null){ | 75 | if($parent == null){ |
76 | $parent = Director::$menu_data; | 76 | $parent = Model::$menu_data; |
77 | } | 77 | } |
78 | $parent->addChild($page); | 78 | $parent->addChild($page); |
79 | $parent->reindexPositions(); | 79 | $parent->reindexPositions(); |
@@ -127,12 +127,12 @@ class PageManagementController | |||
127 | /* partie "blocs" */ | 127 | /* partie "blocs" */ |
128 | static public function addBloc(EntityManager $entityManager): void | 128 | static public function addBloc(EntityManager $entityManager): void |
129 | { | 129 | { |
130 | $director = new Director($entityManager); | 130 | $model = new Model($entityManager); |
131 | $director->makeMenuAndPaths(); // on a besoin de page_path qui dépend de menu_data | 131 | $model->makeMenuAndPaths(); // on a besoin de page_path qui dépend de menu_data |
132 | $page = Director::$page_path->getLast(); | 132 | $page = Model::$page_path->getLast(); |
133 | $director->findUniqueNodeByName('main'); | 133 | $model->findUniqueNodeByName('main'); |
134 | $director->findItsChildren(); | 134 | $model->findItsChildren(); |
135 | $main = $director->getNode(); | 135 | $main = $model->getNode(); |
136 | $position = count($main->getChildren()) + 1; // position dans la fraterie | 136 | $position = count($main->getChildren()) + 1; // position dans la fraterie |
137 | 137 | ||
138 | if(!in_array($_POST["bloc_select"], array_keys(Blocks::$blocks), true)) // 3è param: contrôle du type | 138 | if(!in_array($_POST["bloc_select"], array_keys(Blocks::$blocks), true)) // 3è param: contrôle du type |
@@ -185,12 +185,12 @@ class PageManagementController | |||
185 | 185 | ||
186 | static public function deleteBloc(EntityManager $entityManager): void | 186 | static public function deleteBloc(EntityManager $entityManager): void |
187 | { | 187 | { |
188 | $director = new Director($entityManager); | 188 | $model = new Model($entityManager); |
189 | $director->makeMenuAndPaths(); | 189 | $model->makeMenuAndPaths(); |
190 | $director->findUniqueNodeByName('main'); | 190 | $model->findUniqueNodeByName('main'); |
191 | $director->findItsChildren(); | 191 | $model->findItsChildren(); |
192 | //$director->findNodeById((int)$_POST['delete_bloc_id']); | 192 | //$model->findNodeById((int)$_POST['delete_bloc_id']); |
193 | $main = $director->getNode(); | 193 | $main = $model->getNode(); |
194 | $bloc = null; | 194 | $bloc = null; |
195 | foreach($main->getChildren() as $child){ | 195 | foreach($main->getChildren() as $child){ |
196 | if($child->getId() === (int)$_POST['delete_bloc_id']){ | 196 | if($child->getId() === (int)$_POST['delete_bloc_id']){ |
@@ -212,13 +212,13 @@ class PageManagementController | |||
212 | static public function renameBloc(EntityManager $entityManager, array $json): void | 212 | static public function renameBloc(EntityManager $entityManager, array $json): void |
213 | { | 213 | { |
214 | if(isset($json['bloc_title']) && $json['bloc_title'] !== null && isset($json['bloc_id']) && is_int($json['bloc_id'])){ | 214 | if(isset($json['bloc_title']) && $json['bloc_title'] !== null && isset($json['bloc_id']) && is_int($json['bloc_id'])){ |
215 | $director = new Director($entityManager); | 215 | $model = new Model($entityManager); |
216 | $director->findNodeById($json['bloc_id']); | 216 | $model->findNodeById($json['bloc_id']); |
217 | 217 | ||
218 | // le titre (du JSON en BDD) est récupéré sous forme de tableau, modifié et renvoyé | 218 | // le titre (du JSON en BDD) est récupéré sous forme de tableau, modifié et renvoyé |
219 | $data = $director->getNode()->getNodeData()->getData(); | 219 | $data = $model->getNode()->getNodeData()->getData(); |
220 | $data['title'] = htmlspecialchars($json['bloc_title']); | 220 | $data['title'] = htmlspecialchars($json['bloc_title']); |
221 | $director->getNode()->getNodeData()->updateData('title', htmlspecialchars($json['bloc_title'])); | 221 | $model->getNode()->getNodeData()->updateData('title', htmlspecialchars($json['bloc_title'])); |
222 | 222 | ||
223 | $entityManager->flush(); | 223 | $entityManager->flush(); |
224 | echo json_encode(['success' => true, 'title' => $data['title']]); | 224 | echo json_encode(['success' => true, 'title' => $data['title']]); |
@@ -232,11 +232,11 @@ class PageManagementController | |||
232 | static public function SwitchBlocsPositions(EntityManager $entityManager, array $json): void | 232 | static public function SwitchBlocsPositions(EntityManager $entityManager, array $json): void |
233 | { | 233 | { |
234 | if(isset($json['id1']) && is_int($json['id1']) && isset($json['id2']) && is_int($json['id2']) && isset($_GET['page'])){ | 234 | if(isset($json['id1']) && is_int($json['id1']) && isset($json['id2']) && is_int($json['id2']) && isset($_GET['page'])){ |
235 | $director = new Director($entityManager); | 235 | $model = new Model($entityManager); |
236 | $director->makeMenuAndPaths(); // true pour $director->findItsChildren(); | 236 | $model->makeMenuAndPaths(); // true pour $model->findItsChildren(); |
237 | $director->findUniqueNodeByName('main'); | 237 | $model->findUniqueNodeByName('main'); |
238 | $director->findItsChildren(); | 238 | $model->findItsChildren(); |
239 | $main = $director->getNode(); | 239 | $main = $model->getNode(); |
240 | $main->sortChildren(true); // régénère les positions avant inversion | 240 | $main->sortChildren(true); // régénère les positions avant inversion |
241 | 241 | ||
242 | $bloc1 = null; | 242 | $bloc1 = null; |
@@ -271,8 +271,8 @@ class PageManagementController | |||
271 | static public function changeArticlesOrder(EntityManager $entityManager, array $json): void | 271 | static public function changeArticlesOrder(EntityManager $entityManager, array $json): void |
272 | { | 272 | { |
273 | if(isset($json['id']) && isset($json['chrono_order'])){ | 273 | if(isset($json['id']) && isset($json['chrono_order'])){ |
274 | $director = new Director($entityManager); | 274 | $model = new Model($entityManager); |
275 | $director->findNodeById($json['id']); | 275 | $model->findNodeById($json['id']); |
276 | 276 | ||
277 | if($json['chrono_order'] === 'chrono'){ | 277 | if($json['chrono_order'] === 'chrono'){ |
278 | $chrono_order = true; | 278 | $chrono_order = true; |
@@ -284,7 +284,7 @@ class PageManagementController | |||
284 | echo json_encode(['success' => false]); | 284 | echo json_encode(['success' => false]); |
285 | die; | 285 | die; |
286 | } | 286 | } |
287 | $director->getNode()->getNodeData()->setChronoOrder($chrono_order); | 287 | $model->getNode()->getNodeData()->setChronoOrder($chrono_order); |
288 | $entityManager->flush(); | 288 | $entityManager->flush(); |
289 | 289 | ||
290 | echo json_encode(['success' => true, 'chrono_order' => $json['chrono_order']]); | 290 | echo json_encode(['success' => true, 'chrono_order' => $json['chrono_order']]); |
@@ -298,16 +298,16 @@ class PageManagementController | |||
298 | static public function changePresentation(EntityManager $entityManager, array $json): void | 298 | static public function changePresentation(EntityManager $entityManager, array $json): void |
299 | { | 299 | { |
300 | if(isset($json['id']) && isset($json['presentation'])){ | 300 | if(isset($json['id']) && isset($json['presentation'])){ |
301 | $director = new Director($entityManager); | 301 | $model = new Model($entityManager); |
302 | $director->findNodeById($json['id']); | 302 | $model->findNodeById($json['id']); |
303 | 303 | ||
304 | if(in_array($json['presentation'], array_keys(Blocks::$presentations))){ | 304 | if(in_array($json['presentation'], array_keys(Blocks::$presentations))){ |
305 | $director->getNode()->getNodeData()->setPresentation($json['presentation']); | 305 | $model->getNode()->getNodeData()->setPresentation($json['presentation']); |
306 | $entityManager->flush(); | 306 | $entityManager->flush(); |
307 | 307 | ||
308 | $response_data = ['success' => true, 'presentation' => $json['presentation']]; | 308 | $response_data = ['success' => true, 'presentation' => $json['presentation']]; |
309 | if($json['presentation'] === 'grid'){ | 309 | if($json['presentation'] === 'grid'){ |
310 | $response_data['cols_min_width'] = $director->getNode()->getNodeData()->getColsMinWidth(); | 310 | $response_data['cols_min_width'] = $model->getNode()->getNodeData()->getColsMinWidth(); |
311 | } | 311 | } |
312 | echo json_encode($response_data); | 312 | echo json_encode($response_data); |
313 | } | 313 | } |
@@ -323,9 +323,9 @@ class PageManagementController | |||
323 | static public function changeColsMinWidth(EntityManager $entityManager, array $json): void | 323 | static public function changeColsMinWidth(EntityManager $entityManager, array $json): void |
324 | { | 324 | { |
325 | if(isset($json['id']) && isset($json['cols_min_width'])){ | 325 | if(isset($json['id']) && isset($json['cols_min_width'])){ |
326 | $director = new Director($entityManager); | 326 | $model = new Model($entityManager); |
327 | $director->findNodeById($json['id']); | 327 | $model->findNodeById($json['id']); |
328 | $director->getNode()->getNodeData()->setColsMinWidth((int)$json['cols_min_width']); // attention conversion? | 328 | $model->getNode()->getNodeData()->setColsMinWidth((int)$json['cols_min_width']); // attention conversion? |
329 | 329 | ||
330 | $entityManager->flush(); | 330 | $entityManager->flush(); |
331 | echo json_encode(['success' => true, 'cols_min_width' => $json['cols_min_width']]); | 331 | echo json_encode(['success' => true, 'cols_min_width' => $json['cols_min_width']]); |
@@ -338,10 +338,10 @@ class PageManagementController | |||
338 | static public function changePaginationLimit(EntityManager $entityManager, array $json): void | 338 | static public function changePaginationLimit(EntityManager $entityManager, array $json): void |
339 | { | 339 | { |
340 | if(isset($json['id']) && isset($json['pagination_limit'])){ | 340 | if(isset($json['id']) && isset($json['pagination_limit'])){ |
341 | $director = new Director($entityManager); | 341 | $model = new Model($entityManager); |
342 | $director->findNodeById($json['id']); | 342 | $model->findNodeById($json['id']); |
343 | $old_limit = $director->getNode()->getNodeData()->getPaginationLimit() ?? 12; | 343 | $old_limit = $model->getNode()->getNodeData()->getPaginationLimit() ?? 12; |
344 | $director->getNode()->getNodeData()->setPaginationLimit((int)$json['pagination_limit']); // attention conversion? | 344 | $model->getNode()->getNodeData()->setPaginationLimit((int)$json['pagination_limit']); // attention conversion? |
345 | 345 | ||
346 | $entityManager->flush(); | 346 | $entityManager->flush(); |
347 | 347 | ||
diff --git a/src/controller/ViewController.php b/src/controller/ViewController.php index feee0a0..0f21118 100644 --- a/src/controller/ViewController.php +++ b/src/controller/ViewController.php | |||
@@ -49,10 +49,10 @@ class ViewController extends AbstractBuilder // ViewController est aussi le prem | |||
49 | 49 | ||
50 | 50 | ||
51 | /* 2/ accès au modèle */ | 51 | /* 2/ accès au modèle */ |
52 | $director = new Director($entityManager); | 52 | $model = new Model($entityManager); |
53 | $director->makeMenuAndPaths(); | 53 | $model->makeMenuAndPaths(); |
54 | $director->getWholePageData($request); | 54 | $model->getWholePageData($request); |
55 | self::$root_node = $director->getNode(); | 55 | self::$root_node = $model->getNode(); |
56 | 56 | ||
57 | 57 | ||
58 | /* 3/ 2ème contrôle utilisant les données récupérées */ | 58 | /* 3/ 2ème contrôle utilisant les données récupérées */ |