<?php
namespace App\Controller\Frontend;
use App\Entity\Gos\Cart;
use App\Entity\Gos\ProductCart;
use App\Entity\Gos\Uniqskills\Course;
use App\Entity\Gos\User;
use App\Utils\Cart\CartPriceCalculator;
use App\Utils\CartServices;
use App\Utils\FacebookPixel\Api\FacebookPixelService;
use App\Utils\OrderGifts;
use App\Utils\PortalSettingsService;
use App\Utils\SalesManago\SalesManagoCartUtils;
use App\Utils\Shipping\ShippingTypes\ShippingTypesPicker;
use App\Utils\Uniqskills\DomainListener;
use App\Utils\Uniqskills\MixPanel\MixPanelService;
use App\Utils\Uniqskills\SuggestedCoursesService;
use App\Utils\UpsellingUtils;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ProductCartController extends AbstractController
{
private $em;
private $cartServices;
private $giftsService;
private $domainListener;
private $salesManagoCartUtils;
private $suggestedCoursesService;
private $mixPanelService;
private $portalSettingsService;
private $facebookPixelService;
private $shippingTypesPicker;
public function __construct(
EntityManagerInterface $entityManager,
CartServices $cartServices,
OrderGifts $orderGifts,
DomainListener $domainListener,
SalesManagoCartUtils $salesManagoCartUtils,
SuggestedCoursesService $suggestedCoursesService,
MixPanelService $mixPanelService,
PortalSettingsService $portalSettingsService,
FacebookPixelService $facebookPixelService,
ShippingTypesPicker $shippingTypesPicker
) {
$this->em = $entityManager;
$this->cartServices = $cartServices;
$this->giftsService = $orderGifts;
$this->domainListener = $domainListener;
$this->salesManagoCartUtils = $salesManagoCartUtils;
$this->suggestedCoursesService = $suggestedCoursesService;
$this->mixPanelService = $mixPanelService;
$this->portalSettingsService = $portalSettingsService;
$this->facebookPixelService = $facebookPixelService;
$this->shippingTypesPicker = $shippingTypesPicker;
}
/**
* @Route("/cart/view/{newCartHash}", name="fmProductCartFrontendView")
* @Route("/{_locale}/cart/{newCartHash}", name="fmUniqskillsCartIndex",
* host="www.%us_url%",
* requirements={"newCartHash"="^(?!.*(address|participants)).*$", "_locale"="^(?!.*(en)|(pl)).*$"}))
*/
public function viewAction(
Request $request,
LoggerInterface $logger,
UpsellingUtils $upsellingUtils,
CartPriceCalculator $cartPriceCalculator,
$newCartHash = null,
$iframe = true
)
{
// task GOS-1569
if (in_array($request->getLocale(), ['es', 'pt', 'ru', 'de', 'ua']) == true) return $this->redirectToRoute('fmUniqskillsHomePage');
/** @var Cart $cart*/
$findCart = $this->cartServices->findCart($this->getUser(), $newCartHash);
$cart = $findCart['cart'];
$mergeCarts = $findCart['mergeCarts'];
$requestSource = $this->domainListener->getRequestSource($request);
$isUniqskills = $request->get('_route') === 'fmUniqskillsCartIndex';
$suggestedCourses = null;
if (null === $portal = $this->portalSettingsService->getPortalSettings())
{
$logger->debug('No portal in product cart. cartHash: ' . $cart->getHash(), [
'userAgent' => $_SERVER["HTTP_USER_AGENT"] ?? null,
'referer' => $_SERVER["HTTP_REFERER"] ?? null,
]);
return new Response("Portal settings not set", Response::HTTP_BAD_REQUEST);
}
if ($cart->hasProducts())
{
if ($request->isMethod('POST'))
{
$this->giftsService->processCartProducts($cart);
if ($cart->hasEventProduct() || ($isUniqskills && $cart->hasUniqskillsMultiUserCourse()))
{
if ($isUniqskills)
{
return $this->redirectToRoute('fmUniqskillsCartParticipants');
}
return $this->redirectToRoute('fmOrderParticipants', ['source' => $requestSource]);
}
if ($isUniqskills)
{
return $this->redirectToRoute('fmUniqskillsCartAddress');
}
return $this->redirectToRoute('fmOrderUserBase', ['source' => $requestSource]);
}
if ($isUniqskills)
{
$suggestedCourses = [];
$isProlongation = $request->query->get('isProlongation');
/** @var ProductCart $productCart */
foreach ($cart->getProductCart() as $productCart)
{
if ($productCart->getProductVariant() === null)
{
continue;
}
if ($productCart->getProductVariant()->getCourses())
{
/** @var Course $course */
$course = $productCart->getProductVariant()->getCourses()->get(0);
$suggestedCourses = array_merge($suggestedCourses, $this->suggestedCoursesService->findSuggestedCourses($course));
}
}
if ($isProlongation == 'true')
{
$this->cartServices->findPaymentSystemForProlongationVariant($cart);
$this->em->refresh($cart);
}
}
}
$this->salesManagoCartUtils->saveCartEvent($cart, $this->getUser());
if (!($this->getUser() instanceof User))
{
$request->getSession()->set('name', 'noLogin');
}
if ($portal->checkIsCmsLiterkaPortal() === true)
{
$productVariantsList = [];
foreach ($cart->getProductCart() as $productCart)
{
if ($productCart->getProductVariant() != null) $productVariantsList[] = $productCart->getProductVariant();
}
$literkaProducts = $this->cartServices->getLiterkaProduct($portal, $productVariantsList);
}
$cart->setShippingType(null);
//only in new cart user can select shipping type
if ($portal->getEnableNewBasket())
{
$selectableShippingTypes = $this->shippingTypesPicker->find($cart);
$this->cartServices->setShippingType($cart, $selectableShippingTypes);
}
$price = $cartPriceCalculator->getCartPrice($cart);
$parameters = array(
'cart' => $cart,
'mergeCarts' => $mergeCarts,
'newCartHash' => $newCartHash,
'price' => isset($price) ? $price : null,
'error' => isset($price['error']) ? $price['error'] : false,
'iframe' => $iframe,
'source' => $requestSource,
'suggestedCourses' => $suggestedCourses,
'literkaProducts' => isset($literkaProducts) ? $literkaProducts : null,
'upsellings' => $upsellingUtils->prepareForCart($cart, 4 , [], false),
'additionalTextProductVariant' => $this->cartServices->getAdditionalTextProductVariant($cart)
);
if ($mergeCarts && isset($findCart['oldCart']))
{
$parameters['oldCart'] = $findCart['oldCart'];
}
if ($isUniqskills)
{
$this->mixPanelService->sendCartRenderEvent([
'step' => 'initial',
'products' => $price ?? null,
], $this->getUser());
$this->facebookPixelService->setEvent('InitiateCheckout', $cart->getId())->request();
return $this->render('uniqskills/cart/index.html.twig', $parameters);
}
if ($portal->getUseFbPixelInIframe() === true)
{
$this->facebookPixelService->setEvent('InitiateCheckout', $cart->getId())->request();
}
if ($_ENV['APP_ENV'] === 'dev')
{
$parameters['newCartNextStep'] = $_ENV['NEW_CART_DEV_URL'] . '/' . $cart->getHash();
}
else
{
$domain = str_replace('http://', '', $portal->getDomain());
$domain = str_replace('https://', '', $domain);
$domain = str_replace('www.', '', $domain);
$newCartLink = sprintf('%s://www.zamowienia.%s/%s', 'https', $domain, $cart->getHash());
$parameters['newCartNextStep'] = $newCartLink;
}
if ($request->isXmlHttpRequest() || $iframe === false)
{
if ($portal->getEnableNewBasket())
{
return $this->render('frontend/product_cart_2/ajax_view.html.twig', $parameters);
}
return $this->render('frontend/product_cart/ajax_view.html.twig', $parameters);
}
if ($portal->getEnableNewBasket())
{
return $this->render('frontend/product_cart_2/view.html.twig', $parameters);
}
return $this->render('frontend/product_cart/view.html.twig', $parameters);
}
/**
* @Route("/cart/mergeCart", name="fmProductCartFrontendMergeCart")
*/
public function mergeCartAction(Request $request, MergeCart $mergeCart): Response
{
$userCartHash = ($this->getUser() && $this->getUser()->getCart()) ? $this->getUser()->getCart()->getHash() : null;
$newCartHash = $request->request->get('newCartHash');
$cookieCartHash = $request->cookies->get('cartHash');
$method = $request->request->get('method', false);
if ($method)
{
if ($method === 'mergeCart')
{
$mergeCart->mergeCart(
empty($newCartHash) ? $cookieCartHash : $newCartHash,
$userCartHash
);
}
else
{
$mergeCart->takeNewCart(
empty($newCartHash) ? $cookieCartHash : $newCartHash,
$userCartHash
);
}
return new Response('ok');
}
return new Response('error');
}
public function cartStepAction(Request $request)
{
$cartSteps = array(
0 => array(
'name' => 'cart.steps.cart',
'path' => 'fmProductCartFrontendView',
'class' => ''
),
2 => array(
'name' => 'cart.steps.login',
'path' => 'fmOrderUserBase',
'class' => ''
),
3 => array(
'name' => 'cart.steps.address',
'path' => 'fmOrderAddressBase',
'class' => ''
),
4 => array(
'name' => 'cart.steps.summary',
'path' => 'fmOrderSummaryBase',
'class' => ''
),
);
$findCart = $this->cartServices->findCart($this->getUser());
/** @var Cart $cart */
$cart = $findCart['cart'];
if ($cart->hasEventProduct())
{
$cartSteps[1] = array(
'name' => 'cart.steps.participants',
'path' => 'fmOrderParticipants',
'class' => ''
);
}
ksort($cartSteps);
$active = true;
foreach ($cartSteps as $key => $cartStep)
{
if ($cartStep['path'] == $request->get('_route'))
{
$cartSteps[$key]['class'] = 'active currently-active';
$active = false;
}
else if ($active)
{
$cartSteps[$key]['class'] = 'active hidden-xs';
}
else
{
$cartSteps[$key]['class'] = 'hidden-xs';
}
}
$colSm = (12 / count($cartSteps));
return $this->render('frontend/product_cart_2/cart_step.html.twig', array(
'cartSteps' => $cartSteps,
'colSm' => (int)$colSm
));
}
}