diff options
-rw-r--r-- | public/css/menu.css | 6 | ||||
-rw-r--r-- | src/controller/post.php | 56 | ||||
-rw-r--r-- | src/model/Position.php | 31 | ||||
-rw-r--r-- | src/model/entities/Page.php | 8 | ||||
-rw-r--r-- | src/view/MenuBuilder.php | 7 | ||||
-rw-r--r-- | 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 @@ | |||
64 | { | 64 | { |
65 | margin: 5px; | 65 | margin: 5px; |
66 | } | 66 | } |
67 | input | ||
68 | { | ||
69 | vertical-align: middle; | ||
70 | } | ||
71 | .menu_entry_checkbox | 67 | .menu_entry_checkbox |
72 | { | 68 | { |
73 | margin-left: 2px; | 69 | margin-left: 2px; |
@@ -86,7 +82,7 @@ input | |||
86 | cursor: pointer; | 82 | cursor: pointer; |
87 | } | 83 | } |
88 | 84 | ||
89 | .menu form | 85 | .menu .url_form_zone form |
90 | { | 86 | { |
91 | padding: 10px; | 87 | padding: 10px; |
92 | background-color: #f0f0f0f0; | 88 | 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 @@ | |||
3 | 3 | ||
4 | declare(strict_types=1); | 4 | declare(strict_types=1); |
5 | 5 | ||
6 | use App\Entity\Page; | ||
7 | |||
6 | if($_SERVER['REQUEST_METHOD'] === 'POST' && $_SESSION['admin'] === true) | 8 | if($_SERVER['REQUEST_METHOD'] === 'POST' && $_SESSION['admin'] === true) |
7 | { | 9 | { |
8 | /* -- formulaires HTML -- */ | 10 | /* -- formulaires HTML classiques -- */ |
9 | /*if(isset($_POST['from']) // page d'où vient la requête | 11 | if($_SERVER['CONTENT_TYPE'] === 'application/x-www-form-urlencoded') |
10 | && isset($_POST)) // données | ||
11 | { | 12 | { |
12 | echo "requête envoyée en validant un formulaire"; | 13 | // création d'une entrée de menu avec une URL |
13 | }*/ | 14 | if(isset($_POST["label_input"]) && isset($_POST["url_input"]) && isset($_POST["location"])){ |
15 | echo $_POST["label_input"] . '<br>'; | ||
16 | echo $_POST["url_input"] . '<br>'; | ||
17 | echo $_POST["location"] . '<br>'; // id entrée précédente | ||
18 | |||
19 | Director::$menu_data = new Menu($entityManager); | ||
20 | $previous_page = Director::$menu_data->findPageById((int)$_POST["location"]); // (int) à cause de declare(strict_types=1); | ||
21 | $parent = $previous_page->getParent(); | ||
22 | |||
23 | $page = new Page($_POST["label_input"], $_POST["url_input"], true, true, false, $previous_page->getPosition(), $parent); | ||
24 | // 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 | ||
25 | // 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 | ||
26 | if($parent == null){ | ||
27 | $parent = Director::$menu_data; | ||
28 | } | ||
29 | $parent->addChild($page); // true pour réindexer les positions en BDD | ||
30 | $parent->reindex(); | ||
31 | |||
32 | $entityManager->persist($page); | ||
33 | $entityManager->flush(); | ||
34 | |||
35 | header("Location: " . new URL(['page' => $_GET['from']])); | ||
36 | } | ||
37 | // suppression d'une entrée de menu avec une URL | ||
38 | 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 | ||
39 | Director::$menu_data = new Menu($entityManager); | ||
40 | $page = Director::$menu_data->findPageById((int)$_POST["delete"]); | ||
41 | $parent = $page->getParent(); | ||
42 | if($parent == null){ | ||
43 | $parent = Director::$menu_data; | ||
44 | } | ||
45 | |||
46 | $parent->removeChild($page); // suppression de $children avant de trier | ||
47 | $parent->reindex(); | ||
14 | 48 | ||
49 | $entityManager->remove($page); // suppression en BDD | ||
50 | $entityManager->flush(); | ||
51 | header("Location: " . new URL(['page' => $_GET['from']])); | ||
52 | } | ||
53 | else{ | ||
54 | header("Location: " . new URL(['error' => 'paramètres inconnus'])); | ||
55 | } | ||
56 | } | ||
15 | /* -- requêtes AJAX -- */ | 57 | /* -- requêtes AJAX -- */ |
16 | require '../src/controller/ajax.php'; | 58 | else{ |
59 | require '../src/controller/ajax.php'; | ||
60 | } | ||
17 | } | 61 | } |
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); | |||
7 | 7 | ||
8 | trait Position | 8 | trait Position |
9 | { | 9 | { |
10 | // tri par insertion du tableau des enfants | ||
10 | public function sortChildren(bool $reindexation = false): void | 11 | public function sortChildren(bool $reindexation = false): void |
11 | { | 12 | { |
12 | // tri par insertion du tableau des enfants | ||
13 | for($i = 1; $i < count($this->children); $i++) | 13 | for($i = 1; $i < count($this->children); $i++) |
14 | { | 14 | { |
15 | $tmp = $this->children[$i]; | 15 | $tmp = $this->children[$i]; |
@@ -29,31 +29,18 @@ trait Position | |||
29 | } | 29 | } |
30 | } | 30 | } |
31 | 31 | ||
32 | // nouvelles positions (tableau $children => BDD) | ||
33 | if($reindexation){ | 32 | if($reindexation){ |
34 | $i = 1; | 33 | $this->reindex(); |
35 | foreach($this->children as $child){ | ||
36 | $child->setPosition($i); | ||
37 | $i++; | ||
38 | } | ||
39 | } | 34 | } |
40 | } | 35 | } |
41 | 36 | ||
42 | /*private function sortChildren(): void | 37 | // nouvelles positions (tableau $children => BDD) |
38 | public function reindex(): void | ||
43 | { | 39 | { |
44 | $iteration = count($this->children); | 40 | $i = 1; |
45 | while($iteration > 1) | 41 | foreach($this->children as $child){ |
46 | { | 42 | $child->setPosition($i); |
47 | for($i = 0; $i < $iteration - 1; $i++) | 43 | $i++; |
48 | { | ||
49 | if($this->children[$i]->getPosition() > $this->children[$i + 1]->getPosition()) | ||
50 | { | ||
51 | $tmp = $this->children[$i]; | ||
52 | $this->children[$i] = $this->children[$i + 1]; | ||
53 | $this->children[$i + 1] = $tmp; | ||
54 | } | ||
55 | } | ||
56 | $iteration--; | ||
57 | } | 44 | } |
58 | }*/ | 45 | } |
59 | } \ No newline at end of file | 46 | } \ 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 | |||
135 | } | 135 | } |
136 | public function removeChild(self $child): void | 136 | public function removeChild(self $child): void |
137 | { | 137 | { |
138 | foreach($this->children as $index => $candidate){ | 138 | $this->children->removeElement($child); |
139 | if($candidate === $child){ | 139 | $this->children = new ArrayCollection(array_values($this->children->toArray())); // réindexer en passant par un tableau |
140 | unset($this->children[$index]); | 140 | $this->sortChildren(false); |
141 | } | ||
142 | } | ||
143 | } | 141 | } |
144 | 142 | ||
145 | public function findPageById(int $id): ?Page | 143 | 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 | |||
62 | <button>' . $entry->getPageName() . '</button>'; | 62 | <button>' . $entry->getPageName() . '</button>'; |
63 | 63 | ||
64 | if(str_starts_with($entry->getEndOfPath(), 'http')){ | 64 | if(str_starts_with($entry->getEndOfPath(), 'http')){ |
65 | $this->html .= '<span id="edit-i..."><img class="move_entry_icon" src="assets/edit.svg" onclick="editUrlEntry(' . $entry->getId() . ')"></span> | 65 | $this->html .= '<span id="edit-i' . $entry->getId() . '"><img class="move_entry_icon" src="assets/edit.svg" onclick="editUrlEntry(' . $entry->getId() . ')"></span> |
66 | <i class="url">' . $entry->getEndOfPath() . '</i> | 66 | <i class="url">' . $entry->getEndOfPath() . '</i> |
67 | <span id="delete-i..."><img class="move_entry_icon" src="assets/delete-bin.svg" onclick="deleteUrlEntry(' . $entry->getId() . ')"></span>'; | 67 | <form style="display: inline;" id="delete-i' . $entry->getId() . '" method="post" action="' . new URL(['from' => 'menu_chemins']) . '"> |
68 | <input type="hidden" name="delete" value="' . $entry->getId() . '"> | ||
69 | <input type="image" class="move_entry_icon" src="assets/delete-bin.svg" alt="delete link button"> | ||
70 | </form>'; | ||
68 | } | 71 | } |
69 | else{ | 72 | else{ |
70 | $this->html .= '<i class="path">' . $entry->getPagePath() . '</i>'; | 73 | $this->html .= '<i class="path">' . $entry->getPagePath() . '</i>'; |
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 | |||
@@ -7,14 +7,14 @@ | |||
7 | <p>Ajouter au menu un lien vers un site web quelconque avec le formulaire ci-dessous:</p> | 7 | <p>Ajouter au menu un lien vers un site web quelconque avec le formulaire ci-dessous:</p> |
8 | <form method="post" action="<?= new URL(['from' => 'menu_chemins']) ?>"> | 8 | <form method="post" action="<?= new URL(['from' => 'menu_chemins']) ?>"> |
9 | <p> | 9 | <p> |
10 | <label for="url_input">Adresse URL:</label> | ||
11 | <input id="url_input" type="url" name="url_input"> | ||
12 | </p> | ||
13 | <p> | ||
14 | <label for="label_input">Nom:</label> | 10 | <label for="label_input">Nom:</label> |
15 | <input id="label_input" type="text" name="label_input"> | 11 | <input id="label_input" type="text" name="label_input"> |
16 | </p> | 12 | </p> |
17 | <p> | 13 | <p> |
14 | <label for="url_input">Adresse URL:</label> | ||
15 | <input id="url_input" type="url" name="url_input"> | ||
16 | </p> | ||
17 | <p> | ||
18 | <label>Placer le lien juste après cette entrée:</label> | 18 | <label>Placer le lien juste après cette entrée:</label> |
19 | <select id="location" name="location"> | 19 | <select id="location" name="location"> |
20 | <?= $this->options ?> | 20 | <?= $this->options ?> |