src/Controller/FridgeController.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use App\Service\HuskyClient;
  9. use App\Entity\Fridge;
  10. use App\Entity\Product;
  11. use App\Form\FridgeType;
  12. use App\Repository\FridgeRepository;
  13. class FridgeController extends AbstractController
  14. {
  15. /**
  16. * @Route("/fridge/browse/{page}", defaults={"page"=0}, name="browse_fridge")
  17. */
  18. public function browseFridge(Request $request, $page): Response
  19. {
  20. if ($page == 0) {
  21. $page = 1;
  22. }
  23. $user = $this->getUser();
  24. $is_admin = ( array_search('ROLE_ADMIN', $user->getRoles()) !== False );
  25. $session = $request->getSession();
  26. $frepo = $this->getDoctrine()->getRepository(Fridge::class);
  27. if ( $is_admin )
  28. $fridges = $frepo->findAllByPage($page);
  29. else
  30. $fridges = $frepo->findBy(['owner' => $user]);
  31. $count = count($fridges);
  32. $sdate = (new \DateTimeImmutable())->modify('this monday');
  33. return $this->render('browse_fridge.html.twig', [
  34. 'controller_name' => 'FridgeController',
  35. 'fridges' => $fridges,
  36. 'start_date' => $sdate,
  37. 'page_title' => 'Frigoríficos',
  38. 'pages' => ['page_id' => 'browse_fridge', 'last' => floor(($count-1)/$frepo->getLinesPerPage())+1, 'current' => $page]
  39. ]);
  40. }
  41. /**
  42. * @Route("/fridge/edit/{fridge_id}", name="edit_fridge")
  43. */
  44. public function editFridge(Request $request, $fridge_id)
  45. {
  46. $frepo = $this->getDoctrine()->getRepository(Fridge::class);
  47. if ($fridge_id == 'new') {
  48. $fridge = new Fridge();
  49. } else {
  50. $fridge = $frepo->find($fridge_id);
  51. }
  52. $form = $this->createForm(FridgeType::class, $fridge);
  53. $form->handleRequest($request);
  54. if ($form->isSubmitted() && $form->isValid()) {
  55. $fridge = $form->getData();
  56. $entityManager = $this->getDoctrine()->getManager();
  57. $entityManager->persist($fridge);
  58. $entityManager->flush();
  59. if ($fridge_id == 'new') {
  60. $this->addFlash('success', 'El nuevo refrigerador se ha añadido con éxito');
  61. } else {
  62. $this->addFlash('success', 'Los cambios se han guardado');
  63. }
  64. return $this->redirectToRoute('browse_fridge');
  65. } else {
  66. return $this->render(
  67. 'edit_fridge.html.twig',
  68. array(
  69. 'form' => $form->createView(),
  70. 'page_title' => ($fridge_id == 'new' ? 'Nuevo refrigerador' : 'Editar refrigerador'),
  71. 'fridge_id' => $fridge_id,
  72. )
  73. );
  74. }
  75. }
  76. /**
  77. * @Route("/fridge/products/{fridge_id}", name="view_fridge_products")
  78. */
  79. public function viewFridgeProducts(Request $request, $fridge_id)
  80. {
  81. $frepo = $this->getDoctrine()->getRepository(Fridge::class);
  82. $prepo = $this->getDoctrine()->getRepository(Product::class);
  83. $fridge = $frepo->find($fridge_id);
  84. $products = $prepo->findLastProductStocks($fridge);
  85. return $this->render(
  86. 'view_fridge_products.html.twig',
  87. array(
  88. 'fridge' => $fridge,
  89. 'products' => $products,
  90. 'page_title' => 'Productos en refrigerador '.$fridge->getFriendlyName(),
  91. )
  92. );
  93. }
  94. }