From 28d300b48ded1d472d3593f3afc0c71c4fd2e0aa Mon Sep 17 00:00:00 2001 From: polo Date: Sun, 2 Nov 2025 20:08:52 +0100 Subject: =?UTF-8?q?blocage=20suppression=20derni=C3=A8re=20page,=20page=20?= =?UTF-8?q?par=20d=C3=A9faut=20=3D=20premi=C3=A8re=20entr=C3=A9e=20du=20me?= =?UTF-8?q?nu=20et=20non=20plus=20"accueil"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controller/PageManagementController.php | 45 ++++++++++++++++++++--------- src/installation.php | 4 +-- src/model/Path.php | 23 +++++++-------- src/view/FooterBuilder.php | 6 ++-- src/view/templates/modify_page.php | 2 +- 5 files changed, 47 insertions(+), 33 deletions(-) (limited to 'src') diff --git a/src/controller/PageManagementController.php b/src/controller/PageManagementController.php index 45c6027..7078866 100644 --- a/src/controller/PageManagementController.php +++ b/src/controller/PageManagementController.php @@ -88,20 +88,37 @@ class PageManagementController static public 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); + $menu = new Menu($entityManager); + $page = $menu->findPageById((int)$_POST['page_id']); + $url = new URL; + + // test dernière page + // => $menu a un enfant et pas de petits enfants + // => $menu->getChildren()->isEmpty() n'est théoriquement pas possible + if($menu->getChildren()->isEmpty() || (count($menu->getChildren()) === 1 && $menu->getChildren()[0]->getChildren()->isEmpty())){ + $url->addParams(['page' => $page->getEndOfPath(), 'error' => 'you_tried_to_remove_the_last_page']); + } + else{ + $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(); + + // destination vers la première page dispo + // traite les cas prévus dans le if plus haut + $menu->removeChild($page); + $next_page = $menu->getChildren()->isEmpty() ? $next_page = $page->getChildren()[0] : $menu->getChildren()[0]; + $url->addParams(['page' => $next_page->getEndOfPath()]); + } + + header("Location: " . $url); die; } diff --git a/src/installation.php b/src/installation.php index b87b988..3d75449 100644 --- a/src/installation.php +++ b/src/installation.php @@ -94,10 +94,10 @@ function fillStartingDatabase(EntityManager $entityManager){ $article = new Page('Article', 'article', "", true, false, false, NULL, NULL); $connection = new Page('Connexion', 'connection', "Connexion", true, false, false, NULL, NULL); $my_account = new Page('Mon compte', 'user_edit', "Mon compte", true, false, false, NULL, NULL); - $menu_paths = new Page("Menu et chemins", 'menu_chemins', "Menu et chemins", true, false, false, NULL, NULL); + $menu_paths = new Page("Menu et chemins", 'menu_paths', "Menu et chemins", true, false, false, NULL, NULL); $menu_paths->addCSS('menu'); $menu_paths->addJS('menu'); - $new_page = new Page('Nouvelle page', 'nouvelle_page', "Nouvelle page", true, false, false, NULL, NULL); + $new_page = new Page('Nouvelle page', 'new_page', "Nouvelle page", true, false, false, NULL, NULL); $new_page->addCSS('new_page'); $new_page->addJS('new_page'); diff --git a/src/model/Path.php b/src/model/Path.php index 8ef9100..0b4bb67 100644 --- a/src/model/Path.php +++ b/src/model/Path.php @@ -25,27 +25,22 @@ class Path extends Page // produit un tableau de Page en comparant le chemin demandé avec les données dans Menu // succès => une exception est lancée pour sortir des fonctions imbriquées // echec => redirection vers la page erreur 404 - private function findPage(Page|Menu $menu, array $path_array) + private function findPage(Page|Menu $menu, array $path_array): void { // recherche dans les autres pages if($menu instanceof Menu){ - foreach($menu->getOtherPages() as $page) - { - if($path_array[0] === $page->getEndOfPath()) - { + foreach($menu->getOtherPages() as $page){ + if($path_array[0] === $page->getEndOfPath()){ $this->current_page[] = $page; throw new Exception(); } } } // recherche dans le menu - foreach($menu->getChildren() as $page) - { - if($path_array[0] === $page->getEndOfPath()) - { + foreach($menu->getChildren() as $page){ + if($path_array[0] === $page->getEndOfPath()){ $this->current_page[] = $page; - if(count($path_array) > 1) - { + if(count($path_array) > 1){ array_shift($path_array); // $this->path_array n'est pas modifié, un tableau PHP est passé à une fonction par copie $this->findPage($page, $path_array); } @@ -54,9 +49,11 @@ class Path extends Page } } } + // rien trouvé - URL::setPath('erreur404.html'); - header('Location: '. new URL); + // pas parfait, échoue si le premier enfant de "children" n'est pas au premier niveau + // est-ce que ça peut arriver? + header('Location: '. new URL(['page' => Model::$menu->getChildren()[0]->getEndOfPath()])); die; } diff --git a/src/view/FooterBuilder.php b/src/view/FooterBuilder.php index d810170..8d24f25 100644 --- a/src/view/FooterBuilder.php +++ b/src/view/FooterBuilder.php @@ -58,8 +58,8 @@ class FooterBuilder extends AbstractBuilder $mode = 'administrateur'; $div_admin = 'logged_in'; } - $link_new_page = new URL(['page' => 'nouvelle_page']); - $link_change_paths = new URL(['page' => 'menu_chemins']); + $link_new_page = new URL(['page' => 'new_page']); + $link_change_paths = new URL(['page' => 'menu_paths']); $link_change_password = new URL(['page' => 'user_edit', 'from' => CURRENT_PAGE]); isset($_GET['id']) ? $link_change_password->addParams(['id' => $_GET['id']]) : ''; @@ -100,7 +100,7 @@ class FooterBuilder extends AbstractBuilder private function makePageModifModeButton(): string { $link_edit_page = new URL(['page' => CURRENT_PAGE]); - if(!in_array(CURRENT_PAGE, ['article', 'nouvelle_page', 'menu_chemins'])) // ajouter 'user_edit' et 'connection' le jour où ces pages auront un footer + if(!in_array(CURRENT_PAGE, ['article', 'new_page', 'menu_paths'])) // ajouter 'user_edit' et 'connection' le jour où ces pages auront un footer { if(MainBuilder::$modif_mode){ $link_edit_label = 'Sortir du mode modification'; diff --git a/src/view/templates/modify_page.php b/src/view/templates/modify_page.php index ecacb02..d32179c 100644 --- a/src/view/templates/modify_page.php +++ b/src/view/templates/modify_page.php @@ -24,7 +24,7 @@
-
+ -- cgit v1.2.3