From 9934a32f7e02c484d6b122c9af860ab1ca9b2dca Mon Sep 17 00:00:00 2001 From: polo Date: Sat, 2 Aug 2025 17:03:42 +0200 Subject: =?UTF-8?q?r=C3=A9organisation=202=20requ=C3=AAtes=20"form":=20s?= =?UTF-8?q?=C3=A9paration=20routage=20et=20contr=C3=B4leurs=20avec=20des?= =?UTF-8?q?=20fonctions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/index.php | 11 +- public/js/calendar_admin.js | 2 + src/controller/ajax.php | 99 ----------- src/controller/ajax_admin.php | 10 +- src/controller/ajax_email.php | 105 +++++++++++ src/controller/password.php | 3 +- src/controller/post.php | 297 -------------------------------- src/controller/post_functions_admin.php | 231 +++++++++++++++++++++++++ src/controller/post_router.php | 120 +++++++++++++ src/view/MainBuilder.php | 2 +- 10 files changed, 478 insertions(+), 402 deletions(-) delete mode 100644 src/controller/ajax.php create mode 100644 src/controller/ajax_email.php delete mode 100644 src/controller/post.php create mode 100644 src/controller/post_functions_admin.php create mode 100644 src/controller/post_router.php diff --git a/public/index.php b/public/index.php index d05e799..09af532 100644 --- a/public/index.php +++ b/public/index.php @@ -54,9 +54,18 @@ if(!empty($_GET['id'])) } /* -- contrôleurs qui traitent les POST (formulaires ou AJAX) -- */ -require '../src/controller/post.php'; +require '../src/controller/post_router.php'; + /* -- affichage d'une page -- */ +// mode modification d'une page activé +if($_SESSION['admin'] && isset($_GET['page']) && isset($_GET['action']) && $_GET['action'] === 'modif_page' + && $_GET['page'] !== 'connexion' && $_GET['page'] !== 'article' && $_GET['page'] !== 'nouvelle_page' && $_GET['page'] !== 'menu_chemins') +{ + // les contrôles de la 2è ligne devraient utiliser un tableau + MainBuilder::$modif_mode = true; +} + // contrôleur accédant au modèle $director = new Director($entityManager, true); $director->makeRootNode($id); diff --git a/public/js/calendar_admin.js b/public/js/calendar_admin.js index 90e2942..a2fb1ac 100644 --- a/public/js/calendar_admin.js +++ b/public/js/calendar_admin.js @@ -229,7 +229,9 @@ document.addEventListener('DOMContentLoaded', function(){ event_end = new Date(event_end).toISOString(); } + // contrôle date/heure de fin après le début if(event_start > event_end || (!event_all_day && event_start == event_end)){ + toastNotify("Erreur: la fin de l'évènement doit se situer après son début."); return; } diff --git a/src/controller/ajax.php b/src/controller/ajax.php deleted file mode 100644 index 16a6c1f..0000000 --- a/src/controller/ajax.php +++ /dev/null @@ -1,99 +0,0 @@ - exceptions - $mail->CharSet = 'UTF-8'; - - try{ - // Paramètres du serveur - $mail->isSMTP(); - $mail->Host = Config::$smtp_host; - $mail->SMTPAuth = true; - $mail->Port = 25; - - if($mail->SMTPAuth){ - $mail->Username = Config::$smtp_username; // e-mail - $mail->Password = Config::$smtp_password; - $mail->SMTPSecure = Config::$smtp_secure; // tls (starttls) ou ssl (smtps) - if($mail->SMTPSecure === 'tls'){ - $mail->Port = 587; - } - elseif($mail->SMTPSecure === 'ssl'){ - $mail->Port = 465; - } - } - //var_dump($mail->smtpConnect());die; // test de connexion - - // Expéditeur et destinataire - $mail->setFrom(strtolower(Config::$email_from), Config::$email_from_name); // expéditeur - $mail->addAddress(strtolower($recipient), Config::$email_dest_name); // destinataire - - // Contenu - $mail->isHTML(true); - if($true_email){ - $mail->Subject = 'Message envoyé par: ' . $name . ' (' . $email . ') depuis le site web'; - - } - else{ - $mail->Subject = "TEST d'un envoi d'e-mail depuis le site web"; - } - $mail->Body = $message; - $mail->AltBody = $message; - - $mail->send(); - return true; - } - catch(Exception $e){ - return false; - //echo "Le message n'a pas pu être envoyé. Erreur : {$mail->ErrorInfo}"; - } -} - - -// détection des requêtes envoyées avec fetch (application/json) et récupération du JSON -if($_SERVER['CONTENT_TYPE'] === 'application/json') -{ - $data = file_get_contents('php://input'); - $json = json_decode($data, true); - - if(isset($_GET['action'])) - { - /* -- bloc Formulaire -- */ - if($_GET['action'] === 'send_email'){ - $captcha_solution = (isset($_SESSION['captcha']) && is_int($_SESSION['captcha'])) ? $_SESSION['captcha'] : 0; - $captcha_try = isset($json['captcha']) ? Captcha::controlInput($json['captcha']) : 0; - - // contrôles des entrées - $name = htmlspecialchars(trim($json['name'])); - $email = strtolower(htmlspecialchars(trim($json['email']))); - $message = htmlspecialchars(trim($json['message'])); - - // destinataire = e-mail par défaut dans config.ini OU choisi par l'utilisateur - $form_data = $entityManager->find('App\Entity\NodeData', $json['id']); - $recipient = $form_data->getData()['email'] ?? Config::$email_dest; - - if($captcha_try != 0 && $captcha_solution != 0 && ($captcha_try === $captcha_solution) - && filter_var($email, FILTER_VALIDATE_EMAIL) && isset($json['hidden']) && empty($json['hidden']) - && sendEmail($recipient, true, $name, $email, $message)) - { - $db_email = new Email($email, Config::$email_dest, $message); - $entityManager->persist($db_email); - $entityManager->flush(); - echo json_encode(['success' => true]); - } - else{ - echo json_encode(['success' => false]); - } - die; - } - } -} \ No newline at end of file diff --git a/src/controller/ajax_admin.php b/src/controller/ajax_admin.php index 2318ac1..b69be77 100644 --- a/src/controller/ajax_admin.php +++ b/src/controller/ajax_admin.php @@ -59,9 +59,7 @@ function curlDownloadImage(string $url, $maxRetries = 3, $timeout = 10): string| return false; // échec après trois tentatives } - -// détection des requêtes d'upload d'image de tinymce -if(strpos($_SERVER['CONTENT_TYPE'], 'multipart/form-data') !== false && isset($_GET['action']) && $_GET['action'] === 'upload_image') +function imageUploadTinyMce(): void { if(isset($_FILES['file'])){ $file = $_FILES['file']; @@ -101,6 +99,12 @@ if(strpos($_SERVER['CONTENT_TYPE'], 'multipart/form-data') !== false && isset($_ } die; } + +// détection des requêtes d'upload d'image de tinymce +if(strpos($_SERVER['CONTENT_TYPE'], 'multipart/form-data') !== false && isset($_GET['action']) && $_GET['action'] === 'upload_image') +{ + imageUploadTinyMce(); +} // 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 elseif(isset($_GET['action']) && $_GET['action'] == 'upload_image_url') { diff --git a/src/controller/ajax_email.php b/src/controller/ajax_email.php new file mode 100644 index 0000000..1138e04 --- /dev/null +++ b/src/controller/ajax_email.php @@ -0,0 +1,105 @@ + exceptions + $mail->CharSet = 'UTF-8'; + + try{ + // Paramètres du serveur + $mail->isSMTP(); + $mail->Host = Config::$smtp_host; + $mail->SMTPAuth = true; + $mail->Port = 25; + + if($mail->SMTPAuth){ + $mail->Username = Config::$smtp_username; // e-mail + $mail->Password = Config::$smtp_password; + $mail->SMTPSecure = Config::$smtp_secure; // tls (starttls) ou ssl (smtps) + if($mail->SMTPSecure === 'tls'){ + $mail->Port = 587; + } + elseif($mail->SMTPSecure === 'ssl'){ + $mail->Port = 465; + } + } + //var_dump($mail->smtpConnect());die; // test de connexion + + // Expéditeur et destinataire + $mail->setFrom(strtolower(Config::$email_from), Config::$email_from_name); // expéditeur + $mail->addAddress(strtolower($recipient), Config::$email_dest_name); // destinataire + + // Contenu + $mail->isHTML(true); + if($true_email){ + $mail->Subject = 'Message envoyé par: ' . $name . ' (' . $email . ') depuis le site web'; + + } + else{ + $mail->Subject = "TEST d'un envoi d'e-mail depuis le site web"; + } + $mail->Body = $message; + $mail->AltBody = $message; + + $mail->send(); + return true; + } + catch(Exception $e){ + return false; + //echo "Le message n'a pas pu être envoyé. Erreur : {$mail->ErrorInfo}"; + } +} + +function submitEmail(array $json, EntityManager $entityManager): void +{ + $captcha_solution = (isset($_SESSION['captcha']) && is_int($_SESSION['captcha'])) ? $_SESSION['captcha'] : 0; + $captcha_try = isset($json['captcha']) ? Captcha::controlInput($json['captcha']) : 0; + + // contrôles des entrées + $name = htmlspecialchars(trim($json['name'])); + $email = strtolower(htmlspecialchars(trim($json['email']))); + $message = htmlspecialchars(trim($json['message'])); + + // destinataire = e-mail par défaut dans config.ini OU choisi par l'utilisateur + $form_data = $entityManager->find('App\Entity\NodeData', $json['id']); + $recipient = $form_data->getData()['email'] ?? Config::$email_dest; + + if($captcha_try != 0 && $captcha_solution != 0 && ($captcha_try === $captcha_solution) + && filter_var($email, FILTER_VALIDATE_EMAIL) && isset($json['hidden']) && empty($json['hidden']) + && sendEmail($recipient, true, $name, $email, $message)) + { + $db_email = new Email($email, Config::$email_dest, $message); + $entityManager->persist($db_email); + $entityManager->flush(); + echo json_encode(['success' => true]); + } + else{ + echo json_encode(['success' => false]); + } + die; +} + + +// détection des requêtes envoyées avec fetch (application/json) et récupération du JSON +if($_SERVER['CONTENT_TYPE'] === 'application/json') +{ + $data = file_get_contents('php://input'); + $json = json_decode($data, true); + + if(isset($_GET['action'])) + { + // formulaire de contact + if($_GET['action'] === 'send_email'){ + submitEmail($json, $entityManager); + } + } +} \ No newline at end of file diff --git a/src/controller/password.php b/src/controller/password.php index 62b1542..e91cc16 100644 --- a/src/controller/password.php +++ b/src/controller/password.php @@ -213,7 +213,7 @@ function connect(LoginBuilder $builder, EntityManager $entityManager) } -function changePassword(EntityManager $entityManager) +function changePassword(EntityManager $entityManager): void { // fonction exécutée à priori deux fois d'affilée: affichage puis traitement de la saisie @@ -284,6 +284,7 @@ function changePassword(EntityManager $entityManager) { $success = false; echo($alertJSNewPassword); + //header("Location: " . new URL(['page' => $_GET['from']])); // choisir "location" entre PHP et JS die; } echo($formulaireModifMDP); diff --git a/src/controller/post.php b/src/controller/post.php deleted file mode 100644 index 5d9500b..0000000 --- a/src/controller/post.php +++ /dev/null @@ -1,297 +0,0 @@ -findPageById((int)$_POST["page_location"]); // (int) à cause de declare(strict_types=1); - $parent = $previous_page->getParent(); - - $page = new Page( - trim(htmlspecialchars($_POST["page_name"])), - trim(htmlspecialchars($_POST["page_name_path"])), - true, true, false, - $previous_page->getPosition(), - $parent); // peut et DOIT être null si on est au 1er niveau - - // on a donné à la nouvelle entrée la même position qu'à la précédente, - // addChild l'ajoute à la fin du tableau "children" puis on trie - // 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 - if($parent == null){ - $parent = Director::$menu_data; - } - $parent->addChild($page); - $parent->reindexPositions(); - - $page->setPagePath(ltrim($parent->getPagePath() . '/' . $page->getEndOfPath(), '/')); - - // noeud "head" - $node = new Node( - 'head', - null, [], - 1, // position d'un head = 1 - null, // pas de parent - $page); - $node->useDefaultAttributes(); // fichiers CSS et JS - - $data = new NodeData([ - // pas de titre, il est dans $page - 'description' => trim(htmlspecialchars($_POST["page_description"]))], - $node); - - $bulk_data = $entityManager - ->createQuery('SELECT n FROM App\Entity\Image n WHERE n.file_name LIKE :name') - ->setParameter('name', '%favicon%') - ->getResult(); - $data->setImages(new ArrayCollection($bulk_data)); - - $entityManager->persist($page); - $entityManager->persist($node); - $entityManager->persist($data); - $entityManager->flush(); - - // page créée, direction la page en mode modification pour ajouter des blocs - header("Location: " . new URL(['page' => $page->getPagePath(), 'action' => 'modif_page'])); - die; - } - - /* -- suppression d'une page -- */ - elseif(isset($_POST['page_id']) && $_POST['page_id'] !== null - && isset($_POST['submit_hidden']) && $_POST['submit_hidden'] === '') - { - $page = $entityManager->find('App\Entity\Page', (int)$_POST['page_id']); - $nodes = $entityManager->getRepository('App\Entity\Node')->findBy(['page' => $page]); - $data = []; - foreach($nodes as $node){ - $data[] = $entityManager->getRepository('App\Entity\NodeData')->findOneBy(['node' => $node]); - $entityManager->remove($node); - } - foreach($data as $one_data){ - $entityManager->remove($one_data); - } - $entityManager->remove($page); // suppression en BDD - - $entityManager->flush(); - header("Location: " . new URL); - die; - } - - - /* -- mode Modification d'une page -- */ - - // modification des titres, chemins et descriptions - elseif(isset($_POST['page_menu_path']) && $_POST['page_menu_path'] !== null - && isset($_POST['page_id']) && $_POST['page_id'] !== null - && isset($_POST['page_name_path_hidden']) && $_POST['page_name_path_hidden'] === '') - { - $director = new Director($entityManager, true); - $page = Director::$page_path->getLast(); - $path = htmlspecialchars($_POST['page_menu_path']); - - // mise en snake_case: filtre caractères non-alphanumériques, minuscule, doublons d'underscore, trim des underscores - $path = trim(preg_replace('/_+/', '_', strtolower(preg_replace('/[^a-zA-Z0-9]/', '_', $path))), '_'); - $page->setEndOfPath($path); - foreach(Director::$menu_data->getChildren() as $child){ - if($child->getEndOfPath() === Director::$page_path->getArray()[0]->getEndOfPath()){ - $child->fillChildrenPagePath(); // MAJ de $page_path - } - } - $entityManager->flush(); - header("Location: " . new URL(['page' => $page->getPagePath(), 'action' => 'modif_page'])); - die; - } - // ajout d'un bloc dans une page - elseif(isset($_POST['bloc_title']) && $_POST['bloc_title'] !== null - && isset($_POST['bloc_select']) && $_POST['bloc_select'] !== null - && isset($_POST['bloc_title_hidden']) && $_POST['bloc_title_hidden'] === '') // contrôle anti-robot avec input hidden - { - $director = new Director($entityManager, true); // on a besoin de page_path qui dépend de menu_data - $page = Director::$page_path->getLast(); - $director->findUniqueNodeByName('main'); - $director->findItsChildren(); - $main = $director->getNode(); - $position = count($main->getChildren()) + 1; // position dans la fraterie - - $blocks = ['blog', 'grid', 'calendar', 'galery', 'form']; // même liste dans FormBuilder.php - if(!in_array($_POST["bloc_select"], $blocks, true)) // 3è param: contrôle du type - { - header("Location: " . new URL(['page' => $_GET['page'], 'error' => 'bad_bloc_type'])); - die; - } - - if($_POST["bloc_select"] === 'calendar' || $_POST["bloc_select"] === 'form'){ - $dql = 'SELECT n FROM App\Entity\Node n WHERE n.page = :page AND n.name_node = :name'; // noeud 'head' de la page - $bulk_data = $entityManager - ->createQuery($dql) - ->setParameter('page', $page) - ->setParameter('name', 'head') - ->getResult(); - - if(count($bulk_data) != 1){ // 1 head par page - header("Location: " . new URL(['page' => $_GET['page'], 'error' => 'head_node_not_found'])); - die; - } - - $bulk_data[0]->addAttribute('css_array', $_POST["bloc_select"]); - if($_POST["bloc_select"] === 'form'){ - $bulk_data[0]->addAttribute('js_array', $_POST["bloc_select"]); - } - $entityManager->persist($bulk_data[0]); - } - - $bloc = new Node( - $_POST["bloc_select"], - null, [], - $position, - $main, - $page); - $data = new NodeData( - ['title' => trim(htmlspecialchars($_POST["bloc_title"]))], - $bloc); - - $entityManager->persist($bloc); - $entityManager->persist($data); - $entityManager->flush(); - header("Location: " . new URL(['page' => $_GET['page'], 'action' => 'modif_page'])); - die; - } - // suppression d'un bloc de page - elseif(isset($_POST['delete_bloc_id']) && $_POST['delete_bloc_id'] !== null - && isset($_POST['delete_bloc_hidden']) && $_POST['delete_bloc_hidden'] === '') // contrôle anti-robot avec input hidden - { - $director = new Director($entityManager, true); - $director->findUniqueNodeByName('main'); - $director->findItsChildren(); - //$director->findNodeById((int)$_POST['delete_bloc_id']); - $main = $director->getNode(); - $bloc; - foreach($main->getChildren() as $child){ - if($child->getId() === (int)$_POST['delete_bloc_id']){ - $bloc = $child; - break; - } - } - $main->removeChild($bloc); // réindex le tableau $children au passage - $main->reindexPositions(); - - $entityManager->remove($bloc); // suppression en BDD - $entityManager->flush(); - header("Location: " . new URL(['page' => $_GET['page'], 'action' => 'modif_page'])); - die; - } - - - /* -- page Menu et chemins -- */ - - // création d'une entrée de menu avec une URL - elseif(isset($_POST["label_input"]) && isset($_POST["url_input"]) && isset($_POST["location"])){ - Director::$menu_data = new Menu($entityManager); - $previous_page = Director::$menu_data->findPageById((int)$_POST["location"]); // (int) à cause de declare(strict_types=1); - $parent = $previous_page->getParent(); - - $page = new Page( - trim(htmlspecialchars($_POST["label_input"])), - filter_var($_POST["url_input"], FILTER_VALIDATE_URL), - true, true, false, - $previous_page->getPosition(), - $parent); // peut et DOIT être null si on est au 1er niveau - - // on a donné à la nouvelle entrée la même position qu'à la précédente, - // addChild l'ajoute à la fin du tableau "children" puis on trie - // 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 - if($parent == null){ - $parent = Director::$menu_data; - } - $parent->addChild($page); // true pour réindexer les positions en BDD - $parent->reindexPositions(); - - $entityManager->persist($page); - $entityManager->flush(); - header("Location: " . new URL(['page' => $_GET['from']])); - die; - } - // suppression d'une entrée de menu avec une URL - elseif(isset($_POST['delete']) && isset($_POST['x']) && isset($_POST['y'])){ // 2 params x et y sont là parce qu'on a cliqué sur une image - Director::$menu_data = new Menu($entityManager); - $page = Director::$menu_data->findPageById((int)$_POST["delete"]); - $parent = $page->getParent(); - if($parent == null){ - $parent = Director::$menu_data; - } - - $parent->removeChild($page); // suppression de $children avant de trier - $parent->reindexPositions(); - - $entityManager->remove($page); // suppression en BDD - $entityManager->flush(); - header("Location: " . new URL(['page' => $_GET['from']])); - die; - } - elseif(isset($_GET['action']) && $_GET['action'] === 'modif_mdp' - && isset($_POST['login']) && isset($_POST['old_password']) && isset($_POST['new_password']) - && isset($_POST['modify_password_hidden']) && empty($_POST['modify_password_hidden'])) - { - changePassword($entityManager); - header("Location: " . new URL(['page' => $_GET['from']])); - die; - } - else{ - header("Location: " . new URL(['error' => 'paramètres inconnus'])); - die; - } - } - - /* -- requêtes AJAX -- */ - else{ - require '../src/controller/ajax_admin.php'; - } - - require '../src/controller/ajax_calendar_admin.php'; - } -} -elseif($_SERVER['REQUEST_METHOD'] === 'GET'){ - require '../src/controller/ajax_calendar_visitor.php'; // fullcalendar utilise un GET pour récupérer les données -} \ No newline at end of file diff --git a/src/controller/post_functions_admin.php b/src/controller/post_functions_admin.php new file mode 100644 index 0000000..b47850a --- /dev/null +++ b/src/controller/post_functions_admin.php @@ -0,0 +1,231 @@ +findPageById((int)$_POST["page_location"]); // (int) à cause de declare(strict_types=1); + $parent = $previous_page->getParent(); + + $page = new Page( + trim(htmlspecialchars($_POST["page_name"])), + trim(htmlspecialchars($_POST["page_name_path"])), + true, true, false, + $previous_page->getPosition(), + $parent); // peut et DOIT être null si on est au 1er niveau + + // on a donné à la nouvelle entrée la même position qu'à la précédente, + // addChild l'ajoute à la fin du tableau "children" puis on trie + // 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 + if($parent == null){ + $parent = Director::$menu_data; + } + $parent->addChild($page); + $parent->reindexPositions(); + + $page->setPagePath(ltrim($parent->getPagePath() . '/' . $page->getEndOfPath(), '/')); + + // noeud "head" + $node = new Node( + 'head', + null, [], + 1, // position d'un head = 1 + null, // pas de parent + $page); + $node->useDefaultAttributes(); // fichiers CSS et JS + + $data = new NodeData([ + // pas de titre, il est dans $page + 'description' => trim(htmlspecialchars($_POST["page_description"]))], + $node); + + $bulk_data = $entityManager + ->createQuery('SELECT n FROM App\Entity\Image n WHERE n.file_name LIKE :name') + ->setParameter('name', '%favicon%') + ->getResult(); + $data->setImages(new ArrayCollection($bulk_data)); + + $entityManager->persist($page); + $entityManager->persist($node); + $entityManager->persist($data); + $entityManager->flush(); + + // page créée, direction la page en mode modification pour ajouter des blocs + header("Location: " . new URL(['page' => $page->getPagePath(), 'action' => 'modif_page'])); + die; +} + +function deletePage(EntityManager $entityManager): void +{ + $page = $entityManager->find('App\Entity\Page', (int)$_POST['page_id']); + $nodes = $entityManager->getRepository('App\Entity\Node')->findBy(['page' => $page]); + $data = []; + foreach($nodes as $node){ + $data[] = $entityManager->getRepository('App\Entity\NodeData')->findOneBy(['node' => $node]); + $entityManager->remove($node); + } + foreach($data as $one_data){ + $entityManager->remove($one_data); + } + $entityManager->remove($page); // suppression en BDD + + $entityManager->flush(); + header("Location: " . new URL); + die; +} + +function pageMenuPathUpdate(EntityManager $entityManager): void +{ + $director = new Director($entityManager, true); + $page = Director::$page_path->getLast(); + $path = htmlspecialchars($_POST['page_menu_path']); + + // mise en snake_case: filtre caractères non-alphanumériques, minuscule, doublons d'underscore, trim des underscores + $path = trim(preg_replace('/_+/', '_', strtolower(preg_replace('/[^a-zA-Z0-9]/', '_', $path))), '_'); + $page->setEndOfPath($path); + foreach(Director::$menu_data->getChildren() as $child){ + if($child->getEndOfPath() === Director::$page_path->getArray()[0]->getEndOfPath()){ + $child->fillChildrenPagePath(); // MAJ de $page_path + } + } + $entityManager->flush(); + header("Location: " . new URL(['page' => $page->getPagePath(), 'action' => 'modif_page'])); + die; +} + +function addBloc(EntityManager $entityManager): void +{ + $director = new Director($entityManager, true); // on a besoin de page_path qui dépend de menu_data + $page = Director::$page_path->getLast(); + $director->findUniqueNodeByName('main'); + $director->findItsChildren(); + $main = $director->getNode(); + $position = count($main->getChildren()) + 1; // position dans la fraterie + + $blocks = ['blog', 'grid', 'calendar', 'galery', 'form']; // même liste dans FormBuilder.php + if(!in_array($_POST["bloc_select"], $blocks, true)) // 3è param: contrôle du type + { + header("Location: " . new URL(['page' => $_GET['page'], 'error' => 'bad_bloc_type'])); + die; + } + + if($_POST["bloc_select"] === 'calendar' || $_POST["bloc_select"] === 'form'){ + $dql = 'SELECT n FROM App\Entity\Node n WHERE n.page = :page AND n.name_node = :name'; // noeud 'head' de la page + $bulk_data = $entityManager + ->createQuery($dql) + ->setParameter('page', $page) + ->setParameter('name', 'head') + ->getResult(); + + if(count($bulk_data) != 1){ // 1 head par page + header("Location: " . new URL(['page' => $_GET['page'], 'error' => 'head_node_not_found'])); + die; + } + + $bulk_data[0]->addAttribute('css_array', $_POST["bloc_select"]); + if($_POST["bloc_select"] === 'form'){ + $bulk_data[0]->addAttribute('js_array', $_POST["bloc_select"]); + } + $entityManager->persist($bulk_data[0]); + } + + $bloc = new Node( + $_POST["bloc_select"], + null, [], + $position, + $main, + $page); + $data = new NodeData( + ['title' => trim(htmlspecialchars($_POST["bloc_title"]))], + $bloc); + + $entityManager->persist($bloc); + $entityManager->persist($data); + $entityManager->flush(); + header("Location: " . new URL(['page' => $_GET['page'], 'action' => 'modif_page'])); + die; +} + +function deleteBloc(EntityManager $entityManager): void +{ + $director = new Director($entityManager, true); + $director->findUniqueNodeByName('main'); + $director->findItsChildren(); + //$director->findNodeById((int)$_POST['delete_bloc_id']); + $main = $director->getNode(); + $bloc = null; + foreach($main->getChildren() as $child){ + if($child->getId() === (int)$_POST['delete_bloc_id']){ + $bloc = $child; + break; + } + } + if(!empty($bloc)){ // si $bloc est null c'est que le HTML a été modifié volontairement + $main->removeChild($bloc); // réindex le tableau $children au passage + $main->reindexPositions(); + $entityManager->remove($bloc); // suppression en BDD + $entityManager->flush(); + } + header("Location: " . new URL(['page' => $_GET['page'], 'action' => 'modif_page'])); + die; +} + +function newUrlMenuEntry(EntityManager $entityManager): void +{ + Director::$menu_data = new Menu($entityManager); + $previous_page = Director::$menu_data->findPageById((int)$_POST["location"]); // (int) à cause de declare(strict_types=1); + $parent = $previous_page->getParent(); + + $page = new Page( + trim(htmlspecialchars($_POST["label_input"])), + filter_var($_POST["url_input"], FILTER_VALIDATE_URL), + true, true, false, + $previous_page->getPosition(), + $parent); // peut et DOIT être null si on est au 1er niveau + + // on a donné à la nouvelle entrée la même position qu'à la précédente, + // addChild l'ajoute à la fin du tableau "children" puis on trie + // 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 + if($parent == null){ + $parent = Director::$menu_data; + } + $parent->addChild($page); // true pour réindexer les positions en BDD + $parent->reindexPositions(); + + $entityManager->persist($page); + $entityManager->flush(); + header("Location: " . new URL(['page' => $_GET['from']])); + die; +} + +function deleteUrlMenuEntry(EntityManager $entityManager): void +{ + Director::$menu_data = new Menu($entityManager); + $page = Director::$menu_data->findPageById((int)$_POST["delete"]); + $parent = $page->getParent(); + if($parent == null){ + $parent = Director::$menu_data; + } + + $parent->removeChild($page); // suppression de $children avant de trier + $parent->reindexPositions(); + + $entityManager->remove($page); // suppression en BDD + $entityManager->flush(); + header("Location: " . new URL(['page' => $_GET['from']])); + die; +} \ No newline at end of file diff --git a/src/controller/post_router.php b/src/controller/post_router.php new file mode 100644 index 0000000..e71852a --- /dev/null +++ b/src/controller/post_router.php @@ -0,0 +1,120 @@ + 'paramètres inconnus'])); + die; + } + } + + /* -- requêtes AJAX -- */ + else{ + require '../src/controller/ajax_admin.php'; + require '../src/controller/ajax_calendar_admin.php'; + } + } +} +// cas particulier d'un GET ajax non-admin par fullcalendar +elseif($_SERVER['REQUEST_METHOD'] === 'GET'){ + // non-admin + require '../src/controller/ajax_calendar_visitor.php'; + + if($_SESSION['admin'] === true){ + // ... + } +} \ No newline at end of file diff --git a/src/view/MainBuilder.php b/src/view/MainBuilder.php index 4bd79c3..7f3a0f0 100644 --- a/src/view/MainBuilder.php +++ b/src/view/MainBuilder.php @@ -107,7 +107,7 @@ class MainBuilder extends AbstractBuilder $bloc_edit .= '
-

+

'. "\n"; } -- cgit v1.2.3