src/Controller/admin/CategoryController.php line 115

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace App\Controller\admin;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Knp\Component\Pager\PaginatorInterface;
  19. use App\Entity\Category;
  20. use App\Entity\Subcategory;
  21. /**
  22.  * Controller used to manage current user.
  23.  *
  24.  * @author Romain Monteil <monteil.romain@gmail.com>
  25.  */
  26. #[Route('/admin/category')]
  27. class CategoryController extends AbstractController {
  28.     #[Route('/categories-list'methods: ['GET'], name'list_categories')]
  29.     public function listCategories(Request $requestManagerRegistry $doctrine) {
  30.         $em $doctrine->getManager();
  31.         $categories $em->getRepository(Category::class)->findAll();
  32.         return $this->render('admin/category/listCategories.html.twig', [
  33.                     'categories' => $categories,
  34.         ]);
  35.     }
  36.     #[Route('/add-category'methods: ['GET''POST'], name'add_category')]
  37.     public function addCategory(Request $requestManagerRegistry $doctrine) {
  38.         $em $doctrine->getManager();
  39.         if ($request->isMethod('post')) {
  40.             $category = new Category();
  41.             $category->setName($request->get('name'));
  42.             $category->setDescription($request->get('description'));
  43.             $category->setOrdre($request->get('ordre'));
  44.             $category->setColor($request->get('color'));
  45.             $category->setPublished(1);
  46.             $em->persist($category);
  47.             $em->flush();
  48.             if ($request->files->get('image')) {
  49.                 $file $request->files->get('image');
  50.                 $category->setImage("uploads/category/cat-" $category->getId() . "/" $file->getClientOriginalName());
  51.                 $file->move("uploads/category/cat-" $category->getId() . "/"$file->getClientOriginalName());
  52.             }
  53.             $em->persist($category);
  54.             $em->flush();
  55.             $this->addFlash('success''Category created successfully');
  56.             return $this->redirectToRoute('list_categories');
  57.         }
  58.         return $this->render('admin/category/addCategory.html.twig', [
  59.         ]);
  60.     }
  61.     #[Route('/subcategories-list'methods: ['GET'], name'list_subcategories')]
  62.     public function listSubcategories(Request $requestManagerRegistry $doctrine) {
  63.         $em $doctrine->getManager();
  64.         $subcategories $em->getRepository(Subcategory::class)->findAll();
  65.         return $this->render('admin/category/listSubcategories.html.twig', [
  66.                     'subcategories' => $subcategories,
  67.         ]);
  68.     }
  69.     #[Route('/add-subcategory'methods: ['GET''POST'], name'add_subcategory')]
  70.     public function addSubcategory(Request $requestManagerRegistry $doctrine) {
  71.         $em $doctrine->getManager();
  72.         $categories $em->getRepository(Category::class)->findAll();
  73.         if ($request->isMethod('post')) {
  74.             $subcategory = new Subcategory();
  75.             $subcategory->setName($request->get('name'));
  76.             $subcategory->setDescription($request->get('description'));
  77.             $subcategory->setOrdre($request->get('ordre'));
  78.             $subcategory->setPublished(1);
  79.             $em->persist($subcategory);
  80.             $em->flush();
  81.             $category $em->getRepository(Category::class)->find($request->get('category'));
  82.             $category->addSubcategory($subcategory);
  83.             if ($request->files->get('image')) {
  84.                 $file $request->files->get('image');
  85.                 $subcategory->setImage("uploads/category/cat-" $subcategory->getId() . "/" $file->getClientOriginalName());
  86.                 $file->move("uploads/category/cat-" $subcategory->getId() . "/"$file->getClientOriginalName());
  87.             }
  88.             $em->persist($subcategory);
  89.             $em->flush();
  90.             $this->addFlash('success''Category created successfully');
  91.             return $this->redirectToRoute('list_subcategories');
  92.         }
  93.         return $this->render('admin/category/addSubcategory.html.twig', [
  94.                     'categories' => $categories,
  95.         ]);
  96.     }
  97.     #[Route('/edit-category-{id}'methods: ['GET''POST'], name'edit_category')]
  98.     public function editCategory(Request $request$idManagerRegistry $doctrine) {
  99.         $em $doctrine->getManager();
  100.         $category $em->getRepository(Category::class)->find($id);
  101.         if ($request->isMethod('post')) {
  102.             $category->setName($request->get('name'));
  103.             $category->setDescription($request->get('description'));
  104.             $category->setOrdre($request->get('ordre'));
  105.             $category->setColor($request->get('color'));
  106.             $category->setPublished(1);
  107.             $em->persist($category);
  108.             $em->flush();
  109.             if ($request->files->get('image')) {
  110.                 $file $request->files->get('image');
  111.                 $category->setImage("uploads/category/cat-" $category->getId() . "/" $file->getClientOriginalName());
  112.                 $file->move("uploads/category/cat-" $category->getId() . "/"$file->getClientOriginalName());
  113.             }
  114.             $em->persist($category);
  115.             $em->flush();
  116.             $this->addFlash('success''Category created successfully');
  117.             return $this->redirectToRoute('list_categories');
  118.         }
  119.         return $this->render('admin/category/edit/editCategory.html.twig', [
  120.                     'category' => $category,
  121.         ]);
  122.     }
  123.     #[Route('/edit-subcategory-{id}'methods: ['GET''POST'], name'edit_subcategory')]
  124.     public function editSubcategory(Request $request$idManagerRegistry $doctrine) {
  125.         $em $doctrine->getManager();
  126.         $subcategory $em->getRepository(Category::class)->find($id);
  127.         $categories $em->getRepository(Category::class)->findAll();
  128.         if ($request->isMethod('post')) {
  129.             $subcategory->setName($request->get('name'));
  130.             $subcategory->setDescription($request->get('description'));
  131.             $subcategory->setOrdre($request->get('ordre'));
  132.             $subcategory->setPublished(1);
  133.             $em->persist($subcategory);
  134.             $em->flush();
  135.             $category $em->getRepository(Category::class)->find($request->get('category'));
  136.             $category->addSubcategory($subcategory);
  137.             if ($request->files->get('image')) {
  138.                 $file $request->files->get('image');
  139.                 $subcategory->setImage("uploads/category/cat-" $category->getId() . "/" $file->getClientOriginalName());
  140.                 $file->move("uploads/category/cat-" $category->getId() . "/"$file->getClientOriginalName());
  141.             }
  142.             $em->persist($category);
  143.             $em->flush();
  144.             $this->addFlash('success''Subcategory created successfully');
  145.             return $this->redirectToRoute('list_categories');
  146.         }
  147.         return $this->render('admin/category/edit/editSubcategory.html.twig', [
  148.                     'subcategory' => $subcategory,
  149.                     'categories' => $categories,
  150.         ]);
  151.     }
  152. }