<?php
namespace App\Controller;
use App\Entity\Publication;
use App\Entity\Author;
use App\Form\PublicationType;
use App\Form\PercentageType;
use App\Repository\PublicationRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use App\Repository\PublicationAuthorRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use App\Entity\Organization;
/**
* @Route("/publication")
*/
class PublicationController extends AbstractController
{
/**
* @Route("/", name="publication_index", methods={"GET"})
*/
public function index(PublicationRepository $publicationRepository): Response
{
$publications = $publicationRepository->findByValidated(true);
return $this->render('publication/index.html.twig', [
'title' => 'Publicaciones Validadas',
'button' => 'editar',
'publications' => $publications,
]);
}
/**
* @Route("/mine", name="my_publications_index", methods={"GET"})
*/
public function myIndex(PublicationRepository $publicationRepository): Response
{
$cAuthor = $this->getUser()->getAuthor();
$publications = $cAuthor->getPublications();
// Agrupar publicaciones por año
$groupedPublications = [];
foreach ($publications as $publication) {
$year = $publication->getPublishedYear();
if (!isset($groupedPublications[$year])) {
$groupedPublications[$year] = [];
}
$groupedPublications[$year][] = $publication;
}
krsort($groupedPublications);
return $this->render('publication/mine.html.twig', [
'publications' => $publications,
'groupedPublications' => $groupedPublications,
]);
}
/**
* @Route("/author/{id}", name="publications_author", methods={"GET"})
*/
public function author(Author $author): Response
{
$publications = $author->getPublications();
return $this->render('publication/author.html.twig', [
'publications' => $publications,
'author' => $author,
]);
}
/**
* @Route("/toValidate", name="publication_to_validate", methods={"GET"})
*/
public function validateIndex(PublicationRepository $publicationRepository): Response
{
//$publications = $publicationRepository->findByValidated(false);
$publications = $publicationRepository->findBy(['publishedYear' => 2024, 'validated' => false]);
return $this->render('publication/index.html.twig', [
'title' => 'Publicaciones por Validar',
'button' => 'validar',
'publications' => $publications,
]);
}
/**
* @Route("/new", name="publication_new", methods={"GET", "POST"})
*/
public function new(Request $request, EntityManagerInterface $entityManager): Response
{
$publication = new Publication();
$form = $this->createForm(PublicationType::class, $publication);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($publication);
$entityManager->flush();
return $this->redirectToRoute('publication_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('publication/new.html.twig', [
'publication' => $publication,
'form' => $form,
]);
}
/**
* @Route("/byOrganization", name="publications_by_organization", methods={"GET", "POST"})
*/
public function organization(Request $request, PublicationRepository $publicationRepository): Response
{
$form = $this->createFormBuilder()
->add('organization', EntityType::class, [
'label' => 'Organización',
'class' => Organization::class,
'choice_label' => 'name',
'expanded' => false,
'multiple' => false,
'attr' => ['class' => 'select2'],
])
->getForm();
$form->handleRequest($request);
$publications = null;
if ($form->isSubmitted() && $form->isValid()) {
$organization = $form->get('organization')->getData();
//find by organization and by api = expanded
$publications = $publicationRepository->findByOrganization($organization);
}
return $this->renderForm('publication/byOrganization.html.twig', [
'publications' => $publications,
'form' => $form,
]);
}
/**
* @Route("/{id}", name="publication_show", methods={"GET"})
*/
public function show(Publication $publication): Response
{
return $this->render('publication/show.html.twig', [
'publication' => $publication,
]);
}
/**
* @Route("/{id}/percentage", name="publication_percentage", methods={"GET", "POST"})
*/
public function percentage(Request $request, Publication $publication, EntityManagerInterface $entityManager): Response
{
$form = $this->createForm(PercentageType::class, $publication);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();
return $this->redirectToRoute('my_publications_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('publication/percentage.html.twig', [
'publication' => $publication,
'form' => $form,
]);
}
/**
* @Route("/{id}/edit", name="publication_edit", methods={"GET", "POST"})
*/
public function edit(Request $request, Publication $publication, EntityManagerInterface $entityManager,PublicationAuthorRepository $publicationAuthorRepository): Response
{
$form = $this->createForm(PublicationType::class, $publication);
$form->add('deletingIds', HiddenType::class, [
'mapped' => false,
'data' => json_encode([]),
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$deletingIds = json_decode($form->get('deletingIds')->getData());
foreach ($deletingIds as $deleteId) {
$pa = $publicationAuthorRepository->find($deleteId);
$publication->removePublicationAuthor($pa);
}
$entityManager->flush();
return $this->redirectToRoute('publication_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('publication/edit.html.twig', [
'publication' => $publication,
'form' => $form,
]);
}
/**
* @Route("/{id}", name="publication_delete", methods={"POST"})
*/
public function delete(Request $request, Publication $publication, EntityManagerInterface $entityManager): Response
{
if ($this->isCsrfTokenValid('delete'.$publication->getId(), $request->request->get('_token'))) {
$entityManager->remove($publication);
$entityManager->flush();
}
return $this->redirectToRoute('publication_index', [], Response::HTTP_SEE_OTHER);
}
}