diff options
| author | polo <ordipolo@gmx.fr> | 2025-08-03 00:23:11 +0200 |
|---|---|---|
| committer | polo <ordipolo@gmx.fr> | 2025-08-03 00:23:11 +0200 |
| commit | 547d7feed68e89957f062b8ed9b988f28c5830ce (patch) | |
| tree | 7e07ea2b2065468900201cddacad5db559446a7d /src/controller/MenuAndPathsController.php | |
| parent | 9934a32f7e02c484d6b122c9af860ab1ca9b2dca (diff) | |
| download | cms-547d7feed68e89957f062b8ed9b988f28c5830ce.tar.gz cms-547d7feed68e89957f062b8ed9b988f28c5830ce.tar.bz2 cms-547d7feed68e89957f062b8ed9b988f28c5830ce.zip | |
réorganisation 3: classes controller
Diffstat (limited to 'src/controller/MenuAndPathsController.php')
| -rw-r--r-- | src/controller/MenuAndPathsController.php | 189 |
1 files changed, 189 insertions, 0 deletions
diff --git a/src/controller/MenuAndPathsController.php b/src/controller/MenuAndPathsController.php new file mode 100644 index 0000000..d429287 --- /dev/null +++ b/src/controller/MenuAndPathsController.php | |||
| @@ -0,0 +1,189 @@ | |||
| 1 | <?php | ||
| 2 | // src/controller/MenuAndPathsController.php | ||
| 3 | |||
| 4 | declare(strict_types=1); | ||
| 5 | |||
| 6 | use App\Entity\Page; | ||
| 7 | use Doctrine\ORM\EntityManager; | ||
| 8 | |||
| 9 | class MenuAndPathsController | ||
| 10 | { | ||
| 11 | static public function newUrlMenuEntry(EntityManager $entityManager): void | ||
| 12 | { | ||
| 13 | Director::$menu_data = new Menu($entityManager); | ||
| 14 | $previous_page = Director::$menu_data->findPageById((int)$_POST["location"]); // (int) à cause de declare(strict_types=1); | ||
| 15 | $parent = $previous_page->getParent(); | ||
| 16 | |||
| 17 | $page = new Page( | ||
| 18 | trim(htmlspecialchars($_POST["label_input"])), | ||
| 19 | filter_var($_POST["url_input"], FILTER_VALIDATE_URL), | ||
| 20 | true, true, false, | ||
| 21 | $previous_page->getPosition(), | ||
| 22 | $parent); // peut et DOIT être null si on est au 1er niveau | ||
| 23 | |||
| 24 | // on a donné à la nouvelle entrée la même position qu'à la précédente, | ||
| 25 | // addChild l'ajoute à la fin du tableau "children" puis on trie | ||
| 26 | // 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 | ||
| 27 | if($parent == null){ | ||
| 28 | $parent = Director::$menu_data; | ||
| 29 | } | ||
| 30 | $parent->addChild($page); // true pour réindexer les positions en BDD | ||
| 31 | $parent->reindexPositions(); | ||
| 32 | |||
| 33 | $entityManager->persist($page); | ||
| 34 | $entityManager->flush(); | ||
| 35 | header("Location: " . new URL(['page' => $_GET['from']])); | ||
| 36 | die; | ||
| 37 | } | ||
| 38 | |||
| 39 | static public function deleteUrlMenuEntry(EntityManager $entityManager): void | ||
| 40 | { | ||
| 41 | Director::$menu_data = new Menu($entityManager); | ||
| 42 | $page = Director::$menu_data->findPageById((int)$_POST["delete"]); | ||
| 43 | $parent = $page->getParent(); | ||
| 44 | if($parent == null){ | ||
| 45 | $parent = Director::$menu_data; | ||
| 46 | } | ||
| 47 | |||
| 48 | $parent->removeChild($page); // suppression de $children avant de trier | ||
| 49 | $parent->reindexPositions(); | ||
| 50 | |||
| 51 | $entityManager->remove($page); // suppression en BDD | ||
| 52 | $entityManager->flush(); | ||
| 53 | header("Location: " . new URL(['page' => $_GET['from']])); | ||
| 54 | die; | ||
| 55 | } | ||
| 56 | |||
| 57 | static public function MoveOneLevelUp(EntityManager $entityManager, array $json): void | ||
| 58 | { | ||
| 59 | $id = $json['id']; | ||
| 60 | $page = Director::$menu_data->findPageById((int)$id); | ||
| 61 | |||
| 62 | $parent = $page->getParent(); // peut être null | ||
| 63 | if($parent === null){ | ||
| 64 | // 1er niveau: ne rien faire | ||
| 65 | echo json_encode(['success' => false]); | ||
| 66 | die; | ||
| 67 | } | ||
| 68 | // BDD | ||
| 69 | else{ | ||
| 70 | $page->setPosition($parent->getPosition() + 1); // nouvelle position | ||
| 71 | |||
| 72 | // 2ème niveau: le parent devient $menu_data, puis null après tri | ||
| 73 | if($parent->getParent() === null){ | ||
| 74 | // connexion dans les deux sens | ||
| 75 | $page->setParent(Director::$menu_data); // => pour la persistance | ||
| 76 | |||
| 77 | //Director::$menu_data->addChild($page); // => pour sortChildren | ||
| 78 | $page->getParent()->addChild($page); // => pour sortChildren | ||
| 79 | //Director::$menu_data->sortChildren(true); // positions décaléees des nouveaux petits frères | ||
| 80 | $page->getParent()->sortChildren(true); // positions décaléees des nouveaux petits frères | ||
| 81 | $page->setParent(null); | ||
| 82 | |||
| 83 | // affichage | ||
| 84 | $page->setPagePath($page->getEndOfPath()); | ||
| 85 | $page->fillChildrenPagePath(); | ||
| 86 | } | ||
| 87 | // 3ème niveau et plus | ||
| 88 | else{ | ||
| 89 | $page->setParent($parent->getParent()); // nouveau parent | ||
| 90 | $page->getParent()->addChild($page); // => pour sortChildren | ||
| 91 | $page->getParent()->sortChildren(true); // positions décaléees des nouveaux petits frères | ||
| 92 | $page->fillChildrenPagePath($page->getParent()->getPagePath()); | ||
| 93 | } | ||
| 94 | //$parent->sortChildren(true); // positions des enfants restants, inutile si la fonction est récursive? | ||
| 95 | $entityManager->flush(); | ||
| 96 | |||
| 97 | // affichage | ||
| 98 | $parent->removeChild($page); | ||
| 99 | $nav_builder = new NavBuilder(); | ||
| 100 | $menu_builder = new MenuBuilder(null, false); | ||
| 101 | echo json_encode(['success' => true, 'nav' => $nav_builder->render(), 'menu_buttons' => $menu_builder->render()]); | ||
| 102 | die; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | static public function MoveOneLevelDown(EntityManager $entityManager, array $json): void | ||
| 107 | { | ||
| 108 | $id = $json['id']; | ||
| 109 | $page = Director::$menu_data->findPageById((int)$id); | ||
| 110 | |||
| 111 | $parent = $page->getParent(); // peut être null | ||
| 112 | if($parent == null){ | ||
| 113 | $parent = Director::$menu_data; | ||
| 114 | } | ||
| 115 | |||
| 116 | // BDD | ||
| 117 | $parent->sortChildren(true); // trie et réindexe par sécurité: 1, 2, 3... | ||
| 118 | if($page->getPosition() > 1){ | ||
| 119 | foreach($parent->getChildren() as $child){ | ||
| 120 | if($child->getPosition() === $page->getPosition() - 1){ | ||
| 121 | $page->setParent($child); | ||
| 122 | break; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | $page->setPosition(count($page->getParent()->getChildren()) + 1); | ||
| 126 | } | ||
| 127 | $entityManager->flush(); | ||
| 128 | |||
| 129 | // affichage | ||
| 130 | $parent->removeChild($page); | ||
| 131 | $page->getParent()->addChild($page); | ||
| 132 | $page->fillChildrenPagePath($page->getParent()->getPagePath()); // variable non mappée $page_path | ||
| 133 | $nav_builder = new NavBuilder(); | ||
| 134 | $menu_builder = new MenuBuilder(null, false); | ||
| 135 | |||
| 136 | echo json_encode(['success' => true, 'nav' => $nav_builder->render(), 'menu_buttons' => $menu_builder->render()]); | ||
| 137 | die; | ||
| 138 | } | ||
| 139 | |||
| 140 | static public function SwitchPositions(EntityManager $entityManager, array $json): void | ||
| 141 | { | ||
| 142 | $id1 = $json['id1']; | ||
| 143 | $id2 = $json['id2']; | ||
| 144 | |||
| 145 | // vérifier qu'ils ont le même parent | ||
| 146 | $page1 = Director::$menu_data->findPageById((int)$id1); | ||
| 147 | $page2 = Director::$menu_data->findPageById((int)$id2); | ||
| 148 | |||
| 149 | // double le contrôle fait en JS | ||
| 150 | if($page1->getParent() === $page2->getParent()) // comparaison stricte d'objet (même instance du parent?) | ||
| 151 | { | ||
| 152 | // inversion | ||
| 153 | $tmp = $page1->getPosition(); | ||
| 154 | $page1->setPosition($page2->getPosition()); | ||
| 155 | $page2->setPosition($tmp); | ||
| 156 | Director::$menu_data->sortChildren(true); // modifie tableau children | ||
| 157 | $entityManager->flush(); | ||
| 158 | |||
| 159 | // nouveau menu | ||
| 160 | $nav_builder = new NavBuilder(); | ||
| 161 | echo json_encode(['success' => true, 'nav' => $nav_builder->render()]); | ||
| 162 | } | ||
| 163 | else{ | ||
| 164 | echo json_encode(['success' => false]); | ||
| 165 | } | ||
| 166 | |||
| 167 | die; | ||
| 168 | } | ||
| 169 | |||
| 170 | static public function displayInMenu(EntityManager $entityManager, array $json): void | ||
| 171 | { | ||
| 172 | $id = $json['id']; | ||
| 173 | $checked = $json['checked']; | ||
| 174 | |||
| 175 | $page = Director::$menu_data->findPageById((int)$id); | ||
| 176 | if($page->isHidden() === $checked){ | ||
| 177 | $page->setHidden(!$checked); | ||
| 178 | $entityManager->flush(); | ||
| 179 | |||
| 180 | // nouveau menu | ||
| 181 | $nav_builder = new NavBuilder(); | ||
| 182 | echo json_encode(['success' => true, 'nav' => $nav_builder->render()]); | ||
| 183 | } | ||
| 184 | else{ | ||
| 185 | echo json_encode(['success' => false]); | ||
| 186 | } | ||
| 187 | die; | ||
| 188 | } | ||
| 189 | } \ No newline at end of file | ||
