src/Controller/SiteShopController.php line 56

Open in your IDE?
  1. <?php 
  2. declare(strict_types=1);
  3. namespace App\Controller;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Sylius\Bundle\CustomerBundle\Form\Type\CustomerType;
  9. use WhiteOctober\BreadcrumbsBundle\Model\Breadcrumbs;
  10. use App\Entity\SiteShop;
  11. use App\Entity\SitePageCMS;
  12. use App\Entity\SiteShopTranslation;
  13. use App\Entity\Customer\Customer;
  14. use App\EventListener\AdminResourceListener;
  15. use App\Form\Type\SiteShopType;
  16. class SiteShopController extends AbstractController
  17. {
  18.     public function indexAction(Breadcrumbs $breadcrumbs): Response
  19.     {
  20.         
  21.         $em $this->get('doctrine')->getManager();
  22.         $shopRepository $em->getRepository(SiteShop::class);
  23.         $shops $shopRepository->createQueryBuilder('a')
  24.         // ->where('a.enabled = 1')
  25.         ->andwhere('a.marche = false')   
  26.         ->orderBy('a.postalCode''ASC')
  27.         ->getQuery()
  28.         ->getResult();
  29.         $marches $shopRepository->createQueryBuilder('a')
  30.         // ->where('a.enabled = 1')
  31.         ->andwhere('a.marche = true')   
  32.         ->orderBy('a.postalCode''ASC')
  33.         ->getQuery()
  34.         ->getResult();
  35.         // Page CMS "Les marchés"
  36.         $pageCmsRepository $em->getRepository(SitePageCMS::class);
  37.         $pageCms $pageCmsRepository->findOneById(AdminResourceListener::PAGE_CMS_LES_MARCHE_ID);
  38.         $breadcrumbs->addRouteItem("sylius.ui.home""sylius_shop_homepage");
  39.         $breadcrumbs->addItem("Nos boutiques");
  40.         return $this->render('site_list_shop_index.html.twig', [
  41.             'shops' => $shops,
  42.             'marches' => $marches,
  43.             'pageCms' => $pageCms
  44.         ]);
  45.     }
  46.     public function showAction(Breadcrumbs $breadcrumbs$slug): Response
  47.     {
  48.         $em $this->get('doctrine')->getManager();
  49.         $shopRepository $em->getRepository(SiteShop::class);
  50.         $shop $shopRepository->findOneBySlug($slug);
  51.         if (empty($shop)) {
  52.             throw new NotFoundHttpException("Cette boutique n'existe pas.");
  53.         }
  54.         $breadcrumbs->addRouteItem("sylius.ui.home""sylius_shop_homepage");
  55.         $breadcrumbs->addRouteItem("Nos boutiques""app_nosboutiques");
  56.         $breadcrumbs->addItem($shop->getName());
  57.         return $this->render('site_list_shop_show.html.twig', [
  58.             'shop' => $shop
  59.         ]);
  60.     }
  61.     public function selectOrUpdateAction(Request $request): Response
  62.     {
  63.         // Non connecté ? 
  64.         if ($this->isGranted('ROLE_USER') == false) {
  65.             return $this->redirectToRoute('sylius_shop_login');
  66.         }
  67.  
  68.         $em $this->get('doctrine')->getManager();
  69.         $repoCustomer $em->getRepository(Customer::class);
  70.         $customer $repoCustomer->findOneBy(['email' => $this->getUser()->getEmail()]);
  71.         if($customer->getIsShop() == 1){
  72.             $shopRepository $em->getRepository(SiteShop::class);
  73.             $siteshop $shopRepository->findOneBy(['contactEmail' => $this->getUser()->getEmail()]);
  74.            
  75.             $form $this->createForm(SiteShopType::class, $siteshop);
  76.            
  77.             if ($request->isMethod('POST')) {
  78.                
  79.                 // $data = $form->getData();
  80.                 $shopTranslationRepository $em->getRepository(SiteShopTranslation::class);
  81.                 foreach( $form->getData()->getTranslations() as $siteshoptranslation){
  82.                     $translationtodelete =  $shopTranslationRepository->find($siteshoptranslation->getId());
  83.                     $siteshop->removeTranslation($translationtodelete);
  84.                 }
  85.                 $em->persist($siteshop);
  86.                 $em->flush();
  87.                 $data $request->request->get('app_site_shop');
  88.                 foreach( $data['translations'] as $locale => $datatranslation){
  89.                     $translation = new SiteShopTranslation();
  90.                     $translation->setTranslatable($siteshop);
  91.                     $translation->setLocale($locale);
  92.                     $translation->setOpenHoursText($datatranslation['openHoursText']);
  93.                     $translation->setNewsText($datatranslation['newsText']);
  94.                     $translation->setDates($datatranslation['dates']);
  95.                     $siteshop->addTranslation($translation);
  96.                 }
  97.                 if(isset($data['enabled']) && $data['enabled'] == 1){
  98.                     $siteshop->setEnabled(true);
  99.                 }else{
  100.                     $siteshop->setEnabled(false);
  101.                 }                
  102.         
  103.                 $em->persist($siteshop);
  104.                 $em->flush();
  105.                 $request->getSession()->getFlashBag()->add(
  106.                     'success',
  107.                     "Les informations sont modifées"
  108.                 );
  109.                 return $this->redirectToRoute($request->get('_route'));
  110.             }
  111.         }else{
  112.             
  113.             $form $this->createForm(CustomerType::class, $customer);
  114.             if ($request->isMethod('POST')) {
  115.                 $data $request->request->get('sylius_customer');
  116.                 if (empty($data['favoriteShop']) == false) {
  117.                     $shopRepository $em->getRepository(SiteShop::class);
  118.                     $shop $shopRepository->find($data['favoriteShop']);
  119.                     $customer->setFavoriteShop($shop);
  120.                     $em->persist($customer);
  121.                     $em->flush();
  122.     
  123.                     $request->getSession()->getFlashBag()->add(
  124.                         'success',
  125.                         "Votre boutique favorite est enregistrée"
  126.                     );
  127.     
  128.                     return $this->redirectToRoute($request->get('_route'));
  129.                 }
  130.             }
  131.         }
  132.         
  133.         return $this->render('/account/site_account_select_my_shop.html.twig', [
  134.             'form' => $form->createView(),
  135.             'id_shop' => empty($customer->getFavoriteShop()) ? null : (int) $customer->getFavoriteShop()->getId()
  136.         ]);
  137.     }
  138. }