src/Controller/CartWebhostingController.php line 159

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\CartDomain;
  4. use App\Entity\CartWebhosting;
  5. use App\Repository\CartDomainRepository;
  6. use App\Repository\CartWebhostingRepository;
  7. use App\Repository\ProductWebhostingRepository;
  8. use App\Repository\SettingRepository;
  9. use App\Service\CartWebhostingService;
  10. use DateTime;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\HttpFoundation\RedirectResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpFoundation\Session\Session;
  19. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Symfony\Contracts\Translation\TranslatorInterface;
  22. #[Route('/cart-webhosting')]
  23. class CartWebhostingController extends AbstractController
  24. {
  25.     private Session $session;
  26.     private string $sessionId;
  27.     public function __construct(private RequestStack $request)
  28.     {
  29.         if (!$this->request->getSession()) {
  30.             $session = new Session();
  31.             $this->session   $session;
  32.             $this->sessionId $session->getId();
  33.         } else {
  34.             $this->session   $this->request->getSession();
  35.             $this->sessionId $this->request->getSession()->getId();
  36.         }
  37.     }
  38.     #[Route('/'name'app_cart_webhosting'methods: ['GET'])]
  39.     public function index(): Response
  40.     {
  41.         return $this->render('cart_webhosting/index.html.twig', [
  42.             'controller_name' => 'CartWebhostingController',
  43.         ]);
  44.     }
  45.     #[Route('/new'name'app_cart_webhosting_new'methods: ['GET''POST'])]
  46.     public function new(Request $requestCartWebhostingRepository $cartWebhostingRepositoryCartDomainRepository $cartDomainRepositoryProductWebhostingRepository $productWebhostingRepositoryCartWebhostingService $cartWebhostingService): Response
  47.     {
  48.         $domain             $request->query->get('domain');
  49.         $domainNameComplete urldecode($domain);
  50.         if ($this->getUser()) {
  51.             $user $this->getUser();
  52.         }
  53.         $domainParts explode('.'$domainNameComplete);
  54.         $domainName  $domainParts[0];
  55.         $domainTld   $domainParts[1];
  56.         $domainExist $cartDomainRepository->findOneBy(['domain_name' => $domainName'domain_tld' => $domainTld]);
  57.         $cartWebhostingExist $cartWebhostingRepository->findOneBy(['session_id' => $this->sessionId]);
  58.         $action 'app_cart_webhosting_show';
  59.         if ($domainExist) {
  60.             $cartWebhostingExist $cartWebhostingRepository->find($domainExist->getCartWebhosting()->getId());
  61.             $action 'app_cart_webhosting_packages, {"id": ' $cartWebhostingExist->getId() . '}';
  62.         }
  63.         if (!$cartWebhostingExist) {
  64.             $cartWebhosting = new CartWebhosting();
  65.             $cartWebhosting->setSessionId($this->sessionId);
  66.             $cartWebhosting->setClient($user ?? null);
  67.             $cartWebhosting->setMonthlyPrice(0);
  68.             $cartWebhosting->setRegDatetime(new DateTime());
  69.             $cartWebhostingRepository->add($cartWebhostingtrue);
  70.             $cartWebhostingExist $cartWebhosting;
  71.         }
  72.         if (!$cartWebhostingExist->getProductWebhosting()) {
  73.             $action 'app_cart_webhosting_packages';
  74.         }
  75.         if (!$domainExist) {
  76.             $cartDomain = new CartDomain();
  77.             $cartDomain->setCartWebhosting($cartWebhostingExist);
  78.             $cartDomain->setSessionId($this->sessionId);
  79.             $cartDomain->setDomainName($domainName);
  80.             $cartDomain->setDomainTld($domainTld);
  81.             $cartDomain->setRegDatetime(new DateTime());
  82.             $cartDomain->setDomainPrice(0);
  83.             $cartDomainRepository->add($cartDomaintrue);
  84.         }
  85.         if ($cartWebhostingExist->getProductWebhosting()) {
  86.             $cartWebhostingService->updateDomainPrices($cartWebhostingExist->getCartDomains(), $cartWebhostingExist->getProductWebhosting());
  87.         }
  88.         return $this->render('_partials/continue.html.twig', [
  89.             'action' => $action,
  90.         ]);
  91.     }
  92.     #[Route('/webhosting-packages'name'app_cart_webhosting_packages'methods: ['GET''POST'])]
  93.     public function showWebhostingPackages(Request $requestCartWebhostingRepository $cartWebhostingRepositoryProductWebhostingRepository $productWebhostingRepository): Response
  94.     {
  95.         $cartWebhosting $cartWebhostingRepository->findOneBy(['session_id' => $this->sessionId]);
  96.         if ($request->query->get('id')) {
  97.             $cartWebhosting $cartWebhostingRepository->find($request->query->get('id'));
  98.         }
  99.         if ($this->getUser()) {
  100.             $cartWebhosting $cartWebhostingRepository->findOneBy(['client' => $this->getUser()]);
  101.         }
  102.         $domainsInCart $cartWebhosting->getCartDomains();
  103.         return $this->render('site/webhosting.html.twig', [
  104.             'cart_webhosting_id' => $cartWebhosting->getId(),
  105.             'product_webhosting' => $productWebhostingRepository->findBy(['is_public' => true'is_active' => true]),
  106.             'domains_in_cart'    => $domainsInCart,
  107.         ]);
  108.     }
  109.     #[Route('/edit'name'app_cart_webhosting_edit'methods: ['GET''POST'])]
  110.     public function edit(Request $requestCartWebhostingRepository $cartWebhostingRepositoryProductWebhostingRepository $productWebhostingRepositoryCartWebhostingService $cartWebhostingService): JsonResponse
  111.     {
  112.         $cartWebhostingId $request->query->get('cart_id');
  113.         $webhostingId     $request->query->get('id');
  114.         $cartWebhosting    $cartWebhostingRepository->find($cartWebhostingId);
  115.         $productWebhosting $productWebhostingRepository->find($webhostingId);
  116.         if ($cartWebhosting && $productWebhosting) {
  117.             $cartWebhosting->setProductWebhosting($productWebhosting);
  118.             $cartWebhosting->setMonthlyPrice($productWebhosting->getPackagePrice());
  119.             $cartWebhosting->setRegDatetime(new DateTime());
  120.             $cartWebhostingRepository->add($cartWebhostingtrue);
  121.             /**
  122.              * calculate and update domain prices
  123.              */
  124.             if ($cartWebhosting->getCartDomains()) {
  125.                 $cartWebhostingService->updateDomainPrices($cartWebhosting->getCartDomains(), $productWebhosting);
  126.             }
  127.         }
  128.         //return $this->redirectToRoute('app_cart_webhosting_show', ['id' => $cartWebhostingId], Response::HTTP_SEE_OTHER);
  129.         return new JsonResponse(['success' => true]);
  130.     }
  131.     #[Route('/show'name'app_cart_webhosting_show'methods: ['GET''POST'])]
  132.     public function show(Request $requestCartWebhostingRepository $cartWebhostingRepositoryCartWebhostingService $cartWebhostingServiceSettingRepository $settingRepository): Response
  133.     {
  134.         $domainData         = [];
  135.         $cartWebhostingData = [];
  136.         $cartWebhosting     = [];
  137.         $id                 $request->query->get('id');
  138.         $sessionId          $this->sessionId;
  139.         $settings           $settingRepository->findSettings();
  140.         if ($id && $sessionId) {
  141.             $cartWebhosting $cartWebhostingRepository->find($id);
  142.         }
  143.         if ($sessionId) {
  144.             $cartWebhosting $cartWebhostingRepository->findOneBy(['session_id' => $sessionId]);
  145.         }
  146.         if ($this->getUser()) {
  147.             $cartWebhosting $cartWebhostingRepository->findOneBy(['client' => $this->getUser()]);
  148.         }
  149.         if ($cartWebhosting) {
  150.             $cartWebhostingData    $cartWebhosting;
  151.             $cartWebhostingDomains $cartWebhosting->getCartDomains();
  152.             $productWebhosting     $cartWebhosting->getProductWebhosting() ?? null;
  153.             $domainData            $cartWebhostingService->calcDomainPrices($cartWebhostingDomains$productWebhosting);
  154.         }
  155.         //dd($domainData);
  156.         $this->session->set('oldSessionId'$this->sessionId);
  157.         return $this->render('cart_webhosting/show.html.twig', [
  158.             'cart_product_webhosting' => $cartWebhostingData,
  159.             'cart_domains_webhosting' => $domainData,
  160.             'vat_value'               => $settings->getVat(),
  161.         ]);
  162.     }
  163.     #[Route('/remove/domain'name'app_cart_webhosting_remove_domain'methods: ['GET'])]
  164.     public function removeDomain(Request $requestCartDomainRepository $cartDomainRepositoryCartWebhostingRepository $cartWebhostingRepositoryTranslatorInterface $translator): JsonResponse
  165.     {
  166.         $id             $request->query->get('id');
  167.         $sessionId      $this->sessionId;
  168.         $amountDomains  0;
  169.         $domainToDelete null;
  170.         if ($sessionId) {
  171.             $domains       $cartDomainRepository->findBy(['session_id' => $sessionId]);
  172.             $amountDomains count($domains);
  173.         }
  174.         if ($amountDomains 1) {
  175.             $domainToDelete $cartDomainRepository->find($id);
  176.             $cartDomainRepository->remove($domainToDeletetrue);
  177.         }
  178.         if ($amountDomains == 1) {
  179.             $domainToDelete $cartDomainRepository->find($id);
  180.             $cartDomainRepository->remove($domainToDeletetrue);
  181.             $webhostingToDelete $cartWebhostingRepository->find($domainToDelete->getCartWebhosting());
  182.             $cartWebhostingRepository->remove($webhostingToDeletetrue);
  183.         }
  184.         if ($domainToDelete) {
  185.             $this->addFlash('success'$domainToDelete->getDomainName() . '.' $domainToDelete->getDomainTld() . ' ' $translator->trans('lang.removed'));
  186.         }
  187.         return new JsonResponse(['success' => true]);
  188.     }
  189.     #[Route('/remove/webhosting'name'app_cart_webhosting_remove_webhosting'methods: ['GET'])]
  190.     public function removeWebhostingAndDomains(
  191.         Request $request,
  192.         CartDomainRepository $cartDomainRepository,
  193.         CartWebhostingRepository $cartWebhostingRepository,
  194.         TranslatorInterface $translator
  195.     ): JsonResponse {
  196.         $sessionId $this->sessionId;
  197.         if ($sessionId) {
  198.             $domains    $cartDomainRepository->findBy(['session_id' => $sessionId]);
  199.             $webhosting $cartWebhostingRepository->findOneBy(['session_id' => $sessionId]);
  200.             foreach ($domains as $domain) {
  201.                 $cartDomainRepository->remove($domaintrue);
  202.             }
  203.             $cartWebhostingRepository->remove($webhostingtrue);
  204.         }
  205.         $this->addFlash('success'$translator->trans('lang.allRemoved'));
  206.         return new JsonResponse(['success' => true]);
  207.     }
  208. }