src/Controller/Back/ShopController.php line 122

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Back;
  3. use App\Entity\Shop;
  4. use App\Form\ShopType;
  5. use App\Repository\ShopRepository;
  6. use App\Services\UploadFileService;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\File\UploadedFile;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\String\Slugger\SluggerInterface;
  13. /**
  14.  * Class ShopController
  15.  * @package App\Controller\Back
  16.  *
  17.  * @Route("/admin/boutique")
  18.  */
  19. class ShopController extends AbstractController
  20. {
  21.     /**
  22.      * @var EntityManagerInterface
  23.      */
  24.     private EntityManagerInterface $em;
  25.     /**
  26.      * @var UploadFileService
  27.      */
  28.     private UploadFileService $uploadFileService;
  29.     /**
  30.      * @var SluggerInterface
  31.      */
  32.     private SluggerInterface $slugger;
  33.     public function __construct(EntityManagerInterface $emUploadFileService $uploadFileServiceSluggerInterface $slugger)
  34.     {
  35.         $this->em $em;
  36.         $this->uploadFileService $uploadFileService;
  37.         $this->slugger $slugger;
  38.     }
  39.     /**
  40.      * @Route("/liste", name="back_shop_list")
  41.      */
  42.     public function list(ShopRepository $shopRepository)
  43.     {
  44.         return $this->render('back/shop/list.html.twig', [
  45.             'shops' => $shopRepository->findBy([], ['id' => 'DESC'])
  46.         ]);
  47.     }
  48.     /**
  49.      * @Route("/ajout", name="back_shop_add")
  50.      */
  51.     public function add(Request $request)
  52.     {
  53.         $shop = new Shop();
  54.         $form $this->createForm(ShopType::class, $shop)->handleRequest($request);
  55.         if ($form->isSubmitted() && $form->isValid()) {
  56.             if ($shop->getLogo() instanceof UploadedFile) {
  57.                 $image $this->uploadFileService->uploadImage($shop->getLogo(), UploadFileService::SHOP_IMAGE_DIR);
  58.                 $shop->setLogo($image);
  59.             }
  60.             $sluggedName $this->slugger->slug($shop->getName())->lower();
  61.             $shop->setSlug($sluggedName);
  62.             $this->em->persist($shop);
  63.             $this->em->flush();
  64.             $this->addFlash('success''Boutique ajoutée');
  65.             return $this->redirectToRoute('back_shop_list');
  66.         }
  67.         return $this->render('back/shop/add.html.twig', [
  68.             'form' => $form->createView(),
  69.             'shop' => $shop
  70.         ]);
  71.     }
  72.     /**
  73.      * @Route("/editer/{id}", name="back_shop_edit")
  74.      */
  75.     public function edit(Shop $shopRequest $request)
  76.     {
  77.         $oldPhoto $shop->getLogo();
  78.         $form $this->createForm(ShopType::class, $shop)->handleRequest($request);
  79.         if ($form->isSubmitted() && $form->isValid()) {
  80.             if ($shop->getLogo() instanceof UploadedFile) {
  81.                 $image $this->uploadFileService->uploadImage($shop->getLogo(), UploadFileService::SHOP_IMAGE_DIR);
  82.                 $shop->setLogo($image);
  83.             } else {
  84.                 $shop->setLogo($oldPhoto);
  85.             }
  86.             $sluggedName $this->slugger->slug($shop->getName())->lower();
  87.             $shop->setSlug($sluggedName);
  88.             $this->em->flush();
  89.             $this->addFlash('success''Boutique editée');
  90.             return $this->redirectToRoute('back_shop_list');
  91.         }
  92.         return $this->render('back/shop/edit.html.twig', [
  93.             'form' => $form->createView(),
  94.             'shop' => $shop
  95.         ]);
  96.     }
  97.     /**
  98.      * @Route("/supprimer/{id}", name="back_shop_remove")
  99.      */
  100.     public function remove(Shop $shop)
  101.     {
  102.         $this->em->remove($shop);
  103.         $this->em->flush();
  104.         $this->addFlash('success''Boutique supprimée');
  105.         return $this->redirectToRoute('back_shop_list');
  106.     }
  107. }