diff options
| author | polo <ordipolo@gmx.fr> | 2026-10-23 00:47:32 +0200 |
|---|---|---|
| committer | polo <ordipolo@gmx.fr> | 2026-10-23 00:47:32 +0200 |
| commit | f27cca08ff786ceff1870147f6d6bad14d71117f (patch) | |
| tree | d36597a72025313d6faf4ed228755fa267ebe2f6 /src/controller/PageManagementController.php | |
| parent | c7309f900ef05bd5cb313110ec159b761af394ec (diff) | |
| download | cms-f27cca08ff786ceff1870147f6d6bad14d71117f.tar.gz cms-f27cca08ff786ceff1870147f6d6bad14d71117f.tar.bz2 cms-f27cca08ff786ceff1870147f6d6bad14d71117f.zip | |
dossiers avec majuscule, namespace pour toutes les classes, autoload psr-4, UserDataService::createZip déplacé dans Backup
Diffstat (limited to 'src/controller/PageManagementController.php')
| -rw-r--r-- | src/controller/PageManagementController.php | 362 |
1 files changed, 0 insertions, 362 deletions
diff --git a/src/controller/PageManagementController.php b/src/controller/PageManagementController.php deleted file mode 100644 index 11aab09..0000000 --- a/src/controller/PageManagementController.php +++ /dev/null | |||
| @@ -1,362 +0,0 @@ | |||
| 1 | <?php | ||
| 2 | // src/controller/PageManagementController.php | ||
| 3 | |||
| 4 | declare(strict_types=1); | ||
| 5 | |||
| 6 | use App\Entity\Page; | ||
| 7 | use App\Entity\Node; | ||
| 8 | use App\Entity\NodeData; | ||
| 9 | use App\Entity\EmailForm; | ||
| 10 | use App\Entity\Presentation; | ||
| 11 | //use App\Entity\Image; | ||
| 12 | use Doctrine\ORM\EntityManager; | ||
| 13 | use Symfony\Component\HttpFoundation\InputBag; | ||
| 14 | use Symfony\Component\HttpFoundation\JsonResponse; | ||
| 15 | use Symfony\Component\HttpFoundation\RedirectResponse; | ||
| 16 | use Symfony\Component\HttpFoundation\Request; | ||
| 17 | |||
| 18 | class PageManagementController | ||
| 19 | { | ||
| 20 | /* -- partie page -- */ | ||
| 21 | static public function setPageTitle(EntityManager $entityManager, array $json): JsonResponse | ||
| 22 | { | ||
| 23 | $page = $entityManager->find('App\Entity\Page', $json['page_id']); | ||
| 24 | $page->setPageName(htmlspecialchars($json['title'])); | ||
| 25 | $entityManager->flush(); | ||
| 26 | return new JsonResponse(['success' => true, 'title' => $page->getPageName()]); | ||
| 27 | } | ||
| 28 | |||
| 29 | static public function updatePageMenuPath(EntityManager $entityManager, string $page_menu_path): RedirectResponse | ||
| 30 | { | ||
| 31 | Model::$menu = new Menu($entityManager); | ||
| 32 | Model::$page_path = new Path(); | ||
| 33 | $page = Model::$page_path->getLast(); | ||
| 34 | $path = htmlspecialchars($page_menu_path); | ||
| 35 | |||
| 36 | // mise en snake_case: filtre caractères non-alphanumériques, minuscule, doublons d'underscore, trim des underscores | ||
| 37 | $path = trim(preg_replace('/_+/', '_', strtolower(preg_replace('/[^a-zA-Z0-9]/', '_', $path))), '_'); | ||
| 38 | $page->setEndOfPath($path); | ||
| 39 | foreach(Model::$menu->getChildren() as $child){ | ||
| 40 | if($child->getEndOfPath() === Model::$page_path->getArray()[0]->getEndOfPath()){ | ||
| 41 | $child->fillChildrenPagePath(); // MAJ de $page_path | ||
| 42 | } | ||
| 43 | } | ||
| 44 | $entityManager->flush(); | ||
| 45 | return new RedirectResponse((string)new URL(['page' => $page->getPagePath(), 'mode' => 'page_modif'])); | ||
| 46 | } | ||
| 47 | |||
| 48 | static public function setPageDescription(EntityManager $entityManager, array $json): JsonResponse | ||
| 49 | { | ||
| 50 | $page = $entityManager->find('App\Entity\Page', $json['page_id']); | ||
| 51 | $page->setDescription(htmlspecialchars($json['description'])); | ||
| 52 | $entityManager->flush(); | ||
| 53 | return new JsonResponse(['success' => true, 'description' => $page->getDescription()]); | ||
| 54 | } | ||
| 55 | |||
| 56 | static public function newPage(EntityManager $entityManager, InputBag $post): RedirectResponse | ||
| 57 | { | ||
| 58 | // titre et chemin | ||
| 59 | Model::$menu = new Menu($entityManager); | ||
| 60 | $previous_page = Model::$menu->findPageById((int)$post->get("page_location")); // (int) à cause de declare(strict_types=1); | ||
| 61 | $parent = $previous_page->getParent(); | ||
| 62 | |||
| 63 | $page = new Page( | ||
| 64 | trim(htmlspecialchars($post->get("page_name"))), | ||
| 65 | trim(htmlspecialchars($post->get("page_name_path"))), | ||
| 66 | trim(htmlspecialchars($post->get("page_description"))), | ||
| 67 | true, true, false, | ||
| 68 | $previous_page->getPosition(), | ||
| 69 | $parent); // peut et DOIT être null si on est au 1er niveau | ||
| 70 | |||
| 71 | // on a donné à la nouvelle entrée la même position qu'à la précédente, | ||
| 72 | // addChild l'ajoute à la fin du tableau "children" puis on trie | ||
| 73 | // 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 | ||
| 74 | if($parent == null){ | ||
| 75 | $parent = Model::$menu; | ||
| 76 | } | ||
| 77 | $parent->addChild($page); | ||
| 78 | $parent->reindexPositions(); | ||
| 79 | |||
| 80 | $page->setPagePath(ltrim($parent->getPagePath() . '/' . $page->getEndOfPath(), '/')); | ||
| 81 | |||
| 82 | $entityManager->persist($page); | ||
| 83 | $entityManager->flush(); | ||
| 84 | |||
| 85 | // page créée, direction la page en mode modification pour ajouter des blocs | ||
| 86 | return new RedirectResponse((string)new URL(['page' => $page->getPagePath(), 'mode' => 'page_modif'])); | ||
| 87 | } | ||
| 88 | |||
| 89 | static public function deletePage(EntityManager $entityManager, string $page_id): RedirectResponse | ||
| 90 | { | ||
| 91 | $menu = new Menu($entityManager); | ||
| 92 | $page = $menu->findPageById((int)$page_id); | ||
| 93 | $url = new URL; | ||
| 94 | |||
| 95 | // test dernière page | ||
| 96 | // => $menu a un enfant et pas de petits enfants | ||
| 97 | // => $menu->getChildren()->isEmpty() n'est théoriquement pas possible | ||
| 98 | if($menu->getChildren()->isEmpty() || (count($menu->getChildren()) === 1 && $menu->getChildren()[0]->getChildren()->isEmpty())){ | ||
| 99 | $url->addParams(['page' => $page->getEndOfPath(), 'error' => 'you_tried_to_remove_the_last_page']); | ||
| 100 | } | ||
| 101 | else{ | ||
| 102 | $nodes = $entityManager->getRepository('App\Entity\Node')->findBy(['page' => $page]); | ||
| 103 | $data = []; | ||
| 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 | return new RedirectResponse((string)$url); | ||
| 122 | } | ||
| 123 | |||
| 124 | /* partie "blocs" */ | ||
| 125 | static public function addBloc(EntityManager $entityManager, Request $request): RedirectResponse | ||
| 126 | { | ||
| 127 | $model = new Model($entityManager); | ||
| 128 | $model->makeMenuAndPaths(); // on a besoin de page_path qui dépend de menu | ||
| 129 | $page = Model::$page_path->getLast(); | ||
| 130 | $model->findUniqueNodeByName('main'); | ||
| 131 | $model->findItsChildren(); | ||
| 132 | $main = $model->getNode(); | ||
| 133 | $position = count($main->getChildren()) + 1; // position dans la fraterie | ||
| 134 | |||
| 135 | if(!in_array($request->request->get("bloc_select"), array_keys(Presentation::BLOCKS), true)){ // 3è param: contrôle du type | ||
| 136 | // utiliser une flash error | ||
| 137 | return new RedirectResponse((string)new URL(['page' => $request->query->get('page'), 'error' => 'bad_bloc_type'])); | ||
| 138 | } | ||
| 139 | |||
| 140 | if(in_array($request->request->get("bloc_select"), ['calendar', 'form'])){ | ||
| 141 | $page->addCSS($request->request->get("bloc_select")); | ||
| 142 | if($request->request->get("bloc_select") === 'form'){ | ||
| 143 | $page->addJS($request->request->get("bloc_select")); | ||
| 144 | } | ||
| 145 | $entityManager->persist($page); | ||
| 146 | } | ||
| 147 | |||
| 148 | $block = new Node($request->request->get("bloc_select"), $position, $main, $page); | ||
| 149 | |||
| 150 | $DataClass = match($request->request->get("bloc_select")){ | ||
| 151 | 'form' => EmailForm::class, | ||
| 152 | 'post_block', 'news_block' => Presentation::class, | ||
| 153 | 'calendar' => NodeData::class, | ||
| 154 | default => throw new InvalidArgumentException("Erreur: type de bloc inconnu"), | ||
| 155 | }; | ||
| 156 | $data = new $DataClass(['title' => trim(htmlspecialchars($request->request->get("bloc_title")))], $block); | ||
| 157 | |||
| 158 | // valeurs par défaut | ||
| 159 | if($request->request->get("bloc_select") === 'post_block'){ | ||
| 160 | $data->setPresentation('fullwidth'); | ||
| 161 | } | ||
| 162 | elseif($request->request->get("bloc_select") === 'news_block'){ | ||
| 163 | $data->setPresentation('grid'); | ||
| 164 | } | ||
| 165 | elseif($request->request->get("bloc_select") === 'galery'){ | ||
| 166 | $data->setPresentation('mosaic'); // un jour on mettra carousel | ||
| 167 | } | ||
| 168 | // else = null par défaut | ||
| 169 | |||
| 170 | $entityManager->persist($block); | ||
| 171 | $entityManager->persist($data); | ||
| 172 | $entityManager->flush(); | ||
| 173 | return new RedirectResponse((string)new URL(['page' => $request->query->get('page'), 'mode' => 'page_modif'])); | ||
| 174 | } | ||
| 175 | |||
| 176 | static public function deleteBloc(EntityManager $entityManager, Request $request): RedirectResponse | ||
| 177 | { | ||
| 178 | $model = new Model($entityManager); | ||
| 179 | $model->makeMenuAndPaths(); | ||
| 180 | $model->findUniqueNodeByName('main'); | ||
| 181 | $model->findItsChildren(); | ||
| 182 | $main = $model->getNode(); | ||
| 183 | |||
| 184 | $block = null; | ||
| 185 | $type = ''; | ||
| 186 | $nb_same_type = 0; | ||
| 187 | foreach($main->getChildren() as $child){ | ||
| 188 | if($child->getId() === (int)$request->request->get('delete_bloc_id')){ | ||
| 189 | $block = $child; | ||
| 190 | $type = $block->getName(); | ||
| 191 | } | ||
| 192 | if($child->getName() === $type){ | ||
| 193 | $nb_same_type++; | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | // nettoyage fichiers CSS et JS si on retire le derner bloc de ce type | ||
| 198 | if($nb_same_type === 1 && in_array($block->getName(), ['calendar', 'form'])){ | ||
| 199 | $page = $block->getPage(); | ||
| 200 | $page->removeCSS($block->getName()); | ||
| 201 | if($block->getName() === 'form'){ | ||
| 202 | $page->removeJS($block->getName()); | ||
| 203 | } | ||
| 204 | } | ||
| 205 | |||
| 206 | if(!empty($block)){ // si $block est null c'est que le HTML a été modifié volontairement | ||
| 207 | $main->removeChild($block); // réindex le tableau $children au passage | ||
| 208 | $main->reindexPositions(); | ||
| 209 | if(isset($page)){ | ||
| 210 | $entityManager->persist($page); | ||
| 211 | } | ||
| 212 | $block->getNodeData()->setNode(null); | ||
| 213 | $entityManager->remove($block); | ||
| 214 | try{ | ||
| 215 | $entityManager->flush(); | ||
| 216 | } | ||
| 217 | catch(Exception $e){ | ||
| 218 | // utiliser une flash error | ||
| 219 | return new RedirectResponse((string)new URL(['page' => $request->query->get('page'), 'mode' => 'page_modif', 'error' => $e->getMessage()])); | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | return new RedirectResponse((string)new URL(['page' => $request->query->get('page'), 'mode' => 'page_modif'])); | ||
| 224 | } | ||
| 225 | |||
| 226 | static public function renameBloc(EntityManager $entityManager, array $json): JsonResponse | ||
| 227 | { | ||
| 228 | if(isset($json['bloc_title']) && $json['bloc_title'] !== null && isset($json['bloc_id']) && is_int($json['bloc_id'])){ | ||
| 229 | $model = new Model($entityManager); | ||
| 230 | $model->findNodeById($json['bloc_id']); | ||
| 231 | |||
| 232 | // le titre (du JSON en BDD) est récupéré sous forme de tableau, modifié et renvoyé | ||
| 233 | $data = $model->getNode()->getNodeData()->getData(); | ||
| 234 | $data['title'] = htmlspecialchars($json['bloc_title']); | ||
| 235 | $model->getNode()->getNodeData()->updateData('title', htmlspecialchars($json['bloc_title'])); | ||
| 236 | |||
| 237 | $entityManager->flush(); | ||
| 238 | return new JsonResponse(['success' => true, 'title' => $data['title']]); | ||
| 239 | } | ||
| 240 | else{ | ||
| 241 | return new JsonResponse(['success' => false]); | ||
| 242 | } | ||
| 243 | } | ||
| 244 | |||
| 245 | static public function SwitchBlocsPositions(EntityManager $entityManager, array $json, string $page): JsonResponse | ||
| 246 | { | ||
| 247 | if(isset($json['id1']) && is_int($json['id1']) && isset($json['id2']) && is_int($json['id2']) && isset($page)){ | ||
| 248 | $model = new Model($entityManager); | ||
| 249 | $model->makeMenuAndPaths(); // true pour $model->findItsChildren(); | ||
| 250 | $model->findUniqueNodeByName('main'); | ||
| 251 | $model->findItsChildren(); | ||
| 252 | $main = $model->getNode(); | ||
| 253 | $main->sortChildren(true); // régénère les positions avant inversion | ||
| 254 | |||
| 255 | $bloc1 = null; | ||
| 256 | $bloc2 = null; | ||
| 257 | foreach($main->getChildren() as $child){ | ||
| 258 | if($child->getId() === $json['id1']){ | ||
| 259 | $bloc1 = $child; | ||
| 260 | break; | ||
| 261 | } | ||
| 262 | } | ||
| 263 | foreach($main->getChildren() as $child){ | ||
| 264 | if($child->getId() === $json['id2']){ | ||
| 265 | $bloc2 = $child; | ||
| 266 | break; | ||
| 267 | } | ||
| 268 | } | ||
| 269 | |||
| 270 | // inversion | ||
| 271 | $tmp = $bloc1->getPosition(); | ||
| 272 | $bloc1->setPosition($bloc2->getPosition()); | ||
| 273 | $bloc2->setPosition($tmp); | ||
| 274 | |||
| 275 | $entityManager->flush(); | ||
| 276 | return new JsonResponse(['success' => true]); | ||
| 277 | } | ||
| 278 | else{ | ||
| 279 | return new JsonResponse(['success' => false]); | ||
| 280 | } | ||
| 281 | } | ||
| 282 | |||
| 283 | static public function changeArticlesOrder(EntityManager $entityManager, array $json): JsonResponse | ||
| 284 | { | ||
| 285 | if(isset($json['id']) && isset($json['chrono_order'])){ | ||
| 286 | $model = new Model($entityManager); | ||
| 287 | $model->findNodeById($json['id']); | ||
| 288 | |||
| 289 | if($json['chrono_order'] === 'chrono'){ | ||
| 290 | $chrono_order = true; | ||
| 291 | } | ||
| 292 | elseif($json['chrono_order'] === 'antichrono'){ | ||
| 293 | $chrono_order = false; | ||
| 294 | } | ||
| 295 | else{ | ||
| 296 | return new JsonResponse(['success' => false]); | ||
| 297 | } | ||
| 298 | $model->getNode()->getNodeData()->setChronoOrder($chrono_order); | ||
| 299 | $entityManager->flush(); | ||
| 300 | |||
| 301 | return new JsonResponse(['success' => true, 'chrono_order' => $json['chrono_order']]); | ||
| 302 | } | ||
| 303 | else{ | ||
| 304 | return new JsonResponse(['success' => false]); | ||
| 305 | } | ||
| 306 | } | ||
| 307 | |||
| 308 | static public function changePresentation(EntityManager $entityManager, array $json): JsonResponse | ||
| 309 | { | ||
| 310 | if(isset($json['id']) && isset($json['presentation'])){ | ||
| 311 | $model = new Model($entityManager); | ||
| 312 | $model->findNodeById($json['id']); | ||
| 313 | |||
| 314 | if(in_array($json['presentation'], array_keys(Presentation::PRESENTATIONS))){ | ||
| 315 | $model->getNode()->getNodeData()->setPresentation($json['presentation']); | ||
| 316 | $entityManager->flush(); | ||
| 317 | |||
| 318 | $response_data = ['success' => true, 'presentation' => $json['presentation']]; | ||
| 319 | if($json['presentation'] === 'grid'){ | ||
| 320 | $response_data['cols_min_width'] = $model->getNode()->getNodeData()->getColsMinWidth(); | ||
| 321 | } | ||
| 322 | return new JsonResponse($response_data); | ||
| 323 | } | ||
| 324 | else{ | ||
| 325 | return new JsonResponse(['success' => false]); | ||
| 326 | } | ||
| 327 | } | ||
| 328 | else{ | ||
| 329 | return new JsonResponse(['success' => false]); | ||
| 330 | } | ||
| 331 | } | ||
| 332 | static public function changeColsMinWidth(EntityManager $entityManager, array $json): JsonResponse | ||
| 333 | { | ||
| 334 | if(isset($json['id']) && isset($json['cols_min_width'])){ | ||
| 335 | $model = new Model($entityManager); | ||
| 336 | $model->findNodeById($json['id']); | ||
| 337 | $model->getNode()->getNodeData()->setColsMinWidth((int)$json['cols_min_width']); // attention conversion? | ||
| 338 | |||
| 339 | $entityManager->flush(); | ||
| 340 | return new JsonResponse(['success' => true, 'cols_min_width' => $json['cols_min_width']]); | ||
| 341 | } | ||
| 342 | else{ | ||
| 343 | return new JsonResponse(['success' => false]); | ||
| 344 | } | ||
| 345 | } | ||
| 346 | static public function changePaginationLimit(EntityManager $entityManager, array $json): JsonResponse | ||
| 347 | { | ||
| 348 | if(isset($json['id']) && isset($json['pagination_limit'])){ | ||
| 349 | $model = new Model($entityManager); | ||
| 350 | $model->findNodeById($json['id']); | ||
| 351 | $old_limit = $model->getNode()->getNodeData()->getPaginationLimit() ?? 12; | ||
| 352 | $model->getNode()->getNodeData()->setPaginationLimit((int)$json['pagination_limit']); // attention conversion? | ||
| 353 | |||
| 354 | $entityManager->flush(); | ||
| 355 | |||
| 356 | return new JsonResponse(['success' => true, 'old_limit' => $old_limit, 'new_limit' => $json['pagination_limit']]); | ||
| 357 | } | ||
| 358 | else{ | ||
| 359 | return new JsonResponse(['success' => false]); | ||
| 360 | } | ||
| 361 | } | ||
| 362 | } \ No newline at end of file | ||
