From eb3e1eb8c8365d3b3d1d39f24314ba420255afc2 Mon Sep 17 00:00:00 2001 From: polo Date: Mon, 21 Apr 2025 20:36:10 +0200 Subject: page menu et chemin, partie1 --- src/controller/Director.php | 2 +- src/controller/ajax.php | 44 ++++++++++++++++++++- src/controller/installation.php | 10 ++++- src/controller/password.php | 2 +- src/model/Position.php | 63 +++++++++++++++++++++++++++++ src/model/entities/Node.php | 33 ++-------------- src/model/entities/Page.php | 38 ++++++++++-------- src/view/FooterBuilder.php | 2 +- src/view/HeaderBuilder.php | 2 + src/view/MenuBuilder.php | 88 +++++++++++++++++++++++++++++++++++++++++ src/view/NavBuilder.php | 9 ++++- src/view/templates/header.php | 2 +- src/view/templates/menu.php | 15 +++++++ 13 files changed, 254 insertions(+), 56 deletions(-) create mode 100644 src/model/Position.php create mode 100644 src/view/MenuBuilder.php create mode 100644 src/view/templates/menu.php (limited to 'src') diff --git a/src/controller/Director.php b/src/controller/Director.php index b7be9b8..a2528ed 100644 --- a/src/controller/Director.php +++ b/src/controller/Director.php @@ -11,7 +11,7 @@ class Director { private EntityManager $entityManager; static public Menu $menu_data; // pour NavBuilder - static public Path $page_path; // pour BreadcrumbBuilder + static public ?Path $page_path = null; // pour $current dans NavBuilder et pour BreadcrumbBuilder private Page $page; private Node $node; private Node $article; diff --git a/src/controller/ajax.php b/src/controller/ajax.php index a4b61e4..c774bf3 100644 --- a/src/controller/ajax.php +++ b/src/controller/ajax.php @@ -6,7 +6,7 @@ declare(strict_types=1); use App\Entity\Article; use App\Entity\Node; -// détection des requêtes de tinymce +// détection des requêtes de tinymce ou touchant aux articles if($_SERVER['CONTENT_TYPE'] === 'application/json' && isset($_GET['action'])) { // récupération des données @@ -204,6 +204,48 @@ if(strpos($_SERVER['CONTENT_TYPE'], 'multipart/form-data') !== false && isset($_ die; } +if($_SERVER['CONTENT_TYPE'] === 'application/json' && isset($_GET['menu_edit'])) +{ + // récupération des données + $data = file_get_contents('php://input'); + $json = json_decode($data, true); + + if($_GET['menu_edit'] === 'switch_positions' && isset($json['id1']) && isset($json['id2'])) + { + //$menu = new Menu($entityManager); + Director::$menu_data = new Menu($entityManager); + + $id1 = $json['id1']; + $id2 = $json['id2']; + + // vérifier qu'ils ont le même parent + $page1 = Director::$menu_data->findPageById((int)$id1); + $page2 = Director::$menu_data->findPageById((int)$id2); + + // double le contrôle fait en JS + if($page1->getParent() === $page2->getParent()) // comparaison stricte d'objet (même instance du parent?) + { + // inversion + $tmp = $page1->getPosition(); + $page1->setPosition($page2->getPosition()); + $page2->setPosition($tmp); + Director::$menu_data->sortChildren(true); // modifie tableau children + $entityManager->flush(); + + // menu utilisant les nouvelles données + //Director::$page_path = new Path(); + $nav_builder = new NavBuilder(); // builder appelé sans envoi du noeud correspondant + + echo json_encode(['success' => true, 'path1' => '', 'path2' => '', 'nav' => $nav_builder->render()]); + } + else{ + echo json_encode(['success' => false]); + } + + die; + } +} + // détection des requêtes de type XHR, pas d'utilité pour l'instant /*elseif(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'){ echo "requête XHR reçue par le serveur"; diff --git a/src/controller/installation.php b/src/controller/installation.php index c2b38fd..ff168eb 100644 --- a/src/controller/installation.php +++ b/src/controller/installation.php @@ -80,9 +80,9 @@ function makeStartPage(EntityManager $entityManager){ $accueil = new Page('Accueil', 'accueil', true, true, 1, NULL); $connection = new Page('Connexion', 'connexion', true, false, NULL, NULL); $article = new Page('Article', 'article', true, false, NULL, NULL); + $menu_paths = new Page("Menu et chemins", 'menu_chemins', true, false, NULL, NULL); $edit_page = new Page("Modification d'une page", 'modif_page', true, false, NULL, NULL); $new_page = new Page('Nouvelle page', 'nouvelle_page', true, false, NULL, NULL); - $edit_paths = new Page("Menu et chemins", 'menu_chemins', true, false, NULL, NULL); /* -- table node -- */ // paramètres: name_node, article_timestamp, attributes, position, parent, page, article @@ -95,6 +95,8 @@ function makeStartPage(EntityManager $entityManager){ $head_login = new Node('head', NULL, ["stop" => true, 'css_array' => ['body', 'head', 'nav', 'main'], 'js_array' => ['main']], 1, NULL, $connection, NULL); $login = new Node('login', NULL, [], 1, $main, $connection, NULL); $head_article = new Node('head', NULL, ['css_array' => ['body', 'head', 'nav', 'main', 'foot'], 'js_array' => ['main']], 1, NULL, $article, NULL); + $head_edit_menu = new Node('head', NULL, ['css_array' => ['body', 'head', 'nav', 'main', 'menu'], 'js_array' => ['main']], 1, NULL, $menu_paths, NULL); + $edit_menu = new Node('menu', NULL, [], 1, $main, $menu_paths, NULL); /* -- table image -- */ // paramètres: file_name, file_path, file_path_mini, mime_type, alt @@ -111,13 +113,14 @@ function makeStartPage(EntityManager $entityManager){ $footer_data = new NodeData(["adresse" => "17, rue Raymonde Folgoas Guillou, 29120 Pont-l’Abbé", "contact_nom" => "Les Nageurs Bigoudens", "e_mail" => "nb.secretariat@orange.fr", "logo_footer" => "assets/logo-nb-et-ffn.png"], $footer); $head_login_data = new NodeData(["description" => "Connexion", "title" => "Connexion"], $head_login, new ArrayCollection([$favicon])); $head_article_data = new NodeData(["description" => "", "title" => ""], $head_article, new ArrayCollection([$favicon])); + $head_edit_menu_data = new NodeData(["description" => "Menu et chemins", "title" => "Menu et chemins"], $head_edit_menu, new ArrayCollection([$favicon])); $entityManager->persist($accueil); $entityManager->persist($connection); $entityManager->persist($article); + $entityManager->persist($menu_paths); $entityManager->persist($edit_page); $entityManager->persist($new_page); - $entityManager->persist($edit_paths); $entityManager->persist($head_accueil); $entityManager->persist($header); $entityManager->persist($nav); @@ -127,6 +130,8 @@ function makeStartPage(EntityManager $entityManager){ $entityManager->persist($head_login); $entityManager->persist($login); $entityManager->persist($head_article); + $entityManager->persist($head_edit_menu); + $entityManager->persist($edit_menu); $entityManager->persist($favicon); $entityManager->persist($logo); $entityManager->persist($facebook); @@ -137,6 +142,7 @@ function makeStartPage(EntityManager $entityManager){ $entityManager->persist($footer_data); $entityManager->persist($head_login_data); $entityManager->persist($head_article_data); + $entityManager->persist($head_edit_menu_data); $entityManager->flush(); header('Location: ' . new URL); diff --git a/src/controller/password.php b/src/controller/password.php index 47db637..66a617a 100644 --- a/src/controller/password.php +++ b/src/controller/password.php @@ -307,7 +307,7 @@ function getUser(string $login, EntityManager $entityManager): ?User } -function disconnect(EntityManager $entityManager) +function disconnect() { // nettoyage complet $_SESSION = []; // mémoire vive diff --git a/src/model/Position.php b/src/model/Position.php new file mode 100644 index 0000000..74d173a --- /dev/null +++ b/src/model/Position.php @@ -0,0 +1,63 @@ +children); $i++) + { + $tmp = $this->children[$i]; + $j = $i - 1; + + // Déplacez les éléments du tableau qui sont plus grands que la clé + // à une position devant leur position actuelle + while ($j >= 0 && $this->children[$j]->getPosition() > $tmp->getPosition()) { + $this->children[$j + 1] = $this->children[$j]; + $j = $j - 1; + } + $this->children[$j + 1] = $tmp; + } + + foreach ($this->children as $child) { + if (count($child->children) > 0) { + $child->sortChildren($reposition); + } + } + + // nouvelles positions (tableau $children => BDD) + if($reposition){ + $i = 1; + foreach($this->children as $child){ + $child->setPosition($i); + $i++; + } + } + } + + /*private function sortChildren(): void + { + $iteration = count($this->children); + while($iteration > 1) + { + for($i = 0; $i < $iteration - 1; $i++) + { + if($this->children[$i]->getPosition() > $this->children[$i + 1]->getPosition()) + { + $tmp = $this->children[$i]; + $this->children[$i] = $this->children[$i + 1]; + $this->children[$i + 1] = $tmp; + } + } + $iteration--; + } + }*/ +} \ No newline at end of file diff --git a/src/model/entities/Node.php b/src/model/entities/Node.php index a52a7e6..103163b 100644 --- a/src/model/entities/Node.php +++ b/src/model/entities/Node.php @@ -12,6 +12,8 @@ use Doctrine\ORM\Mapping as ORM; #[ORM\Table(name: TABLE_PREFIX . "node")] class Node { + use \Position; + #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(type: "integer")] @@ -135,36 +137,7 @@ class Node $this->children[] = $child; $this->sortChildren(false); } - // utiliser $position pour afficher les éléments dans l'ordre - public function sortChildren(bool $reposition = false): void - { - // ordre du tableau des enfants - // inefficace quand des noeuds ont la même position - - // tri par insertion - for($i = 1; $i < count($this->children); $i++) - { - $tmp = $this->children[$i]; - $j = $i - 1; - - // Déplacez les éléments du tableau qui sont plus grands que la clé - // à une position devant leur position actuelle - while ($j >= 0 && $this->children[$j]->getPosition() > $tmp->getPosition()) { - $this->children[$j + 1] = $this->children[$j]; - $j = $j - 1; - } - $this->children[$j + 1] = $tmp; - } - - // nouvelles positions - if($reposition){ - $i = 1; - foreach($this->children as $child){ - $child->setPosition($i); - $i++; - } - } - } + public function removeChild(self $child): void { foreach($this->children as $key => $object){ diff --git a/src/model/entities/Page.php b/src/model/entities/Page.php index c40a297..c30305c 100644 --- a/src/model/entities/Page.php +++ b/src/model/entities/Page.php @@ -13,6 +13,8 @@ use Doctrine\Common\Collections\ArrayCollection; #[ORM\Table(name: TABLE_PREFIX . "page")] class Page { + use \Position; + #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(type: "integer")] @@ -56,11 +58,11 @@ class Page $this->children = new ArrayCollection(); } - // getters - /*public function getId(): int + // getters/setters + public function getId(): int { return $this->id_page; - }*/ + } public function getPageName(): string { return $this->name_page; @@ -85,6 +87,10 @@ class Page { return $this->position; } + public function setPosition(int $position): void + { + $this->position = $position; + } public function getParent(): ?Page { return $this->parent; @@ -105,25 +111,23 @@ class Page public function addChild(self $child): void { $this->children[] = $child; - $this->sortChildren(); + $this->sortChildren(false); } - // utiliser $position pour afficher les éléments dans l'ordre - private function sortChildren(): void + public function findPageById(int $id): ?Page { - $iteration = count($this->children); - while($iteration > 1) - { - for($i = 0; $i < $iteration - 1; $i++) - { - if($this->children[$i]->getPosition() > $this->children[$i + 1]->getPosition()) - { - $tmp = $this->children[$i]; - $this->children[$i] = $this->children[$i + 1]; - $this->children[$i + 1] = $tmp; + $target = null; + foreach($this->children as $page){ + if($page->getId() === $id){ + return $page; + } + if(count($page->getChildren()) > 0){ + $target = $page->findPageById($id); + if($target !== null){ + return $target; } } - $iteration--; } + return $target; } } diff --git a/src/view/FooterBuilder.php b/src/view/FooterBuilder.php index 49da71c..5a7748f 100644 --- a/src/view/FooterBuilder.php +++ b/src/view/FooterBuilder.php @@ -29,7 +29,7 @@ class FooterBuilder extends AbstractBuilder $empty_admin_zone = 'empty_admin_zone'; $link_edit_page = new URL(['page' => CURRENT_PAGE, 'action' => 'modif_page']); $link_new_page = new URL(['from' => CURRENT_PAGE, 'page' => 'nouvelle_page']); - $link_change_paths = new URL(['from' => CURRENT_PAGE, 'page' => 'menu_chemins']); + $link_change_paths = new URL(['page' => 'menu_chemins']); $link_change_password = new URL(['from' => CURRENT_PAGE, 'action' => 'modif_mdp']); isset($_GET['id']) ? $link_change_password->addParams(['id' => $_GET['id']]) : ''; diff --git a/src/view/HeaderBuilder.php b/src/view/HeaderBuilder.php index eef2663..28aae13 100644 --- a/src/view/HeaderBuilder.php +++ b/src/view/HeaderBuilder.php @@ -16,7 +16,9 @@ class HeaderBuilder extends AbstractBuilder { if($child->getName() === 'nav'){ $this->nav = $child; + // actuellement le noeud nav ne contient aucune info utile et l'envoyer à NavBuilder est inutile $nav_builder = new NavBuilder($this->nav); + $nav = $nav_builder->render(); } elseif($child->getName() === 'breadcrumb'){ diff --git a/src/view/MenuBuilder.php b/src/view/MenuBuilder.php new file mode 100644 index 0000000..0fcfbe5 --- /dev/null +++ b/src/view/MenuBuilder.php @@ -0,0 +1,88 @@ +getName() . '.php'; + + if(file_exists($viewFile)) + { + /*if(!empty($node->getNodeData()->getData())) + { + extract($node->getNodeData()->getData()); + }*/ + + // ajouter un article + $new_article = ''; + if($_SESSION['admin']) + { + $this->unfoldMenu(Director::$menu_data, 0 - $this->margin_left_multiplier); + } + else{ + header('Location: ' . new URL); + die; + } + + ob_start(); + require $viewFile; + $this->html = ob_get_clean(); // pas de concaténation ici, on écrase + } + } + + private function unfoldMenu(Page $menu, int $margin_left): void + { + $margin_left += $this->margin_left_multiplier; + $this->html .= '
' . "\n"; + + foreach($menu->getChildren() as $entry) + { + $div_style = 'margin-left: ' . $margin_left . 'px;'; + $checked = $entry->IsInMenu() ? 'checked' : ''; + $this->html .= '
+ + + + + + + + '; + + if(str_starts_with($entry->getEndOfPath(), 'http')){ + $this->html .= ' + ' . $entry->getEndOfPath() . ' + '; + } + else{ + $this->html .= '' . $entry->getPagePath() . ''; + } + + + // supprimer me label "visible" et griser le texte et bouton en JS à la place + + /* + => flèche gauche: position = position du parent + 1, parent = grand-parent, recalculer les positions + => flèche droite: position = nombre d'éléments de la fraterie + 1, l'élément précédent devient le parent + => flèches haut et bas: inversement de position, comme pour les noeuds, mais dans la table page + => checkbox: in_menu ^= 1 + */ + + if(count($entry->getChildren()) > 0){ + $this->unfoldMenu($entry, $margin_left); + } + $this->html .= '
' . "\n"; + } + $this->html .= "
\n"; + $margin_left -= $this->margin_left_multiplier; + } +} \ No newline at end of file diff --git a/src/view/NavBuilder.php b/src/view/NavBuilder.php index 603fcfa..2718569 100644 --- a/src/view/NavBuilder.php +++ b/src/view/NavBuilder.php @@ -1,5 +1,7 @@ html .= ''; } diff --git a/src/view/templates/header.php b/src/view/templates/header.php index 0bf4396..4b633a6 100644 --- a/src/view/templates/header.php +++ b/src/view/templates/header.php @@ -1,7 +1,7 @@
-
+ diff --git a/src/view/templates/menu.php b/src/view/templates/menu.php new file mode 100644 index 0000000..a7f318e --- /dev/null +++ b/src/view/templates/menu.php @@ -0,0 +1,15 @@ + \ No newline at end of file -- cgit v1.2.3