<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Controller\admin;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Knp\Component\Pager\PaginatorInterface;
use App\Entity\Category;
use App\Entity\Subcategory;
/**
* Controller used to manage current user.
*
* @author Romain Monteil <monteil.romain@gmail.com>
*/
#[Route('/admin/category')]
class CategoryController extends AbstractController {
#[Route('/categories-list', methods: ['GET'], name: 'list_categories')]
public function listCategories(Request $request, ManagerRegistry $doctrine) {
$em = $doctrine->getManager();
$categories = $em->getRepository(Category::class)->findAll();
return $this->render('admin/category/listCategories.html.twig', [
'categories' => $categories,
]);
}
#[Route('/add-category', methods: ['GET', 'POST'], name: 'add_category')]
public function addCategory(Request $request, ManagerRegistry $doctrine) {
$em = $doctrine->getManager();
if ($request->isMethod('post')) {
$category = new Category();
$category->setName($request->get('name'));
$category->setDescription($request->get('description'));
$category->setOrdre($request->get('ordre'));
$category->setColor($request->get('color'));
$category->setPublished(1);
$em->persist($category);
$em->flush();
if ($request->files->get('image')) {
$file = $request->files->get('image');
$category->setImage("uploads/category/cat-" . $category->getId() . "/" . $file->getClientOriginalName());
$file->move("uploads/category/cat-" . $category->getId() . "/", $file->getClientOriginalName());
}
$em->persist($category);
$em->flush();
$this->addFlash('success', 'Category created successfully');
return $this->redirectToRoute('list_categories');
}
return $this->render('admin/category/addCategory.html.twig', [
]);
}
#[Route('/subcategories-list', methods: ['GET'], name: 'list_subcategories')]
public function listSubcategories(Request $request, ManagerRegistry $doctrine) {
$em = $doctrine->getManager();
$subcategories = $em->getRepository(Subcategory::class)->findAll();
return $this->render('admin/category/listSubcategories.html.twig', [
'subcategories' => $subcategories,
]);
}
#[Route('/add-subcategory', methods: ['GET', 'POST'], name: 'add_subcategory')]
public function addSubcategory(Request $request, ManagerRegistry $doctrine) {
$em = $doctrine->getManager();
$categories = $em->getRepository(Category::class)->findAll();
if ($request->isMethod('post')) {
$subcategory = new Subcategory();
$subcategory->setName($request->get('name'));
$subcategory->setDescription($request->get('description'));
$subcategory->setOrdre($request->get('ordre'));
$subcategory->setPublished(1);
$em->persist($subcategory);
$em->flush();
$category = $em->getRepository(Category::class)->find($request->get('category'));
$category->addSubcategory($subcategory);
if ($request->files->get('image')) {
$file = $request->files->get('image');
$subcategory->setImage("uploads/category/cat-" . $subcategory->getId() . "/" . $file->getClientOriginalName());
$file->move("uploads/category/cat-" . $subcategory->getId() . "/", $file->getClientOriginalName());
}
$em->persist($subcategory);
$em->flush();
$this->addFlash('success', 'Category created successfully');
return $this->redirectToRoute('list_subcategories');
}
return $this->render('admin/category/addSubcategory.html.twig', [
'categories' => $categories,
]);
}
#[Route('/edit-category-{id}', methods: ['GET', 'POST'], name: 'edit_category')]
public function editCategory(Request $request, $id, ManagerRegistry $doctrine) {
$em = $doctrine->getManager();
$category = $em->getRepository(Category::class)->find($id);
if ($request->isMethod('post')) {
$category->setName($request->get('name'));
$category->setDescription($request->get('description'));
$category->setOrdre($request->get('ordre'));
$category->setColor($request->get('color'));
$category->setPublished(1);
$em->persist($category);
$em->flush();
if ($request->files->get('image')) {
$file = $request->files->get('image');
$category->setImage("uploads/category/cat-" . $category->getId() . "/" . $file->getClientOriginalName());
$file->move("uploads/category/cat-" . $category->getId() . "/", $file->getClientOriginalName());
}
$em->persist($category);
$em->flush();
$this->addFlash('success', 'Category created successfully');
return $this->redirectToRoute('list_categories');
}
return $this->render('admin/category/edit/editCategory.html.twig', [
'category' => $category,
]);
}
#[Route('/edit-subcategory-{id}', methods: ['GET', 'POST'], name: 'edit_subcategory')]
public function editSubcategory(Request $request, $id, ManagerRegistry $doctrine) {
$em = $doctrine->getManager();
$subcategory = $em->getRepository(Category::class)->find($id);
$categories = $em->getRepository(Category::class)->findAll();
if ($request->isMethod('post')) {
$subcategory->setName($request->get('name'));
$subcategory->setDescription($request->get('description'));
$subcategory->setOrdre($request->get('ordre'));
$subcategory->setPublished(1);
$em->persist($subcategory);
$em->flush();
$category = $em->getRepository(Category::class)->find($request->get('category'));
$category->addSubcategory($subcategory);
if ($request->files->get('image')) {
$file = $request->files->get('image');
$subcategory->setImage("uploads/category/cat-" . $category->getId() . "/" . $file->getClientOriginalName());
$file->move("uploads/category/cat-" . $category->getId() . "/", $file->getClientOriginalName());
}
$em->persist($category);
$em->flush();
$this->addFlash('success', 'Subcategory created successfully');
return $this->redirectToRoute('list_categories');
}
return $this->render('admin/category/edit/editSubcategory.html.twig', [
'subcategory' => $subcategory,
'categories' => $categories,
]);
}
}