diff options
Diffstat (limited to 'src/controller/PageManagementController.php')
| -rw-r--r-- | src/controller/PageManagementController.php | 45 |
1 files changed, 31 insertions, 14 deletions
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 | |||
| 88 | 88 | ||
| 89 | static public function deletePage(EntityManager $entityManager): void | 89 | static public function deletePage(EntityManager $entityManager): void |
| 90 | { | 90 | { |
| 91 | $page = $entityManager->find('App\Entity\Page', (int)$_POST['page_id']); | 91 | $menu = new Menu($entityManager); |
| 92 | $nodes = $entityManager->getRepository('App\Entity\Node')->findBy(['page' => $page]); | 92 | $page = $menu->findPageById((int)$_POST['page_id']); |
| 93 | $data = []; | 93 | $url = new URL; |
| 94 | foreach($nodes as $node){ | 94 | |
| 95 | $data[] = $entityManager->getRepository('App\Entity\NodeData')->findOneBy(['node' => $node]); | 95 | // test dernière page |
| 96 | $entityManager->remove($node); | 96 | // => $menu a un enfant et pas de petits enfants |
| 97 | } | 97 | // => $menu->getChildren()->isEmpty() n'est théoriquement pas possible |
| 98 | foreach($data as $one_data){ | 98 | if($menu->getChildren()->isEmpty() || (count($menu->getChildren()) === 1 && $menu->getChildren()[0]->getChildren()->isEmpty())){ |
| 99 | $entityManager->remove($one_data); | 99 | $url->addParams(['page' => $page->getEndOfPath(), 'error' => 'you_tried_to_remove_the_last_page']); |
| 100 | } | 100 | } |
| 101 | $entityManager->remove($page); // suppression en BDD | 101 | else{ |
| 102 | 102 | $nodes = $entityManager->getRepository('App\Entity\Node')->findBy(['page' => $page]); | |
| 103 | $entityManager->flush(); | 103 | $data = []; |
| 104 | header("Location: " . new URL); | 104 | foreach($nodes as $node){ |
| 105 | $data[] = $entityManager->getRepository('App\Entity\NodeData')->findOneBy(['node' => $node]); | ||
| 106 | $entityManager->remove($node); | ||
| 107 | } | ||
| 108 | foreach($data as $one_data){ | ||
| 109 | $entityManager->remove($one_data); | ||
| 110 | } | ||
| 111 | $entityManager->remove($page); // suppression en BDD | ||
| 112 | $entityManager->flush(); | ||
| 113 | |||
| 114 | // destination vers la première page dispo | ||
| 115 | // traite les cas prévus dans le if plus haut | ||
| 116 | $menu->removeChild($page); | ||
| 117 | $next_page = $menu->getChildren()->isEmpty() ? $next_page = $page->getChildren()[0] : $menu->getChildren()[0]; | ||
| 118 | $url->addParams(['page' => $next_page->getEndOfPath()]); | ||
| 119 | } | ||
| 120 | |||
| 121 | header("Location: " . $url); | ||
| 105 | die; | 122 | die; |
| 106 | } | 123 | } |
| 107 | 124 | ||
