ArticleController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Article;
  4. use App\Form\ArticleType;
  5. use App\Repository\ArticleRepository;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\DependencyInjection\Attribute\Target;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Attribute\Route;
  12. use Symfony\Component\Security\Http\Attribute\IsGranted;
  13. use Symfony\Component\Workflow\WorkflowInterface;
  14. #[Route('/article')]
  15. #[IsGranted('ROLE_AUTHOR')]
  16. final class ArticleController extends AbstractController
  17. {
  18. #[Route(name: 'app_article_index', methods: ['GET'])]
  19. public function index(ArticleRepository $articleRepository): Response
  20. {
  21. return $this->render('article/index.html.twig', [
  22. 'articles' => $articleRepository->findBy(criteria: [], orderBy: ['publicationDate' => 'DESC']),
  23. ]);
  24. }
  25. #[Route('/new', name: 'app_article_new', methods: ['GET', 'POST'])]
  26. public function new(
  27. Request $request,
  28. EntityManagerInterface $entityManager,
  29. #[Target('blog_publishing')]
  30. WorkflowInterface $workflow
  31. ): Response
  32. {
  33. $article = new Article($this->getUser());
  34. $article->setContent('');
  35. $workflow->getMarking($article);
  36. $transitions = $workflow->getEnabledTransitions($article);
  37. $transitionsChoice = [];
  38. foreach($transitions as $transition) {
  39. $transitionsChoice[$transition->getName()] = $transition->getName();
  40. }
  41. $form = $this->createForm(ArticleType::class, $article, [
  42. 'transitions' => $transitionsChoice
  43. ]);
  44. $form->handleRequest($request);
  45. if ($form->isSubmitted() && $form->isValid()) {
  46. $publicationChoice = $form->get('publicationChoice')->getData();
  47. if ($publicationChoice !== null) {
  48. $workflow->apply($article, $publicationChoice);
  49. }
  50. $entityManager->persist($article);
  51. $entityManager->flush();
  52. return $this->redirectToRoute('app_article_index', [], Response::HTTP_SEE_OTHER);
  53. }
  54. return $this->render('article/new.html.twig', [
  55. 'article' => $article,
  56. 'form' => $form,
  57. ]);
  58. }
  59. #[Route('/{id}', name: 'app_article_show', methods: ['GET'])]
  60. #[IsGranted('view', 'article')]
  61. public function show(Article $article): Response
  62. {
  63. return $this->render('article/show.html.twig', [
  64. 'article' => $article,
  65. ]);
  66. }
  67. #[Route('/{id}/edit', name: 'app_article_edit', methods: ['GET', 'POST'])]
  68. #[IsGranted('edit', 'article')]
  69. public function edit(
  70. Request $request,
  71. Article $article,
  72. EntityManagerInterface $entityManager,
  73. #[Target('blog_publishing')]
  74. WorkflowInterface $workflow
  75. ): Response
  76. {
  77. $transitions = $workflow->getEnabledTransitions($article);
  78. $transitionsChoice = [];
  79. foreach($transitions as $transition) {
  80. $transitionsChoice[$transition->getName()] = $transition->getName();
  81. }
  82. $form = $this->createForm(ArticleType::class, $article, [
  83. 'transitions' => $transitionsChoice
  84. ]);
  85. $form->handleRequest($request);
  86. if ($form->isSubmitted() && $form->isValid()) {
  87. $publicationChoice = $form->get('publicationChoice')->getData();
  88. if ($publicationChoice !== null) {
  89. $workflow->apply($article, $publicationChoice);
  90. }
  91. $entityManager->flush();
  92. return $this->redirectToRoute('app_article_index', [], Response::HTTP_SEE_OTHER);
  93. }
  94. return $this->render('article/edit.html.twig', [
  95. 'article' => $article,
  96. 'form' => $form,
  97. ]);
  98. }
  99. #[Route('/{id}', name: 'app_article_delete', methods: ['POST'])]
  100. #[IsGranted('edit', 'article')]
  101. public function delete(Request $request, Article $article, EntityManagerInterface $entityManager): Response
  102. {
  103. if ($this->isCsrfTokenValid('delete'.$article->getId(), $request->getPayload()->getString('_token'))) {
  104. $entityManager->remove($article);
  105. $entityManager->flush();
  106. }
  107. return $this->redirectToRoute('app_article_index', [], Response::HTTP_SEE_OTHER);
  108. }
  109. }