src/Controller/DefaultController.php line 100

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller;
  4. use App\Entity\Advice;
  5. use App\Entity\AdviceCategory;
  6. use App\Entity\ContactData;
  7. use App\Entity\CropType;
  8. use App\Entity\Distributor;
  9. use App\Entity\DistributorBranch;
  10. use App\Entity\Module;
  11. use App\Entity\News;
  12. use App\Entity\NewsFilter;
  13. use App\Entity\Page;
  14. use App\Entity\PageModule;
  15. use App\Entity\Product;
  16. use App\Entity\ProductDistributorDistributorBranch;
  17. use App\Entity\ProductType;
  18. use App\Entity\WeedAtlas;
  19. use App\Form\ContactFormType;
  20. use App\Form\SearchFormType;
  21. use App\Service\ApiGoogleService;
  22. use App\Service\ApiMapService;
  23. use App\Service\BreadcrumbService;
  24. use App\Service\Notification\ContactNotificationService;
  25. use App\Service\PdfFileService;
  26. use App\Service\QRCodeGeneratorService;
  27. use App\Service\RtcpService;
  28. use App\Util\CacheInterface;
  29. use DateTime;
  30. use Doctrine\ORM\EntityManagerInterface;
  31. use Doctrine\Persistence\ManagerRegistry;
  32. use Exception;
  33. use InvalidArgumentException;
  34. use JMS\Serializer\SerializationContext;
  35. use JMS\Serializer\SerializerInterface;
  36. use Knp\Component\Pager\PaginatorInterface;
  37. use Psr\Cache\CacheItemPoolInterface;
  38. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  39. use Symfony\Bundle\FrameworkBundle\Console\Application;
  40. use Symfony\Component\Console\Input\ArrayInput;
  41. use Symfony\Component\Console\Output\BufferedOutput;
  42. use Symfony\Component\HttpFoundation\Cookie;
  43. use Symfony\Component\HttpFoundation\JsonResponse;
  44. use Symfony\Component\HttpFoundation\RedirectResponse;
  45. use Symfony\Component\HttpFoundation\Request;
  46. use Symfony\Component\HttpFoundation\RequestStack;
  47. use Symfony\Component\HttpFoundation\Response;
  48. use Symfony\Component\HttpKernel\KernelInterface;
  49. use Symfony\Component\Routing\Annotation\Route;
  50. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  51. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  52. use Symfony\Component\Validator\Validator\ValidatorInterface;
  53. class DefaultController extends BaseController
  54. {
  55.     private $randomStringService;
  56.     private $imageProvider;
  57.     private $descService;
  58.     private $logger;
  59.     private $searchService;
  60.     private $personalDataInspector;
  61.     private $personalDataAdministrator;
  62.     public function __construct(
  63.         EntityManagerInterface $em,
  64.         SerializerInterface $serializer,
  65.         private TokenStorageInterface $tokenStorage,
  66.         private RequestStack $requestStack,
  67.         PaginatorInterface $paginator,
  68.         ValidatorInterface $validator,
  69.         UserPasswordEncoderInterface $passwordEncoder,
  70.         private BreadcrumbService $breadcrumbs,
  71.         private CacheItemPoolInterface $cachePool,
  72.         private CacheInterface $cacheUtil,
  73.         private ApiGoogleService $apiGoogle,
  74.         private QRCodeGeneratorService $QRCodeGenerator,
  75.         private PdfFileService $pdfFile,
  76.         private ContactNotificationService $contactNotificationService,
  77.     ) {
  78.         $data = new DateTime('now');
  79.         $this->month = (int)$data->format('m');
  80.         $this->year $data->format('Y');
  81.         $this->user $tokenStorage->getToken()->getUser();
  82.         $this->request $requestStack->getCurrentRequest();
  83.         $this->paginator $paginator;
  84.         $this->em $em;
  85.         $this->serializer $serializer;
  86.         $this->validator $validator;
  87.         $this->passwordEncoder $passwordEncoder;
  88.     }
  89.     /**
  90.      * @Route("/contact", name="app_contact")
  91.      */
  92.     public function contact(Request $requestRtcpService $rtcpService)
  93.     {
  94.         try {
  95.             list($domain$locale) = $this->getRequestParameter();
  96.             /** @var Page $pageObject */
  97.             $pageObject $this->em->getRepository(Page::class)->getPageByParameterByUrl('contact'$domain$locale);
  98.             if (!$pageObject) {
  99.                 throw $this->createNotFoundException();
  100.             }
  101.             $seoPage = [];
  102.             $seoPage['meta-title'] = $pageObject->getMetaTitle();
  103.             $seoPage['meta-desc'] = $pageObject->getMetaDescription();
  104.             $seoPage['meta-keywords'] = $pageObject->getMetaKeywords();
  105.             $seoPage['nofollow'] = $pageObject->getInformationNoFollow();
  106.         } catch (Exception) {
  107.             $seoPage = [];
  108.         }
  109.          $contactData = new ContactData();
  110.         $contactData->setRtcp($rtcpService->getCurrentCode());
  111.         $form $this->createForm(ContactFormType::class, $contactData);
  112.         $form->handleRequest($request);
  113.         if ($form->isSubmitted() && $form->isValid()) {
  114.             $contactData $form->getData();
  115.             $this->contactNotificationService->sendContactEmail($contactData);
  116.             $this->addFlash('success''pages.contact.flash.sent');
  117.             return $this->redirectToRoute('app_contact', ['_locale' => $request->getLocale()]);
  118.         }
  119.         return $this->render(
  120.             'main/contact.html.twig',
  121.             [
  122.                 'domain' => $domain,
  123.                 'locale' => $locale,
  124.                 'seo' => $seoPage,
  125.                 'form' => $form->createView(),
  126.                 'page' => $pageObject
  127.             ]
  128.         );
  129.     }
  130.     /**
  131.      * @Route("/change-domain/{domain}", name="main_changeDomain")
  132.      */
  133.     public function changeDomain($domainRequest $request)
  134.     {
  135.         $cookie = new Cookie(
  136.             'domain',
  137.             $domain,
  138.             99999999999
  139.         );
  140.         $res = new Response();
  141.         $res->headers->setCookie($cookie);
  142.         $res->send();
  143.         return new Response("OK"Response::HTTP_OK);
  144.     }
  145.     /**
  146.      * @Route("/remove-cache", name="main_removeCache")
  147.      */
  148.     public function removeCache(KernelInterface $kernelRequest $request)
  149.     {
  150.         $application = new Application($kernel);
  151.         $application->setAutoExit(false);
  152.         $input = new ArrayInput([
  153.             'command' => 'sonata:cache:flush-all',
  154.         ]);
  155.         $output = new BufferedOutput();
  156.         $application->run($input$output);
  157.         $this->cacheUtil->deleteAll($this->cachePool);
  158.         $request->getSession()->getFlashBag()->add("success""Cache wyczyszczony.");
  159.         $referer $request->headers->get('referer');
  160.         return $this->redirect($referer);
  161.     }
  162.     // /**
  163.     //  * @Route("/{_locale}/{url}", name="app_page", requirements={"_locale"="%app_locales%", "url"=".+"}, defaults={"_locale": "pl"})
  164.     //  * @Route("/{url}", name="app_page_default", requirements={"url"="^(?!admin|profiler).+"}, defaults={"_locale": "pl"})
  165.     //  */
  166.     // public function pageItem($url, ManagerRegistry $doctrine): Response
  167.     // {
  168.     //     return $this->page($url);
  169.     // }
  170.     private function page(string $url null): Response
  171.     {
  172.         list($domain$locale) = $this->getRequestParameter();
  173.         /** @var Page $page */
  174.         $page $this->em->getRepository(Page::class)->getPageByParameterByUrl($url$domain null$locale null);
  175.         if (!$page) {
  176.             throw $this->createNotFoundException();
  177.         }
  178.         $response $this->cacheUtil->getItem($this->cachePool$page->getId() . "_" $domain->getName() . '_' $locale->getSlug());
  179.         if (!$response) {
  180.             $modules $page->getModules();
  181.             $list = [];
  182.             $i 0;
  183.             /** @var PageModule $item */
  184.             foreach ($modules as $item) {
  185.                 /** @var Module $module */
  186.                 $module $item->getModule();
  187.                 if ($module && $item->getActive()) {
  188.                     $list[$i]['id'] = $module->getId();
  189.                     $list[$i]['type'] = $module->getTypeName();
  190.                     $list[$i]['countItems'] = $module->getCountItems();
  191.                     $list[$i]['template'] = $module->getTemplate();
  192.                     $i++;
  193.                 }
  194.             }
  195.             $response $this->serializer->serialize($list'json'SerializationContext::create()->setSerializeNull(true));
  196.             $this->cacheUtil->saveItem($this->cachePool$page->getId() . "_" $domain->getName() . '_' $locale->getSlug(), $response);
  197.         }
  198.         $modules json_decode($responsetrue);
  199.         $seo null;
  200.         return $this->render('main/page.html.twig', ['page' => $page'modules' => $modules'seo' => $seo'domain' => $domain'locale' => $locale]);
  201.     }
  202.     /**
  203.      * @Route("/qr", name="app_qr_generate")
  204.      */
  205.     public function qrCodeGenerate(Request $request): Response
  206.     {
  207.         list($domain$locale) = $this->getRequestParameter();
  208.         $tripId = (int) $request->get("trip-id"null);
  209.         $orderId = (int) $request->get("order-id"null);
  210.         if (empty($tripId) || empty($orderId)) {
  211.             return new JsonResponse([
  212.                 'status' => false,
  213.                 'message' => 'The trip-id and order-id parameters are necessary.'
  214.             ]);
  215.         }
  216.         try {
  217.             $qrCodeFile $this->QRCodeGenerator->createQRCode($tripId$orderId);
  218.             $pdfFile $this->pdfFile->saveTicketToPdfFile($qrCodeFile$tripId$orderId);
  219.             return new JsonResponse([
  220.                 'status' => true,
  221.                 'message' => $pdfFile
  222.             ]);
  223.         } catch (Exception $e) {
  224.             return new JsonResponse([
  225.                 'status' => false,
  226.                 'message' => $e->getMessage()
  227.             ]);
  228.         }
  229.     }
  230. }