diff options
| author | polo <ordipolo@gmx.fr> | 2025-09-10 01:02:36 +0200 |
|---|---|---|
| committer | polo <ordipolo@gmx.fr> | 2025-09-10 01:02:36 +0200 |
| commit | efd79d15adef2a27347c25ebb19754e9937f9715 (patch) | |
| tree | 6114ec988ef4dfd7cdf2e2ca07cc9762f8fec4c3 /src | |
| parent | 5e41bea598ff38b3c520b69fd92ee3412e716df2 (diff) | |
| download | cms-efd79d15adef2a27347c25ebb19754e9937f9715.tar.gz cms-efd79d15adef2a27347c25ebb19754e9937f9715.tar.bz2 cms-efd79d15adef2a27347c25ebb19754e9937f9715.zip | |
modification d'une URL page Menu et chemin, htmlspecialchars sur les URL du menu à l'affichage
Diffstat (limited to 'src')
| -rw-r--r-- | src/controller/MenuAndPathsController.php | 30 | ||||
| -rw-r--r-- | src/router.php | 5 | ||||
| -rw-r--r-- | src/view/MenuBuilder.php | 30 | ||||
| -rw-r--r-- | src/view/NavBuilder.php | 2 | ||||
| -rw-r--r-- | src/view/templates/menu.php | 11 |
5 files changed, 56 insertions, 22 deletions
diff --git a/src/controller/MenuAndPathsController.php b/src/controller/MenuAndPathsController.php index d429287..5779b39 100644 --- a/src/controller/MenuAndPathsController.php +++ b/src/controller/MenuAndPathsController.php | |||
| @@ -14,9 +14,15 @@ class MenuAndPathsController | |||
| 14 | $previous_page = Director::$menu_data->findPageById((int)$_POST["location"]); // (int) à cause de declare(strict_types=1); | 14 | $previous_page = Director::$menu_data->findPageById((int)$_POST["location"]); // (int) à cause de declare(strict_types=1); |
| 15 | $parent = $previous_page->getParent(); | 15 | $parent = $previous_page->getParent(); |
| 16 | 16 | ||
| 17 | $url_input = trim($_POST["url_input"]); // faire htmlspecialchars à l'affichage | ||
| 18 | if(!filter_var($url_input, FILTER_VALIDATE_URL) || !str_starts_with($url_input, 'http')){ | ||
| 19 | header("Location: " . new URL(['page' => $_GET['from'], 'error' => 'invalide_url'])); | ||
| 20 | die; | ||
| 21 | } | ||
| 22 | |||
| 17 | $page = new Page( | 23 | $page = new Page( |
| 18 | trim(htmlspecialchars($_POST["label_input"])), | 24 | trim(htmlspecialchars($_POST["label_input"])), |
| 19 | filter_var($_POST["url_input"], FILTER_VALIDATE_URL), | 25 | $url_input, |
| 20 | true, true, false, | 26 | true, true, false, |
| 21 | $previous_page->getPosition(), | 27 | $previous_page->getPosition(), |
| 22 | $parent); // peut et DOIT être null si on est au 1er niveau | 28 | $parent); // peut et DOIT être null si on est au 1er niveau |
| @@ -24,7 +30,7 @@ class MenuAndPathsController | |||
| 24 | // on a donné à la nouvelle entrée la même position qu'à la précédente, | 30 | // on a donné à la nouvelle entrée la même position qu'à la précédente, |
| 25 | // addChild l'ajoute à la fin du tableau "children" puis on trie | 31 | // addChild l'ajoute à la fin du tableau "children" puis on trie |
| 26 | // 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 | 32 | // 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 |
| 27 | if($parent == null){ | 33 | if(!$parent){ |
| 28 | $parent = Director::$menu_data; | 34 | $parent = Director::$menu_data; |
| 29 | } | 35 | } |
| 30 | $parent->addChild($page); // true pour réindexer les positions en BDD | 36 | $parent->addChild($page); // true pour réindexer les positions en BDD |
| @@ -36,6 +42,25 @@ class MenuAndPathsController | |||
| 36 | die; | 42 | die; |
| 37 | } | 43 | } |
| 38 | 44 | ||
| 45 | static public function editUrlEntry(EntityManager $entityManager, array $json): void | ||
| 46 | { | ||
| 47 | $url_input = trim($json['url_input']); // faire htmlspecialchars à l'affichage | ||
| 48 | $page = $entityManager->find('App\Entity\Page', $json['id']); | ||
| 49 | |||
| 50 | if(!$page){ | ||
| 51 | echo json_encode(['success' => false, 'message' => "id invalide"]); | ||
| 52 | } | ||
| 53 | elseif(!filter_var($url_input, FILTER_VALIDATE_URL) || !str_starts_with($url_input, 'http')){ | ||
| 54 | echo json_encode(['success' => false, 'message' => "la chaîne envoyée n'est pas une URL valide"]); | ||
| 55 | } | ||
| 56 | else{ | ||
| 57 | $page->setEndOfPath($url_input); | ||
| 58 | $entityManager->flush(); | ||
| 59 | echo json_encode(['success' => true, 'url_input' => $url_input]); | ||
| 60 | } | ||
| 61 | die; | ||
| 62 | } | ||
| 63 | |||
| 39 | static public function deleteUrlMenuEntry(EntityManager $entityManager): void | 64 | static public function deleteUrlMenuEntry(EntityManager $entityManager): void |
| 40 | { | 65 | { |
| 41 | Director::$menu_data = new Menu($entityManager); | 66 | Director::$menu_data = new Menu($entityManager); |
| @@ -163,7 +188,6 @@ class MenuAndPathsController | |||
| 163 | else{ | 188 | else{ |
| 164 | echo json_encode(['success' => false]); | 189 | echo json_encode(['success' => false]); |
| 165 | } | 190 | } |
| 166 | |||
| 167 | die; | 191 | die; |
| 168 | } | 192 | } |
| 169 | 193 | ||
diff --git a/src/router.php b/src/router.php index 04441a9..3c3c773 100644 --- a/src/router.php +++ b/src/router.php | |||
| @@ -156,9 +156,12 @@ elseif($_SERVER['REQUEST_METHOD'] === 'POST'){ | |||
| 156 | elseif($_GET['menu_edit'] === 'switch_positions' && isset($json['id1']) && isset($json['id2'])){ | 156 | elseif($_GET['menu_edit'] === 'switch_positions' && isset($json['id1']) && isset($json['id2'])){ |
| 157 | MenuAndPathsController::switchPositions($entityManager, $json); | 157 | MenuAndPathsController::switchPositions($entityManager, $json); |
| 158 | } | 158 | } |
| 159 | elseif($_GET['menu_edit'] === 'displayInMenu' && isset($json['id']) && isset($json['checked'])){ | 159 | elseif($_GET['menu_edit'] === 'display_in_menu' && isset($json['id']) && isset($json['checked'])){ |
| 160 | MenuAndPathsController::displayInMenu($entityManager, $json); | 160 | MenuAndPathsController::displayInMenu($entityManager, $json); |
| 161 | } | 161 | } |
| 162 | elseif($_GET['menu_edit'] === 'edit_url_entry' && isset($json['id']) && isset($json['url_input'])){ | ||
| 163 | MenuAndPathsController::editUrlEntry($entityManager, $json); | ||
| 164 | } | ||
| 162 | } | 165 | } |
| 163 | 166 | ||
| 164 | /* -- mode Modification d'une page -- */ | 167 | /* -- mode Modification d'une page -- */ |
diff --git a/src/view/MenuBuilder.php b/src/view/MenuBuilder.php index 9d4dda1..bc64e30 100644 --- a/src/view/MenuBuilder.php +++ b/src/view/MenuBuilder.php | |||
| @@ -13,18 +13,13 @@ class MenuBuilder extends AbstractBuilder | |||
| 13 | //private int $margin_left_multiplier = 29; | 13 | //private int $margin_left_multiplier = 29; |
| 14 | private string $options = ''; | 14 | private string $options = ''; |
| 15 | 15 | ||
| 16 | public function __construct(Node $node = null, bool $template = true) | 16 | public function __construct(Node $node, bool $template = true) |
| 17 | { | 17 | { |
| 18 | //parent::__construct($node); | 18 | // impossible de me rappeler pourquoi j'ai écrit ce test sur $node, pourquoi $node serait null? |
| 19 | $viewFile = $node === null ? self::VIEWS_PATH . 'menu.php' : self::VIEWS_PATH . $node->getName() . '.php'; | 19 | $viewFile = $node === null ? self::VIEWS_PATH . 'menu.php' : self::VIEWS_PATH . $node->getName() . '.php'; |
| 20 | 20 | ||
| 21 | if(file_exists($viewFile)) | 21 | if(file_exists($viewFile)) |
| 22 | { | 22 | { |
| 23 | /*if(!empty($node->getNodeData()->getData())) | ||
| 24 | { | ||
| 25 | extract($node->getNodeData()->getData()); | ||
| 26 | }*/ | ||
| 27 | |||
| 28 | if($_SESSION['admin']){ | 23 | if($_SESSION['admin']){ |
| 29 | $this->unfoldMenu(Director::$menu_data); | 24 | $this->unfoldMenu(Director::$menu_data); |
| 30 | 25 | ||
| @@ -63,13 +58,26 @@ class MenuBuilder extends AbstractBuilder | |||
| 63 | </span> | 58 | </span> |
| 64 | <button>' . $entry->getPageName() . '</button>'; | 59 | <button>' . $entry->getPageName() . '</button>'; |
| 65 | 60 | ||
| 61 | // seul la modification des URL est possible pour l'instant, les noms des entrées de menu attendront | ||
| 66 | if(str_starts_with($entry->getEndOfPath(), 'http')){ | 62 | if(str_starts_with($entry->getEndOfPath(), 'http')){ |
| 67 | $this->html .= '<span id="edit-i' . $entry->getId() . '"><img class="move_entry_icon" src="assets/edit.svg" onclick="editUrlEntry(' . $entry->getId() . ')"></span> | 63 | $this->html .= '<form style="display: inline;" id="delete-i' . $entry->getId() . '" method="post" action="' . new URL(['from' => 'menu_chemins']) . '"> |
| 68 | <i class="url">' . $entry->getEndOfPath() . '</i> | ||
| 69 | <form style="display: inline;" id="delete-i' . $entry->getId() . '" method="post" action="' . new URL(['from' => 'menu_chemins']) . '"> | ||
| 70 | <input type="hidden" name="delete" value="' . $entry->getId() . '"> | 64 | <input type="hidden" name="delete" value="' . $entry->getId() . '"> |
| 71 | <input type="image" class="move_entry_icon" src="assets/delete-bin.svg" alt="delete link button" onclick="return confirm(\'Voulez-vous vraiment supprimer cette entrée?\');"> | 65 | <input type="image" class="move_entry_icon" src="assets/delete-bin.svg" alt="delete link button" onclick="return confirm(\'Voulez-vous vraiment supprimer cette entrée?\');"> |
| 72 | </form>'; | 66 | </form> |
| 67 | <span class="url"> | ||
| 68 | <input type="url" value="' . htmlspecialchars($entry->getEndOfPath()) . '"> | ||
| 69 | <img class="move_entry_icon" src="assets/save.svg" onclick="editUrlEntry(' . $entry->getId() . ')"> | ||
| 70 | </span>'; | ||
| 71 | |||
| 72 | // code à recycler pour pouvoir modifier le nom de l'entrée de menu correspondant aux liens | ||
| 73 | /*$this->html .= '<span id="cancel-i' . $entry->getId() . '"> | ||
| 74 | <input type="hidden" name="cancel" value="' . $entry->getId() . '"> | ||
| 75 | <button class="hidden" onclick="cancelUrlEntry(' . $entry->getId() . ')">Annuler</button> | ||
| 76 | </span> | ||
| 77 | <span id="submit-i' . $entry->getId() . '"> | ||
| 78 | <input type="hidden" name="submit" value="' . $entry->getId() . '"> | ||
| 79 | <input type="submit" class="hidden" onclick="submitUrlEntry(' . $entry->getId() . ')"> | ||
| 80 | </span>';*/ | ||
| 73 | } | 81 | } |
| 74 | else{ | 82 | else{ |
| 75 | $this->html .= '<i class="path">' . $entry->getPagePath() . '</i>'; | 83 | $this->html .= '<i class="path">' . $entry->getPagePath() . '</i>'; |
diff --git a/src/view/NavBuilder.php b/src/view/NavBuilder.php index a9cf49c..2cbdef9 100644 --- a/src/view/NavBuilder.php +++ b/src/view/NavBuilder.php | |||
| @@ -38,7 +38,7 @@ class NavBuilder extends AbstractBuilder | |||
| 38 | { | 38 | { |
| 39 | if(str_starts_with($data->getEndOfPath(), 'http')) // lien vers autre site | 39 | if(str_starts_with($data->getEndOfPath(), 'http')) // lien vers autre site |
| 40 | { | 40 | { |
| 41 | $link .= '<a href="' . $data->getEndOfPath() . '" target="_blank">'; | 41 | $link .= '<a href="' . htmlspecialchars($data->getEndOfPath()) . '" target="_blank">'; |
| 42 | } | 42 | } |
| 43 | elseif($data->getEndOfPath() != '') // lien relatif | 43 | elseif($data->getEndOfPath() != '') // lien relatif |
| 44 | { | 44 | { |
diff --git a/src/view/templates/menu.php b/src/view/templates/menu.php index 55c9ff9..d78c665 100644 --- a/src/view/templates/menu.php +++ b/src/view/templates/menu.php | |||
| @@ -2,18 +2,18 @@ | |||
| 2 | <section class="menu"> | 2 | <section class="menu"> |
| 3 | <h3>Menu et chemins</h3> | 3 | <h3>Menu et chemins</h3> |
| 4 | <div class="new_page_button"> | 4 | <div class="new_page_button"> |
| 5 | <p >Créer une <a href="<?= new URL(['page' => 'nouvelle_page']) ?>"><button style="color: #ff1d04;">Nouvelle page</button></a>.</p> | 5 | <p>Créer une <a href="<?= new URL(['page' => 'nouvelle_page']) ?>"><button style="color: #ff1d04;">Nouvelle page</button></a>.</p> |
| 6 | </div> | 6 | </div> |
| 7 | <div class="url_form_zone"> | 7 | <div class="url_form_zone"> |
| 8 | <p>Ajouter au menu un lien vers un site web quelconque avec le formulaire ci-dessous:</p> | 8 | <p>Créer une entrée dans le menu avec une adresse vers un site quelconque:</p> |
| 9 | <form method="post" action="<?= new URL(['from' => 'menu_chemins']) ?>"> | 9 | <form method="post" action="<?= new URL(['from' => 'menu_chemins']) ?>"> |
| 10 | <p> | 10 | <p> |
| 11 | <label for="label_input">Nom:</label> | 11 | <label for="label_input">Nom dans le menu:</label> |
| 12 | <input id="label_input" type="text" name="label_input"> | 12 | <input id="label_input" type="text" name="label_input"> |
| 13 | </p> | 13 | </p> |
| 14 | <p> | 14 | <p> |
| 15 | <label for="url_input">Adresse URL:</label> | 15 | <label for="url_input">Adresse (collez votre lien):</label> |
| 16 | <input id="url_input" type="url" name="url_input"> | 16 | <input id="url_input" type="url" name="url_input" placeholder="http://"> |
| 17 | </p> | 17 | </p> |
| 18 | <p> | 18 | <p> |
| 19 | <label>Placer le lien juste après cette entrée:</label> | 19 | <label>Placer le lien juste après cette entrée:</label> |
| @@ -38,5 +38,4 @@ | |||
| 38 | <div id="menu_edit_buttons"> | 38 | <div id="menu_edit_buttons"> |
| 39 | <?= $this->html ?> | 39 | <?= $this->html ?> |
| 40 | </div> | 40 | </div> |
| 41 | |||
| 42 | </section> \ No newline at end of file | 41 | </section> \ No newline at end of file |
