<?php
namespace App\Controller\Peaj;
use App\Controller\BaseController;
use App\Entity\Order\Order;
use App\Entity\Peaj\Category;
use App\Entity\Peaj\Peaj;
use App\Manager\BitbagStatic\BlockManager;
use App\Manager\Cart\CartManager;
use App\Manager\CountryManager;
use App\Manager\Partner\PartnerManager;
use App\Manager\Peaj\PeajManager;
use App\Service\AuthTokenService;
use App\Service\StaticPagesBitbagService;
use BitBag\SyliusCmsPlugin\Entity\Page;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
class PeajController extends BaseController
{
public function indexAction(Request $request): Response
{
$locale = $request->getLocale();
$categories = $this->getEntityManager()->getRepository(Category::class)
->findAllAsArray($this->getContainer()->get('translator'));
$countries = CountryManager::getActiveCountriesForRoVignette($this->getEntityManager());
$pageRepository = $this->getEntityManager()->getRepository(Page::class);
$companyServicesPeajPage = $pageRepository->findOneEnabledByCode('company-services-peaj', $locale);
$aboutPeaj = StaticPagesBitbagService::getAllFilteredBlocks($this->getEntityManager(), BlockManager::BLOCK_ACCORDION_TEXT_PEAJ_CODE_PATTERN);
return $this->render('@templates/Front/Peaj/index.html.twig', [
'categories' => $categories,
'countries' => $countries,
'accordionBlocksAboutPeaj' => $aboutPeaj,
'companyServicesPeajPage' => $companyServicesPeajPage,
'number_of_passes' => $this->getContainer()->get('app.service.peaj.peaj')->getDefaultNumberOfPasses(),
]);
}
public function submitData(Request $request): Response
{
$peajService = $this->getContainer()->get('app.service.peaj.peaj');
if ($errors = $peajService->validate($request->request->all())) {
return $this->json(['violations' => $errors]);
}
$response = $peajService->storeNewPeajInCart($request, Peaj::FLAG_SOURCE_PRODUCT_PAGE);
$params = [];
if ($request->get('publicToken')) {
$params['publicToken'] = $request->get('publicToken');
}
$response['redirectUrl'] = $this->generateUrl('app_shop_customer_info', $params);
return new JsonResponse($response);
}
public function iframeAction(Request $request): Response
{
$partnerService = $this->getContainer()->get('app.service.partner.partner');
$partnerService->verifyToken($request, PartnerManager::CAN_ISSUE_PEAJ_INDEX);
$partner = $partnerService->retrievePartner($request->get('partnerPublicToken'));
$request->getSession()->set('iframeSourceLink', $this->generateUrl('app_shop_peaj_iframe', [
'partnerPublicToken' => $partner->getPublicToken()
]));
PartnerManager::checkQRCodeFlow($request);
$categories = $this->getEntityManager()->getRepository(Category::class)
->findAllAsArray($this->getContainer()->get('translator'));
$countries = CountryManager::getActiveCountriesForRoVignette($this->getEntityManager());
return $this->render('@templates/Front/Peaj/iframe.html.twig', [
'iframe' => true,
'publicToken' => $partner->getPublicToken(),
'categories' => $categories,
'countries' => $countries,
'number_of_passes' => $this->getContainer()->get('app.service.peaj.peaj')->getDefaultNumberOfPasses(),
]);
}
public function addToCartById(Order $order, Request $request): Response
{
$clientIp = $request->getClientIp();
$session = $request->getSession();
foreach ($order->getPeaj() as $peaj) {
if (!PeajManager::isPeajAlreadyInCart($session, $peaj->getRegistrationPlate())) {
$cartItem = PeajManager::constructPeajForCartFromObject(
$peaj, $clientIp, Peaj::FLAG_SOURCE_REMARKETING
);
CartManager::setToCart($session, CartManager::PEAJ_PRODUCT_NAME, [$cartItem]);
}
}
$authTokenService = new AuthTokenService();
if ($token = $authTokenService->authenticateUserWithToken($request, $this->getEntityManager())) {
$this->get('security.token_storage')->setToken($token);
$event = new InteractiveLoginEvent($request, $token);
$this->dispatcher->dispatch($event, 'security.interactive_login');
$session = $request->getSession();
$session->set('_sylius.cart.FASHION_WEB', 180);
$session->save();
}
$utm = [
'utm_source' => $request->get('utm_source'),
'utm_medium' => $request->get('utm_medium'),
'utm_campaign' => $request->get('utm_campaign'),
'utm_term' => $request->get('utm_term'),
'utm_content' => $request->get('utm_content'),
'utm_id' => $request->get('utm_id')
];
return $this->render('@templates/ShopCustom/Redirect/wait_to_be_redirected.html.twig', [
'url' => $this->generateUrl('app_shop_customer_info', $utm)
]);
}
public function removeFromCart(Request $request): Response
{
$peajService = $this->getContainer()->get('app.service.peaj.peaj');
$result = $peajService->removePeajFromCartSessionByRegistrationPlate($request, $request->get('registrationPlate'));
return new Response(json_encode(array('result' => $result)));
}
public function findNumberOfPassesByCategory(Request $request): JsonResponse
{
$categoryRepo = $this->getEntityManager()->getRepository(Category::class);
if (!($categoryId = $request->get('category_id')) || !($category = $categoryRepo->find($categoryId))) {
return $this->json(['success' => false]);
}
$prices = $category->getPrices();
$numberOfPasses = [];
foreach ($prices as $key => $price) {
$availability = $price->getAvailability();
$numberOfPasses[$key] = [
'name' => str_replace(' pod Fetesti', '', $availability->getName()),
'price_id' => $price->getId(),
'price' => $price->getCustomerPrice(),
'currency' => $price->getCustomerCurrency(),
'quantity' => $availability->getQuantity(),
'available' => $availability->getMonthsAvailable(),
'availability_id' => $availability->getId(),
];
}
return $this->json(['success' => true, 'number_of_passes' => $numberOfPasses]);
}
}