<?php
namespace App\Controller\Back;
use App\Entity\Shop;
use App\Form\ShopType;
use App\Repository\ShopRepository;
use App\Services\UploadFileService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\String\Slugger\SluggerInterface;
/**
* Class ShopController
* @package App\Controller\Back
*
* @Route("/admin/boutique")
*/
class ShopController extends AbstractController
{
/**
* @var EntityManagerInterface
*/
private EntityManagerInterface $em;
/**
* @var UploadFileService
*/
private UploadFileService $uploadFileService;
/**
* @var SluggerInterface
*/
private SluggerInterface $slugger;
public function __construct(EntityManagerInterface $em, UploadFileService $uploadFileService, SluggerInterface $slugger)
{
$this->em = $em;
$this->uploadFileService = $uploadFileService;
$this->slugger = $slugger;
}
/**
* @Route("/liste", name="back_shop_list")
*/
public function list(ShopRepository $shopRepository)
{
return $this->render('back/shop/list.html.twig', [
'shops' => $shopRepository->findBy([], ['id' => 'DESC'])
]);
}
/**
* @Route("/ajout", name="back_shop_add")
*/
public function add(Request $request)
{
$shop = new Shop();
$form = $this->createForm(ShopType::class, $shop)->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if ($shop->getLogo() instanceof UploadedFile) {
$image = $this->uploadFileService->uploadImage($shop->getLogo(), UploadFileService::SHOP_IMAGE_DIR);
$shop->setLogo($image);
}
$sluggedName = $this->slugger->slug($shop->getName())->lower();
$shop->setSlug($sluggedName);
$this->em->persist($shop);
$this->em->flush();
$this->addFlash('success', 'Boutique ajoutée');
return $this->redirectToRoute('back_shop_list');
}
return $this->render('back/shop/add.html.twig', [
'form' => $form->createView(),
'shop' => $shop
]);
}
/**
* @Route("/editer/{id}", name="back_shop_edit")
*/
public function edit(Shop $shop, Request $request)
{
$oldPhoto = $shop->getLogo();
$form = $this->createForm(ShopType::class, $shop)->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if ($shop->getLogo() instanceof UploadedFile) {
$image = $this->uploadFileService->uploadImage($shop->getLogo(), UploadFileService::SHOP_IMAGE_DIR);
$shop->setLogo($image);
} else {
$shop->setLogo($oldPhoto);
}
$sluggedName = $this->slugger->slug($shop->getName())->lower();
$shop->setSlug($sluggedName);
$this->em->flush();
$this->addFlash('success', 'Boutique editée');
return $this->redirectToRoute('back_shop_list');
}
return $this->render('back/shop/edit.html.twig', [
'form' => $form->createView(),
'shop' => $shop
]);
}
/**
* @Route("/supprimer/{id}", name="back_shop_remove")
*/
public function remove(Shop $shop)
{
$this->em->remove($shop);
$this->em->flush();
$this->addFlash('success', 'Boutique supprimée');
return $this->redirectToRoute('back_shop_list');
}
}