Linux SRV-16 6.18.44-paas #1 SMP PREEMPT_DYNAMIC Thu Aug 13 10:08:12 UTC 2026 x86_64
/
proc
/
212
/
root
/
proc
/
212
/
task
/
212
/
root
/
proc
/
70528
/
cwd
/
src
/
Controller
/
back
/
//proc/212/root/proc/212/task/212/root/proc/70528/cwd/src/Controller/back/CategoryController.php
<?php namespace App\Controller\back; use App\Entity\Category; use App\Form\CategoryType; use App\Repository\ProductRepository; use App\Repository\CategoryRepository; use Doctrine\Persistence\ManagerRegistry; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; /** * @Route("/admin/category") */ class CategoryController extends AbstractController { /** * @Route("/create", name="back_category_create") */ public function index(Request $request, ManagerRegistry $doctrine): Response { $category = new Category(); $form = $this->createForm(CategoryType::class, $category); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $em = $doctrine->getManager(); $em->persist($category); $em->flush(); $this->addFlash("success", "Catégorie <u>" . $category->getName() . "</u> ajoutée !"); return $this->redirectToRoute("back_category_list"); } return $this->render("back/category/create.html.twig", [ "form" => $form->createView() ]); } /** * @Route("/", name="back_category_list") */ public function listCategory(CategoryRepository $categoryRepository) { $categories = $categoryRepository->findBy(["active" => 1]); return $this->render("back/category/list.html.twig", [ "categories" => $categories ]); } /** * @Route("/archived", name="back_category_archived") */ public function archivedCategories(CategoryRepository $categoryRepository) { $categories = $categoryRepository->findBy(["active" => 0]); return $this->render("back/category/archived.html.twig", [ "categories" => $categories ]); } /** * @Route("/{id}/add/{productId}", name="back_category_add_product") */ public function addProductToCategory($id, $productId, ProductRepository $productRepository, CategoryRepository $categoryRepository, ManagerRegistry $doctrine) { $product = $productRepository->find($productId); $category = $categoryRepository->find($id); $product->setCategory($category); $em = $doctrine->getManager(); $em->persist($product); $em->flush(); return $this->json("success"); } /** * @Route("/{id}/products", name="back_category_products") */ public function categoryProducts($id, CategoryRepository $categoryRepository) { $category = $categoryRepository->find($id); return $this->render("back/category/products.html.twig", [ "category" => $category ]); } /** * @Route("/{id}/edit", name="back_category_edit") */ public function editCategory($id, CategoryRepository $categoryRepository, Request $request, ManagerRegistry $doctrine) { $category = $categoryRepository->find($id); $form = $this->createForm(CategoryType::class, $category); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $em = $doctrine->getManager(); $em->persist($category); $em->flush(); $this->addFlash("success", "Catégorie modifiée !"); return $this->redirectToRoute("back_category_list"); } return $this->render("back/category/edit.html.twig", [ "form" => $form->createView(), "category" => $category ]); } /** * @Route("/{id}/archive", name="back_category_archive") */ public function archiveCategory($id, CategoryRepository $categoryRepository, ManagerRegistry $doctrine) { $category = $categoryRepository->find($id); $em = $doctrine->getManager(); $category->setActive(false); $em->persist($category); $em->flush(); return $this->redirectToRoute("back_category_list"); } /** * @Route("/{id}/unarchive", name="back_category_unarchive") */ public function unarchiveCategory($id, CategoryRepository $categoryRepository, ManagerRegistry $doctrine) { $category = $categoryRepository->find($id); $em = $doctrine->getManager(); $category->setActive(true); $em->persist($category); $em->flush(); return $this->redirectToRoute("back_category_list"); } }