<?php
namespace App\Controller;
use App\Datatable\IngredientsData;
use App\Entity\Categories;
use App\Entity\IngredientCategories;
use App\Entity\Ingredients;
use App\Entity\UnitMeasures;
use App\Helper\Generator;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class MainController extends AbstractController
{
/**
* @Route("/", name="home")
*/
public function indexAction()
{
if ($this->isGranted('ROLE_PARTNER')) {
return $this->redirectToRoute('app_partners_vouchers');
}
return $this->redirectToRoute('users');
}
/**
* @Route("/categories", name="categories")
* @Security("is_granted('ROLE_ADMIN')", statusCode=404, message="Resource not found.")
*/
public function categoriesAction(EntityManagerInterface $em)
{
$page = array(
'pageTitle' => 'Categories',
'activePage' => 'Categories',
'data' => $em
->getRepository(Categories::class)
->findBy([
'active' => TRUE,
]),
);
return $this->render('categories/index.html.twig', $page);
}
/**
* @Route("/categories/create", name="create_categories")
* @Template("categories/create.html.twig")
* @Security("is_granted('ROLE_ADMIN')", statusCode=404, message="Resource not found.")
*/
public function createCategoryAction(Request $request, EntityManagerInterface $em)
{
$category = new Categories();
$form = $this->createForm(\App\Form\Category::class, $category, array(
'action' => $this->generateUrl('create_categories'),
));
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$category = $form->getData();
$category->setActive(TRUE);
$file = $category->getImage();
$fileName = Generator::generateUniqueFileName() . '.' . $file->guessExtension();
$file->move(
$this->getParameter('category_path'),
$fileName
);
$category->setImage($fileName);
$em->persist($category);
$em->flush();
return $this->redirectToRoute('categories');
}
return array(
'pageTitle' => 'Categories',
'activePage' => 'Categories',
'form' => $form->createView(),
);
}
/**
* @Route("/categories/update/{id}", name="update_categories")
* @Template("categories/update.html.twig")
* @Security("is_granted('ROLE_ADMIN')", statusCode=404, message="Resource not found.")
*/
public function updateCategoryAction($id, Request $request, EntityManagerInterface $em)
{
$category = $em->getRepository(Categories::class)
->findOneBy(['id' => $id, 'active' => TRUE]);
$form = $this->createForm(\App\Form\Category::class, $category, array(
'action' => $this->generateUrl('update_categories', ['id' => $id]),
));
$currentFilename = $category->getImage();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$category = $form->getData();
$file = $category->getImage();
if ($file) {
$fileName = Generator::generateUniqueFileName() . '.' . $file->guessExtension();
$file->move(
$this->getParameter('category_path'),
$fileName
);
$category->setImage($fileName);
} else {
$category->setImage($currentFilename);
}
$em->persist($category);
$em->flush();
return $this->redirectToRoute('categories');
}
return array(
'pageTitle' => 'Categories',
'activePage' => 'Categories',
'form' => $form->createView(),
);
}
/**
* @Route("/categories/delete/{id}", name="delete_category")
* @Security("is_granted('ROLE_ADMIN')", statusCode=404, message="Resource not found.")
*/
public function deleteCategoryAction($id, EntityManagerInterface $em)
{
$category = $em->getRepository(Categories::class)
->findOneBy(['id' => $id]);
if ($category) {
$category->setActive(FALSE);
$em->persist($category);
$em->flush();
}
return $this->redirectToRoute('categories');
}
/**
* @Route("/unit_measures", name="unit_measures")
* @Security("is_granted('ROLE_ADMIN')", statusCode=404, message="Resource not found.")
*/
public function unitMeasuresAction(EntityManagerInterface $em)
{
$page = array(
'pageTitle' => 'Unit Measures',
'activePage' => 'Unit Measures',
'data' => $em
->getRepository(UnitMeasures::class)
->findBy([
'active' => TRUE,
]),
);
return $this->render('unit_measures/index.html.twig', $page);
}
/**
* @Route("/unit_measures/create", name="create_unit_measures")
* @Template("unit_measures/create.html.twig")
* @Security("is_granted('ROLE_ADMIN')", statusCode=404, message="Resource not found.")
*/
public function createUnitMeasuresAction(Request $request, EntityManagerInterface $em)
{
$unit = new UnitMeasures();
$form = $this->createForm(\App\Form\UnitMeasure::class, $unit, array(
'action' => $this->generateUrl('create_unit_measures'),
));
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$unit = $form->getData();
$unit->setActive(TRUE);
$em->persist($unit);
$em->flush();
return $this->redirectToRoute('unit_measures');
}
return array(
'pageTitle' => 'Unit Measures',
'activePage' => 'Unit Measures',
'form' => $form->createView(),
);
}
/**
* @Route("/unit_measures/update/{id}", name="update_unit_measures")
* @Template("unit_measures/update.html.twig")
* @Security("is_granted('ROLE_ADMIN')", statusCode=404, message="Resource not found.")
*/
public function updateUnitMeasuresAction($id, Request $request, EntityManagerInterface $em)
{
$unit = $em->getRepository(UnitMeasures::class)
->findOneBy(['id' => $id, 'active' => TRUE]);
$form = $this->createForm(\App\Form\UnitMeasure::class, $unit, array(
'action' => $this->generateUrl('update_unit_measures', ['id' => $id]),
));
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$unit = $form->getData();
$em->persist($unit);
$em->flush();
return $this->redirectToRoute('unit_measures');
}
return array(
'pageTitle' => 'Unit Measures',
'activePage' => 'Unit Measures',
'form' => $form->createView(),
);
}
/**
* @Route("/unit_measures/delete/{id}", name="delete_unit_measure")
* @Security("is_granted('ROLE_ADMIN')", statusCode=404, message="Resource not found.")
*/
public function deleteUnitAction($id, EntityManagerInterface $em)
{
$unit = $em->getRepository(UnitMeasures::class)
->findOneBy(['id' => $id]);
if ($unit) {
$unit->setActive(FALSE);
$em->persist($unit);
$em->flush();
}
return $this->redirectToRoute('unit_measures');
}
/**
* @Route("/ingredients", name="ingredients")
* @Security("is_granted('ROLE_ADMIN')", statusCode=404, message="Resource not found.")
*/
public function ingredientsAction(EntityManagerInterface $em)
{
$page = array(
'pageTitle' => 'Ingredients',
'activePage' => 'Ingredients',
'data' => $em
->getRepository(Ingredients::class)
->findBy([
'active' => TRUE,
]),
);
return $this->render('ingredients/index.html.twig', $page);
}
/**
* @Route("/ingredients/data", name="ingredients_data")
* @Security("is_granted('ROLE_ADMIN')", statusCode=404, message="Resource not found.")
*/
public function ingredientsDataAction(Request $request, EntityManagerInterface $em)
{
$supplies = new IngredientsData();
return new JsonResponse(
$supplies->getData($request, $em)
);
}
/**
* @Route("/ingredients/create", name="create_ingredient")
* @Template("ingredients/create.html.twig")
* @Security("is_granted('ROLE_ADMIN')", statusCode=404, message="Resource not found.")
*/
public function createIngredientAction(Request $request, EntityManagerInterface $em)
{
$ingredient = new Ingredients();
$form = $this->createForm(\App\Form\Ingredient::class, $ingredient, array(
'action' => $this->generateUrl('create_ingredient'),
));
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$ingredient = $form->getData();
if ($ingredient->getUnitMeasureId() instanceof UnitMeasures) {
$ingredient->setUnitMeasureId($ingredient->getUnitMeasureId()->getId());
} else {
$ingredient->setUnitMeasureId(0);
}
if ($ingredient->getCategoryId() instanceof IngredientCategories) {
$ingredient->setCategoryId($ingredient->getCategoryId()->getId());
} else {
$ingredient->setCategoryId(0);
}
$ingredient->setActive(TRUE);
$ingredient->setEnergy(0);
$ingredient->setCarbohydrate(0);
$ingredient->setProtein(0);
$ingredient->setSugarsTotal(0);
$ingredient->setFiberTotalDietary(0);
$ingredient->setTotalFat(0);
$ingredient->setFattyAcidsTotalSaturated(0);
$ingredient->setFattyAcidsTotalMonosaturated(0);
$ingredient->setFattyAcidsTotalPolyunsaturated(0);
$ingredient->setCholesterol(0);
$ingredient->setRetinol(0);
$ingredient->setVitaminA(0);
$ingredient->setCaroteneAlpha(0);
$ingredient->setCaroteneBeta(0);
$ingredient->setCryptoxanthinBeta(0);
$ingredient->setLycopene(0);
$ingredient->setLuteinZeaxanthin(0);
$ingredient->setThiamin(0);
$ingredient->setRiboflavin(0);
$ingredient->setNiacin(0);
$ingredient->setVitaminB6(0);
$ingredient->setFolicAcid(0);
$ingredient->setFolateFood(0);
$ingredient->setFolateDfe(0);
$ingredient->setFolateTotal(0);
$ingredient->setCholineTotal(0);
$ingredient->setVitaminB12(0);
$ingredient->setVitaminB12Added(0);
$ingredient->setVitaminC(0);
$ingredient->setVitaminDD2D3(0);
$ingredient->setVitaminEAlphaTocopherol(0);
$ingredient->setVitaminEAdded(0);
$ingredient->setVitaminKPhylloquinone(0);
$ingredient->setCalcium(0);
$ingredient->setPhosphorus(0);
$ingredient->setMagnesium(0);
$ingredient->setIron(0);
$ingredient->setZinc(0);
$ingredient->setCopper(0);
$ingredient->setSelenium(0);
$ingredient->setPotassium(0);
$ingredient->setSodium(0);
$ingredient->setCaffeine(0);
$ingredient->setTheobromine(0);
$ingredient->setAlcohol(0);
$ingredient->setWater(0);
$em->persist($ingredient);
$em->flush();
return $this->redirectToRoute('ingredients');
}
return array(
'pageTitle' => 'Ingredients',
'activePage' => 'Ingredients',
'form' => $form->createView(),
);
}
/**
* @Route("/ingredients/update/{id}", name="update_ingredient")
* @Template("ingredients/update.html.twig")
* @Security("is_granted('ROLE_ADMIN')", statusCode=404, message="Resource not found.")
*/
public function updateIngredientAction($id, Request $request, EntityManagerInterface $em)
{
$ingredient = $em->getRepository(Ingredients::class)
->findOneBy(['id' => $id, 'active' => TRUE]);
$form = $this->createForm(\App\Form\Ingredient::class, $ingredient, array(
'action' => $this->generateUrl('update_ingredient', ['id' => $id]),
));
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$ingredient = $form->getData();
if ($ingredient->getUnitMeasureId() instanceof UnitMeasures) {
$ingredient->setUnitMeasureId($ingredient->getUnitMeasureId()->getId());
} else {
$ingredient->setUnitMeasureId(0);
}
if ($ingredient->getCategoryId() instanceof IngredientCategories) {
$ingredient->setCategoryId($ingredient->getCategoryId()->getId());
} else {
$ingredient->setCategoryId(0);
}
$em->persist($ingredient);
$em->flush();
return $this->redirectToRoute('ingredients');
}
return array(
'pageTitle' => 'Ingredients',
'activePage' => 'Ingredients',
'form' => $form->createView(),
);
}
/**
* @Route("/ingredients/delete/{id}", name="delete_ingredient")
* @Security("is_granted('ROLE_ADMIN')", statusCode=404, message="Resource not found.")
*/
public function deleteIngredientAction($id, EntityManagerInterface $em)
{
$ingredient = $em->getRepository(Ingredients::class)
->findOneBy(['id' => $id]);
if ($ingredient) {
$ingredient->setActive(FALSE);
$em->persist($ingredient);
$em->flush();
}
return $this->redirectToRoute('ingredients');
}
}