src/Controller/Institution/ProductController.php line 92

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Institution;
  3. use App\Controller\BaseController;
  4. use App\Entity\Addressing\Country;
  5. use App\Entity\Addressing\County;
  6. use App\Entity\Institution\Product;
  7. use App\Entity\Institution\ProductType;
  8. use App\Manager\BitbagStatic\BlockManager;
  9. use App\Manager\Cart\CartManager;
  10. use App\Manager\Institution\InstitutionManager;
  11. use App\Manager\Partner\PartnerManager;
  12. use App\Service\Institution\OrderProductService;
  13. use App\Service\StaticPagesBitbagService;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. class ProductController extends BaseController
  18. {
  19.     public function indexAction(Request $request): Response
  20.     {
  21.         $request->getSession()->remove('iframeSourceLink');
  22.         $slug $request->get('slug');
  23.         /** @var Product $product */
  24.         $product $this->getEntityManager()->getRepository(Product::class)->findBySlug($slug);
  25.         $country $this->getEntityManager()->getRepository(Country::class)->find(Country::RO_COUNTRY_ID);
  26.         $counties $this->getEntityManager()->getRepository(County::class)->findAllByCountryForSelect($country);
  27.         $productAsArray $product->toArray();
  28.         $productAsArray['hasLegalPerson'] = $product->getProductType()->getInstitution()->isLegalPerson();
  29.         $productAsArray['hasPhysicalPerson'] = $product->getProductType()->getInstitution()->isPhysicalPerson();
  30.         $productAsArray['physicalIBANs'] = $product->getProductType()->getPhysicalIBANs();
  31.         $productAsArray['legalIBANs'] = $product->getProductType()->getLegalIBANs();
  32.         $productAsArray['category'] = $product->getProductType()->getCategory();
  33.         $selectedCountyCode $request->get('county')
  34.             ? explode('-'$request->get('county'))[1] : null;
  35.         if (strlen($selectedCountyCode) <= 2) {
  36.             foreach ($counties as $county) {
  37.                 if (strtoupper($county['value']) == strtoupper($selectedCountyCode)) {
  38.                     $productAsArray['selectedCounty'] = strtoupper($county['value']);
  39.                 }
  40.             }
  41.         }
  42.         if ($request->get('city')) {
  43.             $parts explode('-'$request->get('city'));
  44.             $cityId $parts[sizeof($parts) - 1];
  45.             if ($cityId) {
  46.                 $productAsArray['selectedCity'] = (int)$cityId;
  47.             }
  48.         }
  49.         if ($publicToken $request->get('partnerPublicToken')) {
  50.             $partnerService $this->getContainer()->get('app.service.partner.partner');
  51.             $partnerService->verifyToken($requestPartnerManager::CAN_ISSUE_FINES_INDEX);
  52.             $partner $partnerService->retrievePartner($publicToken);
  53.             if ($product->getProductType()->getCategory() === ProductType::CATEGORY_FINE) {
  54.                 $routeName 'app_shop_fines_iframe';
  55.             } else {
  56.                 $routeName 'app_shop_tariffs_iframe';
  57.             }
  58.             $request->getSession()->set('iframeSourceLink',
  59.                 $this->generateUrl($routeName, ['partnerPublicToken' => $partner->getPublicToken()])
  60.             );
  61.             CartManager::removeAllProductsFromCartBySession($request->getSession());
  62.         }
  63.         return $this->render('@templates/Institution/product.html.twig', [
  64.             'publicToken' => $publicToken,
  65.             'product' => $productAsArray,
  66.             'accordionBlocksFaq' => StaticPagesBitbagService::getAllFilteredBlocksAsArray($this->getEntityManager(),
  67.                 $product->getProductType()->getCategory() == ProductType::CATEGORY_FINE ?
  68.                     BlockManager::BLOCK_ACCORDION_TEXT_FINES_CODE_PATTERN :
  69.                     BlockManager::BLOCK_ACCORDION_TEXT_TARIFFS_CODE_PATTERN),
  70.             'keywordsForSeo' => StaticPagesBitbagService::getFirstFilteredBlocks($this->getEntityManager(),
  71.                 $product->getProductType()->getCategory() == ProductType::CATEGORY_FINE ?
  72.                     BlockManager::BLOCK_SEO_META_FINES_KEYWORD_CODE_PATTERN :
  73.                     BlockManager::BLOCK_SEO_META_TARIFFS_KEYWORD_CODE_PATTERN),
  74.             'descriptionForSeo' => StaticPagesBitbagService::getFirstFilteredBlocks($this->getEntityManager(),
  75.                 $product->getProductType()->getCategory() == ProductType::CATEGORY_FINE ?
  76.                     BlockManager::BLOCK_SEO_META_FINES_DESCRIPTION_CODE_PATTERN :
  77.                     BlockManager::BLOCK_SEO_META_TARIFFS_DESCRIPTION_CODE_PATTERN),
  78.             'counties' => $counties
  79.         ]);
  80.     }
  81.     public function retrieveProductsByCountyAndCityAction(Request $request): Response
  82.     {
  83.         if (!($countyCode $request->get('county'))) {
  84.             return new JsonResponse();
  85.         }
  86.         $cityId $request->get('city') ? (int)$request->get('city') : null;
  87.         $countyText $request->get('countyText') ? trim($request->get('countyText')) : null;
  88.         $cityText $request->get('cityText') ? trim($request->get('cityText')) : null;
  89.         $router $this->getContainer()->get('router');
  90.         $type $request->get('type');
  91.         $publicToken $request->getSession()->get(PartnerManager::SESSION_PARTNER_PUBLIC_TOKEN_NAME);
  92.         $products InstitutionManager::getProductsByLocation(
  93.             $this->getEntityManager(), $router$type$countyCode$countyText$cityId$cityText$publicToken
  94.         );
  95.         return $this->json($products);
  96.     }
  97.     public function submitData(): Response
  98.     {
  99.         /** @var OrderProductService $orderProductService */
  100.         $orderProductService $this->getContainer()->get('app.service.institution.order_product');
  101.         $response $orderProductService->storeNewProductInCart(
  102.             $this->getEntityManager(),
  103.             $this->getContainer()->getParameter('kernel.project_dir')
  104.         );
  105.         return new JsonResponse($response);
  106.     }
  107.     public function removeFromCart(): Response
  108.     {
  109.         /** @var OrderProductService $orderProductService */
  110.         $orderProductService $this->getContainer()->get('app.service.institution.order_product');
  111.         $result $orderProductService->removeFromCartSession();
  112.         return new JsonResponse(['result' => $result]);
  113.     }
  114.     public function fines(Request $request): Response
  115.     {
  116.         return $this->renderList($request, ['title' => 'Plata Amenzi''category' => 'fines''type' => 'fine']);
  117.     }
  118.     public function tariffs(Request $request): Response
  119.     {
  120.         return $this->renderList($request, ['title' => 'Plata Tarife''category' => 'tariffs''type' => 'tariff']);
  121.     }
  122.     private function renderList(Request $request, array $params): Response
  123.     {
  124.         $params $this->addProductToParams($request$params);
  125.         return $this->render('@templates/Front/Institution/list.html.twig'$params);
  126.     }
  127.     public function finesIframe(Request $request): Response
  128.     {
  129.         $partnerService $this->getContainer()->get('app.service.partner.partner');
  130.         $partnerService->verifyToken($requestPartnerManager::CAN_ISSUE_FINES_INDEX);
  131.         $partner $partnerService->retrievePartner($request->get('partnerPublicToken'));
  132.         $request->getSession()->set('iframeSourceLink'$this->generateUrl('app_shop_fines_iframe', [
  133.             'partnerPublicToken' => $partner->getPublicToken()
  134.         ]));
  135.         return $this->renderIframe($request, [
  136.             'title' => 'Plata Amenzi',
  137.             'category' => 'fines',
  138.             'type' => 'fine',
  139.             'publicToken' => $partner->getPublicToken(),
  140.         ]);
  141.     }
  142.     public function tariffsIframe(Request $request): Response
  143.     {
  144.         $partnerService $this->getContainer()->get('app.service.partner.partner');
  145.         $partnerService->verifyToken($requestPartnerManager::CAN_ISSUE_FINES_INDEX);
  146.         $partner $partnerService->retrievePartner($request->get('partnerPublicToken'));
  147.         $request->getSession()->set('iframeSourceLink'$this->generateUrl('app_shop_tariffs_iframe', [
  148.             'partnerPublicToken' => $partner->getPublicToken()
  149.         ]));
  150.         return $this->renderIframe($request, [
  151.             'title' => 'Plata Tarife',
  152.             'category' => 'tariffs',
  153.             'type' => 'tariff',
  154.             'publicToken' => $partner->getPublicToken(),
  155.         ]);
  156.     }
  157.     private function renderIframe(Request $request, array $params): Response
  158.     {
  159.         PartnerManager::checkQRCodeFlow($request);
  160.         $params $this->addProductToParams($request$params);
  161.         return $this->render('@templates/Front/Institution/iframe.html.twig'$params);
  162.     }
  163.     private function addProductToParams(Request $request, array $params): array
  164.     {
  165.         $country $this->getEntityManager()->getRepository(Country::class)->find(Country::RO_COUNTRY_ID);
  166.         $params['counties'] = $this->getEntityManager()->getRepository(County::class)->findAllByCountryForSelect($country);
  167.         if (!($productSlug $request->get('slug'))) {
  168.             $params['product'] = null;
  169.         } else {
  170.             /** @var Product $product */
  171.             $product $this->getEntityManager()->getRepository(Product::class)->findBySlug($productSlug);
  172.             $params['product'] = $product->toArray();
  173.             $params['product']['hasLegalPerson'] = $product->getProductType()->getInstitution()->isLegalPerson();
  174.             $params['product']['hasPhysicalPerson'] = $product->getProductType()->getInstitution()->isPhysicalPerson();
  175.             $params['product']['physicalIBANs'] = $product->getProductType()->getPhysicalIBANs();
  176.             $params['product']['legalIBANs'] = $product->getProductType()->getLegalIBANs();
  177.         }
  178.         return $params;
  179.     }
  180. }