<?php
declare(strict_types=1);
namespace App\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Sylius\Bundle\CustomerBundle\Form\Type\CustomerType;
use WhiteOctober\BreadcrumbsBundle\Model\Breadcrumbs;
use App\Entity\SiteShop;
use App\Entity\SitePageCMS;
use App\Entity\SiteShopTranslation;
use App\Entity\Customer\Customer;
use App\EventListener\AdminResourceListener;
use App\Form\Type\SiteShopType;
class SiteShopController extends AbstractController
{
public function indexAction(Breadcrumbs $breadcrumbs): Response
{
$em = $this->get('doctrine')->getManager();
$shopRepository = $em->getRepository(SiteShop::class);
$shops = $shopRepository->createQueryBuilder('a')
// ->where('a.enabled = 1')
->andwhere('a.marche = false')
->orderBy('a.postalCode', 'ASC')
->getQuery()
->getResult();
$marches = $shopRepository->createQueryBuilder('a')
// ->where('a.enabled = 1')
->andwhere('a.marche = true')
->orderBy('a.postalCode', 'ASC')
->getQuery()
->getResult();
// Page CMS "Les marchés"
$pageCmsRepository = $em->getRepository(SitePageCMS::class);
$pageCms = $pageCmsRepository->findOneById(AdminResourceListener::PAGE_CMS_LES_MARCHE_ID);
$breadcrumbs->addRouteItem("sylius.ui.home", "sylius_shop_homepage");
$breadcrumbs->addItem("Nos boutiques");
return $this->render('site_list_shop_index.html.twig', [
'shops' => $shops,
'marches' => $marches,
'pageCms' => $pageCms
]);
}
public function showAction(Breadcrumbs $breadcrumbs, $slug): Response
{
$em = $this->get('doctrine')->getManager();
$shopRepository = $em->getRepository(SiteShop::class);
$shop = $shopRepository->findOneBySlug($slug);
if (empty($shop)) {
throw new NotFoundHttpException("Cette boutique n'existe pas.");
}
$breadcrumbs->addRouteItem("sylius.ui.home", "sylius_shop_homepage");
$breadcrumbs->addRouteItem("Nos boutiques", "app_nosboutiques");
$breadcrumbs->addItem($shop->getName());
return $this->render('site_list_shop_show.html.twig', [
'shop' => $shop
]);
}
public function selectOrUpdateAction(Request $request): Response
{
// Non connecté ?
if ($this->isGranted('ROLE_USER') == false) {
return $this->redirectToRoute('sylius_shop_login');
}
$em = $this->get('doctrine')->getManager();
$repoCustomer = $em->getRepository(Customer::class);
$customer = $repoCustomer->findOneBy(['email' => $this->getUser()->getEmail()]);
if($customer->getIsShop() == 1){
$shopRepository = $em->getRepository(SiteShop::class);
$siteshop = $shopRepository->findOneBy(['contactEmail' => $this->getUser()->getEmail()]);
$form = $this->createForm(SiteShopType::class, $siteshop);
if ($request->isMethod('POST')) {
// $data = $form->getData();
$shopTranslationRepository = $em->getRepository(SiteShopTranslation::class);
foreach( $form->getData()->getTranslations() as $siteshoptranslation){
$translationtodelete = $shopTranslationRepository->find($siteshoptranslation->getId());
$siteshop->removeTranslation($translationtodelete);
}
$em->persist($siteshop);
$em->flush();
$data = $request->request->get('app_site_shop');
foreach( $data['translations'] as $locale => $datatranslation){
$translation = new SiteShopTranslation();
$translation->setTranslatable($siteshop);
$translation->setLocale($locale);
$translation->setOpenHoursText($datatranslation['openHoursText']);
$translation->setNewsText($datatranslation['newsText']);
$translation->setDates($datatranslation['dates']);
$siteshop->addTranslation($translation);
}
if(isset($data['enabled']) && $data['enabled'] == 1){
$siteshop->setEnabled(true);
}else{
$siteshop->setEnabled(false);
}
$em->persist($siteshop);
$em->flush();
$request->getSession()->getFlashBag()->add(
'success',
"Les informations sont modifées"
);
return $this->redirectToRoute($request->get('_route'));
}
}else{
$form = $this->createForm(CustomerType::class, $customer);
if ($request->isMethod('POST')) {
$data = $request->request->get('sylius_customer');
if (empty($data['favoriteShop']) == false) {
$shopRepository = $em->getRepository(SiteShop::class);
$shop = $shopRepository->find($data['favoriteShop']);
$customer->setFavoriteShop($shop);
$em->persist($customer);
$em->flush();
$request->getSession()->getFlashBag()->add(
'success',
"Votre boutique favorite est enregistrée"
);
return $this->redirectToRoute($request->get('_route'));
}
}
}
return $this->render('/account/site_account_select_my_shop.html.twig', [
'form' => $form->createView(),
'id_shop' => empty($customer->getFavoriteShop()) ? null : (int) $customer->getFavoriteShop()->getId()
]);
}
}