From 68b6058e2a27fc251c117c4efeb141392a0c9736 Mon Sep 17 00:00:00 2001 From: polo Date: Sun, 6 Apr 2025 12:18:49 +0200 Subject: =?UTF-8?q?nouvel=20article,=20boutons=20dans=20les=20builders,=20?= =?UTF-8?q?makeArticleNode,=20JS=20MAJ=20page,=20tri=20quand=20d=C3=A9plac?= =?UTF-8?q?ement=20ou=20suppression?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controller/Director.php | 93 ++++++++++++++++++++++++++++++++------------- src/controller/Security.php | 2 +- src/controller/ajax.php | 88 ++++++++++++++++++++++++++++++++---------- 3 files changed, 135 insertions(+), 48 deletions(-) (limited to 'src/controller') diff --git a/src/controller/Director.php b/src/controller/Director.php index 5ff8f47..db84661 100644 --- a/src/controller/Director.php +++ b/src/controller/Director.php @@ -13,17 +13,29 @@ class Director static public Menu $menu_data; // pour NavBuilder static public Path $page_path; // pour BreadcrumbBuilder private Page $page; - private Node $root_node; + private Node $node; + private Node $article; - public function __construct(EntityManager $entityManager) + public function __construct(EntityManager $entityManager, bool $for_display = false) { $this->entityManager = $entityManager; - self::$menu_data = new Menu($entityManager); // Menu est un modèle mais pas une entité - self::$page_path = new Path(); - $this->page = self::$page_path->getLast(); - $this->root_node = new Node; // instance mère "vide" ne possédant rien d'autre que des enfants + if($for_display){ + self::$menu_data = new Menu($entityManager); // Menu est un modèle mais pas une entité + self::$page_path = new Path(); + $this->page = self::$page_path->getLast(); + } + $this->node = new Node; // instance mère "vide" ne possédant rien d'autre que des enfants } + public function getNode(): Node + { + return $this->node; + } + public function getArticleNode(): Node + { + return $this->article; + } + public function makeRootNode(string $id = ''): void { // on récupère toutes les entrées @@ -44,25 +56,10 @@ class Director ->setParameter('id', $id) ->getResult(); } - $this->feedObjects($bulk_data); - } - - public function makeArticleNode(string $id = ''): bool - { - $bulk_data = $this->entityManager - ->createQuery('SELECT n FROM App\Entity\Node n WHERE n.article_timestamp = :id') - ->setParameter('id', $id) - ->getResult(); - - if(count($bulk_data) === 0){ - return false; - } - - $this->root_node = $bulk_data[0]; - return true; + $this->feedRootNodeObjects($bulk_data); } - private function feedObjects(array $bulk_data): void // $bulk_data = tableau de Node + private function feedRootNodeObjects(array $bulk_data): void // $bulk_data = tableau de Node { // puis on les range // (attention, risque de disfonctionnement si les noeuds de 1er niveau ne sont pas récupérés en 1er dans la BDD) @@ -71,7 +68,7 @@ class Director // premier niveau if($node->getParent() == null) { - $this->root_node->addChild($node); + $this->node->addChild($node); // spécifique page article if($node->getName() === 'main' && $this->page->getEndOfPath() == 'article'){ @@ -94,8 +91,50 @@ class Director } } - public function getRootNode(): Node - { - return $this->root_node; + // récupération d'un article pour modification + public function makeArticleNode(string $id = '', bool $get_section = false): bool + { + if($get_section){ + $dql = 'SELECT n, p FROM App\Entity\Node n LEFT JOIN n.parent p WHERE n.article_timestamp = :id'; + } + else{ + $dql = 'SELECT n FROM App\Entity\Node n WHERE n.article_timestamp = :id'; + } + // n est l'article et p son $parent + $bulk_data = $this->entityManager + ->createQuery($dql) + ->setParameter('id', $id) + ->getResult(); + + if(count($bulk_data) === 0){ + return false; + } + + if($get_section){ + $this->article = $bulk_data[0]; + $this->makeSectionNode($bulk_data[0]->getParent()->getId()); + } + else{ + $this->article = $bulk_data[0]; + } + + return true; + } + + // récupération des articles d'un bloc
à la création d'un article + public function makeSectionNode(int $section_id): bool + { + $section = $this->entityManager->find('App\Entity\Node', (string)$section_id); + + $bulk_data = $this->entityManager + ->createQuery('SELECT n FROM App\Entity\Node n WHERE n.parent = :parent') + ->setParameter('parent', $section) + ->getResult(); + + foreach($bulk_data as $article){ + $section->addChild($article); // pas de flush, on ne va pas écrire dans la BDD à chaque nouvelle page + } + $this->node = $section; + return true; } } diff --git a/src/controller/Security.php b/src/controller/Security.php index 818a2bd..f9092e2 100644 --- a/src/controller/Security.php +++ b/src/controller/Security.php @@ -9,7 +9,7 @@ class Security 'safe'=>1, // protection contre les élements et attributs dangereux // liste blanche d'éléments HTML - 'elements'=> 'h1, h2, h3, h4, h5, h6, p, s, em, span, strong, a, ul, ol, li, sup, sub, code, blockquote, div, pre, table, caption, colgroup, col, tbody, tr, th, td, figure, img, figcaption, iframe, small', + 'elements'=> 'h1, h2, h3, h4, h5, h6, p, br, s, em, span, strong, a, ul, ol, li, sup, sub, code, blockquote, div, pre, table, caption, colgroup, col, tbody, tr, th, td, figure, img, figcaption, iframe, small', // liste noire d'attributs HTML 'deny_attribute'=> 'id, class' // on garde 'style' diff --git a/src/controller/ajax.php b/src/controller/ajax.php index 86acd39..b5c2e51 100644 --- a/src/controller/ajax.php +++ b/src/controller/ajax.php @@ -3,6 +3,9 @@ declare(strict_types=1); +use App\Entity\Article; +use App\Entity\Node; + // détection des requêtes de tinymce if($_SERVER['CONTENT_TYPE'] === 'application/json' && isset($_GET['action'])) { @@ -15,13 +18,44 @@ if($_SERVER['CONTENT_TYPE'] === 'application/json' && isset($_GET['action'])) if(json_last_error() === JSON_ERROR_NONE) { $id = $json['id']; - $id[0] = 'i'; $content = Security::secureString($json['content']); - $director = new Director($entityManager); + + // nouvel article + if($id[0] === 'n') + { + if($content === ''){ + echo json_encode(['success' => false, 'message' => 'pas de données à sauvegarder']); + die; + } + $section_id = (int)substr($id, 1); // id du bloc
+ $director->makeSectionNode($section_id); + $node = $director->getNode(); // =
+ + $timestamp = time(); + $date = new \DateTime; + $date->setTimestamp($timestamp); + + $article = new Article($content, $date); // le "current" timestamp est obtenu par la BDD + $article_node = new Node('article', 'i' . (string)$timestamp, [], count($node->getChildren()) + 1, $node, $node->getPage(), $article); + + $entityManager->persist($article_node); + $entityManager->flush(); + + // id_node tout juste généré + //$article_node->getId(); + + echo json_encode(['success' => true, 'article_id' => $article_node->getArticleTimestamp()]); + die; + } + // modification article + else{ + $id[0] = 'i'; // id de l'article node + } + if($director->makeArticleNode($id)) // une entrée est trouvée { - $node = $director->getRootNode(); + $node = $director->getArticleNode(); // article switch($json['id'][0]){ case 'i': $node->getArticle()->setContent($content); @@ -42,8 +76,9 @@ if($_SERVER['CONTENT_TYPE'] === 'application/json' && isset($_GET['action'])) $entityManager->flush(); echo json_encode(['success' => true]); } - else{ - echo json_encode(['success' => false, 'message' => 'Aucune entrée trouvée en BDD']); + else + { + echo json_encode(['success' => false, 'message' => 'article non identifié']); } } else{ @@ -53,16 +88,18 @@ if($_SERVER['CONTENT_TYPE'] === 'application/json' && isset($_GET['action'])) } elseif($_GET['action'] === 'delete_article' && isset($json['id'])) { - $id = $json['id']; - $director = new Director($entityManager); - $director->makeArticleNode($id); - $node = $director->getRootNode(); - $entityManager->remove($node); + $director->makeArticleNode($json['id'], true); + $article = $director->getArticleNode(); + $section = $director->getNode(); + + $entityManager->remove($article); + $section->removeChild($article); + $section->sortChildren(true); // régénère les positions $entityManager->flush(); // test avec une nouvelle requête qui ne devrait rien trouver - if(!$director->makeArticleNode($id)) + if(!$director->makeArticleNode($json['id'])) { echo json_encode(['success' => true]); @@ -78,14 +115,25 @@ if($_SERVER['CONTENT_TYPE'] === 'application/json' && isset($_GET['action'])) elseif($_GET['action'] === 'switch_positions' && isset($json['id1']) && isset($json['id2'])) { $director = new Director($entityManager); - $director->makeArticleNode($json['id1']); - $node1 = $director->getRootNode(); - $director->makeArticleNode($json['id2']); - $node2 = $director->getRootNode(); - - $tmp = $node1->getPosition(); - $node1->setPosition($node2->getPosition()); - $node2->setPosition($tmp); + $director->makeArticleNode($json['id1'], true); + $article1 = $director->getArticleNode(); + $section = $director->getNode(); + + $section->sortChildren(true); // régénère les positions avant inversion + + $article2; + foreach($section->getChildren() as $child){ + if($child->getArticleTimestamp() === $json['id2']) // type string + { + $article2 = $child; + break; + } + } + + // inversion + $tmp = $article1->getPosition(); + $article1->setPosition($article2->getPosition()); + $article2->setPosition($tmp); $entityManager->flush(); echo json_encode(['success' => true]); @@ -99,7 +147,7 @@ if($_SERVER['CONTENT_TYPE'] === 'application/json' && isset($_GET['action'])) $director = new Director($entityManager); $director->makeArticleNode($id); - $node = $director->getRootNode(); + $node = $director->getArticleNode(); $node->getArticle()->setDateTime($date); $entityManager->flush(); -- cgit v1.2.3