diff options
Diffstat (limited to 'src/Controller/ArticleController.php')
| -rw-r--r-- | src/Controller/ArticleController.php | 244 |
1 files changed, 244 insertions, 0 deletions
diff --git a/src/Controller/ArticleController.php b/src/Controller/ArticleController.php new file mode 100644 index 0000000..075e3b4 --- /dev/null +++ b/src/Controller/ArticleController.php | |||
| @@ -0,0 +1,244 @@ | |||
| 1 | <?php | ||
| 2 | // src/Controller/ArticleController.php | ||
| 3 | |||
| 4 | declare(strict_types=1); | ||
| 5 | |||
| 6 | namespace App\Controller; | ||
| 7 | |||
| 8 | use Doctrine\ORM\EntityManager; | ||
| 9 | use Symfony\Component\HttpFoundation\Request; | ||
| 10 | use Symfony\Component\HttpFoundation\Response; | ||
| 11 | use Symfony\Component\HttpFoundation\JsonResponse; | ||
| 12 | use Symfony\Component\HttpFoundation\RedirectResponse; | ||
| 13 | use App\Entity\Node; | ||
| 14 | use App\Entity\Article; | ||
| 15 | use App\Entity\Presentation; | ||
| 16 | use App\Model\Model; | ||
| 17 | use App\Service\URL; | ||
| 18 | use App\Service\Security; | ||
| 19 | |||
| 20 | class ArticleController | ||
| 21 | { | ||
| 22 | static public function fetch(EntityManager $entityManager, Request $request): JsonResponse | ||
| 23 | { | ||
| 24 | if($request->query->has('id') && !empty($request->query->get('id')) && $request->query->has('last_article')){ | ||
| 25 | $id = (int)$request->get('id'); // type et nettoie | ||
| 26 | $model = new Model($entityManager); | ||
| 27 | $model->findNodeById($id); | ||
| 28 | $parent_block = $model->getNode(); | ||
| 29 | |||
| 30 | if(Presentation::hasPresentation($parent_block->getName())){ | ||
| 31 | $get_articles_return = $model->getNextArticles($parent_block, $request); | ||
| 32 | $bulk_data = $get_articles_return[0]; | ||
| 33 | |||
| 34 | if($parent_block->getName() === 'post_block'){ | ||
| 35 | $builder_name = 'App\View\PostBuilder'; | ||
| 36 | } | ||
| 37 | elseif($parent_block->getName() === 'news_block'){ | ||
| 38 | $builder_name = 'App\View\NewBuilder'; | ||
| 39 | } | ||
| 40 | else{ | ||
| 41 | return new JsonResponse(['success' => false, 'error' => 'server side error'], JsonResponse::HTTP_INTERNAL_SERVER_ERROR); | ||
| 42 | } | ||
| 43 | |||
| 44 | $html = ''; | ||
| 45 | foreach($bulk_data as $article){ | ||
| 46 | $builder = new $builder_name($article); | ||
| 47 | $html .= $builder->render(); | ||
| 48 | } | ||
| 49 | |||
| 50 | return new JsonResponse(['success' => true, 'html' => $html, 'truncated' => $get_articles_return[1]]); | ||
| 51 | } | ||
| 52 | else{ | ||
| 53 | return new JsonResponse(['success' => false, 'error' => 'server side error'], JsonResponse::HTTP_INTERNAL_SERVER_ERROR); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | else{ | ||
| 57 | return new JsonResponse(['success' => false, 'error' => 'bad parameters'], JsonResponse::HTTP_BAD_REQUEST); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | static public function editorSubmit(EntityManager $entityManager, array $json): JsonResponse | ||
| 62 | { | ||
| 63 | if(json_last_error() === JSON_ERROR_NONE){ | ||
| 64 | $id = $json['id']; | ||
| 65 | if(in_array($id[0], ['t', 'p', 'i', 'd'])){ | ||
| 66 | $id = substr($id, 1); | ||
| 67 | } | ||
| 68 | |||
| 69 | $model = new Model($entityManager); | ||
| 70 | $content = $json['content']; | ||
| 71 | |||
| 72 | // nettoyage | ||
| 73 | if(is_array($content)){ // cas d'une nouvelle "news" | ||
| 74 | foreach($content as $one_input){ | ||
| 75 | $one_input = Security::secureHTML($one_input); | ||
| 76 | } | ||
| 77 | } | ||
| 78 | else{ // autres cas | ||
| 79 | $content = Security::secureHTML($json['content']); | ||
| 80 | } | ||
| 81 | |||
| 82 | // nouvel article | ||
| 83 | if($json['id'][0] === 'n'){ // ici $id est un bloc | ||
| 84 | $section_id = (int)substr($id, 1); // id du bloc <section> | ||
| 85 | if(!$model->findNodeById($section_id)){ // erreur mauvais id | ||
| 86 | return new JsonResponse(['success' => false, 'error' => 'article_not_saved, bad id']); | ||
| 87 | } | ||
| 88 | $model->makeSectionNode(); | ||
| 89 | $section = $model->getNode(); | ||
| 90 | |||
| 91 | // ajout d'une news | ||
| 92 | if(is_array($content)){ | ||
| 93 | if($section->getPage()->getEndOfPath() !== $json['from']){ // erreur mauvais from | ||
| 94 | return new JsonResponse(['success' => false, 'error' => 'article_not_saved, bad from']); | ||
| 95 | } | ||
| 96 | |||
| 97 | $date = new \DateTime($content['d'] . ':' . (new \DateTime)->format('s')); // l'input type="datetime-local" ne donne pas les secondes, on les ajoute: 'hh:mm' . ':ss' | ||
| 98 | $article = new Article($content['i'], $date, $content['t'], $content['p']); | ||
| 99 | $article_node = new Node('new', count($section->getChildren()) + 1, $section, $section->getPage(), $article); | ||
| 100 | } | ||
| 101 | // ajout d'un post | ||
| 102 | else{ | ||
| 103 | $timestamp = time(); | ||
| 104 | $date = new \DateTime; | ||
| 105 | $date->setTimestamp($timestamp); | ||
| 106 | |||
| 107 | $article = new Article($content, $date); | ||
| 108 | $placement = $json['placement'] === 'first' ? 0 : count($section->getChildren()) + 1; // | ||
| 109 | $article_node = new Node('post', $placement, $section, $section->getPage(), $article); | ||
| 110 | |||
| 111 | if($json['placement'] === 'first'){ | ||
| 112 | $section->addChild($article_node); | ||
| 113 | $section->sortChildren(true); // mettre $article_node au début du tableau puis régénérer les positions (0 devient 1, 1 devient 2...) | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | $entityManager->persist($article_node); | ||
| 118 | $entityManager->flush(); | ||
| 119 | |||
| 120 | return new JsonResponse(['success' => true, 'article_id' => $article_node->getId()]); | ||
| 121 | } | ||
| 122 | // modification article | ||
| 123 | //else{} | ||
| 124 | |||
| 125 | if($model->makeArticleNode($id)){ // une entrée est trouvée | ||
| 126 | $node = $model->getArticleNode(); | ||
| 127 | switch($json['id'][0]){ | ||
| 128 | case 'i': | ||
| 129 | $node->getArticle()->setContent($content); | ||
| 130 | break; | ||
| 131 | case 'p': | ||
| 132 | $node->getArticle()->setPreview($content); // html de l'éditeur | ||
| 133 | break; | ||
| 134 | case 't': | ||
| 135 | $node->getArticle()->setTitle($content); // html de l'éditeur | ||
| 136 | break; | ||
| 137 | case 'd': | ||
| 138 | return new JsonResponse(['success' => false, 'message' => 'l\'action editor_submit ne supporte pas les dates, utiliser date_submit.']); | ||
| 139 | default: // modif article simple (id sans lettre devant) | ||
| 140 | $node->getArticle()->setContent($content); | ||
| 141 | } | ||
| 142 | $entityManager->flush(); | ||
| 143 | return new JsonResponse(['success' => true]); | ||
| 144 | } | ||
| 145 | else{ | ||
| 146 | return new JsonResponse(['success' => false, 'message' => 'article non identifié']); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | else{ | ||
| 150 | return new JsonResponse(['success' => false, 'message' => 'Erreur de décodage JSON']); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | static public function deleteArticle(EntityManager $entityManager, Request $request): Response | ||
| 155 | { | ||
| 156 | $model = new Model($entityManager); | ||
| 157 | |||
| 158 | if($request->headers->get('Content-Type') === 'application/json'){ | ||
| 159 | $id = json_decode($request->getContent(), true)['id']; | ||
| 160 | } | ||
| 161 | elseif($request->headers->get('Content-Type') === 'application/x-www-form-urlencoded'){ | ||
| 162 | $id = $request->query->get('id'); | ||
| 163 | } | ||
| 164 | // ni JSON ni form, c'est quoi? un POST vide? | ||
| 165 | else{ | ||
| 166 | return new Response('la méthode deleteArticle ne peut être appelée de cette manière'); | ||
| 167 | } | ||
| 168 | |||
| 169 | if(!$model->makeArticleNode($id, true)){ | ||
| 170 | $params = ['false', "Erreur 500 pas d\'article à supprimer"]; | ||
| 171 | } | ||
| 172 | else{ | ||
| 173 | $article = $model->getArticleNode(); | ||
| 174 | $section = $model->getNode(); | ||
| 175 | |||
| 176 | $entityManager->remove($article); | ||
| 177 | $section->removeChild($article); | ||
| 178 | $section->sortChildren(true); // régénère les positions | ||
| 179 | |||
| 180 | try{ | ||
| 181 | $entityManager->flush(); | ||
| 182 | $params = ['true', 'Article supprimé avec succès']; | ||
| 183 | } | ||
| 184 | catch(\Exception $e){ | ||
| 185 | $params = ['false', 'Erreur 500 ' . $e->getMessage()]; | ||
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | if($request->headers->get('Content-Type') === 'application/json'){ | ||
| 190 | return new JsonResponse( | ||
| 191 | ['success' => $params[0], 'message' => $params[1]], | ||
| 192 | $params[0] ? JsonResponse::HTTP_OK : JsonResponse::HTTP_INTERNAL_SERVER_ERROR | ||
| 193 | ); | ||
| 194 | } | ||
| 195 | elseif($request->headers->get('Content-Type') === 'application/x-www-form-urlencoded'){ | ||
| 196 | $url = new URL(['page' => $request->query->get('from') ?? '', 'success' => $params[0], 'message' => $params[1]]); | ||
| 197 | return new RedirectResponse((string)$url); | ||
| 198 | } | ||
| 199 | else{ | ||
| 200 | // cas inaccesible | ||
| 201 | throw new \Exception('la méthode deleteArticle ne peut être appelée de cette manière'); | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | static public function switchPositions(EntityManager $entityManager, array $json): JsonResponse | ||
| 206 | { | ||
| 207 | $model = new Model($entityManager); | ||
| 208 | $model->makeArticleNode($json['id1'], true); | ||
| 209 | $article1 = $model->getArticleNode(); | ||
| 210 | $section = $model->getNode(); | ||
| 211 | |||
| 212 | $section->sortChildren(true); // régénère les positions avant inversion | ||
| 213 | $article2 = null; | ||
| 214 | foreach($section->getChildren() as $child){ | ||
| 215 | if((string)$child->getId() === $json['id2']) // type string | ||
| 216 | { | ||
| 217 | $article2 = $child; | ||
| 218 | break; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | |||
| 222 | // inversion | ||
| 223 | $tmp = $article1->getPosition(); | ||
| 224 | $article1->setPosition($article2->getPosition()); | ||
| 225 | $article2->setPosition($tmp); | ||
| 226 | $entityManager->flush(); | ||
| 227 | |||
| 228 | return new JsonResponse(['success' => true]); | ||
| 229 | } | ||
| 230 | |||
| 231 | static public function dateSubmit(EntityManager $entityManager, array $json): JsonResponse | ||
| 232 | { | ||
| 233 | $id = substr($json['id'], 1); | ||
| 234 | $date = new \DateTime($json['date']); | ||
| 235 | |||
| 236 | $model = new Model($entityManager); | ||
| 237 | $model->makeArticleNode($id); | ||
| 238 | $node = $model->getArticleNode(); | ||
| 239 | $node->getArticle()->setDateTime($date); | ||
| 240 | $entityManager->flush(); | ||
| 241 | |||
| 242 | return new JsonResponse(['success' => true]); | ||
| 243 | } | ||
| 244 | } \ No newline at end of file | ||
