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