<?php
namespace App\Controller\higotrigo;
use App\Entity\HtExtra;
use App\Entity\HtFile;
use App\Entity\HtItem;
use App\Entity\HtMenu;
use App\Entity\User;
use App\Form\HtMenuType;
use App\Repository\HtItemRepository;
use App\Repository\HtMenuRepository;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\SerializerInterface;
/**
* @Route("/higotrigo/ht/menu")
*/
class HtMenuController extends AbstractController
{
/**
* @Route("/", name="app_ht_menu_index", methods={"GET"})
*/
public function index(HtMenuRepository $htMenuRepository): Response
{
return $this->render('higotrigo/ht_menu/index.html.twig', [
'ht_menus' => $htMenuRepository->findBy([], ['name' => 'ASC']),
]);
}
/**
* @Route("/new", name="app_ht_menu_new", methods={"GET", "POST"})
*/
public function new(Request $request, HtMenuRepository $htMenuRepository): Response
{
$htMenu = new HtMenu();
$htMenu->setBeneficio(0)
->setIva(10)
->setTotalPrice(0)
->setTemplate(true);
// $htExtra = new HtExtra();
// $htMenu->addHtExtra($htExtra);
$form = $this->createForm(HtMenuType::class, $htMenu);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = $this->getUser();
$htMenu->setCreatedId($user)
->setUpdatedId($user);
$htMenu = $this->_addUserToExtras($htMenu, $user);
$htMenuRepository->add($htMenu, true);
return $this->redirectToRoute('app_ht_menu_edit', [
'id' => $htMenu->getId()
], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('higotrigo/ht_menu/new.html.twig', [
'ht_menu' => $htMenu,
'form' => $form,
]);
}
/**
* @Route("/{id}", name="app_ht_menu_show", methods={"GET"})
*/
public function show(HtMenu $htMenu): Response
{
return $this->render('higotrigo/ht_menu/show.html.twig', [
'ht_menu' => $htMenu,
]);
}
/**
* @Route("/{id}/edit", name="app_ht_menu_edit", methods={"GET", "POST"})
*/
public function edit(Request $request, HtMenu $htMenu, HtMenuRepository $htMenuRepository, EntityManagerInterface $em): Response
{
$form = $this->createForm(HtMenuType::class, $htMenu);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = $this->getUser();
$htMenu->setUpdatedId($user);
$htMenu = $this->_addUserToExtras($htMenu, $user);
$htMenuRepository->add($htMenu, true);
if (!empty($htMenu->getHtItem())) {
$htFile = $htMenu->getHtItem()->getHtFile();
if ($htFile) { $this->_updateDateHtFile($htFile, $em); }
}
// Si se accede desde el modificar menú de un htFile
if($request->query->get('file')){
return $this->redirectToRoute('app_ht_file_edit', [ 'id' => $htFile->getId(), '_fragment' => 'service'], Response::HTTP_SEE_OTHER);
}
}
return $this->renderForm('higotrigo/ht_menu/edit.html.twig', [
'ht_menu' => $htMenu,
'form' => $form,
'file' => $request->query->get('file') ?? 0
]);
}
/**
* @Route("/{id}", name="app_ht_menu_delete", methods={"POST"})
*/
public function delete(Request $request, HtMenu $htMenu, HtMenuRepository $htMenuRepository): Response
{
try {
if ($this->isCsrfTokenValid('delete'.$htMenu->getId(), $request->request->get('_token'))) {
$htMenuRepository->remove($htMenu, true);
}
} catch (\Throwable $th) {
$this->addFlash('mensajeerror', 'El menú no se a podido borrar.');
}
return $this->redirectToRoute('app_ht_menu_index', [], Response::HTTP_SEE_OTHER);
}
/**
* @Route("/{id}/get-form", name="app_ht_menu_get_form", methods={"GET", "POST"})
*/
public function getForm(Request $request, HtMenu $htMenu, HtMenuRepository $htMenuRepository, EntityManagerInterface $em): Response
{
$form = $this->createForm(HtMenuType::class, $htMenu);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = $this->getUser();
$htMenu->setUpdatedId($user);
$htMenu = $this->_addUserToExtras($htMenu, $user);
$htMenuRepository->add($htMenu, true);
$htFile = $htMenu->getHtItem()->getHtFile();
if($htFile){
$this->_updateDateHtFile($htFile, $em);
}
}
return $this->renderForm('higotrigo/ht_menu/_form.html.twig', [
'ht_menu' => $htMenu,
'form' => $form,
'button_label' => 'Guardar', // Si no se envía esto, no aparecerán los escandallos puestos en el menú
'fileView' => true,
]);
}
/**
* @Route("/{menuId}/duplicate-to-ht-item/{itemId}", name="app_ht_menu_duplicate_to_ht_item", methods={"GET"})
* @ParamConverter("htMenu", class="App\Entity\HtMenu", options={"id" = "menuId"})
* @ParamConverter("htItem", class="App\Entity\HtItem", options={"id" = "itemId"})
*/
function duplicateToHtItem(HtMenu $htMenu, HtItem $htItem, HtMenuRepository $htMenuRepository, HtItemRepository $htItemRepository, SerializerInterface $serializerInterface) : JsonResponse {
$user = $this->getuser();
// Comprobar si el htItem ya tiene un menú previo asigado y si eso borrar la relación.
// if($htItem->getHtMenu() != null){
// $htItem->setHtMenu(null);
// $htItemRepository->add($htItem, true);
// }
$htMenuDuplicate = clone $htMenu;
$htMenuDuplicate->setId(null);
$htMenuDuplicate->setName('DUPLICADO DE: '. $htMenu->getName());
$htMenuDuplicate->setHtItem($htItem);
$htMenuDuplicate->setTemplate(false);
$htMenuDuplicate->setCreatedId($user);
$htMenuDuplicate->setUpdatedId($user);
foreach ($htMenuDuplicate->getHtExtras() as $htExtra) {
$htMenuDuplicate->removeHtExtra($htExtra);
$htExtraClone = clone $htExtra;
$htExtraClone->setId(null);
$htExtraClone->setHtMenu(null);
$htMenuDuplicate->addHtExtra($htExtraClone);
}
$htMenuRepository->add($htMenuDuplicate, true);
$data = $serializerInterface->serialize($htMenuDuplicate, 'json', [
AbstractNormalizer::GROUPS => ['htmenu:read'],
]);
return $this->json(json_decode($data), JsonResponse::HTTP_OK);
}
/**
* Recorremos los extras para añadirle el usuario que los crea
*/
private function _addUserToExtras(HtMenu $htMenu, User $user) : HtMenu {
$htExtras = $htMenu->getHtExtras();
foreach ($htExtras as $htExtra) {
$htMenu->removeHtExtra($htExtra);
if($htExtra->getCreatedId() == NULL){
$htExtra->setCreatedId($user);
}
$htExtra->setUpdatedId($user);
$htMenu->addHtExtra($htExtra);
}
return $htMenu;
}
/**
* Modificar la fecha update del htFile
*/
private function _updateDateHtFile(HtFile $htFile, EntityManagerInterface $em) : void {
$htFile->setUpdatedAt(new \DateTimeImmutable());
$em->persist($htFile);
$em->flush();
}
}