src/Controller/MainController.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Datatable\IngredientsData;
  4. use App\Entity\Categories;
  5. use App\Entity\IngredientCategories;
  6. use App\Entity\Ingredients;
  7. use App\Entity\UnitMeasures;
  8. use App\Helper\Generator;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. class MainController extends AbstractController
  17. {
  18.     /**
  19.      * @Route("/", name="home")
  20.      */
  21.     public function indexAction()
  22.     {
  23.         if ($this->isGranted('ROLE_PARTNER')) {
  24.             return $this->redirectToRoute('app_partners_vouchers');
  25.         }
  26.         return $this->redirectToRoute('users');
  27.     }
  28.     /**
  29.      * @Route("/categories", name="categories")
  30.      * @Security("is_granted('ROLE_ADMIN')", statusCode=404, message="Resource not found.")
  31.      */
  32.     public function categoriesAction(EntityManagerInterface $em)
  33.     {
  34.         $page = array(
  35.             'pageTitle' => 'Categories',
  36.             'activePage' => 'Categories',
  37.             'data' => $em
  38.                 ->getRepository(Categories::class)
  39.                 ->findBy([
  40.                     'active' => TRUE,
  41.                 ]),
  42.         );
  43.         return $this->render('categories/index.html.twig'$page);
  44.     }
  45.     /**
  46.      * @Route("/categories/create", name="create_categories")
  47.      * @Template("categories/create.html.twig")
  48.      * @Security("is_granted('ROLE_ADMIN')", statusCode=404, message="Resource not found.")
  49.      */
  50.     public function createCategoryAction(Request $requestEntityManagerInterface $em)
  51.     {
  52.         $category = new Categories();
  53.         $form $this->createForm(\App\Form\Category::class, $category, array(
  54.             'action' => $this->generateUrl('create_categories'),
  55.         ));
  56.         $form->handleRequest($request);
  57.         if ($form->isSubmitted() && $form->isValid()) {
  58.             $category $form->getData();
  59.             $category->setActive(TRUE);
  60.             $file $category->getImage();
  61.             $fileName Generator::generateUniqueFileName() . '.' $file->guessExtension();
  62.             $file->move(
  63.                 $this->getParameter('category_path'),
  64.                 $fileName
  65.             );
  66.             $category->setImage($fileName);
  67.             $em->persist($category);
  68.             $em->flush();
  69.             return $this->redirectToRoute('categories');
  70.         }
  71.         return array(
  72.             'pageTitle' => 'Categories',
  73.             'activePage' => 'Categories',
  74.             'form' => $form->createView(),
  75.         );
  76.     }
  77.     /**
  78.      * @Route("/categories/update/{id}", name="update_categories")
  79.      * @Template("categories/update.html.twig")
  80.      * @Security("is_granted('ROLE_ADMIN')", statusCode=404, message="Resource not found.")
  81.      */
  82.     public function updateCategoryAction($idRequest $requestEntityManagerInterface $em)
  83.     {
  84.         $category $em->getRepository(Categories::class)
  85.             ->findOneBy(['id' => $id'active' => TRUE]);
  86.         $form $this->createForm(\App\Form\Category::class, $category, array(
  87.             'action' => $this->generateUrl('update_categories', ['id' => $id]),
  88.         ));
  89.         $currentFilename $category->getImage();
  90.         $form->handleRequest($request);
  91.         if ($form->isSubmitted() && $form->isValid()) {
  92.             $category $form->getData();
  93.             $file $category->getImage();
  94.             if ($file) {
  95.                 $fileName Generator::generateUniqueFileName() . '.' $file->guessExtension();
  96.                 $file->move(
  97.                     $this->getParameter('category_path'),
  98.                     $fileName
  99.                 );
  100.                 $category->setImage($fileName);
  101.             } else {
  102.                 $category->setImage($currentFilename);
  103.             }
  104.             $em->persist($category);
  105.             $em->flush();
  106.             return $this->redirectToRoute('categories');
  107.         }
  108.         return array(
  109.             'pageTitle' => 'Categories',
  110.             'activePage' => 'Categories',
  111.             'form' => $form->createView(),
  112.         );
  113.     }
  114.     /**
  115.      * @Route("/categories/delete/{id}", name="delete_category")
  116.      * @Security("is_granted('ROLE_ADMIN')", statusCode=404, message="Resource not found.")
  117.      */
  118.     public function deleteCategoryAction($idEntityManagerInterface $em)
  119.     {
  120.         $category $em->getRepository(Categories::class)
  121.             ->findOneBy(['id' => $id]);
  122.         if ($category) {
  123.             $category->setActive(FALSE);
  124.             $em->persist($category);
  125.             $em->flush();
  126.         }
  127.         return $this->redirectToRoute('categories');
  128.     }
  129.     /**
  130.      * @Route("/unit_measures", name="unit_measures")
  131.      * @Security("is_granted('ROLE_ADMIN')", statusCode=404, message="Resource not found.")
  132.      */
  133.     public function unitMeasuresAction(EntityManagerInterface $em)
  134.     {
  135.         $page = array(
  136.             'pageTitle' => 'Unit Measures',
  137.             'activePage' => 'Unit Measures',
  138.             'data' => $em
  139.                 ->getRepository(UnitMeasures::class)
  140.                 ->findBy([
  141.                     'active' => TRUE,
  142.                 ]),
  143.         );
  144.         return $this->render('unit_measures/index.html.twig'$page);
  145.     }
  146.     /**
  147.      * @Route("/unit_measures/create", name="create_unit_measures")
  148.      * @Template("unit_measures/create.html.twig")
  149.      * @Security("is_granted('ROLE_ADMIN')", statusCode=404, message="Resource not found.")
  150.      */
  151.     public function createUnitMeasuresAction(Request $requestEntityManagerInterface $em)
  152.     {
  153.         $unit = new UnitMeasures();
  154.         $form $this->createForm(\App\Form\UnitMeasure::class, $unit, array(
  155.             'action' => $this->generateUrl('create_unit_measures'),
  156.         ));
  157.         $form->handleRequest($request);
  158.         if ($form->isSubmitted() && $form->isValid()) {
  159.             $unit $form->getData();
  160.             $unit->setActive(TRUE);
  161.             $em->persist($unit);
  162.             $em->flush();
  163.             return $this->redirectToRoute('unit_measures');
  164.         }
  165.         return array(
  166.             'pageTitle' => 'Unit Measures',
  167.             'activePage' => 'Unit Measures',
  168.             'form' => $form->createView(),
  169.         );
  170.     }
  171.     /**
  172.      * @Route("/unit_measures/update/{id}", name="update_unit_measures")
  173.      * @Template("unit_measures/update.html.twig")
  174.      * @Security("is_granted('ROLE_ADMIN')", statusCode=404, message="Resource not found.")
  175.      */
  176.     public function updateUnitMeasuresAction($idRequest $requestEntityManagerInterface $em)
  177.     {
  178.         $unit $em->getRepository(UnitMeasures::class)
  179.             ->findOneBy(['id' => $id'active' => TRUE]);
  180.         $form $this->createForm(\App\Form\UnitMeasure::class, $unit, array(
  181.             'action' => $this->generateUrl('update_unit_measures', ['id' => $id]),
  182.         ));
  183.         $form->handleRequest($request);
  184.         if ($form->isSubmitted() && $form->isValid()) {
  185.             $unit $form->getData();
  186.             $em->persist($unit);
  187.             $em->flush();
  188.             return $this->redirectToRoute('unit_measures');
  189.         }
  190.         return array(
  191.             'pageTitle' => 'Unit Measures',
  192.             'activePage' => 'Unit Measures',
  193.             'form' => $form->createView(),
  194.         );
  195.     }
  196.     /**
  197.      * @Route("/unit_measures/delete/{id}", name="delete_unit_measure")
  198.      * @Security("is_granted('ROLE_ADMIN')", statusCode=404, message="Resource not found.")
  199.      */
  200.     public function deleteUnitAction($idEntityManagerInterface $em)
  201.     {
  202.         $unit $em->getRepository(UnitMeasures::class)
  203.             ->findOneBy(['id' => $id]);
  204.         if ($unit) {
  205.             $unit->setActive(FALSE);
  206.             $em->persist($unit);
  207.             $em->flush();
  208.         }
  209.         return $this->redirectToRoute('unit_measures');
  210.     }
  211.     /**
  212.      * @Route("/ingredients", name="ingredients")
  213.      * @Security("is_granted('ROLE_ADMIN')", statusCode=404, message="Resource not found.")
  214.      */
  215.     public function ingredientsAction(EntityManagerInterface $em)
  216.     {
  217.         $page = array(
  218.             'pageTitle' => 'Ingredients',
  219.             'activePage' => 'Ingredients',
  220.             'data' => $em
  221.                 ->getRepository(Ingredients::class)
  222.                 ->findBy([
  223.                     'active' => TRUE,
  224.                 ]),
  225.         );
  226.         return $this->render('ingredients/index.html.twig'$page);
  227.     }
  228.     /**
  229.      * @Route("/ingredients/data", name="ingredients_data")
  230.      * @Security("is_granted('ROLE_ADMIN')", statusCode=404, message="Resource not found.")
  231.      */
  232.     public function ingredientsDataAction(Request $requestEntityManagerInterface $em)
  233.     {
  234.         $supplies = new IngredientsData();
  235.         return new JsonResponse(
  236.             $supplies->getData($request$em)
  237.         );
  238.     }
  239.     /**
  240.      * @Route("/ingredients/create", name="create_ingredient")
  241.      * @Template("ingredients/create.html.twig")
  242.      * @Security("is_granted('ROLE_ADMIN')", statusCode=404, message="Resource not found.")
  243.      */
  244.     public function createIngredientAction(Request $requestEntityManagerInterface $em)
  245.     {
  246.         $ingredient = new Ingredients();
  247.         $form $this->createForm(\App\Form\Ingredient::class, $ingredient, array(
  248.             'action' => $this->generateUrl('create_ingredient'),
  249.         ));
  250.         $form->handleRequest($request);
  251.         if ($form->isSubmitted() && $form->isValid()) {
  252.             $ingredient $form->getData();
  253.             if ($ingredient->getUnitMeasureId() instanceof UnitMeasures) {
  254.                 $ingredient->setUnitMeasureId($ingredient->getUnitMeasureId()->getId());
  255.             } else {
  256.                 $ingredient->setUnitMeasureId(0);
  257.             }
  258.             if ($ingredient->getCategoryId() instanceof IngredientCategories) {
  259.                 $ingredient->setCategoryId($ingredient->getCategoryId()->getId());
  260.             } else {
  261.                 $ingredient->setCategoryId(0);
  262.             }
  263.             $ingredient->setActive(TRUE);
  264.             $ingredient->setEnergy(0);
  265.             $ingredient->setCarbohydrate(0);
  266.             $ingredient->setProtein(0);
  267.             $ingredient->setSugarsTotal(0);
  268.             $ingredient->setFiberTotalDietary(0);
  269.             $ingredient->setTotalFat(0);
  270.             $ingredient->setFattyAcidsTotalSaturated(0);
  271.             $ingredient->setFattyAcidsTotalMonosaturated(0);
  272.             $ingredient->setFattyAcidsTotalPolyunsaturated(0);
  273.             $ingredient->setCholesterol(0);
  274.             $ingredient->setRetinol(0);
  275.             $ingredient->setVitaminA(0);
  276.             $ingredient->setCaroteneAlpha(0);
  277.             $ingredient->setCaroteneBeta(0);
  278.             $ingredient->setCryptoxanthinBeta(0);
  279.             $ingredient->setLycopene(0);
  280.             $ingredient->setLuteinZeaxanthin(0);
  281.             $ingredient->setThiamin(0);
  282.             $ingredient->setRiboflavin(0);
  283.             $ingredient->setNiacin(0);
  284.             $ingredient->setVitaminB6(0);
  285.             $ingredient->setFolicAcid(0);
  286.             $ingredient->setFolateFood(0);
  287.             $ingredient->setFolateDfe(0);
  288.             $ingredient->setFolateTotal(0);
  289.             $ingredient->setCholineTotal(0);
  290.             $ingredient->setVitaminB12(0);
  291.             $ingredient->setVitaminB12Added(0);
  292.             $ingredient->setVitaminC(0);
  293.             $ingredient->setVitaminDD2D3(0);
  294.             $ingredient->setVitaminEAlphaTocopherol(0);
  295.             $ingredient->setVitaminEAdded(0);
  296.             $ingredient->setVitaminKPhylloquinone(0);
  297.             $ingredient->setCalcium(0);
  298.             $ingredient->setPhosphorus(0);
  299.             $ingredient->setMagnesium(0);
  300.             $ingredient->setIron(0);
  301.             $ingredient->setZinc(0);
  302.             $ingredient->setCopper(0);
  303.             $ingredient->setSelenium(0);
  304.             $ingredient->setPotassium(0);
  305.             $ingredient->setSodium(0);
  306.             $ingredient->setCaffeine(0);
  307.             $ingredient->setTheobromine(0);
  308.             $ingredient->setAlcohol(0);
  309.             $ingredient->setWater(0);
  310.             $em->persist($ingredient);
  311.             $em->flush();
  312.             return $this->redirectToRoute('ingredients');
  313.         }
  314.         return array(
  315.             'pageTitle' => 'Ingredients',
  316.             'activePage' => 'Ingredients',
  317.             'form' => $form->createView(),
  318.         );
  319.     }
  320.     /**
  321.      * @Route("/ingredients/update/{id}", name="update_ingredient")
  322.      * @Template("ingredients/update.html.twig")
  323.      * @Security("is_granted('ROLE_ADMIN')", statusCode=404, message="Resource not found.")
  324.      */
  325.     public function updateIngredientAction($idRequest $requestEntityManagerInterface $em)
  326.     {
  327.         $ingredient $em->getRepository(Ingredients::class)
  328.             ->findOneBy(['id' => $id'active' => TRUE]);
  329.         $form $this->createForm(\App\Form\Ingredient::class, $ingredient, array(
  330.             'action' => $this->generateUrl('update_ingredient', ['id' => $id]),
  331.         ));
  332.         $form->handleRequest($request);
  333.         if ($form->isSubmitted() && $form->isValid()) {
  334.             $ingredient $form->getData();
  335.             if ($ingredient->getUnitMeasureId() instanceof UnitMeasures) {
  336.                 $ingredient->setUnitMeasureId($ingredient->getUnitMeasureId()->getId());
  337.             } else {
  338.                 $ingredient->setUnitMeasureId(0);
  339.             }
  340.             if ($ingredient->getCategoryId() instanceof IngredientCategories) {
  341.                 $ingredient->setCategoryId($ingredient->getCategoryId()->getId());
  342.             } else {
  343.                 $ingredient->setCategoryId(0);
  344.             }
  345.             $em->persist($ingredient);
  346.             $em->flush();
  347.             return $this->redirectToRoute('ingredients');
  348.         }
  349.         return array(
  350.             'pageTitle' => 'Ingredients',
  351.             'activePage' => 'Ingredients',
  352.             'form' => $form->createView(),
  353.         );
  354.     }
  355.     /**
  356.      * @Route("/ingredients/delete/{id}", name="delete_ingredient")
  357.      * @Security("is_granted('ROLE_ADMIN')", statusCode=404, message="Resource not found.")
  358.      */
  359.     public function deleteIngredientAction($idEntityManagerInterface $em)
  360.     {
  361.         $ingredient $em->getRepository(Ingredients::class)
  362.             ->findOneBy(['id' => $id]);
  363.         if ($ingredient) {
  364.             $ingredient->setActive(FALSE);
  365.             $em->persist($ingredient);
  366.             $em->flush();
  367.         }
  368.         return $this->redirectToRoute('ingredients');
  369.     }
  370. }