Browse Source

[Frontend] Cookies : adaptations

feature/export_comptable
Guillaume 4 years ago
parent
commit
1eb3e6839f
2 changed files with 37 additions and 11 deletions
  1. +22
    -3
      ShopBundle/Controller/Frontend/CartController.php
  2. +15
    -8
      ShopBundle/Services/OrderUtils.php

+ 22
- 3
ShopBundle/Controller/Frontend/CartController.php View File

use Lc\ShopBundle\Context\ProductFamilyInterface; use Lc\ShopBundle\Context\ProductFamilyInterface;
use Lc\ShopBundle\Context\ReductionCartInterface; use Lc\ShopBundle\Context\ReductionCartInterface;
use Lc\ShopBundle\Model\OrderReductionCart; use Lc\ShopBundle\Model\OrderReductionCart;
use Lc\ShopBundle\Services\UserUtils;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security; use Symfony\Component\Security\Core\Security;


class CartController extends BaseController class CartController extends BaseController
{ {
protected $orderUtils ; protected $orderUtils ;
protected $userUtils ;
protected $router ;
protected $productFamilyRepository ; protected $productFamilyRepository ;
protected $orderProductRepository ; protected $orderProductRepository ;


protected $orderProducts = [] ; protected $orderProducts = [] ;




public function __construct(EntityManagerInterface $em, Security $security, MerchantUtilsInterface $merchantUtils, OrderUtilsInterface $orderUtils)
public function __construct(EntityManagerInterface $em, Security $security, MerchantUtilsInterface $merchantUtils, OrderUtilsInterface $orderUtils,
UserUtils $userUtils, UrlGeneratorInterface $router)
{ {
parent::__construct($em, $security, $merchantUtils); parent::__construct($em, $security, $merchantUtils);
$this->orderUtils = $orderUtils ; $this->orderUtils = $orderUtils ;
$this->userUtils = $userUtils ;
$this->router = $router ;
$this->productFamilyRepository = $this->em->getRepository($this->em->getClassMetaData(ProductFamilyInterface::class)->getName()) ; $this->productFamilyRepository = $this->em->getRepository($this->em->getClassMetaData(ProductFamilyInterface::class)->getName()) ;
$this->orderProductRepository = $this->em->getRepository($this->em->getClassMetaData(OrderProductInterface::class)->getName()) ; $this->orderProductRepository = $this->em->getRepository($this->em->getClassMetaData(OrderProductInterface::class)->getName()) ;
} }


public function addProductFamily(Request $request) public function addProductFamily(Request $request)
{ {
$user = $this->security->getUser() ;
$visitor = $this->userUtils->getVisitorCurrent() ;

$return = [] ; $return = [] ;
$data = $request->request->all() ; $data = $request->request->all() ;

if(isset($data['order_products']['id_product_family'])) { if(isset($data['order_products']['id_product_family'])) {
$idProductFamily = $data['order_products']['id_product_family'] ; $idProductFamily = $data['order_products']['id_product_family'] ;
$this->productFamily = $this->productFamilyRepository->find($idProductFamily) ; $this->productFamily = $this->productFamilyRepository->find($idProductFamily) ;

// alerte si cookies non acceptés
if (!$user && !$visitor) {
$this->addFlash('error', 'Vous devez <a href="'.$this->router->generate('frontend_page', ['devAlias' => 'politique-de-confidentialite']).'">accepter les cookies</a> ou vous <a href="'.$this->router->generate('fos_user_security_login').'">connecter</a> pour ajouter un produit.');
return false ;
}

if($this->productFamily) { if($this->productFamily) {
$form = $this->createForm(OrderProductsType::class, ['id_product_family' => $this->productFamily->getId()]); $form = $this->createForm(OrderProductsType::class, ['id_product_family' => $this->productFamily->getId()]);
$form->handleRequest($request); $form->handleRequest($request);
$data = $form->getData() ; $data = $form->getData() ;
foreach($data as $orderProduct) { foreach($data as $orderProduct) {
if($orderProduct instanceof OrderProductInterface) { if($orderProduct instanceof OrderProductInterface) {
$this->orderUtils->addOrderProduct($orderShop, $orderProduct) ;
if($orderProduct->getQuantityOrder() > 0) {
$addOrderProduct = $this->orderUtils->addOrderProduct($orderShop, $orderProduct) ;
if($addOrderProduct && $orderProduct->getQuantityOrder() > 0) {
$this->orderProducts[] = $orderProduct ; $this->orderProducts[] = $orderProduct ;
} }
} }

+ 15
- 8
ShopBundle/Services/OrderUtils.php View File

protected $priceUtils; protected $priceUtils;
protected $productFamilyUtils; protected $productFamilyUtils;
protected $documentUtils ; protected $documentUtils ;
protected $session ;


public function __construct(EntityManagerInterface $em, Security $security, UserUtils $userUtils, public function __construct(EntityManagerInterface $em, Security $security, UserUtils $userUtils,
MerchantUtilsInterface $merchantUtils, PriceUtils $priceUtils, ProductFamilyUtilsInterface $productFamilyUtils, MerchantUtilsInterface $merchantUtils, PriceUtils $priceUtils, ProductFamilyUtilsInterface $productFamilyUtils,
DocumentUtils $documentUtils)
DocumentUtils $documentUtils, SessionInterface $session)
{ {
$this->em = $em; $this->em = $em;
$this->security = $security; $this->security = $security;
$this->priceUtils = $priceUtils; $this->priceUtils = $priceUtils;
$this->productFamilyUtils = $productFamilyUtils; $this->productFamilyUtils = $productFamilyUtils;
$this->documentUtils = $documentUtils ; $this->documentUtils = $documentUtils ;
$this->session = $session ;
} }




} }
} }


if (!$user && !$visitor) {
$this->session->getFlashBag()->add('error', 'Vous devez accepter les cookies ou vous connecter pour avoir un panier sur le site.');
}

return $orderShop; return $orderShop;
} }




public function addOrderProduct($orderShop, $orderProductAdd, $persist = true) public function addOrderProduct($orderShop, $orderProductAdd, $persist = true)
{ {
$return = false ;

$user = $this->security->getUser() ;
$visitor = $this->userUtils->getVisitorCurrent() ;

if(!$orderShop) { if(!$orderShop) {
$orderShop = $this->createOrderShop([ $orderShop = $this->createOrderShop([
'user' => $this->security->getUser(),
'visitor' => $this->userUtils->getVisitorCurrent(),
'user' => $user,
'visitor' => $visitor,
'merchant' => $this->merchantUtils->getMerchantCurrent() 'merchant' => $this->merchantUtils->getMerchantCurrent()
]); ]);
} }
} }


$updated = true; $updated = true;
$return = true ;


break; break;
} }
if ($persist) { if ($persist) {
$this->em->flush(); $this->em->flush();
} }
}


$return = true ;
}
} }
return $return ;
} }


public function compareOrderProductReductionCatalog($orderProductReductionCatalog1, $orderProductReductionCatalog2) public function compareOrderProductReductionCatalog($orderProductReductionCatalog1, $orderProductReductionCatalog2)

Loading…
Cancel
Save