From 2d8ec75f4aaf3b93fd9f6758f8dcb4f1f9f03d0c Mon Sep 17 00:00:00 2001 From: polo Date: Wed, 7 May 2025 01:11:27 +0200 Subject: page menu et chemins, partie5 --- public/css/menu.css | 6 +---- src/controller/post.php | 56 ++++++++++++++++++++++++++++++++++++++++----- src/model/Position.php | 31 ++++++++----------------- src/model/entities/Page.php | 8 +++---- src/view/MenuBuilder.php | 7 ++++-- src/view/templates/menu.php | 8 +++---- 6 files changed, 72 insertions(+), 44 deletions(-) diff --git a/public/css/menu.css b/public/css/menu.css index a338666..3294c2b 100644 --- a/public/css/menu.css +++ b/public/css/menu.css @@ -64,10 +64,6 @@ { margin: 5px; } -input -{ - vertical-align: middle; -} .menu_entry_checkbox { margin-left: 2px; @@ -86,7 +82,7 @@ input cursor: pointer; } -.menu form +.menu .url_form_zone form { padding: 10px; background-color: #f0f0f0f0; diff --git a/src/controller/post.php b/src/controller/post.php index 66de5a0..d2e4477 100644 --- a/src/controller/post.php +++ b/src/controller/post.php @@ -3,15 +3,59 @@ declare(strict_types=1); +use App\Entity\Page; + if($_SERVER['REQUEST_METHOD'] === 'POST' && $_SESSION['admin'] === true) { - /* -- formulaires HTML -- */ - /*if(isset($_POST['from']) // page d'où vient la requête - && isset($_POST)) // données + /* -- formulaires HTML classiques -- */ + if($_SERVER['CONTENT_TYPE'] === 'application/x-www-form-urlencoded') { - echo "requête envoyée en validant un formulaire"; - }*/ + // création d'une entrée de menu avec une URL + if(isset($_POST["label_input"]) && isset($_POST["url_input"]) && isset($_POST["location"])){ + echo $_POST["label_input"] . '
'; + echo $_POST["url_input"] . '
'; + echo $_POST["location"] . '
'; // id entrée précédente + + Director::$menu_data = new Menu($entityManager); + $previous_page = Director::$menu_data->findPageById((int)$_POST["location"]); // (int) à cause de declare(strict_types=1); + $parent = $previous_page->getParent(); + + $page = new Page($_POST["label_input"], $_POST["url_input"], true, true, false, $previous_page->getPosition(), $parent); + // on indique pour la nouvelle entrée la même position que la précédente, puis addChild l'ajoute à la fin du tableau "children" avant de déclencher un tri + // 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 + if($parent == null){ + $parent = Director::$menu_data; + } + $parent->addChild($page); // true pour réindexer les positions en BDD + $parent->reindex(); + + $entityManager->persist($page); + $entityManager->flush(); + + header("Location: " . new URL(['page' => $_GET['from']])); + } + // suppression d'une entrée de menu avec une URL + elseif(isset($_POST['delete']) && isset($_POST['x']) && isset($_POST['y'])){ // 2 params x et y sont là parce qu'on a cliqué sur une image + Director::$menu_data = new Menu($entityManager); + $page = Director::$menu_data->findPageById((int)$_POST["delete"]); + $parent = $page->getParent(); + if($parent == null){ + $parent = Director::$menu_data; + } + + $parent->removeChild($page); // suppression de $children avant de trier + $parent->reindex(); + $entityManager->remove($page); // suppression en BDD + $entityManager->flush(); + header("Location: " . new URL(['page' => $_GET['from']])); + } + else{ + header("Location: " . new URL(['error' => 'paramètres inconnus'])); + } + } /* -- requêtes AJAX -- */ - require '../src/controller/ajax.php'; + else{ + require '../src/controller/ajax.php'; + } } diff --git a/src/model/Position.php b/src/model/Position.php index 76de966..b5040df 100644 --- a/src/model/Position.php +++ b/src/model/Position.php @@ -7,9 +7,9 @@ declare(strict_types=1); trait Position { + // tri par insertion du tableau des enfants public function sortChildren(bool $reindexation = false): void { - // tri par insertion du tableau des enfants for($i = 1; $i < count($this->children); $i++) { $tmp = $this->children[$i]; @@ -29,31 +29,18 @@ trait Position } } - // nouvelles positions (tableau $children => BDD) if($reindexation){ - $i = 1; - foreach($this->children as $child){ - $child->setPosition($i); - $i++; - } + $this->reindex(); } } - /*private function sortChildren(): void + // nouvelles positions (tableau $children => BDD) + public function reindex(): void { - $iteration = count($this->children); - while($iteration > 1) - { - for($i = 0; $i < $iteration - 1; $i++) - { - if($this->children[$i]->getPosition() > $this->children[$i + 1]->getPosition()) - { - $tmp = $this->children[$i]; - $this->children[$i] = $this->children[$i + 1]; - $this->children[$i + 1] = $tmp; - } - } - $iteration--; + $i = 1; + foreach($this->children as $child){ + $child->setPosition($i); + $i++; } - }*/ + } } \ No newline at end of file diff --git a/src/model/entities/Page.php b/src/model/entities/Page.php index aaff1ff..9564342 100644 --- a/src/model/entities/Page.php +++ b/src/model/entities/Page.php @@ -135,11 +135,9 @@ class Page } public function removeChild(self $child): void { - foreach($this->children as $index => $candidate){ - if($candidate === $child){ - unset($this->children[$index]); - } - } + $this->children->removeElement($child); + $this->children = new ArrayCollection(array_values($this->children->toArray())); // réindexer en passant par un tableau + $this->sortChildren(false); } public function findPageById(int $id): ?Page diff --git a/src/view/MenuBuilder.php b/src/view/MenuBuilder.php index 5331c08..780ca7e 100644 --- a/src/view/MenuBuilder.php +++ b/src/view/MenuBuilder.php @@ -62,9 +62,12 @@ class MenuBuilder extends AbstractBuilder '; if(str_starts_with($entry->getEndOfPath(), 'http')){ - $this->html .= ' + $this->html .= ' ' . $entry->getEndOfPath() . ' - '; +
+ + +
'; } else{ $this->html .= '' . $entry->getPagePath() . ''; diff --git a/src/view/templates/menu.php b/src/view/templates/menu.php index 49df4e0..d62b78b 100644 --- a/src/view/templates/menu.php +++ b/src/view/templates/menu.php @@ -6,14 +6,14 @@

Ajouter au menu un lien vers un site web quelconque avec le formulaire ci-dessous:

-

- - -

+

+ + +