<?php
namespace App\Controller;
use App\DTO\AppDTO;
use App\Model\Cart as ModelCart;
use App\Model\Prod as ModelProd;
use App\Service\Auth\Auth as Auth;
use App\Service\Checkout\Checkout;
use App\Repository\DeliveryRepository;
use App\Model\Delivery as ModelDelivery;
use App\Model\Wishlist as ModelWishlist;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;
use App\Service\Paginator as ServicePaginator;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\RequestStack;
use App\RepositoryInterface\PageRepositoryInterface;
use App\RepositoryInterface\ProdRepositoryInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class CartController extends AbstractASController
{
protected CacheInterface $Cache;
protected AppDTO $app;
// Models
protected DeliveryRepository $Deliveries;
protected ProdRepositoryInterface $Prods;
protected PageRepositoryInterface $Pages;
protected ModelWishlist $Wishes;
protected ModelCart $Cart;
protected Auth $Auth;
protected Checkout $Checkout;
protected ModelProd $ModelProd;
public function __construct(DeliveryRepository $Deliveries, PageRepositoryInterface $Pages, ProdRepositoryInterface $Prods, ModelWishlist $Wishes, CacheInterface $Cache, AppDTO $app, Auth $Auth, ModelCart $Cart, Checkout $Checkout, ModelProd $ModelProd, Security $security, RequestStack $requestStack)
{
$this->requestStack = $requestStack;
$this->Cache = $Cache;
$this->app = $app;
$this->Auth = $Auth;
$this->Auth->setUser($security->getUser());
$this->Cart = $Cart;
$this->Checkout = $Checkout;
$this->Pages = $Pages;
$this->Prods = $Prods;
$this->Wishes = $Wishes;
$this->Deliveries = $Deliveries;
$this->ModelProd = $ModelProd;
}
#[Route(path: '/cart', name: 'cart_no_locale', defaults: ['_locale' => '%app.default_lang%'])]
#[Route(path: '/{_locale}/cart', name: 'cart', requirements: ['_locale' => '%app.langs%'])]
public function index(Request $request): Response
{
$start = (int) $request->get('start', 0);
$results = $this->getParameter('cart.results');
$wishlist = $this->Wishes->getOnePage($start, $results);
$deliveries = $this->Deliveries->getByRegion($this->Checkout->getRegion_fias_id());
$freedelivery = $this->Cart->isFreeDelivery((float) $this->app->sett->get('free_delivery_amount'));
$prod_limited_msg = $this->app->labels->get('cart-edit-1').':\n';
foreach ($this->Cart->getProdsLimited() as $k => $v)
{
$prod = $this->Prods->get($v);
$prod_limited_msg .= $prod->getArt().': '.$prod->getNum().' '.$this->app->labels->get('cart-edit-2').'\n';
}
$cart_items = $this->Cart->getCart();
foreach ($cart_items as $k => $v) {
$cart_items[$k]['prod'] = $this->Prods->get($v['id']);
}
return $this->render('cart/edit.html.twig', [
'cart' => $this->Cart,
'cart_items' => $cart_items,
'wishlist' => $wishlist,
'deliveries' => $deliveries,
'freedelivery' => $freedelivery,
'prod_limited_msg' => $prod_limited_msg,
'checkout' => $this->Checkout,
'page' => $this->Pages->getByName('cart'),
'paginator' => new ServicePaginator('cart', count($wishlist), $results, $start),
]);
}
#[Route(path: '/cart/show', name: 'cart_show_no_locale', defaults: ['_locale' => '%app.default_lang%'])]
#[Route(path: '/{_locale}/cart/show', name: 'cart_show', requirements: ['_locale' => '%app.langs%'])]
public function show(Request $request): Response
{
$freedelivery = $this->Cart->isFreeDelivery((float) $this->app->sett->get('free_delivery_amount'));
$cart_items = $this->Cart->getCart();
foreach ($cart_items as $k => $v) {
$cart_items[$k]['prod'] = $this->Prods->get($v['id']);
}
return $this->render('cart/show.html.twig', [
'order' => null,
'cart' => $this->Cart,
'cart_items' => $cart_items,
'freedelivery' => $freedelivery,
'checkout' => $this->Checkout,
]);
}
#[Route(path: '/cart/list', name: 'cart_list_no_locale', defaults: ['_locale' => '%app.default_lang%'])]
#[Route(path: '/{_locale}/cart/list', name: 'cart_list', requirements: ['_locale' => '%app.langs%'])]
public function list(Request $request): Response
{
$cart_items = $this->Cart->getCart();
foreach ($cart_items as $k => $v) {
$cart_items[$k]['prod'] = $this->Prods->get($v['id']);
}
return $this->render('cart/list.html.twig', [
'cart' => $this->Cart,
'cart_items' => $cart_items,
]);
}
#[Route(path: '/cart/clear', name: 'cart_clear')]
public function clear(Request $request): Response
{
$this->Cart->deleteAll();
return new Response("ok");
}
}