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