aboutsummaryrefslogtreecommitdiff
path: root/src/View/MenuBuilder.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/View/MenuBuilder.php')
-rw-r--r--src/View/MenuBuilder.php101
1 files changed, 101 insertions, 0 deletions
diff --git a/src/View/MenuBuilder.php b/src/View/MenuBuilder.php
new file mode 100644
index 0000000..f14cea7
--- /dev/null
+++ b/src/View/MenuBuilder.php
@@ -0,0 +1,101 @@
1<?php
2// src/View/MenuBuilder.php
3//
4// page Menu et chemins en mode admin, fonctionne avec menu.js
5
6declare(strict_types=1);
7
8namespace App\View;
9
10use App\Entity\Node;
11use App\Entity\Page;
12use App\Model\Model;
13use App\Service\URL;
14
15class MenuBuilder extends AbstractBuilder
16{
17 //private int $margin_left_multiplier = 29;
18 private string $options = '';
19
20 public function __construct(?Node $node, bool $template = true)
21 {
22 // $node peut $etre null parce qu'on ne construit pas de page dans MenuAndPathsController, on ne lit pas la table "node", etc
23 $viewFile = $node === null ? self::VIEWS_PATH . 'menu.php' : self::VIEWS_PATH . $node->getName() . '.php';
24
25 if(file_exists($viewFile))
26 {
27 if(IS_ADMIN){
28 $this->unfoldMenu(Model::$menu);
29
30 if($template){
31 $this->unfoldOptions(Model::$menu);
32 }
33 }
34 else{
35 header('Location: ' . new URL);
36 die;
37 }
38
39 // si faux, n'utilise pas le template
40 if($template){
41 ob_start();
42 require $viewFile; // insertion de $this->html généré par unfoldMenu
43 $this->html = ob_get_clean(); // pas de concaténation .= cette fois on écrase
44 }
45 }
46 }
47
48 private function unfoldMenu(Page $page): void
49 {
50 $this->html .= '<div class="level">' . "\n";
51
52 foreach($page->getChildren() as $entry)
53 {
54 $checked = $entry->isHidden() ? '' : 'checked';
55 $this->html .= '<div id="' . $entry->getId() . '" class="menu_edit_entry">
56 <img class="move_entry_icon" onclick="moveOneLevelUp(' . $entry->getId() . ')" src="assets/arrow-left.svg">
57 <img class="move_entry_icon" onclick="moveOneLevelDown(' . $entry->getId() . ')" src="assets/arrow-right.svg">
58 <img class="move_entry_icon" onclick="switchMenuPositions(' . $entry->getId() . ', \'up\')" src="assets/arrow-up.svg">
59 <img class="move_entry_icon" onclick="switchMenuPositions(' . $entry->getId() . ', \'down\')" src="assets/arrow-down.svg">
60 <span class="menu_entry_checkbox">
61 <input type="checkbox" ' . $checked . ' onclick="checkMenuEntry(' . $entry->getId() . ')">
62 </span>';
63
64 // entrées URL: bouton poubelle + nom du lien + adresse
65 if(str_starts_with($entry->getEndOfPath(), 'http')){
66 $this->html .= '<form style="display: inline;" id="delete-i' . $entry->getId() . '" method="post" action="' . new URL(['from' => 'menu_paths']) . '">
67 <input type="hidden" name="delete" value="' . $entry->getId() . '">
68 <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?\');">
69 </form>
70 <span class="url_name">
71 <button class="url_name"><input type="text" value="' . htmlspecialchars($entry->getPageName()) . '"></button>
72 <img class="move_entry_icon" src="assets/save.svg" onclick="editUrl(' . $entry->getId() . ', \'url_name\')">
73 </span>
74 <span class="url_content">
75 <input type="url" value="' . htmlspecialchars($entry->getEndOfPath()) . '" placeholder="https://..." onchange="controlURL(this)">
76 <img class="move_entry_icon" src="assets/save.svg" onclick="editUrl(' . $entry->getId() . ', \'url_content\')">
77 </span>';
78 }
79 else{
80 $this->html .= '<button>' . $entry->getPageName() . '</button>
81 <i class="path">' . $entry->getPagePath() . '</i>';
82 }
83
84 if(count($entry->getChildren()) > 0){
85 $this->unfoldMenu($entry);
86 }
87 $this->html .= '</div>' . "\n";
88 }
89 $this->html .= "</div>\n";
90 }
91
92 private function unfoldOptions(Page $page): void
93 {
94 foreach($page->getChildren() as $entry){
95 $this->options .= '<option value="' . $entry->getId() . '">' . $entry->getPageName() . "</option>\n";
96 if(count($entry->getChildren()) > 0){
97 $this->unfoldOptions($entry);
98 }
99 }
100 }
101} \ No newline at end of file