<?php
namespace App\Controller\RevueManagement;
use App\Entity\LovManagement\NumeroStatus;
use App\Entity\RevueManagement\Image;
use App\Entity\RevueManagement\Numero2;
use App\Entity\RevueManagement\Numero2History;
use App\Entity\RevueManagement\Revue;
use App\Entity\RevueManagement\RevueHistory;
use App\Form\RevueManagement\Numero2Type;
use App\Form\RevueManagement\Numero2DeleteType;
use App\Manager\RevueManagement\ImageManager;
use App\Manager\RevueManagement\ZipManager;
use App\Message\RevueManagement\Numero2Message;
use App\Message\RevueManagement\Numero2UpdateMessage;
use App\Repository\RevueManagement\ImageRepository;
use App\Repository\RevueManagement\Numero2Repository;
use App\Repository\RevueManagement\PageRepository;
use App\Service\RevueManagement\Numero2Service;
use Doctrine\DBAL\Exception as DBALException;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use Omines\DataTablesBundle\Adapter\Doctrine\Event\ORMAdapterQueryEvent;
use Omines\DataTablesBundle\Adapter\Doctrine\ORMAdapter;
use Omines\DataTablesBundle\Adapter\Doctrine\ORMAdapterEvents;
use Omines\DataTablesBundle\Column\DateTimeColumn;
use Omines\DataTablesBundle\Column\TextColumn;
use Omines\DataTablesBundle\Column\TwigStringColumn;
use Omines\DataTablesBundle\DataTableFactory;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Spatie\PdfToImage\Pdf;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\String\Slugger\SluggerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
use Twig\Extension\StringLoaderExtension;
/**
* @Route("/admin/numero2")
*/
class Numero2Controller extends AbstractController
{
private $kernel;
private $messageBus;
private $slugger;
private $translator;
private $imageManager;
private $zipManager;
private $imageRepository;
private $numeroRepository;
private $serializer;
private $numeroService;
public function __construct(MessageBusInterface $messageBus, TranslatorInterface $translator, KernelInterface $kernel, SluggerInterface $slugger, ImageManager $imageManager, ZipManager $zipManager, ImageRepository $imageRepository, Numero2Repository $numeroRepository, SerializerInterface $serializer, Numero2Service $numeroService)
{
$this->kernel = $kernel;
$this->messageBus = $messageBus;
$this->slugger = $slugger;
$this->translator = $translator;
$this->imageManager = $imageManager;
$this->zipManager = $zipManager;
$this->imageRepository = $imageRepository;
$this->numeroRepository = $numeroRepository;
$this->serializer = $serializer;
$this->numeroService = $numeroService;
}
/**
* replaced by admin_numero_omines
*
* @Route("/index", name="admin_numero2_index", methods={"GET"})
* @Security("is_granted('ROLE_GESTION_NUMEROS')")
*/
public function index(Numero2Repository $numeroRepository): Response
{
return $this->render('RevueManagement/Numero2/index.html.twig', [
'numeros' => $numeroRepository->findBy(['isDeleted' => false], ['updateDate' => 'DESC']),
]);
}
/**
* @Route("/new/{id}", name="admin_revue_new_numero2", methods={"GET","POST"})
* @Security("is_granted('ROLE_GESTION_NUMEROS')")
*/
public function new(Request $request, Revue $revue): Response
{
$numero = new Numero2();
$numero->setRevue($revue);
$form = $this->createForm(Numero2Type::class, $numero);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if ($result = $this->numeroService->create($form, $numero)) {
return $this->redirectToRoute('admin_numero2_show', ['id' => $numero->getId()]);
}
}
return $this->render('RevueManagement/Numero2/new.html.twig', [
'numero' => $numero,
'form' => $form->createView(),
'revue' => $revue
]);
}
/**
* @Route("/end/{numero}", name="admin_numero2_end", methods={"GET"})
*/
public function end(Request $request, Numero2 $numero)
{
if ($numero->getNumeroStatus() != NumeroStatus::NUMERO_STATUS_END_TREATEMENT) {
return new JsonResponse(['end' => false]);
}
else {
return new JsonResponse(['end' => true, 'lastNumero2History' => $numero->getLastNumero2History()->getId()]);
}
}
/**
* @Route("/askTreatement/{id}", name="admin_numero2_ask_treatement", methods={"GET","POST"})
* @Security("is_granted('ROLE_GESTION_NUMEROS')")
*/
public function askTreatement(Request $request, Numero2 $numero): Response
{
$this->numeroService->treatement($numero);
//redirect to last result in revue controller, set value to numeroID
return $this->redirectToRoute('admin_revue_show', ['id' => $numero->getRevue()->getId(), 'numeroID' => $numero->getId()]);
//redirect to last result
//return $this->redirectToRoute('admin_indexation_numero2History', ['numero2History' => $numero->getLastNumero2History()->getId()]);
}
/**
* @Route("/edit/{id}", name="admin_numero2_edit", methods={"GET","POST"})
* Security("is_granted('ROLE_GESTION_NUMEROS')")
*/
public function edit(Request $request, Numero2 $numero): Response
{
$form = $this->createForm(Numero2Type::class, $numero);
$form->handleRequest($request);
$old_numero = $this->serializer->serialize($numero, 'json', ['groups' => 'detail']);
if ($form->isSubmitted() && $form->isValid()) {
if ($result = $this->numeroService->update($form, $numero, $old_numero)) {
return $this->redirectToRoute('admin_numero2_show', ['id' => $numero->getId()]);
}
}
return $this->render('RevueManagement/Numero2/edit.html.twig', [
'numero' => $numero,
'form' => $form->createView(),
]);
}
/**
* @Route("/edit_flash/{id}", name="admin_numero2_edit_flash", methods={"GET","POST"})
* @Security("is_granted('ROLE_GESTION_NUMEROS')")
*/
public function editFlash(Request $request, Numero2 $numero): Response
{
$em = $this->getDoctrine()->getManager();
$output = [];
$output['id'] = $request->get('pk');
$output['name'] = $request->get('name');
$output['value'] = $request->get('value');
switch ($request->get('name')) {
case 'description':
$numero->setDescription($request->get('value'));
break;
default:
break;
}
$em->persist($numero);
$em->flush();
return new JsonResponse($output, 200);
}
/**
* @Route("/show/{id}", name="admin_numero2_show", methods={"GET"})
* @Security("is_granted('ROLE_GESTION_NUMEROS')")
*/
public function show(Request $request, Numero2 $numero): Response
{
$form = $this->createForm(Numero2DeleteType::class, $numero);
return $this->render('RevueManagement/Numero2/show.html.twig', [
'numero' => $numero,
'form' => $form->createView(),
'action' => $request->get('action'),
]);
}
/**
* Desactive/Active un numero.
*
* @Route("/disable/{id}", name="admin_numero2_disable", methods={"GET","POST"})
* @Security("is_granted('ROLE_GESTION_NUMEROS')")
*/
public function disable(Numero2 $numero): Response
{
$em = $this->getDoctrine()->getManager();
if ($numero->getIsValid()) {
$numero->setIsValid(false);
$this->addFlash('success', $this->translator->trans('numero.flash.disable'));
$action = 'disable';
} else {
$numero->setIsValid(true);
$this->addFlash('success', $this->translator->trans('numero.flash.enable'));
$action = 'enable';
}
try {
$numero->setUpdateUser($this->getUser());
$em->persist($numero);
$em->flush();
return $this->redirectToRoute('admin_revue_show', ['id' => $numero->getRevue()->getId()]);
} catch (DBALException $exception) {
$this->addFlash('error', $exception->getMessage());
}
return $this->redirectToRoute('admin_numero2_show', ['id' => $numero->getId(), 'action' => $action]);
}
}