src/Controller/PublicationController.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Publication;
  4. use App\Entity\Author;
  5. use App\Form\PublicationType;
  6. use App\Form\PercentageType;
  7. use App\Repository\PublicationRepository;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  14. use App\Repository\PublicationAuthorRepository;
  15. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  16. use App\Entity\Organization;
  17. /**
  18.  * @Route("/publication")
  19.  */
  20. class PublicationController extends AbstractController
  21. {
  22.     /**
  23.      * @Route("/", name="publication_index", methods={"GET"})
  24.      */
  25.     public function index(PublicationRepository $publicationRepository): Response
  26.     {
  27.         $publications $publicationRepository->findByValidated(true);
  28.         return $this->render('publication/index.html.twig', [
  29.             'title' => 'Publicaciones Validadas',
  30.             'button' => 'editar',
  31.             'publications' => $publications,
  32.         ]);
  33.     }
  34.     /**
  35.      * @Route("/mine", name="my_publications_index", methods={"GET"})
  36.      */
  37.     public function myIndex(PublicationRepository $publicationRepository): Response
  38.     {
  39.         $cAuthor $this->getUser()->getAuthor();
  40.         
  41.         $publications $cAuthor->getPublications();
  42.         // Agrupar publicaciones por año
  43.         $groupedPublications = [];
  44.         foreach ($publications as $publication) {
  45.             $year $publication->getPublishedYear();
  46.             if (!isset($groupedPublications[$year])) {
  47.                 $groupedPublications[$year] = [];
  48.             }
  49.             $groupedPublications[$year][] = $publication;
  50.         }
  51.         krsort($groupedPublications);
  52.         return $this->render('publication/mine.html.twig', [
  53.             'publications' => $publications,
  54.             'groupedPublications' => $groupedPublications,
  55.         ]);
  56.     }
  57.     /**
  58.      * @Route("/author/{id}", name="publications_author", methods={"GET"})
  59.      */
  60.     public function author(Author $author): Response
  61.     {        
  62.         $publications $author->getPublications();
  63.         return $this->render('publication/author.html.twig', [
  64.             'publications' => $publications,
  65.             'author' => $author,
  66.         ]);
  67.     }
  68.     /**
  69.      * @Route("/toValidate", name="publication_to_validate", methods={"GET"})
  70.      */
  71.     public function validateIndex(PublicationRepository $publicationRepository): Response
  72.     {
  73.         //$publications = $publicationRepository->findByValidated(false);
  74.         $publications $publicationRepository->findBy(['publishedYear' => 2024'validated' => false]);
  75.         return $this->render('publication/index.html.twig', [
  76.             'title' => 'Publicaciones por Validar',
  77.             'button' => 'validar',
  78.             'publications' => $publications,
  79.         ]);
  80.     }
  81.     /**
  82.      * @Route("/new", name="publication_new", methods={"GET", "POST"})
  83.      */
  84.     public function new(Request $requestEntityManagerInterface $entityManager): Response
  85.     {
  86.         $publication = new Publication();
  87.         $form $this->createForm(PublicationType::class, $publication);
  88.         $form->handleRequest($request);
  89.         if ($form->isSubmitted() && $form->isValid()) {
  90.             $entityManager->persist($publication);
  91.             $entityManager->flush();
  92.             return $this->redirectToRoute('publication_index', [], Response::HTTP_SEE_OTHER);
  93.         }
  94.         return $this->renderForm('publication/new.html.twig', [
  95.             'publication' => $publication,
  96.             'form' => $form,
  97.         ]);
  98.     }
  99.     
  100.     /**
  101.      * @Route("/byOrganization", name="publications_by_organization", methods={"GET", "POST"})
  102.      */
  103.     public function organization(Request $requestPublicationRepository $publicationRepository): Response
  104.     {
  105.         $form $this->createFormBuilder()
  106.             ->add('organization'EntityType::class, [
  107.                 'label' => 'Organización',
  108.                 'class' => Organization::class,
  109.                 'choice_label' => 'name',
  110.                 'expanded'     => false,
  111.                 'multiple'     => false,
  112.                 'attr' => ['class' => 'select2'],
  113.             ])
  114.             ->getForm();
  115.         $form->handleRequest($request);
  116.         $publications null;
  117.         if ($form->isSubmitted() && $form->isValid()) {
  118.             $organization $form->get('organization')->getData();
  119.             //find by organization and by api = expanded
  120.             $publications $publicationRepository->findByOrganization($organization);
  121.         }
  122.         return $this->renderForm('publication/byOrganization.html.twig', [
  123.             'publications' => $publications,
  124.             'form' => $form,
  125.         ]);
  126.     }
  127.     /**
  128.      * @Route("/{id}", name="publication_show", methods={"GET"})
  129.      */
  130.     public function show(Publication $publication): Response
  131.     {
  132.         return $this->render('publication/show.html.twig', [
  133.             'publication' => $publication,
  134.         ]);
  135.     }
  136.     /**
  137.      * @Route("/{id}/percentage", name="publication_percentage", methods={"GET", "POST"})
  138.      */
  139.     public function percentage(Request $requestPublication $publicationEntityManagerInterface $entityManager): Response
  140.     {
  141.         $form $this->createForm(PercentageType::class, $publication);
  142.         $form->handleRequest($request);
  143.         if ($form->isSubmitted() && $form->isValid()) {
  144.             $entityManager->flush();
  145.             return $this->redirectToRoute('my_publications_index', [], Response::HTTP_SEE_OTHER);
  146.         }
  147.         return $this->renderForm('publication/percentage.html.twig', [
  148.             'publication' => $publication,
  149.             'form' => $form,
  150.         ]);
  151.     }
  152.     /**
  153.      * @Route("/{id}/edit", name="publication_edit", methods={"GET", "POST"})
  154.      */
  155.     public function edit(Request $requestPublication $publicationEntityManagerInterface $entityManager,PublicationAuthorRepository $publicationAuthorRepository): Response
  156.     {
  157.         $form $this->createForm(PublicationType::class, $publication);
  158.         $form->add('deletingIds'HiddenType::class, [
  159.             'mapped' => false,
  160.             'data' => json_encode([]),
  161.         ]);
  162.         $form->handleRequest($request);
  163.         if ($form->isSubmitted() && $form->isValid()) {
  164.             $deletingIds json_decode($form->get('deletingIds')->getData());
  165.             foreach ($deletingIds as $deleteId) {
  166.                 $pa $publicationAuthorRepository->find($deleteId);
  167.                 $publication->removePublicationAuthor($pa);
  168.             }
  169.             $entityManager->flush();
  170.             return $this->redirectToRoute('publication_index', [], Response::HTTP_SEE_OTHER);
  171.         }
  172.         return $this->renderForm('publication/edit.html.twig', [
  173.             'publication' => $publication,
  174.             'form' => $form,
  175.         ]);
  176.     }
  177.     /**
  178.      * @Route("/{id}", name="publication_delete", methods={"POST"})
  179.      */
  180.     public function delete(Request $requestPublication $publicationEntityManagerInterface $entityManager): Response
  181.     {
  182.         if ($this->isCsrfTokenValid('delete'.$publication->getId(), $request->request->get('_token'))) {
  183.             $entityManager->remove($publication);
  184.             $entityManager->flush();
  185.         }
  186.         return $this->redirectToRoute('publication_index', [], Response::HTTP_SEE_OTHER);
  187.     }
  188. }