diff options
Diffstat (limited to 'src/controller/ArticleController.php')
| -rw-r--r-- | src/controller/ArticleController.php | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/src/controller/ArticleController.php b/src/controller/ArticleController.php new file mode 100644 index 0000000..e3e4edb --- /dev/null +++ b/src/controller/ArticleController.php | |||
| @@ -0,0 +1,169 @@ | |||
| 1 | <?php | ||
| 2 | // src/controller/ArticleController.php | ||
| 3 | |||
| 4 | declare(strict_types=1); | ||
| 5 | |||
| 6 | use App\Entity\Node; | ||
| 7 | use App\Entity\Article; | ||
| 8 | use Doctrine\ORM\EntityManager; | ||
| 9 | |||
| 10 | class ArticleController | ||
| 11 | { | ||
| 12 | static public function editorSubmit(EntityManager $entityManager, array $json): void | ||
| 13 | { | ||
| 14 | if(json_last_error() === JSON_ERROR_NONE) | ||
| 15 | { | ||
| 16 | $id = $json['id']; | ||
| 17 | $director = new Director($entityManager); | ||
| 18 | |||
| 19 | // cas d'une nouvelle "news" | ||
| 20 | if(is_array($json['content'])){ | ||
| 21 | foreach($json['content'] as $one_input){ | ||
| 22 | $one_input = Security::secureString($one_input); | ||
| 23 | } | ||
| 24 | $content = $json['content']; | ||
| 25 | } | ||
| 26 | else{ | ||
| 27 | $content = Security::secureString($json['content']); | ||
| 28 | } | ||
| 29 | |||
| 30 | // nouvel article | ||
| 31 | if($id[0] === 'n') | ||
| 32 | { | ||
| 33 | $section_id = (int)substr($id, 1); // id du bloc <section> | ||
| 34 | $director->findNodeById($section_id); | ||
| 35 | $director->makeSectionNode(); | ||
| 36 | $node = $director->getNode(); // = <section> | ||
| 37 | |||
| 38 | if(is_array($content)){ | ||
| 39 | $date = new \DateTime($content['d']); | ||
| 40 | $article = new Article($content['i'], $date, $content['t'], $content['p']); | ||
| 41 | $article_node = new Node('new', 'i' . (string)$date->getTimestamp(), [], count($node->getChildren()) + 1, $node, $node->getPage(), $article); | ||
| 42 | |||
| 43 | // id_node tout juste généré | ||
| 44 | //$article_node->getId(); | ||
| 45 | } | ||
| 46 | else{ | ||
| 47 | $timestamp = time(); | ||
| 48 | $date = new \DateTime; | ||
| 49 | $date->setTimestamp($timestamp); | ||
| 50 | |||
| 51 | $article = new Article($content, $date); // le "current" timestamp est obtenu par la BDD | ||
| 52 | $article_node = new Node('article', 'i' . (string)$timestamp, [], count($node->getChildren()) + 1, $node, $node->getPage(), $article); | ||
| 53 | } | ||
| 54 | |||
| 55 | $entityManager->persist($article_node); | ||
| 56 | $entityManager->flush(); | ||
| 57 | |||
| 58 | echo json_encode(['success' => true, 'article_id' => $article_node->getArticleTimestamp()]); | ||
| 59 | die; | ||
| 60 | } | ||
| 61 | // modification article | ||
| 62 | else{ | ||
| 63 | $id[0] = 'i'; // id de l'article node | ||
| 64 | } | ||
| 65 | |||
| 66 | if($director->makeArticleNode($id)) // une entrée est trouvée | ||
| 67 | { | ||
| 68 | $node = $director->getArticleNode(); // article | ||
| 69 | switch($json['id'][0]){ | ||
| 70 | case 'i': | ||
| 71 | $node->getArticle()->setContent($content); | ||
| 72 | break; | ||
| 73 | case 'p': | ||
| 74 | $node->getArticle()->setPreview($content); // html de l'éditeur | ||
| 75 | break; | ||
| 76 | case 't': | ||
| 77 | $node->getArticle()->setTitle($content); // html de l'éditeur | ||
| 78 | break; | ||
| 79 | case 'd': | ||
| 80 | echo json_encode(['success' => false, 'message' => 'l\'action editor_submit ne supporte pas les dates, utiliser date_submit.']); | ||
| 81 | die; | ||
| 82 | default: | ||
| 83 | echo json_encode(['success' => false, 'message' => 'identifiant non utilisable']); | ||
| 84 | die; | ||
| 85 | } | ||
| 86 | $entityManager->flush(); | ||
| 87 | echo json_encode(['success' => true]); | ||
| 88 | } | ||
| 89 | else | ||
| 90 | { | ||
| 91 | echo json_encode(['success' => false, 'message' => 'article non identifié']); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | else{ | ||
| 95 | echo json_encode(['success' => false, 'message' => 'Erreur de décodage JSON']); | ||
| 96 | } | ||
| 97 | die; | ||
| 98 | } | ||
| 99 | |||
| 100 | static public function deleteArticle(EntityManager $entityManager, array $json): void | ||
| 101 | { | ||
| 102 | $director = new Director($entityManager); | ||
| 103 | $director->makeArticleNode($json['id'], true); | ||
| 104 | $article = $director->getArticleNode(); | ||
| 105 | $section = $director->getNode(); | ||
| 106 | |||
| 107 | $entityManager->remove($article); | ||
| 108 | $section->removeChild($article); | ||
| 109 | $section->sortChildren(true); // régénère les positions | ||
| 110 | $entityManager->flush(); | ||
| 111 | |||
| 112 | // test avec une nouvelle requête qui ne devrait rien trouver | ||
| 113 | if(!$director->makeArticleNode($json['id'])) | ||
| 114 | { | ||
| 115 | echo json_encode(['success' => true]); | ||
| 116 | |||
| 117 | // on pourrait afficher une notification "toast" | ||
| 118 | } | ||
| 119 | else{ | ||
| 120 | http_response_code(500); | ||
| 121 | echo json_encode(['success' => false, 'message' => 'Erreur lors de la suppression de l\'article.']); | ||
| 122 | } | ||
| 123 | die; | ||
| 124 | } | ||
| 125 | |||
| 126 | static public function switchPositions(EntityManager $entityManager, array $json): void | ||
| 127 | { | ||
| 128 | $director = new Director($entityManager); | ||
| 129 | $director->makeArticleNode($json['id1'], true); | ||
| 130 | $article1 = $director->getArticleNode(); | ||
| 131 | $section = $director->getNode(); | ||
| 132 | |||
| 133 | $section->sortChildren(true); // régénère les positions avant inversion | ||
| 134 | |||
| 135 | $article2 = null; | ||
| 136 | foreach($section->getChildren() as $child){ | ||
| 137 | if($child->getArticleTimestamp() === $json['id2']) // type string | ||
| 138 | { | ||
| 139 | $article2 = $child; | ||
| 140 | break; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | // inversion | ||
| 145 | $tmp = $article1->getPosition(); | ||
| 146 | $article1->setPosition($article2->getPosition()); | ||
| 147 | $article2->setPosition($tmp); | ||
| 148 | $entityManager->flush(); | ||
| 149 | |||
| 150 | echo json_encode(['success' => true]); | ||
| 151 | die; | ||
| 152 | } | ||
| 153 | |||
| 154 | static public function dateSubmit(EntityManager $entityManager, array $json): void | ||
| 155 | { | ||
| 156 | $id = $json['id']; | ||
| 157 | $id[0] = 'i'; | ||
| 158 | $date = new DateTime($json['date']); | ||
| 159 | |||
| 160 | $director = new Director($entityManager); | ||
| 161 | $director->makeArticleNode($id); | ||
| 162 | $node = $director->getArticleNode(); | ||
| 163 | $node->getArticle()->setDateTime($date); | ||
| 164 | $entityManager->flush(); | ||
| 165 | |||
| 166 | echo json_encode(['success' => true]); | ||
| 167 | die; | ||
| 168 | } | ||
| 169 | } \ No newline at end of file | ||
