<?php
namespace App\Controller\HuVignette;
use App\Controller\BaseController;
use App\Entity\Addressing\Country;
use App\Manager\BitbagStatic\BlockManager;
use App\Entity\HuVignette\Category;
use App\Entity\HuVignette\Price;
use App\Entity\System\ProductStatus;
use App\Manager\Cart\CartManager;
use App\Manager\CountryManager;
use App\Manager\HuVignette\HuVignetteManager;
use App\Manager\Partner\PartnerManager;
use App\Model\ShopUserModel;
use App\Service\HuVignetteService;
use App\Service\StaticPagesBitbagService;
use App\Validator\HuVignetteFormValidator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class HuVignetteController extends BaseController
{
public function indexAction(Request $request)
{
$invalidCategory = 'B2';
$render = '@templates/Front/HuVignette/hu-vignette.html.twig';
if ($this->isMobile) {
$render = '@templates/Mobile/HuVignette/hu-vignette.html.twig';
}
$request->getSession()->remove('iframeSourceLink');
return $this->render($render,
[
'categories' => $this->getDoctrine()->getRepository(Category::class)->findAllCategoriesWithout($invalidCategory),
'countries' => CountryManager::getActiveCountriesForHuVignette($this->getEntityManager()),
'isAdmin' => ShopUserModel::checkIfUserIsAdmin($this->getUser(), $this->getDoctrine()->getManager()),
'accordionBlocksAboutHuVignette' => StaticPagesBitbagService::getAllFilteredBlocks($this->getEntityManager(), BlockManager::BLOCK_ACCORDION_TEXT_HU_VIGNETTE_CODE_PATTERN),
'huVignetteKeywordsForSeo' => StaticPagesBitbagService::getFirstFilteredBlocks($this->getEntityManager(), BlockManager::BLOCK_SEO_META_HU_VIGNETTE_KEYWORD_CODE_PATTERN),
'huVignetteDescriptionForSeo' => StaticPagesBitbagService::getFirstFilteredBlocks($this->getEntityManager(), BlockManager::BLOCK_SEO_META_HU_VIGNETTE_DESCRIPTION_CODE_PATTERN),
'isOneYearHuVignetteAvailableThisYear' => new \DateTime <= new \DateTime(HuVignetteManager::LAST_DATE_FOR_ONE_YEAR_HU_VIGNETTE_AVAILABILITY . '-' . date('Y'))
]);
}
public function getAvailabilitiesByCategory(Request $request)
{
$id = $request->get('id');
$availabilities = null;
if ($id != null) {
$availabilities = $this->getDoctrine()->getRepository(Price::class)->getAvailabilityForCategoryId($id);
$serializer = $this->container->get('serializer');
$availabilities = $serializer->serialize($availabilities, 'json');
}
return new Response($availabilities);
}
public function formSubmitAction(Request $request)
{
$session = $request->getSession();
$validator = new HuVignetteFormValidator($this->getEntityManager(), $this->getSerializer());
$errors = null;
if ($validator->checkForm($request)) {
$errors = json_encode(array('success' => 1));
} else {
if ($session->has('huVignetteErrors')) {
$errors = $session->get('huVignetteErrors');
}
}
$session->set('previousUrl', 'hu-vignette');
return new Response($errors);
}
public function checkVignetteAtApi(Request $request)
{
$errors = null;
$session = $request->getSession();
$order = CartManager::getCartData($session, 'huVignetteGlobalOrder');
$validRegistrationNumber = true;
if ($order) {
$validRegistrationNumber = HuVignetteService::checkHuVignetteValidation($order, $request, $this->getEntityManager());
}
if ($validRegistrationNumber) {
HuVignetteService::checkHuVignette($request);
if ($request->request->has('huVignetteSubmitForm')
&& $request->request->get('huVignetteSubmitForm') == 1) {
$errors = json_encode(array('success' => true));
} elseif ($request->request->has('specialErrors')) {
$vignetteApiError = (array) $request->request->get('specialErrors');
$apiStatusCode = $request->request->get('apiStatusCode');
$errors = json_encode(array('vignetteApiError' => $vignetteApiError, 'apiStatusCode' => $apiStatusCode));
}
} else {
$vignetteApiError = array('alreadyInCart' => 'Already in cart');
$apiStatusCode = 400;
$errors = json_encode(array('vignetteApiError' => $vignetteApiError, 'apiStatusCode' => $apiStatusCode));
}
return new Response($errors);
}
public function removeHuVignetteFromCart(Request $request)
{
$selectedItem = $request->get('guid');
$removeSmsAlert = $request->get('smsAlert');
$huVignettes = CartManager::getCartData($request->getSession(), 'huVignetteGlobalOrder');
$response = 'success';
if ($huVignettes) {
foreach ($huVignettes as $key => $huVignette) {
if ($selectedItem == $huVignette->guid) {
if (isset($removeSmsAlert) && $removeSmsAlert == true) {
if ($huVignette->vignette_alert == 1) {
$huVignette->vignette_alert = 0;
} else {
$response = 'alertNotSet';
}
} else {
unset($huVignettes[$key]);
}
}
}
}
$huVignettes = array_values($huVignettes);
CartManager::setToCart($request->getSession(), 'huVignetteGlobalOrder', $huVignettes);
return new Response(json_encode($response));
}
public function huVignetteIframe(Request $request)
{
$invalidCategory = 'B2';
$partnerService = $this->getContainer()->get('app.service.partner.partner');
$partnerService->verifyToken($request, PartnerManager::CAN_ISSUE_HU_VIGNETTE_INDEX);
$token['partnerPublicToken'] = $request->getSession()->get(PartnerManager::SESSION_PARTNER_PUBLIC_TOKEN_NAME);
$request->getSession()->set('iframeSourceLink', $this->generateUrl('app_shop_ro_vignette_iframe', $token));
PartnerManager::checkQRCodeFlow($request);
$render = '@templates/Front/HuVignette/iframe.html.twig';
if ($this->isMobile) {
$render = '@templates/Mobile/HuVignette/iframe.html.twig';
}
return $this->render($render,
[
'categories' => $this->getDoctrine()->getRepository(Category::class)->findAllCategoriesWithout($invalidCategory),
'countries' => CountryManager::getActiveCountriesForHuVignette($this->getEntityManager()),
'isAdmin' => ShopUserModel::checkIfUserIsAdmin($this->getUser(), $this->getDoctrine()->getManager()),
'accordionBlocksAboutHuVignette' => StaticPagesBitbagService::getAllFilteredBlocks($this->getEntityManager(), BlockManager::BLOCK_ACCORDION_TEXT_HU_VIGNETTE_CODE_PATTERN),
'huVignetteKeywordsForSeo' => StaticPagesBitbagService::getFirstFilteredBlocks($this->getEntityManager(), BlockManager::BLOCK_SEO_META_HU_VIGNETTE_KEYWORD_CODE_PATTERN),
'huVignetteDescriptionForSeo' => StaticPagesBitbagService::getFirstFilteredBlocks($this->getEntityManager(), BlockManager::BLOCK_SEO_META_HU_VIGNETTE_DESCRIPTION_CODE_PATTERN),
'isOneYearHuVignetteAvailableThisYear' => new \DateTime <= new \DateTime(HuVignetteManager::LAST_DATE_FOR_ONE_YEAR_HU_VIGNETTE_AVAILABILITY . '-' . date('Y')),
'publicToken' => $request->get('partnerPublicToken')
]);
}
}