diff options
author | polo <ordipolo@gmx.fr> | 2025-05-11 17:45:14 +0200 |
---|---|---|
committer | polo <ordipolo@gmx.fr> | 2025-05-11 17:45:14 +0200 |
commit | 6ddffd80053e268789f1b168ff3a8ee223681e47 (patch) | |
tree | 7539761c4454d9ff8c92e1d4e64a7da0c103c21b /src | |
parent | 4efa19d4357ab204d62397f1849e4651906e0e70 (diff) | |
download | cms-6ddffd80053e268789f1b168ff3a8ee223681e47.zip |
création de page
Diffstat (limited to 'src')
-rw-r--r-- | src/controller/post.php | 65 | ||||
-rw-r--r-- | src/model/entities/Node.php | 28 | ||||
-rw-r--r-- | src/model/entities/Page.php | 3 | ||||
-rw-r--r-- | src/view/templates/new_page.php | 15 |
4 files changed, 98 insertions, 13 deletions
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) | |||
12 | /* -- formulaires HTML classiques -- */ | 12 | /* -- formulaires HTML classiques -- */ |
13 | if($_SERVER['CONTENT_TYPE'] === 'application/x-www-form-urlencoded') | 13 | if($_SERVER['CONTENT_TYPE'] === 'application/x-www-form-urlencoded') |
14 | { | 14 | { |
15 | /* -- nouvelle page -- */ | ||
16 | if(isset($_POST['page_name']) && $_POST['page_name'] !== null | ||
17 | && isset($_POST['page_name_path']) && $_POST['page_name_path'] !== null | ||
18 | && isset($_POST['page_location']) && $_POST['page_location'] !== null | ||
19 | && isset($_POST['page_description']) && $_POST['page_description'] !== null | ||
20 | && isset($_POST['new_page_hidden']) && $_POST['new_page_hidden'] === '') | ||
21 | { | ||
22 | // titre et chemin | ||
23 | $director = new Director($entityManager, true); | ||
24 | //Director::$menu_data = new Menu($entityManager); | ||
25 | $previous_page = Director::$menu_data->findPageById((int)$_POST["page_location"]); // (int) à cause de declare(strict_types=1); | ||
26 | $parent = $previous_page->getParent(); | ||
27 | |||
28 | $page = new Page( | ||
29 | trim(htmlspecialchars($_POST["page_name"])), | ||
30 | trim(htmlspecialchars($_POST["page_name_path"])), | ||
31 | true, true, false, | ||
32 | $previous_page->getPosition(), | ||
33 | $parent); // peut et DOIT être null si on est au 1er niveau | ||
34 | |||
35 | // on a donné à la nouvelle entrée la même position qu'à la précédente, | ||
36 | // addChild l'ajoute à la fin du tableau "children" puis on trie | ||
37 | // 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 | ||
38 | if($parent == null){ | ||
39 | $parent = Director::$menu_data; | ||
40 | } | ||
41 | $parent->addChild($page); | ||
42 | $parent->reindexPositions(); | ||
43 | |||
44 | $page->setPagePath(ltrim($parent->getPagePath() . '/' . $page->getEndOfPath(), '/')); | ||
45 | |||
46 | // noeud "head" | ||
47 | $node = new Node( | ||
48 | 'head', | ||
49 | null, [], | ||
50 | 1, // position d'un head = 1 | ||
51 | null, // pas de parent | ||
52 | $page); | ||
53 | $node->useDefaultAttributes(); // fichiers CSS et JS | ||
54 | |||
55 | $data = new NodeData([ | ||
56 | 'title' => trim(htmlspecialchars($_POST["page_name"])), | ||
57 | 'description' => trim(htmlspecialchars($_POST["page_description"]))], | ||
58 | $node); | ||
59 | |||
60 | $entityManager->persist($page); | ||
61 | $entityManager->persist($node); | ||
62 | $entityManager->persist($data); | ||
63 | $entityManager->flush(); | ||
64 | |||
65 | // page créée, direction la page en mode modification pour ajouter des blocs | ||
66 | header("Location: " . new URL(['page' => $page->getPagePath(), 'action' => 'modif_page'])); | ||
67 | die; | ||
68 | } | ||
69 | |||
15 | /* -- mode Modification d'une page -- */ | 70 | /* -- mode Modification d'une page -- */ |
16 | if(isset($_POST['page_menu_path']) && $_POST['page_menu_path'] !== null | 71 | |
72 | // modification des titres, chemins et descriptions | ||
73 | elseif(isset($_POST['page_menu_path']) && $_POST['page_menu_path'] !== null | ||
17 | && isset($_POST['page_id']) && $_POST['page_id'] !== null | 74 | && isset($_POST['page_id']) && $_POST['page_id'] !== null |
18 | && isset($_POST['page_name_path_hidden']) && $_POST['page_name_path_hidden'] === '') | 75 | && isset($_POST['page_name_path_hidden']) && $_POST['page_name_path_hidden'] === '') |
19 | { | 76 | { |
20 | $director = new Director($entityManager, true); | 77 | $director = new Director($entityManager, true); |
21 | $page = Director::$page_path->getLast(); | 78 | $page = Director::$page_path->getLast(); |
22 | //$page = $entityManager->find('App\Entity\Page', $_POST['page_id']); | ||
23 | $path = htmlspecialchars($_POST['page_menu_path']); | 79 | $path = htmlspecialchars($_POST['page_menu_path']); |
24 | 80 | ||
25 | // mise en snake_case: filtre caractères non-alphanumériques, minuscule, doublons d'underscore, trim des underscores | 81 | // 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) | |||
101 | filter_var($_POST["url_input"], FILTER_VALIDATE_URL), | 157 | filter_var($_POST["url_input"], FILTER_VALIDATE_URL), |
102 | true, true, false, | 158 | true, true, false, |
103 | $previous_page->getPosition(), | 159 | $previous_page->getPosition(), |
104 | $parent); | 160 | $parent); // peut et DOIT être null si on est au 1er niveau |
105 | 161 | ||
106 | // 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 | 162 | // on a donné à la nouvelle entrée la même position qu'à la précédente, |
163 | // addChild l'ajoute à la fin du tableau "children" puis on trie | ||
107 | // 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 | 164 | // 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 |
108 | if($parent == null){ | 165 | if($parent == null){ |
109 | $parent = Director::$menu_data; | 166 | $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 | |||
55 | 55 | ||
56 | private array $children = []; // tableau de Node | 56 | private array $children = []; // tableau de Node |
57 | private ?self $adopted = null; // = "new" est un enfant de "main" lorsque la page est "article" | 57 | private ?self $adopted = null; // = "new" est un enfant de "main" lorsque la page est "article" |
58 | static private array $default_attributes = ['css_array' => ['body', 'head', 'nav', 'foot'],'js_array' => ['main']]; | ||
58 | 59 | ||
59 | public function __construct(string $name = '', ?string $article_timestamp = null, array $attributes = [], int $position = 0, ?self $parent = null, ?Page $page = null, ?Article $article = null) | 60 | public function __construct(string $name = '', ?string $article_timestamp = null, array $attributes = [], int $position = 0, ?self $parent = null, ?Page $page = null, ?Article $article = null) |
60 | { | 61 | { |
@@ -88,9 +89,34 @@ class Node | |||
88 | { | 89 | { |
89 | return $this->attributes; | 90 | return $this->attributes; |
90 | } | 91 | } |
91 | /*public function setAttributes(array $attributes): void | 92 | public function setDefaultAttributes(array $attributes): void |
92 | { | 93 | { |
93 | $this->attributes = $attributes; | 94 | $this->attributes = $attributes; |
95 | } | ||
96 | public function useDefaultAttributes(): void | ||
97 | { | ||
98 | $this->attributes = self::$default_attributes; | ||
99 | } | ||
100 | /*public function addAttribute(string $key, string $value): void | ||
101 | { | ||
102 | if(!isset($this->attributes[$key])) { // sécurité $key inexistante | ||
103 | $this->attributes[$key] = []; | ||
104 | } | ||
105 | $this->attributes[$key][] = $value; | ||
106 | }*/ | ||
107 | /*public function removeAttribute(string $key, string $value): void | ||
108 | { | ||
109 | if(isset($this->attributes[$key])) // sécurité $key inexistante | ||
110 | { | ||
111 | // supprime et réindex avec un nouveau tableau | ||
112 | $tmp_array = $this->attributes[$key]; | ||
113 | $this->attributes[$key] = []; | ||
114 | foreach($tmp_array as $entry){ | ||
115 | if($entry !== $value){ | ||
116 | $this->attributes[$key][] = $entry; | ||
117 | } | ||
118 | } | ||
119 | } | ||
94 | }*/ | 120 | }*/ |
95 | public function getParent(): ?self | 121 | public function getParent(): ?self |
96 | { | 122 | { |
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 | |||
26 | #[ORM\Column(type: "string", length: 255)] | 26 | #[ORM\Column(type: "string", length: 255)] |
27 | private string $end_of_path; // morceau d'URL plus exactement | 27 | private string $end_of_path; // morceau d'URL plus exactement |
28 | 28 | ||
29 | private string $page_path; | 29 | private string $page_path = ''; |
30 | 30 | ||
31 | #[ORM\Column(type: "boolean")] | 31 | #[ORM\Column(type: "boolean")] |
32 | private bool $reachable; | 32 | private bool $reachable; |
@@ -128,6 +128,7 @@ class Page | |||
128 | return $this->children; | 128 | return $this->children; |
129 | } | 129 | } |
130 | 130 | ||
131 | // utilisée par $menu_path | ||
131 | public function fillChildrenPagePath(string $parent_path = ''): void | 132 | public function fillChildrenPagePath(string $parent_path = ''): void |
132 | { | 133 | { |
133 | $this->page_path = $parent_path != '' ? $parent_path . '/' . $this->end_of_path : $this->end_of_path; | 134 | $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 @@ | |||
2 | <section class="new_page"> | 2 | <section class="new_page"> |
3 | <h3>Création d'une nouvelle page</h3> | 3 | <h3>Création d'une nouvelle page</h3> |
4 | <div class="form_zone"> | 4 | <div class="form_zone"> |
5 | <form method="post" action="<?= new URL(['from' => 'nouvelle_page']) ?>"> | 5 | <form method="post" action="<?= new URL ?>"> |
6 | <p> | 6 | <p> |
7 | <label for="new_page_name">Nom de la page</label> | 7 | <label for="page_name">Nom de la page</label> |
8 | <input id="page_name" type="text" name="new_page_name" onchange="makePageNamePath()" required> | 8 | <input id="page_name" type="text" name="page_name" onchange="makePageNamePath()" required> |
9 | </p> | 9 | </p> |
10 | <p> | 10 | <p> |
11 | <label for="new_page_name_path">Chemin de l'URL</label> | 11 | <label for="page_name_path">Chemin de l'URL</label> |
12 | <input id="page_name_path" type="text" name="new_page_name_path" placeholder="ex: nouvelle_page" required> | 12 | <input id="page_name_path" type="text" name="page_name_path" placeholder="ex: nouvelle_page" required> |
13 | </p> | 13 | </p> |
14 | <p> | 14 | <p> |
15 | <label for="page_location">Placer la page juste après cette entrée</label> | 15 | <label for="page_location">Placer la page juste après cette entrée</label> |
@@ -17,9 +17,10 @@ | |||
17 | <?= $this->options ?> | 17 | <?= $this->options ?> |
18 | </select> | 18 | </select> |
19 | </p> | 19 | </p> |
20 | <input type="hidden" name="new_page_hidden"> | ||
20 | <p> | 21 | <p> |
21 | <label class="label_textarea" for="new_page_description">Description qui apparaît sous le titre dans les moteurs de recherche</label> | 22 | <label class="label_textarea" for="page_description">Description qui apparaît sous le titre dans les moteurs de recherche</label> |
22 | <textarea id="new_page_description" name="new_page_description" cols="40" rows="3" placeholder="ex: nous faisons ceci et cela, etc" required></textarea> | 23 | <textarea id="page_description" name="page_description" cols="40" rows="3" placeholder="ex: nous faisons ceci et cela, etc"></textarea> |
23 | </p> | 24 | </p> |
24 | <input type="submit" value="Créer la page"> | 25 | <input type="submit" value="Créer la page"> |
25 | </form> | 26 | </form> |