diff options
Diffstat (limited to 'src/controller/ajax.php')
-rw-r--r-- | src/controller/ajax.php | 640 |
1 files changed, 61 insertions, 579 deletions
diff --git a/src/controller/ajax.php b/src/controller/ajax.php index ae43b75..7529fe6 100644 --- a/src/controller/ajax.php +++ b/src/controller/ajax.php | |||
@@ -3,511 +3,89 @@ | |||
3 | 3 | ||
4 | declare(strict_types=1); | 4 | declare(strict_types=1); |
5 | 5 | ||
6 | use App\Entity\Page; | 6 | use PHPMailer\PHPMailer\PHPMailer; |
7 | use App\Entity\Node; | 7 | use PHPMailer\PHPMailer\Exception; |
8 | use App\Entity\Article; | 8 | use App\Entity\Email; |
9 | 9 | ||
10 | 10 | // mettre ça ailleurs? | |
11 | // mettre ça ailleurs | 11 | function sendEmail(bool $true_email, string $name = '', string $email = '', string $message = ''): bool |
12 | function imagickCleanImage(string $image_data, string $local_path, string $format = 'jpeg'): bool // "string" parce que file_get_contents... | ||
13 | { | ||
14 | try{ | ||
15 | $imagick = new Imagick(); | ||
16 | $imagick->readImageBlob($image_data); | ||
17 | $imagick->stripImage(); // nettoyage métadonnées | ||
18 | $imagick->setImageFormat($format); | ||
19 | if($format === 'jpeg'){ | ||
20 | $imagick->setImageCompression(Imagick::COMPRESSION_JPEG); | ||
21 | $imagick->setImageCompressionQuality(85); // optionnel | ||
22 | } | ||
23 | $imagick->writeImage($local_path); // enregistrement | ||
24 | $imagick->clear(); | ||
25 | $imagick->destroy(); | ||
26 | return true; | ||
27 | } | ||
28 | catch(Exception $e){ | ||
29 | return false; | ||
30 | } | ||
31 | } | ||
32 | function curlDownloadImage(string $url, $maxRetries = 3, $timeout = 10): string|false | ||
33 | { | ||
34 | $attempt = 0; | ||
35 | $imageData = false; | ||
36 | |||
37 | while($attempt < $maxRetries){ | ||
38 | $ch = curl_init($url); // instance de CurlHandle | ||
39 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
40 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | ||
41 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); | ||
42 | curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); | ||
43 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); | ||
44 | curl_setopt($ch, CURLOPT_USERAGENT, 'TinyMCE-Image-Downloader'); | ||
45 | |||
46 | $imageData = curl_exec($ch); | ||
47 | $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | ||
48 | //$curlError = curl_error($ch); | ||
49 | |||
50 | curl_close($ch); | ||
51 | |||
52 | if($imageData !== false && $httpCode >= 200 && $httpCode < 300){ | ||
53 | return $imageData; | ||
54 | } | ||
55 | |||
56 | $attempt++; | ||
57 | sleep(1); | ||
58 | } | ||
59 | |||
60 | return false; // échec après trois tentatives | ||
61 | } | ||
62 | |||
63 | |||
64 | // détection des requêtes d'upload d'image de tinymce | ||
65 | if(strpos($_SERVER['CONTENT_TYPE'], 'multipart/form-data') !== false && isset($_GET['action']) && $_GET['action'] === 'upload_image') | ||
66 | { | 12 | { |
67 | if(isset($_FILES['file'])){ | 13 | $mail = new PHPMailer(true); // true => exceptions |
68 | $file = $_FILES['file']; | 14 | $mail->CharSet = 'UTF-8'; |
69 | $dest = 'images/'; | ||
70 | $dest_mini = 'images-mini/'; | ||
71 | |||
72 | // Vérifier si les répertoires existent, sinon les créer | ||
73 | if(!is_dir($dest)) { | ||
74 | mkdir($dest, 0700, true); | ||
75 | } | ||
76 | if(!is_dir($dest_mini)) { | ||
77 | mkdir($dest_mini, 0700, true); | ||
78 | } | ||
79 | |||
80 | $allowed_extensions = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'tiff', 'tif']; | ||
81 | $name = Security::secureFileName(pathinfo($file['name'], PATHINFO_FILENAME)); | ||
82 | $extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION)); | ||
83 | if(!in_array($extension, $allowed_extensions) || $extension === 'jpg'){ | ||
84 | $extension = 'jpeg'; | ||
85 | } | ||
86 | $file_path = $dest . $name . '_' . uniqid() . '.' . $extension; | ||
87 | 15 | ||
88 | // créer une miniature de l'image | 16 | try{ |
89 | // | 17 | // Paramètres du serveur |
90 | 18 | $mail->isSMTP(); | |
91 | if(imagickCleanImage(file_get_contents($file['tmp_name']), $file_path, $extension)){ // recréer l’image pour la nettoyer | 19 | $mail->Host = Config::$smtp_host; |
92 | echo json_encode(['location' => $file_path]); // renvoyer l'URL de l'image téléchargée | 20 | $mail->SMTPAuth = true; |
93 | } | 21 | $mail->Port = 25; |
94 | else{ | ||
95 | http_response_code(500); | ||
96 | echo json_encode(['message' => 'Erreur image non valide']); | ||
97 | } | ||
98 | } | ||
99 | else{ | ||
100 | http_response_code(400); | ||
101 | echo json_encode(['message' => 'Erreur 400: Bad Request']); | ||
102 | } | ||
103 | die; | ||
104 | } | ||
105 | // cas du collage d'un contenu HTML, réception d'une URL, téléchargement par le serveur et renvoie de l'adresse sur le serveur | ||
106 | elseif(isset($_GET['action']) && $_GET['action'] == 'upload_image_url'){ | ||
107 | $json = json_decode(file_get_contents('php://input'), true); | ||
108 | |||
109 | if(isset($json['image_url'])){ | ||
110 | $image_data = curlDownloadImage($json['image_url']); // téléchargement de l’image par le serveur avec cURL au lieu de file_get_contents | ||
111 | $dest = 'images/'; | ||
112 | |||
113 | if(!is_dir($dest)) { // Vérifier si le répertoire existe, sinon le créer | ||
114 | mkdir($dest, 0777, true); | ||
115 | } | ||
116 | |||
117 | if($image_data === false){ | ||
118 | http_response_code(400); | ||
119 | echo json_encode(['message' => "Erreur, le serveur n'a pas réussi à télécharger l'image."]); | ||
120 | die; | ||
121 | } | ||
122 | |||
123 | $allowed_extensions = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'tiff', 'tif']; | ||
124 | $url_path = parse_url($json['image_url'], PHP_URL_PATH); | ||
125 | $name = Security::secureFileName(pathinfo($url_path, PATHINFO_FILENAME)); | ||
126 | $extension = strtolower(pathinfo($url_path, PATHINFO_EXTENSION)); | ||
127 | if(!in_array($extension, $allowed_extensions) || $extension === 'jpg'){ | ||
128 | $extension = 'jpeg'; | ||
129 | } | ||
130 | $local_path = $dest . $name . '_' . uniqid() . '.' . $extension; | ||
131 | 22 | ||
132 | if(imagickCleanImage($image_data, $local_path, $extension)){ // recréer l’image pour la nettoyer | 23 | if($mail->SMTPAuth){ |
133 | echo json_encode(['location' => $local_path]); // nouvelle adresse | 24 | $mail->Username = Config::$smtp_username; // e-mail |
25 | $mail->Password = Config::$smtp_password; | ||
26 | $mail->SMTPSecure = Config::$smtp_secure; // tls (starttls) ou ssl (smtps) | ||
27 | if($mail->SMTPSecure === 'tls'){ | ||
28 | $mail->Port = 587; | ||
29 | } | ||
30 | elseif($mail->SMTPSecure === 'ssl'){ | ||
31 | $mail->Port = 465; | ||
32 | } | ||
134 | } | 33 | } |
135 | else{ | 34 | //var_dump($mail->smtpConnect());die; // test de connexion |
136 | http_response_code(500); | ||
137 | echo json_encode(['message' => 'Erreur image non valide']); | ||
138 | } | ||
139 | } | ||
140 | else{ | ||
141 | echo json_encode(['message' => 'Erreur 400: Bad Request']); | ||
142 | } | ||
143 | die; | ||
144 | } | ||
145 | // cas du collage d'une image (code base64) non encapsulée dans du HTML | ||
146 | elseif(isset($_GET['action']) && $_GET['action'] == 'upload_image_base64'){ | ||
147 | $json = json_decode(file_get_contents('php://input'), true); | ||
148 | $dest = 'images/'; | ||
149 | 35 | ||
150 | if(!is_dir('images')){ | 36 | // Expéditeur et destinataire |
151 | mkdir('images', 0777, true); | 37 | $mail->setFrom(strtolower(Config::$email_from), Config::$email_from_name); // expéditeur |
152 | } | 38 | $mail->addAddress(strtolower(Config::$email_dest), Config::$email_dest_name); // destinataire |
153 | |||
154 | // détection de data:image/ et de ;base64, et capture du format dans $type | ||
155 | if(!isset($json['image_base64']) || !preg_match('/^data:image\/(\w+);base64,/', $json['image_base64'], $type)){ | ||
156 | http_response_code(400); | ||
157 | echo json_encode(['message' => 'Données image base64 manquantes ou invalides']); | ||
158 | die; | ||
159 | } | ||
160 | |||
161 | $allowed_extensions = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'tiff', 'tif']; | ||
162 | $extension = strtolower($type[1]); | ||
163 | if(!in_array($extension, $allowed_extensions) || $extension === 'jpg'){ | ||
164 | $extension = 'jpeg'; | ||
165 | } | ||
166 | 39 | ||
167 | $image_data = base64_decode(substr($json['image_base64'], strpos($json['image_base64'], ',') + 1)); // découpe la chaine à la virgule puis convertit en binaire | 40 | // Contenu |
168 | if($image_data === false){ | 41 | $mail->isHTML(true); |
169 | http_response_code(400); | 42 | if($true_email){ |
170 | echo json_encode(['message' => 'Décodage base64 invalide']); | 43 | $mail->Subject = 'Message envoyé par: ' . $name . ' (' . $email . ') depuis le site web'; |
171 | die; | 44 | |
172 | } | 45 | } |
173 | 46 | else{ | |
174 | $local_path = $dest . 'pasted_image_' . uniqid() . '.' . $extension; | 47 | $mail->Subject = "TEST d'un envoi d'e-mail depuis le site web"; |
48 | } | ||
49 | $mail->Body = $message; | ||
50 | $mail->AltBody = $message; | ||
175 | 51 | ||
176 | if(imagickCleanImage($image_data, $local_path)){ | 52 | $mail->send(); |
177 | echo json_encode(['location' => $local_path]); | 53 | return true; |
178 | } | 54 | } |
179 | else{ | 55 | catch(Exception $e){ |
180 | http_response_code(500); | 56 | return false; |
181 | echo json_encode(['message' => 'Erreur image non valide']); | 57 | //echo "Le message n'a pas pu être envoyé. Erreur : {$mail->ErrorInfo}"; |
182 | } | 58 | } |
183 | die; | ||
184 | } | 59 | } |
185 | 60 | ||
186 | // détection des requêtes de type XHR, y en a pas à priori | ||
187 | /*elseif(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'){ | ||
188 | echo "requête XHR reçue par le serveur"; | ||
189 | die; | ||
190 | }*/ | ||
191 | |||
192 | 61 | ||
193 | // détection des requêtes envoyées avec fetch (application/json) et récupération du JSON | 62 | // détection des requêtes envoyées avec fetch (application/json) et récupération du JSON |
194 | if($_SERVER['CONTENT_TYPE'] === 'application/json'){ | 63 | if($_SERVER['CONTENT_TYPE'] === 'application/json') |
64 | { | ||
195 | $data = file_get_contents('php://input'); | 65 | $data = file_get_contents('php://input'); |
196 | $json = json_decode($data, true); | 66 | $json = json_decode($data, true); |
197 | 67 | ||
198 | // requêtes de tinymce ou touchant aux articles | 68 | // requêtes de tinymce ou touchant aux articles |
199 | if(isset($_GET['action'])) | 69 | if(isset($_GET['action'])) |
200 | { | 70 | { |
201 | if($_GET['action'] === 'editor_submit' && isset($json['id']) && isset($json['content'])) | 71 | // e-mail envoyé par le formulaire de contact |
202 | { | 72 | if($_GET['action'] === 'send_email'){ |
203 | if(json_last_error() === JSON_ERROR_NONE) | 73 | $captcha_solution = (isset($_SESSION['captcha']) && is_int($_SESSION['captcha'])) ? $_SESSION['captcha'] : 0; |
204 | { | 74 | $captcha_try = isset($json['captcha']) ? Captcha::controlInput($json['captcha']) : 0; |
205 | $id = $json['id']; | 75 | |
206 | $director = new Director($entityManager); | 76 | // contrôles des entrées |
207 | 77 | $name = htmlspecialchars(trim($json['name'])); | |
208 | // cas d'une nouvelle "news" | 78 | $email = strtolower(htmlspecialchars(trim($json['email']))); |
209 | if(is_array($json['content'])){ | 79 | $message = htmlspecialchars(trim($json['message'])); |
210 | foreach($json['content'] as $one_input){ | ||
211 | $one_input = Security::secureString($one_input); | ||
212 | } | ||
213 | $content = $json['content']; | ||
214 | } | ||
215 | else{ | ||
216 | $content = Security::secureString($json['content']); | ||
217 | } | ||
218 | |||
219 | // nouvel article | ||
220 | if($id[0] === 'n') | ||
221 | { | ||
222 | $section_id = (int)substr($id, 1); // id du bloc <section> | ||
223 | $director->findNodeById($section_id); | ||
224 | $director->makeSectionNode(); | ||
225 | $node = $director->getNode(); // = <section> | ||
226 | |||
227 | if(is_array($content)){ | ||
228 | $date = new \DateTime($content['d']); | ||
229 | $article = new Article($content['i'], $date, $content['t'], $content['p']); | ||
230 | $article_node = new Node('new', 'i' . (string)$date->getTimestamp(), [], count($node->getChildren()) + 1, $node, $node->getPage(), $article); | ||
231 | |||
232 | // id_node tout juste généré | ||
233 | //$article_node->getId(); | ||
234 | } | ||
235 | else{ | ||
236 | $timestamp = time(); | ||
237 | $date = new \DateTime; | ||
238 | $date->setTimestamp($timestamp); | ||
239 | |||
240 | $article = new Article($content, $date); // le "current" timestamp est obtenu par la BDD | ||
241 | $article_node = new Node('article', 'i' . (string)$timestamp, [], count($node->getChildren()) + 1, $node, $node->getPage(), $article); | ||
242 | } | ||
243 | |||
244 | $entityManager->persist($article_node); | ||
245 | $entityManager->flush(); | ||
246 | |||
247 | echo json_encode(['success' => true, 'article_id' => $article_node->getArticleTimestamp()]); | ||
248 | die; | ||
249 | } | ||
250 | // modification article | ||
251 | else{ | ||
252 | $id[0] = 'i'; // id de l'article node | ||
253 | } | ||
254 | |||
255 | if($director->makeArticleNode($id)) // une entrée est trouvée | ||
256 | { | ||
257 | $node = $director->getArticleNode(); // article | ||
258 | switch($json['id'][0]){ | ||
259 | case 'i': | ||
260 | $node->getArticle()->setContent($content); | ||
261 | break; | ||
262 | case 'p': | ||
263 | $node->getArticle()->setPreview($content); // html de l'éditeur | ||
264 | break; | ||
265 | case 't': | ||
266 | $node->getArticle()->setTitle($content); // html de l'éditeur | ||
267 | break; | ||
268 | case 'd': | ||
269 | echo json_encode(['success' => false, 'message' => 'l\'action editor_submit ne supporte pas les dates, utiliser date_submit.']); | ||
270 | die; | ||
271 | default: | ||
272 | echo json_encode(['success' => false, 'message' => 'identifiant non utilisable']); | ||
273 | die; | ||
274 | } | ||
275 | $entityManager->flush(); | ||
276 | echo json_encode(['success' => true]); | ||
277 | } | ||
278 | else | ||
279 | { | ||
280 | echo json_encode(['success' => false, 'message' => 'article non identifié']); | ||
281 | } | ||
282 | } | ||
283 | else{ | ||
284 | echo json_encode(['success' => false, 'message' => 'Erreur de décodage JSON']); | ||
285 | } | ||
286 | die; | ||
287 | } | ||
288 | elseif($_GET['action'] === 'delete_article' && isset($json['id'])) | ||
289 | { | ||
290 | $director = new Director($entityManager); | ||
291 | $director->makeArticleNode($json['id'], true); | ||
292 | $article = $director->getArticleNode(); | ||
293 | $section = $director->getNode(); | ||
294 | |||
295 | $entityManager->remove($article); | ||
296 | $section->removeChild($article); | ||
297 | $section->sortChildren(true); // régénère les positions | ||
298 | $entityManager->flush(); | ||
299 | |||
300 | // test avec une nouvelle requête qui ne devrait rien trouver | ||
301 | if(!$director->makeArticleNode($json['id'])) | ||
302 | { | ||
303 | echo json_encode(['success' => true]); | ||
304 | |||
305 | // on pourrait afficher une notification "toast" | ||
306 | } | ||
307 | else{ | ||
308 | http_response_code(500); | ||
309 | echo json_encode(['success' => false, 'message' => 'Erreur lors de la suppression de l\'article.']); | ||
310 | } | ||
311 | die; | ||
312 | } | ||
313 | // inversion de la position de deux noeuds | ||
314 | elseif($_GET['action'] === 'switch_positions' && isset($json['id1']) && isset($json['id2'])) | ||
315 | { | ||
316 | $director = new Director($entityManager); | ||
317 | $director->makeArticleNode($json['id1'], true); | ||
318 | $article1 = $director->getArticleNode(); | ||
319 | $section = $director->getNode(); | ||
320 | |||
321 | $section->sortChildren(true); // régénère les positions avant inversion | ||
322 | |||
323 | $article2; | ||
324 | foreach($section->getChildren() as $child){ | ||
325 | if($child->getArticleTimestamp() === $json['id2']) // type string | ||
326 | { | ||
327 | $article2 = $child; | ||
328 | break; | ||
329 | } | ||
330 | } | ||
331 | |||
332 | // inversion | ||
333 | $tmp = $article1->getPosition(); | ||
334 | $article1->setPosition($article2->getPosition()); | ||
335 | $article2->setPosition($tmp); | ||
336 | $entityManager->flush(); | ||
337 | |||
338 | echo json_encode(['success' => true]); | ||
339 | die; | ||
340 | } | ||
341 | elseif($_GET['action'] === 'date_submit' && isset($json['id']) && isset($json['date'])) | ||
342 | { | ||
343 | $id = $json['id']; | ||
344 | $id[0] = 'i'; | ||
345 | $date = new DateTime($json['date']); | ||
346 | |||
347 | $director = new Director($entityManager); | ||
348 | $director->makeArticleNode($id); | ||
349 | $node = $director->getArticleNode(); | ||
350 | $node->getArticle()->setDateTime($date); | ||
351 | $entityManager->flush(); | ||
352 | 80 | ||
353 | echo json_encode(['success' => true]); | 81 | if($captcha_try != 0 && $captcha_solution != 0 && ($captcha_try === $captcha_solution) |
354 | die; | 82 | && filter_var($email, FILTER_VALIDATE_EMAIL) && isset($json['hidden']) && empty($json['hidden']) |
355 | } | 83 | && sendEmail(true, $name, $email, $message)) |
356 | 84 | { | |
357 | // config formulaire | 85 | $db_email = new Email(strtolower(Config::$email_from), strtolower(Config::$email_dest), $message); |
358 | elseif($_GET['action'] === 'recipient_email'){ | 86 | $entityManager->persist($db_email); |
359 | $form_data = $entityManager->find('App\Entity\NodeData', $json['id']); | ||
360 | $email = htmlspecialchars(trim($json['email'])); | ||
361 | |||
362 | if(filter_var($email, FILTER_VALIDATE_EMAIL)){ | ||
363 | $form_data->updateData('email', $json['email']); | ||
364 | $entityManager->persist($form_data); | ||
365 | $entityManager->flush(); | ||
366 | echo json_encode(['success' => true]); | ||
367 | die; | ||
368 | } | ||
369 | else{ | ||
370 | echo json_encode(['success' => false]); | ||
371 | die; | ||
372 | } | ||
373 | } | ||
374 | } | ||
375 | |||
376 | |||
377 | /* -- page Menu et chemins -- */ | ||
378 | elseif(isset($_GET['menu_edit'])) | ||
379 | { | ||
380 | // récupération des données | ||
381 | $data = file_get_contents('php://input'); | ||
382 | $json = json_decode($data, true); | ||
383 | Director::$menu_data = new Menu($entityManager); | ||
384 | |||
385 | // flèche gauche <=: position = position du parent + 1, parent = grand-parent, recalculer les positions | ||
386 | if($_GET['menu_edit'] === 'move_one_level_up' && isset($json['id'])){ | ||
387 | $id = $json['id']; | ||
388 | $page = Director::$menu_data->findPageById((int)$id); | ||
389 | |||
390 | $parent = $page->getParent(); // peut être null | ||
391 | if($parent === null){ | ||
392 | // 1er niveau: ne rien faire | ||
393 | echo json_encode(['success' => false]); | ||
394 | die; | ||
395 | } | ||
396 | // BDD | ||
397 | else{ | ||
398 | $page->setPosition($parent->getPosition() + 1); // nouvelle position | ||
399 | |||
400 | // 2ème niveau: le parent devient $menu_data, puis null après tri | ||
401 | if($parent->getParent() === null){ | ||
402 | // connexion dans les deux sens | ||
403 | $page->setParent(Director::$menu_data); // => pour la persistance | ||
404 | |||
405 | //Director::$menu_data->addChild($page); // => pour sortChildren | ||
406 | $page->getParent()->addChild($page); // => pour sortChildren | ||
407 | //Director::$menu_data->sortChildren(true); // positions décaléees des nouveaux petits frères | ||
408 | $page->getParent()->sortChildren(true); // positions décaléees des nouveaux petits frères | ||
409 | $page->setParent(null); | ||
410 | |||
411 | // affichage | ||
412 | $page->setPagePath($page->getEndOfPath()); | ||
413 | $page->fillChildrenPagePath(); | ||
414 | } | ||
415 | // 3ème niveau et plus | ||
416 | else{ | ||
417 | $page->setParent($parent->getParent()); // nouveau parent | ||
418 | $page->getParent()->addChild($page); // => pour sortChildren | ||
419 | $page->getParent()->sortChildren(true); // positions décaléees des nouveaux petits frères | ||
420 | $page->fillChildrenPagePath($page->getParent()->getPagePath()); | ||
421 | } | ||
422 | //$parent->sortChildren(true); // positions des enfants restants, inutile si la fonction est récursive? | ||
423 | $entityManager->flush(); | ||
424 | |||
425 | // affichage | ||
426 | $parent->removeChild($page); | ||
427 | $nav_builder = new NavBuilder(); | ||
428 | $menu_builder = new MenuBuilder(null, false); | ||
429 | echo json_encode(['success' => true, 'nav' => $nav_builder->render(), 'menu_buttons' => $menu_builder->render()]); | ||
430 | die; | ||
431 | } | ||
432 | } | ||
433 | |||
434 | // flèche droite =>: position = nombre d'éléments de la fraterie + 1, l'élément précédent devient le parent | ||
435 | if($_GET['menu_edit'] === 'move_one_level_down' && isset($json['id'])){ | ||
436 | $id = $json['id']; | ||
437 | $page = Director::$menu_data->findPageById((int)$id); | ||
438 | |||
439 | $parent = $page->getParent(); // peut être null | ||
440 | if($parent == null){ | ||
441 | $parent = Director::$menu_data; | ||
442 | } | ||
443 | |||
444 | // BDD | ||
445 | $parent->sortChildren(true); // trie et réindexe par sécurité: 1, 2, 3... | ||
446 | if($page->getPosition() > 1){ | ||
447 | foreach($parent->getChildren() as $child){ | ||
448 | if($child->getPosition() === $page->getPosition() - 1){ | ||
449 | $page->setParent($child); | ||
450 | break; | ||
451 | } | ||
452 | } | ||
453 | $page->setPosition(count($page->getParent()->getChildren()) + 1); | ||
454 | } | ||
455 | $entityManager->flush(); | ||
456 | |||
457 | // affichage | ||
458 | $parent->removeChild($page); | ||
459 | $page->getParent()->addChild($page); | ||
460 | $page->fillChildrenPagePath($page->getParent()->getPagePath()); // variable non mappée $page_path | ||
461 | $nav_builder = new NavBuilder(); | ||
462 | $menu_builder = new MenuBuilder(null, false); | ||
463 | |||
464 | echo json_encode(['success' => true, 'nav' => $nav_builder->render(), 'menu_buttons' => $menu_builder->render()]); | ||
465 | die; | ||
466 | } | ||
467 | |||
468 | if($_GET['menu_edit'] === 'switch_positions' && isset($json['id1']) && isset($json['id2'])) | ||
469 | { | ||
470 | $id1 = $json['id1']; | ||
471 | $id2 = $json['id2']; | ||
472 | |||
473 | // vérifier qu'ils ont le même parent | ||
474 | $page1 = Director::$menu_data->findPageById((int)$id1); | ||
475 | $page2 = Director::$menu_data->findPageById((int)$id2); | ||
476 | |||
477 | // double le contrôle fait en JS | ||
478 | if($page1->getParent() === $page2->getParent()) // comparaison stricte d'objet (même instance du parent?) | ||
479 | { | ||
480 | // inversion | ||
481 | $tmp = $page1->getPosition(); | ||
482 | $page1->setPosition($page2->getPosition()); | ||
483 | $page2->setPosition($tmp); | ||
484 | Director::$menu_data->sortChildren(true); // modifie tableau children | ||
485 | $entityManager->flush(); | 87 | $entityManager->flush(); |
486 | 88 | echo json_encode(['success' => true]); | |
487 | // nouveau menu | ||
488 | $nav_builder = new NavBuilder(); | ||
489 | echo json_encode(['success' => true, 'nav' => $nav_builder->render()]); | ||
490 | } | ||
491 | else{ | ||
492 | echo json_encode(['success' => false]); | ||
493 | } | ||
494 | |||
495 | die; | ||
496 | } | ||
497 | |||
498 | if($_GET['menu_edit'] === 'displayInMenu' && isset($json['id']) && isset($json['checked'])) | ||
499 | { | ||
500 | $id = $json['id']; | ||
501 | $checked = $json['checked']; | ||
502 | |||
503 | $page = Director::$menu_data->findPageById((int)$id); | ||
504 | if($page->isHidden() === $checked){ | ||
505 | $page->setHidden(!$checked); | ||
506 | $entityManager->flush(); | ||
507 | |||
508 | // nouveau menu | ||
509 | $nav_builder = new NavBuilder(); | ||
510 | echo json_encode(['success' => true, 'nav' => $nav_builder->render()]); | ||
511 | } | 89 | } |
512 | else{ | 90 | else{ |
513 | echo json_encode(['success' => false]); | 91 | echo json_encode(['success' => false]); |
@@ -515,100 +93,4 @@ if($_SERVER['CONTENT_TYPE'] === 'application/json'){ | |||
515 | die; | 93 | die; |
516 | } | 94 | } |
517 | } | 95 | } |
518 | |||
519 | |||
520 | /* -- mode Modification d'une page -- */ | ||
521 | |||
522 | // partie "page" | ||
523 | elseif(isset($_GET['page_edit'])) | ||
524 | { | ||
525 | // récupération des données | ||
526 | $data = file_get_contents('php://input'); | ||
527 | $json = json_decode($data, true); | ||
528 | |||
529 | // titre de la page | ||
530 | if($_GET['page_edit'] === 'page_title'){ | ||
531 | $page = $entityManager->find('App\Entity\Page', $json['page_id']); | ||
532 | $page->setPageName(htmlspecialchars($json['title'])); | ||
533 | $entityManager->flush(); | ||
534 | echo json_encode(['success' => true, 'title' => $page->getPageName()]); | ||
535 | } | ||
536 | // titre en snake_case pour le menu | ||
537 | /*elseif($_GET['page_edit'] === 'page_menu_path'){ | ||
538 | $page = $entityManager->find('App\Entity\Page', $json['page_id']); | ||
539 | $page->setEndOfPath(htmlspecialchars($json['page_menu_path'])); | ||
540 | $entityManager->flush(); | ||
541 | echo json_encode(['success' => true, 'page_name_path' => $page->getEndOfPath()]); | ||
542 | }*/ | ||
543 | // description dans les métadonnées | ||
544 | elseif($_GET['page_edit'] === 'page_description'){ | ||
545 | $node_data = $entityManager->find('App\Entity\NodeData', $json['node_data_id']); | ||
546 | $node_data->updateData('description', htmlspecialchars($json['description'])); | ||
547 | $entityManager->flush(); | ||
548 | echo json_encode(['success' => true, 'description' => $node_data->getData()['description']]); | ||
549 | } | ||
550 | die; | ||
551 | } | ||
552 | |||
553 | // partie "blocs" | ||
554 | elseif(isset($_GET['bloc_edit'])) | ||
555 | { | ||
556 | // renommage d'un bloc | ||
557 | if($_GET['bloc_edit'] === 'rename_page_bloc') | ||
558 | { | ||
559 | if(isset($json['bloc_title']) && $json['bloc_title'] !== null && isset($json['bloc_id']) && is_int($json['bloc_id'])){ | ||
560 | $director = new Director($entityManager); | ||
561 | $director->findNodeById($json['bloc_id']); | ||
562 | |||
563 | // le titre (du JSON en BDD) est récupéré sous forme de tableau, modifié et renvoyé | ||
564 | $data = $director->getNode()->getNodeData()->getData(); | ||
565 | $data['title'] = htmlspecialchars($json['bloc_title']); | ||
566 | $director->getNode()->getNodeData()->updateData('title', htmlspecialchars($json['bloc_title'])); | ||
567 | |||
568 | $entityManager->flush(); | ||
569 | echo json_encode(['success' => true, 'title' => $data['title']]); | ||
570 | } | ||
571 | else{ | ||
572 | echo json_encode(['success' => false]); | ||
573 | } | ||
574 | die; | ||
575 | } | ||
576 | // inversion des positions de deux blocs | ||
577 | elseif($_GET['bloc_edit'] === 'switch_blocs_positions') | ||
578 | { | ||
579 | if(isset($json['id1']) && is_int($json['id1']) && isset($json['id2']) && is_int($json['id2']) && isset($_GET['page'])){ | ||
580 | $director = new Director($entityManager, true); | ||
581 | $director->findUniqueNodeByName('main'); | ||
582 | $director->findItsChildren(); | ||
583 | $main = $director->getNode(); | ||
584 | $main->sortChildren(true); // régénère les positions avant inversion | ||
585 | |||
586 | $bloc1; $bloc2; | ||
587 | foreach($main->getChildren() as $child){ | ||
588 | if($child->getId() === $json['id1']){ | ||
589 | $bloc1 = $child; | ||
590 | break; | ||
591 | } | ||
592 | } | ||
593 | foreach($main->getChildren() as $child){ | ||
594 | if($child->getId() === $json['id2']){ | ||
595 | $bloc2 = $child; | ||
596 | break; | ||
597 | } | ||
598 | } | ||
599 | |||
600 | // inversion | ||
601 | $tmp = $bloc1->getPosition(); | ||
602 | $bloc1->setPosition($bloc2->getPosition()); | ||
603 | $bloc2->setPosition($tmp); | ||
604 | |||
605 | $entityManager->flush(); | ||
606 | echo json_encode(['success' => true]); | ||
607 | } | ||
608 | else{ | ||
609 | echo json_encode(['success' => false]); | ||
610 | } | ||
611 | die; | ||
612 | } | ||
613 | } | ||
614 | } \ No newline at end of file | 96 | } \ No newline at end of file |