From 6ddffd80053e268789f1b168ff3a8ee223681e47 Mon Sep 17 00:00:00 2001 From: polo Date: Sun, 11 May 2025 17:45:14 +0200 Subject: =?UTF-8?q?cr=C3=A9ation=20de=20page?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/index.php | 2 +- src/controller/post.php | 65 ++++++++++++++++++++++++++++++++++++++--- src/model/entities/Node.php | 28 +++++++++++++++++- src/model/entities/Page.php | 3 +- src/view/templates/new_page.php | 15 +++++----- 5 files changed, 99 insertions(+), 14 deletions(-) diff --git a/public/index.php b/public/index.php index 9f930fd..aa080c4 100644 --- a/public/index.php +++ b/public/index.php @@ -61,7 +61,7 @@ elseif(isset($_GET['action']) && $_GET['action'] === 'modif_mdp') changePassword($entityManager); } elseif($_SESSION['admin'] && isset($_GET['page']) && isset($_GET['action']) && $_GET['action'] === 'modif_page' - && $_GET['page'] !== 'connexion' && $_GET['page'] !== 'article' && $_GET['page'] !== 'menu_chemins') + && $_GET['page'] !== 'connexion' && $_GET['page'] !== 'article' && $_GET['page'] !== 'nouvelle_page' && $_GET['page'] !== 'menu_chemins') { // les contrôles de la 2è ligne devraient utiliser un tableau MainBuilder::$modif_mode = true; diff --git a/src/controller/post.php b/src/controller/post.php index 631c4ad..6fac796 100644 --- a/src/controller/post.php +++ b/src/controller/post.php @@ -12,14 +12,70 @@ if($_SERVER['REQUEST_METHOD'] === 'POST' && $_SESSION['admin'] === true) /* -- formulaires HTML classiques -- */ if($_SERVER['CONTENT_TYPE'] === 'application/x-www-form-urlencoded') { + /* -- nouvelle page -- */ + if(isset($_POST['page_name']) && $_POST['page_name'] !== null + && isset($_POST['page_name_path']) && $_POST['page_name_path'] !== null + && isset($_POST['page_location']) && $_POST['page_location'] !== null + && isset($_POST['page_description']) && $_POST['page_description'] !== null + && isset($_POST['new_page_hidden']) && $_POST['new_page_hidden'] === '') + { + // titre et chemin + $director = new Director($entityManager, true); + //Director::$menu_data = new Menu($entityManager); + $previous_page = Director::$menu_data->findPageById((int)$_POST["page_location"]); // (int) à cause de declare(strict_types=1); + $parent = $previous_page->getParent(); + + $page = new Page( + trim(htmlspecialchars($_POST["page_name"])), + trim(htmlspecialchars($_POST["page_name_path"])), + true, true, false, + $previous_page->getPosition(), + $parent); // peut et DOIT être null si on est au 1er niveau + + // on a donné à la nouvelle entrée la même position qu'à la précédente, + // addChild l'ajoute à la fin du tableau "children" puis on trie + // 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); + $parent->reindexPositions(); + + $page->setPagePath(ltrim($parent->getPagePath() . '/' . $page->getEndOfPath(), '/')); + + // noeud "head" + $node = new Node( + 'head', + null, [], + 1, // position d'un head = 1 + null, // pas de parent + $page); + $node->useDefaultAttributes(); // fichiers CSS et JS + + $data = new NodeData([ + 'title' => trim(htmlspecialchars($_POST["page_name"])), + 'description' => trim(htmlspecialchars($_POST["page_description"]))], + $node); + + $entityManager->persist($page); + $entityManager->persist($node); + $entityManager->persist($data); + $entityManager->flush(); + + // page créée, direction la page en mode modification pour ajouter des blocs + header("Location: " . new URL(['page' => $page->getPagePath(), 'action' => 'modif_page'])); + die; + } + /* -- mode Modification d'une page -- */ - if(isset($_POST['page_menu_path']) && $_POST['page_menu_path'] !== null + + // modification des titres, chemins et descriptions + elseif(isset($_POST['page_menu_path']) && $_POST['page_menu_path'] !== null && isset($_POST['page_id']) && $_POST['page_id'] !== null && isset($_POST['page_name_path_hidden']) && $_POST['page_name_path_hidden'] === '') { $director = new Director($entityManager, true); $page = Director::$page_path->getLast(); - //$page = $entityManager->find('App\Entity\Page', $_POST['page_id']); $path = htmlspecialchars($_POST['page_menu_path']); // mise en snake_case: filtre caractères non-alphanumériques, minuscule, doublons d'underscore, trim des underscores @@ -101,9 +157,10 @@ if($_SERVER['REQUEST_METHOD'] === 'POST' && $_SESSION['admin'] === true) filter_var($_POST["url_input"], FILTER_VALIDATE_URL), true, true, false, $previous_page->getPosition(), - $parent); + $parent); // peut et DOIT être null si on est au 1er niveau - // 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 + // on a donné à la nouvelle entrée la même position qu'à la précédente, + // addChild l'ajoute à la fin du tableau "children" puis on trie // 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; diff --git a/src/model/entities/Node.php b/src/model/entities/Node.php index 103163b..fea9d50 100644 --- a/src/model/entities/Node.php +++ b/src/model/entities/Node.php @@ -55,6 +55,7 @@ class Node private array $children = []; // tableau de Node private ?self $adopted = null; // = "new" est un enfant de "main" lorsque la page est "article" + static private array $default_attributes = ['css_array' => ['body', 'head', 'nav', 'foot'],'js_array' => ['main']]; public function __construct(string $name = '', ?string $article_timestamp = null, array $attributes = [], int $position = 0, ?self $parent = null, ?Page $page = null, ?Article $article = null) { @@ -88,9 +89,34 @@ class Node { return $this->attributes; } - /*public function setAttributes(array $attributes): void + public function setDefaultAttributes(array $attributes): void { $this->attributes = $attributes; + } + public function useDefaultAttributes(): void + { + $this->attributes = self::$default_attributes; + } + /*public function addAttribute(string $key, string $value): void + { + if(!isset($this->attributes[$key])) { // sécurité $key inexistante + $this->attributes[$key] = []; + } + $this->attributes[$key][] = $value; + }*/ + /*public function removeAttribute(string $key, string $value): void + { + if(isset($this->attributes[$key])) // sécurité $key inexistante + { + // supprime et réindex avec un nouveau tableau + $tmp_array = $this->attributes[$key]; + $this->attributes[$key] = []; + foreach($tmp_array as $entry){ + if($entry !== $value){ + $this->attributes[$key][] = $entry; + } + } + } }*/ public function getParent(): ?self { diff --git a/src/model/entities/Page.php b/src/model/entities/Page.php index 1ad9ddb..7448e5d 100644 --- a/src/model/entities/Page.php +++ b/src/model/entities/Page.php @@ -26,7 +26,7 @@ class Page #[ORM\Column(type: "string", length: 255)] private string $end_of_path; // morceau d'URL plus exactement - private string $page_path; + private string $page_path = ''; #[ORM\Column(type: "boolean")] private bool $reachable; @@ -128,6 +128,7 @@ class Page return $this->children; } + // utilisée par $menu_path public function fillChildrenPagePath(string $parent_path = ''): void { $this->page_path = $parent_path != '' ? $parent_path . '/' . $this->end_of_path : $this->end_of_path; diff --git a/src/view/templates/new_page.php b/src/view/templates/new_page.php index db48496..5747e82 100644 --- a/src/view/templates/new_page.php +++ b/src/view/templates/new_page.php @@ -2,14 +2,14 @@

Création d'une nouvelle page

-
+

- - + +

- - + +

@@ -17,9 +17,10 @@ options ?>

+

- - + +

-- cgit v1.2.3