<?php
namespace App\Controller\Uniqskills\Cart;
use App\Enum\GoogleAnalyticsEvents;
use App\Enum\PortalHashes;
use App\Event\Analitics\InitiateCheckoutEvent;
use App\Form\Frontend\Uniqskills\Cart\CustomerType;
use App\Utils\Api\ApiCartService;
use App\Utils\Uniqskills\Cart\Actions\CartAddressService;
use App\Utils\Uniqskills\Cart\Actions\CartDetailsService;
use App\Utils\Uniqskills\Cart\Actions\CartPortalService;
use App\Utils\Uniqskills\Cart\Actions\CartTermsService;
use App\Utils\Uniqskills\GoogleAnalytics\GoogleAnalyticsEventService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
/**
* @Route("/{_locale}/cart", name="app_uniqskills_cart_")))
*/
class CartStepsController extends AbstractController
{
private $cartDetailsService;
private $cartAddressService;
private $cartPortalService;
private $gaService;
public function __construct(
CartDetailsService $cartDetailsService,
CartAddressService $cartAddressService,
CartTermsService $cartTermsService,
CartPortalService $cartPortalService,
GoogleAnalyticsEventService $gaService,
ApiCartService $apiCartService
) {
$this->cartDetailsService = $cartDetailsService;
$this->cartAddressService = $cartAddressService;
$this->cartPortalService = $cartPortalService;
$this->gaService = $gaService;
}
/**
* @Route("", name="index", host="www.%us_url%")
*/
public function index(): Response
{
if ($_ENV['US_PORTAL_SETTINGS'] === PortalHashes::UK_UNIQSKILLS)
{
return $this->redirectToRoute('uniqskills_homepage');
}
return $this->render('uniqskills/cartcms/index.html.twig');
}
/**
* @Route("/render-index-data", name="render_index_data")
*/
public function indexData(Request $request, EventDispatcherInterface $eventDispatcher): JsonResponse
{
$cart = $this->cartDetailsService->getCart($this->getUser());
$portalSettings = $this->cartPortalService->getPortalSettings();
if ($cart['hash'])
{
$eventDispatcher->dispatch(new InitiateCheckoutEvent($cart['hash']), InitiateCheckoutEvent::CART_INITIATE_CHECKOUT);
}
return $this->json([
'cart' => $this->renderView('uniqskills/cartcms/index_content.html.twig', [
'cart' => $cart,
'portalSettings' => $portalSettings
]),
'gaEvent' => $this->gaService->getEventContent($cart, GoogleAnalyticsEvents::VIEW_CART)
]);
}
/**
* @Route("/details", name="details")
*/
public function details(Request $request): Response
{
if ($_ENV['US_PORTAL_SETTINGS'] === PortalHashes::UK_UNIQSKILLS)
{
return $this->redirectToRoute('uniqskills_homepage');
}
return $this->render('uniqskills/cartcms/details.html.twig');
}
/**
* @Route("/formularz", name="form")
*/
public function form(Request $request): Response
{
if ($_ENV['US_PORTAL_SETTINGS'] === PortalHashes::UK_UNIQSKILLS)
{
return $this->redirectToRoute('uniqskills_homepage');
}
$portalSettings = $this->cartPortalService->getPortalSettings();
$address = $this->cartAddressService->getAddress($this->getUser(), $request->request->all());
$cart = $this->cartDetailsService->getCart($this->getUser());
$positions = $this->cartPortalService->getPositionsForPortal($portalSettings);
$honorifics = $this->cartDetailsService->getHonorificsForPortal($portalSettings);
$form = $this->createForm(CustomerType::class, $address, [
'position' => $positions,
'validation_groups' => false
])->handleRequest($request);
if (empty($cart['products']))
{
return $this->json([]);
}
return $this->json([
'form' => $this->renderView('uniqskills/cartcms/form.html.twig', [
'cart' => $cart,
'address' => $address,
'form' => $form->createView(),
'hidePositions' => empty($positions) || $portalSettings->getHideFieldPosition() === true,
'hideHonorifics' => empty($honorifics)
]),
'gaEvent' => $this->gaService->getEventContent($cart, GoogleAnalyticsEvents::BEGIN_CHECKOUT)
]);
}
}