diff options
Diffstat (limited to 'src/controller/ajax.php')
-rw-r--r-- | src/controller/ajax.php | 88 |
1 files changed, 68 insertions, 20 deletions
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 | ||