src/Controller/higotrigo/HtMenuController.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Controller\higotrigo;
  3. use App\Entity\HtExtra;
  4. use App\Entity\HtFile;
  5. use App\Entity\HtItem;
  6. use App\Entity\HtMenu;
  7. use App\Entity\User;
  8. use App\Form\HtMenuType;
  9. use App\Repository\HtItemRepository;
  10. use App\Repository\HtMenuRepository;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
  19. use Symfony\Component\Serializer\SerializerInterface;
  20. /**
  21.  * @Route("/higotrigo/ht/menu")
  22.  */
  23. class HtMenuController extends AbstractController
  24. {
  25.     /**
  26.      * @Route("/", name="app_ht_menu_index", methods={"GET"})
  27.      */
  28.     public function index(HtMenuRepository $htMenuRepository): Response
  29.     {
  30.         return $this->render('higotrigo/ht_menu/index.html.twig', [
  31.             'ht_menus' => $htMenuRepository->findBy([], ['name' => 'ASC']),
  32.         ]);
  33.     }
  34.     /**
  35.      * @Route("/new", name="app_ht_menu_new", methods={"GET", "POST"})
  36.      */
  37.     public function new(Request $requestHtMenuRepository $htMenuRepository): Response
  38.     {
  39.         $htMenu = new HtMenu();
  40.         $htMenu->setBeneficio(0)
  41.             ->setIva(10)
  42.             ->setTotalPrice(0)
  43.             ->setTemplate(true);
  44.         // $htExtra = new HtExtra();
  45.         // $htMenu->addHtExtra($htExtra);
  46.         $form $this->createForm(HtMenuType::class, $htMenu);
  47.         $form->handleRequest($request);
  48.         if ($form->isSubmitted() && $form->isValid()) {
  49.             $user $this->getUser();
  50.             $htMenu->setCreatedId($user)
  51.                 ->setUpdatedId($user);
  52.             $htMenu $this->_addUserToExtras($htMenu$user);
  53.             $htMenuRepository->add($htMenutrue);
  54.             
  55.             return $this->redirectToRoute('app_ht_menu_edit', [
  56.                 'id' => $htMenu->getId()
  57.             ], Response::HTTP_SEE_OTHER);
  58.         }
  59.         return $this->renderForm('higotrigo/ht_menu/new.html.twig', [
  60.             'ht_menu' => $htMenu,
  61.             'form' => $form,
  62.         ]);
  63.     }
  64.     /**
  65.      * @Route("/{id}", name="app_ht_menu_show", methods={"GET"})
  66.      */
  67.     public function show(HtMenu $htMenu): Response
  68.     {
  69.         return $this->render('higotrigo/ht_menu/show.html.twig', [
  70.             'ht_menu' => $htMenu,
  71.         ]);
  72.     }
  73.     /**
  74.      * @Route("/{id}/edit", name="app_ht_menu_edit", methods={"GET", "POST"})
  75.      */
  76.     public function edit(Request $requestHtMenu $htMenuHtMenuRepository $htMenuRepositoryEntityManagerInterface $em): Response
  77.     {
  78.         $form $this->createForm(HtMenuType::class, $htMenu);
  79.         $form->handleRequest($request);
  80.         if ($form->isSubmitted() && $form->isValid()) {
  81.             $user $this->getUser();
  82.             $htMenu->setUpdatedId($user);
  83.             $htMenu $this->_addUserToExtras($htMenu$user);
  84.             $htMenuRepository->add($htMenutrue);
  85.             if (!empty($htMenu->getHtItem())) {
  86.                 $htFile $htMenu->getHtItem()->getHtFile();
  87.                 if ($htFile) { $this->_updateDateHtFile($htFile$em); }
  88.             }
  89.             // Si se accede desde el modificar menú de un htFile
  90.             if($request->query->get('file')){
  91.                 return $this->redirectToRoute('app_ht_file_edit', [ 'id' =>  $htFile->getId(), '_fragment' => 'service'], Response::HTTP_SEE_OTHER);
  92.             }
  93.         }
  94.         
  95.         return $this->renderForm('higotrigo/ht_menu/edit.html.twig', [
  96.             'ht_menu' => $htMenu,
  97.             'form' => $form,
  98.             'file' => $request->query->get('file') ?? 0
  99.         ]);
  100.     }
  101.     /**
  102.      * @Route("/{id}", name="app_ht_menu_delete", methods={"POST"})
  103.      */
  104.     public function delete(Request $requestHtMenu $htMenuHtMenuRepository $htMenuRepository): Response
  105.     {
  106.         try {
  107.             if ($this->isCsrfTokenValid('delete'.$htMenu->getId(), $request->request->get('_token'))) {
  108.                 $htMenuRepository->remove($htMenutrue);
  109.             }
  110.         } catch (\Throwable $th) {
  111.             $this->addFlash('mensajeerror''El menú no se a podido borrar.');
  112.         }
  113.         
  114.         return $this->redirectToRoute('app_ht_menu_index', [], Response::HTTP_SEE_OTHER);
  115.     }
  116.     /**
  117.      * @Route("/{id}/get-form", name="app_ht_menu_get_form", methods={"GET", "POST"})
  118.      */
  119.     public function getForm(Request $requestHtMenu $htMenuHtMenuRepository $htMenuRepositoryEntityManagerInterface $em): Response
  120.     {
  121.         $form $this->createForm(HtMenuType::class, $htMenu);
  122.         $form->handleRequest($request);
  123.         if ($form->isSubmitted() && $form->isValid()) {
  124.             $user $this->getUser();
  125.             $htMenu->setUpdatedId($user);
  126.             $htMenu $this->_addUserToExtras($htMenu$user);
  127.             $htMenuRepository->add($htMenutrue);
  128.             $htFile $htMenu->getHtItem()->getHtFile();
  129.             if($htFile){
  130.                 $this->_updateDateHtFile($htFile$em);
  131.             }
  132.         }
  133.         return $this->renderForm('higotrigo/ht_menu/_form.html.twig', [
  134.             'ht_menu' => $htMenu,
  135.             'form' => $form,
  136.             'button_label' => 'Guardar'// Si no se envía esto, no aparecerán los escandallos puestos en el menú
  137.             'fileView' => true,
  138.         ]);
  139.     }
  140.     /**
  141.      * @Route("/{menuId}/duplicate-to-ht-item/{itemId}", name="app_ht_menu_duplicate_to_ht_item", methods={"GET"})
  142.      * @ParamConverter("htMenu", class="App\Entity\HtMenu", options={"id" = "menuId"})
  143.      * @ParamConverter("htItem", class="App\Entity\HtItem", options={"id" = "itemId"})
  144.      */
  145.     function duplicateToHtItem(HtMenu $htMenuHtItem $htItemHtMenuRepository $htMenuRepositoryHtItemRepository $htItemRepositorySerializerInterface $serializerInterface) : JsonResponse {
  146.         $user $this->getuser();
  147.         // Comprobar si el htItem ya tiene un menú previo asigado y si eso borrar la relación.
  148. //        if($htItem->getHtMenu() != null){
  149. //            $htItem->setHtMenu(null);
  150. //            $htItemRepository->add($htItem, true);
  151. //        }
  152.         $htMenuDuplicate = clone $htMenu;
  153.         $htMenuDuplicate->setId(null);
  154.         $htMenuDuplicate->setName('DUPLICADO DE: '$htMenu->getName());
  155.         $htMenuDuplicate->setHtItem($htItem);
  156.         $htMenuDuplicate->setTemplate(false);
  157.         $htMenuDuplicate->setCreatedId($user);
  158.         $htMenuDuplicate->setUpdatedId($user);
  159.         foreach ($htMenuDuplicate->getHtExtras() as $htExtra) {
  160.             $htMenuDuplicate->removeHtExtra($htExtra);
  161.             $htExtraClone = clone $htExtra;
  162.             $htExtraClone->setId(null);
  163.             $htExtraClone->setHtMenu(null);
  164.             $htMenuDuplicate->addHtExtra($htExtraClone);
  165.         }
  166.         $htMenuRepository->add($htMenuDuplicatetrue);
  167.         $data $serializerInterface->serialize($htMenuDuplicate'json', [
  168.             AbstractNormalizer::GROUPS => ['htmenu:read'],
  169.         ]);
  170.         return $this->json(json_decode($data), JsonResponse::HTTP_OK);
  171.     }
  172.     /**
  173.      * Recorremos los extras para añadirle el usuario que los crea
  174.      */
  175.     private function _addUserToExtras(HtMenu $htMenuUser $user) : HtMenu {
  176.         $htExtras $htMenu->getHtExtras();
  177.         foreach ($htExtras as $htExtra) {
  178.             $htMenu->removeHtExtra($htExtra);
  179.             if($htExtra->getCreatedId() == NULL){
  180.                 $htExtra->setCreatedId($user);
  181.             }
  182.             $htExtra->setUpdatedId($user);
  183.             $htMenu->addHtExtra($htExtra);
  184.             
  185.         }
  186.         return $htMenu;
  187.     }
  188.     /**
  189.      * Modificar la fecha update del htFile
  190.      */
  191.     private function _updateDateHtFile(HtFile $htFileEntityManagerInterface $em) : void {
  192.         $htFile->setUpdatedAt(new \DateTimeImmutable());
  193.         $em->persist($htFile);
  194.         $em->flush();
  195.     }
  196. }