diff options
Diffstat (limited to 'src/controller')
-rw-r--r-- | src/controller/Director.php | 93 | ||||
-rw-r--r-- | src/controller/Security.php | 2 | ||||
-rw-r--r-- | src/controller/ajax.php | 88 |
3 files changed, 135 insertions, 48 deletions
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 | |||
13 | static public Menu $menu_data; // pour NavBuilder | 13 | static public Menu $menu_data; // pour NavBuilder |
14 | static public Path $page_path; // pour BreadcrumbBuilder | 14 | static public Path $page_path; // pour BreadcrumbBuilder |
15 | private Page $page; | 15 | private Page $page; |
16 | private Node $root_node; | 16 | private Node $node; |
17 | private Node $article; | ||
17 | 18 | ||
18 | public function __construct(EntityManager $entityManager) | 19 | public function __construct(EntityManager $entityManager, bool $for_display = false) |
19 | { | 20 | { |
20 | $this->entityManager = $entityManager; | 21 | $this->entityManager = $entityManager; |
21 | self::$menu_data = new Menu($entityManager); // Menu est un modèle mais pas une entité | 22 | if($for_display){ |
22 | self::$page_path = new Path(); | 23 | self::$menu_data = new Menu($entityManager); // Menu est un modèle mais pas une entité |
23 | $this->page = self::$page_path->getLast(); | 24 | self::$page_path = new Path(); |
24 | $this->root_node = new Node; // instance mère "vide" ne possédant rien d'autre que des enfants | 25 | $this->page = self::$page_path->getLast(); |
26 | } | ||
27 | $this->node = new Node; // instance mère "vide" ne possédant rien d'autre que des enfants | ||
25 | } | 28 | } |
26 | 29 | ||
30 | public function getNode(): Node | ||
31 | { | ||
32 | return $this->node; | ||
33 | } | ||
34 | public function getArticleNode(): Node | ||
35 | { | ||
36 | return $this->article; | ||
37 | } | ||
38 | |||
27 | public function makeRootNode(string $id = ''): void | 39 | public function makeRootNode(string $id = ''): void |
28 | { | 40 | { |
29 | // on récupère toutes les entrées | 41 | // on récupère toutes les entrées |
@@ -44,25 +56,10 @@ class Director | |||
44 | ->setParameter('id', $id) | 56 | ->setParameter('id', $id) |
45 | ->getResult(); | 57 | ->getResult(); |
46 | } | 58 | } |
47 | $this->feedObjects($bulk_data); | 59 | $this->feedRootNodeObjects($bulk_data); |
48 | } | ||
49 | |||
50 | public function makeArticleNode(string $id = ''): bool | ||
51 | { | ||
52 | $bulk_data = $this->entityManager | ||
53 | ->createQuery('SELECT n FROM App\Entity\Node n WHERE n.article_timestamp = :id') | ||
54 | ->setParameter('id', $id) | ||
55 | ->getResult(); | ||
56 | |||
57 | if(count($bulk_data) === 0){ | ||
58 | return false; | ||
59 | } | ||
60 | |||
61 | $this->root_node = $bulk_data[0]; | ||
62 | return true; | ||
63 | } | 60 | } |
64 | 61 | ||
65 | private function feedObjects(array $bulk_data): void // $bulk_data = tableau de Node | 62 | private function feedRootNodeObjects(array $bulk_data): void // $bulk_data = tableau de Node |
66 | { | 63 | { |
67 | // puis on les range | 64 | // puis on les range |
68 | // (attention, risque de disfonctionnement si les noeuds de 1er niveau ne sont pas récupérés en 1er dans la BDD) | 65 | // (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 | |||
71 | // premier niveau | 68 | // premier niveau |
72 | if($node->getParent() == null) | 69 | if($node->getParent() == null) |
73 | { | 70 | { |
74 | $this->root_node->addChild($node); | 71 | $this->node->addChild($node); |
75 | 72 | ||
76 | // spécifique page article | 73 | // spécifique page article |
77 | if($node->getName() === 'main' && $this->page->getEndOfPath() == 'article'){ | 74 | if($node->getName() === 'main' && $this->page->getEndOfPath() == 'article'){ |
@@ -94,8 +91,50 @@ class Director | |||
94 | } | 91 | } |
95 | } | 92 | } |
96 | 93 | ||
97 | public function getRootNode(): Node | 94 | // récupération d'un article pour modification |
98 | { | 95 | public function makeArticleNode(string $id = '', bool $get_section = false): bool |
99 | return $this->root_node; | 96 | { |
97 | if($get_section){ | ||
98 | $dql = 'SELECT n, p FROM App\Entity\Node n LEFT JOIN n.parent p WHERE n.article_timestamp = :id'; | ||
99 | } | ||
100 | else{ | ||
101 | $dql = 'SELECT n FROM App\Entity\Node n WHERE n.article_timestamp = :id'; | ||
102 | } | ||
103 | // n est l'article et p son $parent | ||
104 | $bulk_data = $this->entityManager | ||
105 | ->createQuery($dql) | ||
106 | ->setParameter('id', $id) | ||
107 | ->getResult(); | ||
108 | |||
109 | if(count($bulk_data) === 0){ | ||
110 | return false; | ||
111 | } | ||
112 | |||
113 | if($get_section){ | ||
114 | $this->article = $bulk_data[0]; | ||
115 | $this->makeSectionNode($bulk_data[0]->getParent()->getId()); | ||
116 | } | ||
117 | else{ | ||
118 | $this->article = $bulk_data[0]; | ||
119 | } | ||
120 | |||
121 | return true; | ||
122 | } | ||
123 | |||
124 | // récupération des articles d'un bloc <section> à la création d'un article | ||
125 | public function makeSectionNode(int $section_id): bool | ||
126 | { | ||
127 | $section = $this->entityManager->find('App\Entity\Node', (string)$section_id); | ||
128 | |||
129 | $bulk_data = $this->entityManager | ||
130 | ->createQuery('SELECT n FROM App\Entity\Node n WHERE n.parent = :parent') | ||
131 | ->setParameter('parent', $section) | ||
132 | ->getResult(); | ||
133 | |||
134 | foreach($bulk_data as $article){ | ||
135 | $section->addChild($article); // pas de flush, on ne va pas écrire dans la BDD à chaque nouvelle page | ||
136 | } | ||
137 | $this->node = $section; | ||
138 | return true; | ||
100 | } | 139 | } |
101 | } | 140 | } |
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 | |||
9 | 'safe'=>1, // protection contre les élements et attributs dangereux | 9 | 'safe'=>1, // protection contre les élements et attributs dangereux |
10 | 10 | ||
11 | // liste blanche d'éléments HTML | 11 | // liste blanche d'éléments HTML |
12 | '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', | 12 | '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', |
13 | 13 | ||
14 | // liste noire d'attributs HTML | 14 | // liste noire d'attributs HTML |
15 | 'deny_attribute'=> 'id, class' // on garde 'style' | 15 | '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 @@ | |||
3 | 3 | ||
4 | declare(strict_types=1); | 4 | declare(strict_types=1); |
5 | 5 | ||
6 | use App\Entity\Article; | ||
7 | use App\Entity\Node; | ||
8 | |||
6 | // détection des requêtes de tinymce | 9 | // détection des requêtes de tinymce |
7 | if($_SERVER['CONTENT_TYPE'] === 'application/json' && isset($_GET['action'])) | 10 | if($_SERVER['CONTENT_TYPE'] === 'application/json' && isset($_GET['action'])) |
8 | { | 11 | { |
@@ -15,13 +18,44 @@ if($_SERVER['CONTENT_TYPE'] === 'application/json' && isset($_GET['action'])) | |||
15 | if(json_last_error() === JSON_ERROR_NONE) | 18 | if(json_last_error() === JSON_ERROR_NONE) |
16 | { | 19 | { |
17 | $id = $json['id']; | 20 | $id = $json['id']; |
18 | $id[0] = 'i'; | ||
19 | $content = Security::secureString($json['content']); | 21 | $content = Security::secureString($json['content']); |
20 | |||
21 | $director = new Director($entityManager); | 22 | $director = new Director($entityManager); |
23 | |||
24 | // nouvel article | ||
25 | if($id[0] === 'n') | ||
26 | { | ||
27 | if($content === ''){ | ||
28 | echo json_encode(['success' => false, 'message' => 'pas de données à sauvegarder']); | ||
29 | die; | ||
30 | } | ||
31 | $section_id = (int)substr($id, 1); // id du bloc <section> | ||
32 | $director->makeSectionNode($section_id); | ||
33 | $node = $director->getNode(); // = <section> | ||
34 | |||
35 | $timestamp = time(); | ||
36 | $date = new \DateTime; | ||
37 | $date->setTimestamp($timestamp); | ||
38 | |||
39 | $article = new Article($content, $date); // le "current" timestamp est obtenu par la BDD | ||
40 | $article_node = new Node('article', 'i' . (string)$timestamp, [], count($node->getChildren()) + 1, $node, $node->getPage(), $article); | ||
41 | |||
42 | $entityManager->persist($article_node); | ||
43 | $entityManager->flush(); | ||
44 | |||
45 | // id_node tout juste généré | ||
46 | //$article_node->getId(); | ||
47 | |||
48 | echo json_encode(['success' => true, 'article_id' => $article_node->getArticleTimestamp()]); | ||
49 | die; | ||
50 | } | ||
51 | // modification article | ||
52 | else{ | ||
53 | $id[0] = 'i'; // id de l'article node | ||
54 | } | ||
55 | |||
22 | if($director->makeArticleNode($id)) // une entrée est trouvée | 56 | if($director->makeArticleNode($id)) // une entrée est trouvée |
23 | { | 57 | { |
24 | $node = $director->getRootNode(); | 58 | $node = $director->getArticleNode(); // article |
25 | switch($json['id'][0]){ | 59 | switch($json['id'][0]){ |
26 | case 'i': | 60 | case 'i': |
27 | $node->getArticle()->setContent($content); | 61 | $node->getArticle()->setContent($content); |
@@ -42,8 +76,9 @@ if($_SERVER['CONTENT_TYPE'] === 'application/json' && isset($_GET['action'])) | |||
42 | $entityManager->flush(); | 76 | $entityManager->flush(); |
43 | echo json_encode(['success' => true]); | 77 | echo json_encode(['success' => true]); |
44 | } | 78 | } |
45 | else{ | 79 | else |
46 | echo json_encode(['success' => false, 'message' => 'Aucune entrée trouvée en BDD']); | 80 | { |
81 | echo json_encode(['success' => false, 'message' => 'article non identifié']); | ||
47 | } | 82 | } |
48 | } | 83 | } |
49 | else{ | 84 | else{ |
@@ -53,16 +88,18 @@ if($_SERVER['CONTENT_TYPE'] === 'application/json' && isset($_GET['action'])) | |||
53 | } | 88 | } |
54 | elseif($_GET['action'] === 'delete_article' && isset($json['id'])) | 89 | elseif($_GET['action'] === 'delete_article' && isset($json['id'])) |
55 | { | 90 | { |
56 | $id = $json['id']; | ||
57 | |||
58 | $director = new Director($entityManager); | 91 | $director = new Director($entityManager); |
59 | $director->makeArticleNode($id); | 92 | $director->makeArticleNode($json['id'], true); |
60 | $node = $director->getRootNode(); | 93 | $article = $director->getArticleNode(); |
61 | $entityManager->remove($node); | 94 | $section = $director->getNode(); |
95 | |||
96 | $entityManager->remove($article); | ||
97 | $section->removeChild($article); | ||
98 | $section->sortChildren(true); // régénère les positions | ||
62 | $entityManager->flush(); | 99 | $entityManager->flush(); |
63 | 100 | ||
64 | // test avec une nouvelle requête qui ne devrait rien trouver | 101 | // test avec une nouvelle requête qui ne devrait rien trouver |
65 | if(!$director->makeArticleNode($id)) | 102 | if(!$director->makeArticleNode($json['id'])) |
66 | { | 103 | { |
67 | echo json_encode(['success' => true]); | 104 | echo json_encode(['success' => true]); |
68 | 105 | ||
@@ -78,14 +115,25 @@ if($_SERVER['CONTENT_TYPE'] === 'application/json' && isset($_GET['action'])) | |||
78 | elseif($_GET['action'] === 'switch_positions' && isset($json['id1']) && isset($json['id2'])) | 115 | elseif($_GET['action'] === 'switch_positions' && isset($json['id1']) && isset($json['id2'])) |
79 | { | 116 | { |
80 | $director = new Director($entityManager); | 117 | $director = new Director($entityManager); |
81 | $director->makeArticleNode($json['id1']); | 118 | $director->makeArticleNode($json['id1'], true); |
82 | $node1 = $director->getRootNode(); | 119 | $article1 = $director->getArticleNode(); |
83 | $director->makeArticleNode($json['id2']); | 120 | $section = $director->getNode(); |
84 | $node2 = $director->getRootNode(); | 121 | |
85 | 122 | $section->sortChildren(true); // régénère les positions avant inversion | |
86 | $tmp = $node1->getPosition(); | 123 | |
87 | $node1->setPosition($node2->getPosition()); | 124 | $article2; |
88 | $node2->setPosition($tmp); | 125 | foreach($section->getChildren() as $child){ |
126 | if($child->getArticleTimestamp() === $json['id2']) // type string | ||
127 | { | ||
128 | $article2 = $child; | ||
129 | break; | ||
130 | } | ||
131 | } | ||
132 | |||
133 | // inversion | ||
134 | $tmp = $article1->getPosition(); | ||
135 | $article1->setPosition($article2->getPosition()); | ||
136 | $article2->setPosition($tmp); | ||
89 | $entityManager->flush(); | 137 | $entityManager->flush(); |
90 | 138 | ||
91 | echo json_encode(['success' => true]); | 139 | echo json_encode(['success' => true]); |
@@ -99,7 +147,7 @@ if($_SERVER['CONTENT_TYPE'] === 'application/json' && isset($_GET['action'])) | |||
99 | 147 | ||
100 | $director = new Director($entityManager); | 148 | $director = new Director($entityManager); |
101 | $director->makeArticleNode($id); | 149 | $director->makeArticleNode($id); |
102 | $node = $director->getRootNode(); | 150 | $node = $director->getArticleNode(); |
103 | $node->getArticle()->setDateTime($date); | 151 | $node->getArticle()->setDateTime($date); |
104 | $entityManager->flush(); | 152 | $entityManager->flush(); |
105 | 153 | ||