<?php
namespace App\Controller;
use App\Entity\Zayavka;
use App\Entity\ZayavkaMono;
use App\Service\OtherFunction;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
Class CartController extends AbstractController
{
private $requestStack;
private $em;
public function __construct(RequestStack $requestStack, EntityManagerInterface $em)
{
$this->requestStack = $requestStack;
$this->em = $em;
}
public function cartBlock(){
$zayavkas = [];
if(strpos($this->requestStack->getParentRequest()->getPathInfo(), '/zayavka') === FALSE && strpos($this->requestStack->getParentRequest()->getPathInfo(), '/ajax') === FALSE) {
$clientId = '';
foreach(OtherFunction::getClientId() as $item){
if($item != 'not_identified') {
$clientId = $item;
break;
}
}
if(!empty($clientId)){
$zayavkas['zayavka'] = $this->em->getRepository(Zayavka::class)->getZayavkaCart($clientId);
$zayavkas['mono'] = $this->em->getRepository(ZayavkaMono::class)->getZayavkaCart($clientId);
}
if(!empty($zayavkas)){
foreach ($zayavkas as $zayavka) {
foreach ($zayavka as $item) {
}
}
}
}
return $this->render('cart/block.html.twig', [
'zayavkas' => $zayavkas,
'direction' => $this->getParameter('direction'),
]);
}
/**
* @Route(
* "/ajax/cart/delete/{type}/{id}",
* priority=11,
* name="ajax_cart_delete",
* methods={"GET","POST"},
* requirements={
* "type":"zayavka|mono",
* "id":"\d+",
* })
*/
public function delete($type, $id): Response
{
switch ($type){
case 'zayavka':
$entity = $this->em->getRepository(Zayavka::class)->findOneBy(['id' => $id]);
break;
case 'mono':
$entity = $this->em->getRepository(ZayavkaMono::class)->findOneBy(['id' => $id]);
break;
}
if(!empty($entity) && $entity->getCartDelete() == 0) {
$entity->setCartDelete(1);
$this->em->flush();
}
return $this->json('true');
}
}