diff options
Diffstat (limited to 'src/controller/ajax.php')
-rw-r--r-- | src/controller/ajax.php | 744 |
1 files changed, 387 insertions, 357 deletions
diff --git a/src/controller/ajax.php b/src/controller/ajax.php index a64c39b..a820136 100644 --- a/src/controller/ajax.php +++ b/src/controller/ajax.php | |||
@@ -3,172 +3,9 @@ | |||
3 | 3 | ||
4 | declare(strict_types=1); | 4 | declare(strict_types=1); |
5 | 5 | ||
6 | use App\Entity\Article; | 6 | use App\Entity\Page; |
7 | use App\Entity\Node; | 7 | use App\Entity\Node; |
8 | 8 | use App\Entity\Article; | |
9 | // détection des requêtes de tinymce ou touchant aux articles | ||
10 | if($_SERVER['CONTENT_TYPE'] === 'application/json' && isset($_GET['action'])) | ||
11 | { | ||
12 | // récupération des données | ||
13 | $data = file_get_contents('php://input'); | ||
14 | $json = json_decode($data, true); | ||
15 | |||
16 | if($_GET['action'] === 'editor_submit' && isset($json['id']) && isset($json['content'])) | ||
17 | { | ||
18 | if(json_last_error() === JSON_ERROR_NONE) | ||
19 | { | ||
20 | $id = $json['id']; | ||
21 | $director = new Director($entityManager); | ||
22 | |||
23 | // cas d'une nouvelle "news" | ||
24 | if(is_array($json['content'])){ | ||
25 | foreach($json['content'] as $one_input){ | ||
26 | $one_input = Security::secureString($one_input); | ||
27 | } | ||
28 | $content = $json['content']; | ||
29 | } | ||
30 | else{ | ||
31 | $content = Security::secureString($json['content']); | ||
32 | } | ||
33 | |||
34 | // nouvel article | ||
35 | if($id[0] === 'n') | ||
36 | { | ||
37 | $section_id = (int)substr($id, 1); // id du bloc <section> | ||
38 | $director->findNodeById($section_id); | ||
39 | $director->makeSectionNode(); | ||
40 | $node = $director->getNode(); // = <section> | ||
41 | |||
42 | if(is_array($content)){ | ||
43 | $date = new \DateTime($content['d']); | ||
44 | $article = new Article($content['i'], $date, $content['t'], $content['p']); | ||
45 | $article_node = new Node('new', 'i' . (string)$date->getTimestamp(), [], count($node->getChildren()) + 1, $node, $node->getPage(), $article); | ||
46 | |||
47 | // id_node tout juste généré | ||
48 | //$article_node->getId(); | ||
49 | } | ||
50 | else{ | ||
51 | $timestamp = time(); | ||
52 | $date = new \DateTime; | ||
53 | $date->setTimestamp($timestamp); | ||
54 | |||
55 | $article = new Article($content, $date); // le "current" timestamp est obtenu par la BDD | ||
56 | $article_node = new Node('article', 'i' . (string)$timestamp, [], count($node->getChildren()) + 1, $node, $node->getPage(), $article); | ||
57 | } | ||
58 | |||
59 | $entityManager->persist($article_node); | ||
60 | $entityManager->flush(); | ||
61 | |||
62 | echo json_encode(['success' => true, 'article_id' => $article_node->getArticleTimestamp()]); | ||
63 | die; | ||
64 | } | ||
65 | // modification article | ||
66 | else{ | ||
67 | $id[0] = 'i'; // id de l'article node | ||
68 | } | ||
69 | |||
70 | if($director->makeArticleNode($id)) // une entrée est trouvée | ||
71 | { | ||
72 | $node = $director->getArticleNode(); // article | ||
73 | switch($json['id'][0]){ | ||
74 | case 'i': | ||
75 | $node->getArticle()->setContent($content); | ||
76 | break; | ||
77 | case 'p': | ||
78 | $node->getArticle()->setPreview($content); // html de l'éditeur | ||
79 | break; | ||
80 | case 't': | ||
81 | $node->getArticle()->setTitle($content); // html de l'éditeur | ||
82 | break; | ||
83 | case 'd': | ||
84 | echo json_encode(['success' => false, 'message' => 'l\'action editor_submit ne supporte pas les dates, utiliser date_submit.']); | ||
85 | die; | ||
86 | default: | ||
87 | echo json_encode(['success' => false, 'message' => 'identifiant non utilisable']); | ||
88 | die; | ||
89 | } | ||
90 | $entityManager->flush(); | ||
91 | echo json_encode(['success' => true]); | ||
92 | } | ||
93 | else | ||
94 | { | ||
95 | echo json_encode(['success' => false, 'message' => 'article non identifié']); | ||
96 | } | ||
97 | } | ||
98 | else{ | ||
99 | echo json_encode(['success' => false, 'message' => 'Erreur de décodage JSON']); | ||
100 | } | ||
101 | die; | ||
102 | } | ||
103 | elseif($_GET['action'] === 'delete_article' && isset($json['id'])) | ||
104 | { | ||
105 | $director = new Director($entityManager); | ||
106 | $director->makeArticleNode($json['id'], true); | ||
107 | $article = $director->getArticleNode(); | ||
108 | $section = $director->getNode(); | ||
109 | |||
110 | $entityManager->remove($article); | ||
111 | $section->removeChild($article); | ||
112 | $section->sortChildren(true); // régénère les positions | ||
113 | $entityManager->flush(); | ||
114 | |||
115 | // test avec une nouvelle requête qui ne devrait rien trouver | ||
116 | if(!$director->makeArticleNode($json['id'])) | ||
117 | { | ||
118 | echo json_encode(['success' => true]); | ||
119 | |||
120 | // on pourrait afficher une notification "toast" | ||
121 | } | ||
122 | else{ | ||
123 | http_response_code(500); | ||
124 | echo json_encode(['success' => false, 'message' => 'Erreur lors de la suppression de l\'article.']); | ||
125 | } | ||
126 | die; | ||
127 | } | ||
128 | // inversion de la position de deux noeuds | ||
129 | elseif($_GET['action'] === 'switch_positions' && isset($json['id1']) && isset($json['id2'])) | ||
130 | { | ||
131 | $director = new Director($entityManager); | ||
132 | $director->makeArticleNode($json['id1'], true); | ||
133 | $article1 = $director->getArticleNode(); | ||
134 | $section = $director->getNode(); | ||
135 | |||
136 | $section->sortChildren(true); // régénère les positions avant inversion | ||
137 | |||
138 | $article2; | ||
139 | foreach($section->getChildren() as $child){ | ||
140 | if($child->getArticleTimestamp() === $json['id2']) // type string | ||
141 | { | ||
142 | $article2 = $child; | ||
143 | break; | ||
144 | } | ||
145 | } | ||
146 | |||
147 | // inversion | ||
148 | $tmp = $article1->getPosition(); | ||
149 | $article1->setPosition($article2->getPosition()); | ||
150 | $article2->setPosition($tmp); | ||
151 | $entityManager->flush(); | ||
152 | |||
153 | echo json_encode(['success' => true]); | ||
154 | die; | ||
155 | } | ||
156 | elseif($_GET['action'] === 'date_submit' && isset($json['id']) && isset($json['date'])) | ||
157 | { | ||
158 | $id = $json['id']; | ||
159 | $id[0] = 'i'; | ||
160 | $date = new DateTime($json['date']); | ||
161 | |||
162 | $director = new Director($entityManager); | ||
163 | $director->makeArticleNode($id); | ||
164 | $node = $director->getArticleNode(); | ||
165 | $node->getArticle()->setDateTime($date); | ||
166 | $entityManager->flush(); | ||
167 | |||
168 | echo json_encode(['success' => true]); | ||
169 | die; | ||
170 | } | ||
171 | } | ||
172 | 9 | ||
173 | // détection des requêtes d'upload d'image de tinymce | 10 | // détection des requêtes d'upload d'image de tinymce |
174 | if(strpos($_SERVER['CONTENT_TYPE'], 'multipart/form-data') !== false && isset($_GET['action']) && $_GET['action'] === 'upload_image') | 11 | if(strpos($_SERVER['CONTENT_TYPE'], 'multipart/form-data') !== false && isset($_GET['action']) && $_GET['action'] === 'upload_image') |
@@ -207,221 +44,414 @@ if(strpos($_SERVER['CONTENT_TYPE'], 'multipart/form-data') !== false && isset($_ | |||
207 | } | 44 | } |
208 | 45 | ||
209 | 46 | ||
210 | /* -- page Menu et chemins -- */ | 47 | // détection des requêtes de type XHR, y en a pas à priori |
211 | if($_SERVER['CONTENT_TYPE'] === 'application/json' && isset($_GET['menu_edit'])) | 48 | /*elseif(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'){ |
212 | { | 49 | echo "requête XHR reçue par le serveur"; |
213 | // récupération des données | 50 | die; |
51 | }*/ | ||
52 | |||
53 | |||
54 | // détection des requêtes envoyées avec fetch (application/json) et récupération du JSON | ||
55 | if($_SERVER['CONTENT_TYPE'] === 'application/json'){ | ||
214 | $data = file_get_contents('php://input'); | 56 | $data = file_get_contents('php://input'); |
215 | $json = json_decode($data, true); | 57 | $json = json_decode($data, true); |
216 | Director::$menu_data = new Menu($entityManager); | ||
217 | |||
218 | // flèche gauche <=: position = position du parent + 1, parent = grand-parent, recalculer les positions | ||
219 | if($_GET['menu_edit'] === 'move_one_level_up' && isset($json['id'])){ | ||
220 | $id = $json['id']; | ||
221 | $page = Director::$menu_data->findPageById((int)$id); | ||
222 | |||
223 | $parent = $page->getParent(); // peut être null | ||
224 | if($parent === null){ | ||
225 | // 1er niveau: ne rien faire | ||
226 | echo json_encode(['success' => false]); | ||
227 | die; | ||
228 | } | ||
229 | // BDD | ||
230 | else{ | ||
231 | $page->setPosition($parent->getPosition() + 1); // nouvelle position | ||
232 | |||
233 | // 2ème niveau: le parent devient $menu_data, puis null après tri | ||
234 | if($parent->getParent() === null){ | ||
235 | // connexion dans les deux sens | ||
236 | $page->setParent(Director::$menu_data); // => pour la persistance | ||
237 | |||
238 | //Director::$menu_data->addChild($page); // => pour sortChildren | ||
239 | $page->getParent()->addChild($page); // => pour sortChildren | ||
240 | //Director::$menu_data->sortChildren(true); // positions décaléees des nouveaux petits frères | ||
241 | $page->getParent()->sortChildren(true); // positions décaléees des nouveaux petits frères | ||
242 | $page->setParent(null); | ||
243 | |||
244 | // affichage | ||
245 | $page->setPagePath($page->getEndOfPath()); | ||
246 | $page->fillChildrenPagePath(); | ||
247 | } | ||
248 | // 3ème niveau et plus | ||
249 | else{ | ||
250 | $page->setParent($parent->getParent()); // nouveau parent | ||
251 | $page->getParent()->addChild($page); // => pour sortChildren | ||
252 | $page->getParent()->sortChildren(true); // positions décaléees des nouveaux petits frères | ||
253 | $page->fillChildrenPagePath($page->getParent()->getPagePath()); | ||
254 | } | ||
255 | //$parent->sortChildren(true); // positions des enfants restants, inutile si la fonction est récursive? | ||
256 | $entityManager->flush(); | ||
257 | |||
258 | // affichage | ||
259 | $parent->removeChild($page); | ||
260 | $nav_builder = new NavBuilder(); | ||
261 | $menu_builder = new MenuBuilder(null, false); | ||
262 | echo json_encode(['success' => true, 'nav' => $nav_builder->render(), 'menu_buttons' => $menu_builder->render()]); | ||
263 | die; | ||
264 | } | ||
265 | } | ||
266 | 58 | ||
267 | // flèche droite =>: position = nombre d'éléments de la fraterie + 1, l'élément précédent devient le parent | 59 | // requêtes de tinymce ou touchant aux articles |
268 | if($_GET['menu_edit'] === 'move_one_level_down' && isset($json['id'])){ | 60 | if(isset($_GET['action'])) |
269 | $id = $json['id']; | 61 | { |
270 | $page = Director::$menu_data->findPageById((int)$id); | 62 | if($_GET['action'] === 'editor_submit' && isset($json['id']) && isset($json['content'])) |
63 | { | ||
64 | if(json_last_error() === JSON_ERROR_NONE) | ||
65 | { | ||
66 | $id = $json['id']; | ||
67 | $director = new Director($entityManager); | ||
68 | |||
69 | // cas d'une nouvelle "news" | ||
70 | if(is_array($json['content'])){ | ||
71 | foreach($json['content'] as $one_input){ | ||
72 | $one_input = Security::secureString($one_input); | ||
73 | } | ||
74 | $content = $json['content']; | ||
75 | } | ||
76 | else{ | ||
77 | $content = Security::secureString($json['content']); | ||
78 | } | ||
79 | |||
80 | // nouvel article | ||
81 | if($id[0] === 'n') | ||
82 | { | ||
83 | $section_id = (int)substr($id, 1); // id du bloc <section> | ||
84 | $director->findNodeById($section_id); | ||
85 | $director->makeSectionNode(); | ||
86 | $node = $director->getNode(); // = <section> | ||
87 | |||
88 | if(is_array($content)){ | ||
89 | $date = new \DateTime($content['d']); | ||
90 | $article = new Article($content['i'], $date, $content['t'], $content['p']); | ||
91 | $article_node = new Node('new', 'i' . (string)$date->getTimestamp(), [], count($node->getChildren()) + 1, $node, $node->getPage(), $article); | ||
92 | |||
93 | // id_node tout juste généré | ||
94 | //$article_node->getId(); | ||
95 | } | ||
96 | else{ | ||
97 | $timestamp = time(); | ||
98 | $date = new \DateTime; | ||
99 | $date->setTimestamp($timestamp); | ||
100 | |||
101 | $article = new Article($content, $date); // le "current" timestamp est obtenu par la BDD | ||
102 | $article_node = new Node('article', 'i' . (string)$timestamp, [], count($node->getChildren()) + 1, $node, $node->getPage(), $article); | ||
103 | } | ||
104 | |||
105 | $entityManager->persist($article_node); | ||
106 | $entityManager->flush(); | ||
107 | |||
108 | echo json_encode(['success' => true, 'article_id' => $article_node->getArticleTimestamp()]); | ||
109 | die; | ||
110 | } | ||
111 | // modification article | ||
112 | else{ | ||
113 | $id[0] = 'i'; // id de l'article node | ||
114 | } | ||
115 | |||
116 | if($director->makeArticleNode($id)) // une entrée est trouvée | ||
117 | { | ||
118 | $node = $director->getArticleNode(); // article | ||
119 | switch($json['id'][0]){ | ||
120 | case 'i': | ||
121 | $node->getArticle()->setContent($content); | ||
122 | break; | ||
123 | case 'p': | ||
124 | $node->getArticle()->setPreview($content); // html de l'éditeur | ||
125 | break; | ||
126 | case 't': | ||
127 | $node->getArticle()->setTitle($content); // html de l'éditeur | ||
128 | break; | ||
129 | case 'd': | ||
130 | echo json_encode(['success' => false, 'message' => 'l\'action editor_submit ne supporte pas les dates, utiliser date_submit.']); | ||
131 | die; | ||
132 | default: | ||
133 | echo json_encode(['success' => false, 'message' => 'identifiant non utilisable']); | ||
134 | die; | ||
135 | } | ||
136 | $entityManager->flush(); | ||
137 | echo json_encode(['success' => true]); | ||
138 | } | ||
139 | else | ||
140 | { | ||
141 | echo json_encode(['success' => false, 'message' => 'article non identifié']); | ||
142 | } | ||
143 | } | ||
144 | else{ | ||
145 | echo json_encode(['success' => false, 'message' => 'Erreur de décodage JSON']); | ||
146 | } | ||
147 | die; | ||
148 | } | ||
149 | elseif($_GET['action'] === 'delete_article' && isset($json['id'])) | ||
150 | { | ||
151 | $director = new Director($entityManager); | ||
152 | $director->makeArticleNode($json['id'], true); | ||
153 | $article = $director->getArticleNode(); | ||
154 | $section = $director->getNode(); | ||
155 | |||
156 | $entityManager->remove($article); | ||
157 | $section->removeChild($article); | ||
158 | $section->sortChildren(true); // régénère les positions | ||
159 | $entityManager->flush(); | ||
271 | 160 | ||
272 | $parent = $page->getParent(); // peut être null | 161 | // test avec une nouvelle requête qui ne devrait rien trouver |
273 | if($parent == null){ | 162 | if(!$director->makeArticleNode($json['id'])) |
274 | $parent = Director::$menu_data; | 163 | { |
275 | } | 164 | echo json_encode(['success' => true]); |
276 | 165 | ||
277 | // BDD | 166 | // on pourrait afficher une notification "toast" |
278 | $parent->sortChildren(true); // trie et réindexe par sécurité: 1, 2, 3... | 167 | } |
279 | if($page->getPosition() > 1){ | 168 | else{ |
280 | foreach($parent->getChildren() as $child){ | 169 | http_response_code(500); |
281 | //echo $child->getPageName(); | 170 | echo json_encode(['success' => false, 'message' => 'Erreur lors de la suppression de l\'article.']); |
282 | if($child->getPosition() === $page->getPosition() - 1){ | 171 | } |
283 | $page->setParent($child); | 172 | die; |
284 | break; | 173 | } |
285 | } | 174 | // inversion de la position de deux noeuds |
286 | } | 175 | elseif($_GET['action'] === 'switch_positions' && isset($json['id1']) && isset($json['id2'])) |
287 | $page->setPosition(count($page->getParent()->getChildren()) + 1); | 176 | { |
288 | } | 177 | $director = new Director($entityManager); |
289 | $entityManager->flush(); | 178 | $director->makeArticleNode($json['id1'], true); |
179 | $article1 = $director->getArticleNode(); | ||
180 | $section = $director->getNode(); | ||
181 | |||
182 | $section->sortChildren(true); // régénère les positions avant inversion | ||
183 | |||
184 | $article2; | ||
185 | foreach($section->getChildren() as $child){ | ||
186 | if($child->getArticleTimestamp() === $json['id2']) // type string | ||
187 | { | ||
188 | $article2 = $child; | ||
189 | break; | ||
190 | } | ||
191 | } | ||
290 | 192 | ||
291 | // affichage | 193 | // inversion |
292 | $parent->removeChild($page); | 194 | $tmp = $article1->getPosition(); |
293 | $page->getParent()->addChild($page); | 195 | $article1->setPosition($article2->getPosition()); |
294 | $page->fillChildrenPagePath($page->getParent()->getPagePath()); // variable non mappée $page_path | 196 | $article2->setPosition($tmp); |
295 | $nav_builder = new NavBuilder(); | 197 | $entityManager->flush(); |
296 | $menu_builder = new MenuBuilder(null, false); | ||
297 | 198 | ||
298 | echo json_encode(['success' => true, 'nav' => $nav_builder->render(), 'menu_buttons' => $menu_builder->render()]); | 199 | echo json_encode(['success' => true]); |
299 | die; | 200 | die; |
201 | } | ||
202 | elseif($_GET['action'] === 'date_submit' && isset($json['id']) && isset($json['date'])) | ||
203 | { | ||
204 | $id = $json['id']; | ||
205 | $id[0] = 'i'; | ||
206 | $date = new DateTime($json['date']); | ||
207 | |||
208 | $director = new Director($entityManager); | ||
209 | $director->makeArticleNode($id); | ||
210 | $node = $director->getArticleNode(); | ||
211 | $node->getArticle()->setDateTime($date); | ||
212 | $entityManager->flush(); | ||
213 | |||
214 | echo json_encode(['success' => true]); | ||
215 | die; | ||
216 | } | ||
300 | } | 217 | } |
301 | 218 | ||
302 | if($_GET['menu_edit'] === 'switch_positions' && isset($json['id1']) && isset($json['id2'])) | 219 | |
220 | /* -- page Menu et chemins -- */ | ||
221 | elseif(isset($_GET['menu_edit'])) | ||
303 | { | 222 | { |
304 | $id1 = $json['id1']; | 223 | // récupération des données |
305 | $id2 = $json['id2']; | 224 | $data = file_get_contents('php://input'); |
306 | 225 | $json = json_decode($data, true); | |
307 | // vérifier qu'ils ont le même parent | 226 | Director::$menu_data = new Menu($entityManager); |
308 | $page1 = Director::$menu_data->findPageById((int)$id1); | 227 | |
309 | $page2 = Director::$menu_data->findPageById((int)$id2); | 228 | // flèche gauche <=: position = position du parent + 1, parent = grand-parent, recalculer les positions |
310 | 229 | if($_GET['menu_edit'] === 'move_one_level_up' && isset($json['id'])){ | |
311 | // double le contrôle fait en JS | 230 | $id = $json['id']; |
312 | if($page1->getParent() === $page2->getParent()) // comparaison stricte d'objet (même instance du parent?) | 231 | $page = Director::$menu_data->findPageById((int)$id); |
313 | { | 232 | |
314 | // inversion | 233 | $parent = $page->getParent(); // peut être null |
315 | $tmp = $page1->getPosition(); | 234 | if($parent === null){ |
316 | $page1->setPosition($page2->getPosition()); | 235 | // 1er niveau: ne rien faire |
317 | $page2->setPosition($tmp); | 236 | echo json_encode(['success' => false]); |
318 | Director::$menu_data->sortChildren(true); // modifie tableau children | 237 | die; |
238 | } | ||
239 | // BDD | ||
240 | else{ | ||
241 | $page->setPosition($parent->getPosition() + 1); // nouvelle position | ||
242 | |||
243 | // 2ème niveau: le parent devient $menu_data, puis null après tri | ||
244 | if($parent->getParent() === null){ | ||
245 | // connexion dans les deux sens | ||
246 | $page->setParent(Director::$menu_data); // => pour la persistance | ||
247 | |||
248 | //Director::$menu_data->addChild($page); // => pour sortChildren | ||
249 | $page->getParent()->addChild($page); // => pour sortChildren | ||
250 | //Director::$menu_data->sortChildren(true); // positions décaléees des nouveaux petits frères | ||
251 | $page->getParent()->sortChildren(true); // positions décaléees des nouveaux petits frères | ||
252 | $page->setParent(null); | ||
253 | |||
254 | // affichage | ||
255 | $page->setPagePath($page->getEndOfPath()); | ||
256 | $page->fillChildrenPagePath(); | ||
257 | } | ||
258 | // 3ème niveau et plus | ||
259 | else{ | ||
260 | $page->setParent($parent->getParent()); // nouveau parent | ||
261 | $page->getParent()->addChild($page); // => pour sortChildren | ||
262 | $page->getParent()->sortChildren(true); // positions décaléees des nouveaux petits frères | ||
263 | $page->fillChildrenPagePath($page->getParent()->getPagePath()); | ||
264 | } | ||
265 | //$parent->sortChildren(true); // positions des enfants restants, inutile si la fonction est récursive? | ||
266 | $entityManager->flush(); | ||
267 | |||
268 | // affichage | ||
269 | $parent->removeChild($page); | ||
270 | $nav_builder = new NavBuilder(); | ||
271 | $menu_builder = new MenuBuilder(null, false); | ||
272 | echo json_encode(['success' => true, 'nav' => $nav_builder->render(), 'menu_buttons' => $menu_builder->render()]); | ||
273 | die; | ||
274 | } | ||
275 | } | ||
276 | |||
277 | // flèche droite =>: position = nombre d'éléments de la fraterie + 1, l'élément précédent devient le parent | ||
278 | if($_GET['menu_edit'] === 'move_one_level_down' && isset($json['id'])){ | ||
279 | $id = $json['id']; | ||
280 | $page = Director::$menu_data->findPageById((int)$id); | ||
281 | |||
282 | $parent = $page->getParent(); // peut être null | ||
283 | if($parent == null){ | ||
284 | $parent = Director::$menu_data; | ||
285 | } | ||
286 | |||
287 | // BDD | ||
288 | $parent->sortChildren(true); // trie et réindexe par sécurité: 1, 2, 3... | ||
289 | if($page->getPosition() > 1){ | ||
290 | foreach($parent->getChildren() as $child){ | ||
291 | if($child->getPosition() === $page->getPosition() - 1){ | ||
292 | $page->setParent($child); | ||
293 | break; | ||
294 | } | ||
295 | } | ||
296 | $page->setPosition(count($page->getParent()->getChildren()) + 1); | ||
297 | } | ||
319 | $entityManager->flush(); | 298 | $entityManager->flush(); |
320 | 299 | ||
321 | // nouveau menu | 300 | // affichage |
301 | $parent->removeChild($page); | ||
302 | $page->getParent()->addChild($page); | ||
303 | $page->fillChildrenPagePath($page->getParent()->getPagePath()); // variable non mappée $page_path | ||
322 | $nav_builder = new NavBuilder(); | 304 | $nav_builder = new NavBuilder(); |
323 | echo json_encode(['success' => true, 'nav' => $nav_builder->render()]); | 305 | $menu_builder = new MenuBuilder(null, false); |
324 | } | ||
325 | else{ | ||
326 | echo json_encode(['success' => false]); | ||
327 | } | ||
328 | |||
329 | die; | ||
330 | } | ||
331 | 306 | ||
332 | if($_GET['menu_edit'] === 'displayInMenu' && isset($json['id']) && isset($json['checked'])) | 307 | echo json_encode(['success' => true, 'nav' => $nav_builder->render(), 'menu_buttons' => $menu_builder->render()]); |
333 | { | 308 | die; |
334 | $id = $json['id']; | 309 | } |
335 | $checked = $json['checked']; | ||
336 | 310 | ||
337 | $page = Director::$menu_data->findPageById((int)$id); | 311 | if($_GET['menu_edit'] === 'switch_positions' && isset($json['id1']) && isset($json['id2'])) |
338 | if($page->isHidden() === $checked){ | 312 | { |
339 | $page->setHidden(!$checked); | 313 | $id1 = $json['id1']; |
340 | $entityManager->flush(); | 314 | $id2 = $json['id2']; |
341 | 315 | ||
342 | // nouveau menu | 316 | // vérifier qu'ils ont le même parent |
343 | $nav_builder = new NavBuilder(); | 317 | $page1 = Director::$menu_data->findPageById((int)$id1); |
344 | echo json_encode(['success' => true, 'nav' => $nav_builder->render()]); | 318 | $page2 = Director::$menu_data->findPageById((int)$id2); |
319 | |||
320 | // double le contrôle fait en JS | ||
321 | if($page1->getParent() === $page2->getParent()) // comparaison stricte d'objet (même instance du parent?) | ||
322 | { | ||
323 | // inversion | ||
324 | $tmp = $page1->getPosition(); | ||
325 | $page1->setPosition($page2->getPosition()); | ||
326 | $page2->setPosition($tmp); | ||
327 | Director::$menu_data->sortChildren(true); // modifie tableau children | ||
328 | $entityManager->flush(); | ||
329 | |||
330 | // nouveau menu | ||
331 | $nav_builder = new NavBuilder(); | ||
332 | echo json_encode(['success' => true, 'nav' => $nav_builder->render()]); | ||
333 | } | ||
334 | else{ | ||
335 | echo json_encode(['success' => false]); | ||
336 | } | ||
337 | |||
338 | die; | ||
345 | } | 339 | } |
346 | else{ | 340 | |
347 | echo json_encode(['success' => false]); | 341 | if($_GET['menu_edit'] === 'displayInMenu' && isset($json['id']) && isset($json['checked'])) |
342 | { | ||
343 | $id = $json['id']; | ||
344 | $checked = $json['checked']; | ||
345 | |||
346 | $page = Director::$menu_data->findPageById((int)$id); | ||
347 | if($page->isHidden() === $checked){ | ||
348 | $page->setHidden(!$checked); | ||
349 | $entityManager->flush(); | ||
350 | |||
351 | // nouveau menu | ||
352 | $nav_builder = new NavBuilder(); | ||
353 | echo json_encode(['success' => true, 'nav' => $nav_builder->render()]); | ||
354 | } | ||
355 | else{ | ||
356 | echo json_encode(['success' => false]); | ||
357 | } | ||
358 | die; | ||
348 | } | 359 | } |
349 | die; | ||
350 | } | 360 | } |
351 | } | ||
352 | 361 | ||
353 | 362 | ||
354 | /* -- mode Modification d'une page -- */ | 363 | /* -- mode Modification d'une page -- */ |
355 | if($_SERVER['CONTENT_TYPE'] === 'application/json' && isset($_GET['bloc_edit'])) | ||
356 | { | ||
357 | // récupération des données | ||
358 | $data = file_get_contents('php://input'); | ||
359 | $json = json_decode($data, true); | ||
360 | 364 | ||
361 | // renommage d'un bloc | 365 | // partie "page" |
362 | if($_GET['bloc_edit'] === 'rename_page_bloc') | 366 | elseif(isset($_GET['page_edit'])) |
363 | { | 367 | { |
364 | if(isset($json['bloc_title']) && $json['bloc_title'] !== null && isset($json['bloc_id']) && is_int($json['bloc_id'])){ | 368 | // récupération des données |
365 | $director = new Director($entityManager); | 369 | $data = file_get_contents('php://input'); |
366 | $director->findNodeById($json['bloc_id']); | 370 | $json = json_decode($data, true); |
367 | 371 | ||
368 | // le titre (du JSON en BDD) est récupéré sous forme de tableau, modifié et renvoyé | 372 | // tite de la page |
369 | $data = $director->getNode()->getNodeData()->getData(); | 373 | if($_GET['page_edit'] === 'page_title'){ |
370 | $data['title'] = htmlspecialchars($json['bloc_title']); | 374 | $page = $entityManager->find('App\Entity\Page', $json['page_id']); |
371 | $director->getNode()->getNodeData()->setData($data); | 375 | $page->setPageName(htmlspecialchars($json['title'])); |
372 | 376 | $entityManager->flush(); | |
373 | $entityManager->flush(); | 377 | echo json_encode(['success' => true, 'title' => $page->getPageName()]); |
374 | echo json_encode(['success' => true, 'title' => $data['title']]); | ||
375 | } | ||
376 | else{ | ||
377 | echo json_encode(['success' => false]); | ||
378 | } | 378 | } |
379 | die; | 379 | // titre en snake_case pour le menu |
380 | } | 380 | /*elseif($_GET['page_edit'] === 'page_menu_path'){ |
381 | // inversion des positions de deux blocs | 381 | $page = $entityManager->find('App\Entity\Page', $json['page_id']); |
382 | elseif($_GET['bloc_edit'] === 'switch_blocs_positions') | 382 | $page->setEndOfPath(htmlspecialchars($json['page_menu_path'])); |
383 | { | 383 | $entityManager->flush(); |
384 | if(isset($json['id1']) && is_int($json['id1']) && isset($json['id2']) && is_int($json['id2']) && isset($_GET['page'])){ | 384 | echo json_encode(['success' => true, 'page_name_path' => $page->getEndOfPath()]); |
385 | $director = new Director($entityManager, true); | 385 | }*/ |
386 | $director->findUniqueNodeByName('main'); | 386 | // description dans les métadonnées |
387 | $director->findItsChildren(); | 387 | elseif($_GET['page_edit'] === 'page_description'){ |
388 | $main = $director->getNode(); | 388 | $node_data = $entityManager->find('App\Entity\NodeData', $json['node_data_id']); |
389 | $main->sortChildren(true); // régénère les positions avant inversion | 389 | $node_data->updateData('description', htmlspecialchars($json['description'])); |
390 | 390 | $entityManager->flush(); | |
391 | $bloc1; $bloc2; | 391 | echo json_encode(['success' => true, 'description' => $node_data->getData()['description']]); |
392 | foreach($main->getChildren() as $child){ | ||
393 | if($child->getId() === $json['id1']){ | ||
394 | $bloc1 = $child; | ||
395 | break; | ||
396 | } | ||
397 | } | ||
398 | foreach($main->getChildren() as $child){ | ||
399 | if($child->getId() === $json['id2']){ | ||
400 | $bloc2 = $child; | ||
401 | break; | ||
402 | } | ||
403 | } | ||
404 | |||
405 | // inversion | ||
406 | $tmp = $bloc1->getPosition(); | ||
407 | $bloc1->setPosition($bloc2->getPosition()); | ||
408 | $bloc2->setPosition($tmp); | ||
409 | |||
410 | $entityManager->flush(); | ||
411 | echo json_encode(['success' => true]); | ||
412 | } | ||
413 | else{ | ||
414 | echo json_encode(['success' => false]); | ||
415 | } | 392 | } |
416 | die; | 393 | die; |
417 | } | 394 | } |
418 | } | ||
419 | |||
420 | |||
421 | // détection des requêtes de type XHR?, pas d'utilité à priori | ||
422 | /*elseif(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'){ | ||
423 | echo "requête XHR reçue par le serveur"; | ||
424 | die; | ||
425 | }*/ | ||
426 | |||
427 | 395 | ||
396 | // partie "blocs" | ||
397 | elseif(isset($_GET['bloc_edit'])) | ||
398 | { | ||
399 | // renommage d'un bloc | ||
400 | if($_GET['bloc_edit'] === 'rename_page_bloc') | ||
401 | { | ||
402 | if(isset($json['bloc_title']) && $json['bloc_title'] !== null && isset($json['bloc_id']) && is_int($json['bloc_id'])){ | ||
403 | $director = new Director($entityManager); | ||
404 | $director->findNodeById($json['bloc_id']); | ||
405 | |||
406 | // le titre (du JSON en BDD) est récupéré sous forme de tableau, modifié et renvoyé | ||
407 | $data = $director->getNode()->getNodeData()->getData(); | ||
408 | $data['title'] = htmlspecialchars($json['bloc_title']); | ||
409 | $director->getNode()->getNodeData()->updateData('title', htmlspecialchars($json['bloc_title'])); | ||
410 | |||
411 | $entityManager->flush(); | ||
412 | echo json_encode(['success' => true, 'title' => $data['title']]); | ||
413 | } | ||
414 | else{ | ||
415 | echo json_encode(['success' => false]); | ||
416 | } | ||
417 | die; | ||
418 | } | ||
419 | // inversion des positions de deux blocs | ||
420 | elseif($_GET['bloc_edit'] === 'switch_blocs_positions') | ||
421 | { | ||
422 | if(isset($json['id1']) && is_int($json['id1']) && isset($json['id2']) && is_int($json['id2']) && isset($_GET['page'])){ | ||
423 | $director = new Director($entityManager, true); | ||
424 | $director->findUniqueNodeByName('main'); | ||
425 | $director->findItsChildren(); | ||
426 | $main = $director->getNode(); | ||
427 | $main->sortChildren(true); // régénère les positions avant inversion | ||
428 | |||
429 | $bloc1; $bloc2; | ||
430 | foreach($main->getChildren() as $child){ | ||
431 | if($child->getId() === $json['id1']){ | ||
432 | $bloc1 = $child; | ||
433 | break; | ||
434 | } | ||
435 | } | ||
436 | foreach($main->getChildren() as $child){ | ||
437 | if($child->getId() === $json['id2']){ | ||
438 | $bloc2 = $child; | ||
439 | break; | ||
440 | } | ||
441 | } | ||
442 | |||
443 | // inversion | ||
444 | $tmp = $bloc1->getPosition(); | ||
445 | $bloc1->setPosition($bloc2->getPosition()); | ||
446 | $bloc2->setPosition($tmp); | ||
447 | |||
448 | $entityManager->flush(); | ||
449 | echo json_encode(['success' => true]); | ||
450 | } | ||
451 | else{ | ||
452 | echo json_encode(['success' => false]); | ||
453 | } | ||
454 | die; | ||
455 | } | ||
456 | } | ||
457 | } \ No newline at end of file | ||