123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- namespace App\Controller;
- use App\Entity\Article;
- use App\Form\ArticleType;
- use App\Repository\ArticleRepository;
- use Doctrine\ORM\EntityManagerInterface;
- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
- use Symfony\Component\DependencyInjection\Attribute\Target;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\Routing\Attribute\Route;
- use Symfony\Component\Security\Http\Attribute\IsGranted;
- use Symfony\Component\Workflow\WorkflowInterface;
- #[Route('/article')]
- #[IsGranted('ROLE_AUTHOR')]
- final class ArticleController extends AbstractController
- {
- #[Route(name: 'app_article_index', methods: ['GET'])]
- public function index(ArticleRepository $articleRepository): Response
- {
- return $this->render('article/index.html.twig', [
- 'articles' => $articleRepository->findBy(criteria: [], orderBy: ['publicationDate' => 'DESC']),
- ]);
- }
- #[Route('/new', name: 'app_article_new', methods: ['GET', 'POST'])]
- public function new(
- Request $request,
- EntityManagerInterface $entityManager,
- #[Target('blog_publishing')]
- WorkflowInterface $workflow
- ): Response
- {
- $article = new Article($this->getUser());
- $article->setContent('');
- $workflow->getMarking($article);
- $transitions = $workflow->getEnabledTransitions($article);
- $transitionsChoice = [];
- foreach($transitions as $transition) {
- $transitionsChoice[$transition->getName()] = $transition->getName();
- }
-
- $form = $this->createForm(ArticleType::class, $article, [
- 'transitions' => $transitionsChoice
- ]);
- $form->handleRequest($request);
- if ($form->isSubmitted() && $form->isValid()) {
- $publicationChoice = $form->get('publicationChoice')->getData();
- if ($publicationChoice !== null) {
- $workflow->apply($article, $publicationChoice);
- }
- $entityManager->persist($article);
- $entityManager->flush();
- return $this->redirectToRoute('app_article_index', [], Response::HTTP_SEE_OTHER);
- }
- return $this->render('article/new.html.twig', [
- 'article' => $article,
- 'form' => $form,
- ]);
- }
- #[Route('/{id}', name: 'app_article_show', methods: ['GET'])]
- #[IsGranted('view', 'article')]
- public function show(Article $article): Response
- {
- return $this->render('article/show.html.twig', [
- 'article' => $article,
- ]);
- }
- #[Route('/{id}/edit', name: 'app_article_edit', methods: ['GET', 'POST'])]
- #[IsGranted('edit', 'article')]
- public function edit(
- Request $request,
- Article $article,
- EntityManagerInterface $entityManager,
- #[Target('blog_publishing')]
- WorkflowInterface $workflow
- ): Response
- {
- $transitions = $workflow->getEnabledTransitions($article);
- $transitionsChoice = [];
- foreach($transitions as $transition) {
- $transitionsChoice[$transition->getName()] = $transition->getName();
- }
-
- $form = $this->createForm(ArticleType::class, $article, [
- 'transitions' => $transitionsChoice
- ]);
- $form->handleRequest($request);
- if ($form->isSubmitted() && $form->isValid()) {
- $publicationChoice = $form->get('publicationChoice')->getData();
- if ($publicationChoice !== null) {
- $workflow->apply($article, $publicationChoice);
- }
- $entityManager->flush();
- return $this->redirectToRoute('app_article_index', [], Response::HTTP_SEE_OTHER);
- }
- return $this->render('article/edit.html.twig', [
- 'article' => $article,
- 'form' => $form,
- ]);
- }
- #[Route('/{id}', name: 'app_article_delete', methods: ['POST'])]
- #[IsGranted('edit', 'article')]
- public function delete(Request $request, Article $article, EntityManagerInterface $entityManager): Response
- {
- if ($this->isCsrfTokenValid('delete'.$article->getId(), $request->getPayload()->getString('_token'))) {
- $entityManager->remove($article);
- $entityManager->flush();
- }
- return $this->redirectToRoute('app_article_index', [], Response::HTTP_SEE_OTHER);
- }
- }
|