<?php
namespace App\Controller;
use App\Entity\CartDomain;
use App\Entity\CartWebhosting;
use App\Repository\CartDomainRepository;
use App\Repository\CartWebhostingRepository;
use App\Repository\ProductWebhostingRepository;
use App\Repository\SettingRepository;
use App\Service\CartWebhostingService;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
#[Route('/cart-webhosting')]
class CartWebhostingController extends AbstractController
{
private Session $session;
private string $sessionId;
public function __construct(private RequestStack $request)
{
if (!$this->request->getSession()) {
$session = new Session();
$this->session = $session;
$this->sessionId = $session->getId();
} else {
$this->session = $this->request->getSession();
$this->sessionId = $this->request->getSession()->getId();
}
}
#[Route('/', name: 'app_cart_webhosting', methods: ['GET'])]
public function index(): Response
{
return $this->render('cart_webhosting/index.html.twig', [
'controller_name' => 'CartWebhostingController',
]);
}
#[Route('/new', name: 'app_cart_webhosting_new', methods: ['GET', 'POST'])]
public function new(Request $request, CartWebhostingRepository $cartWebhostingRepository, CartDomainRepository $cartDomainRepository, ProductWebhostingRepository $productWebhostingRepository, CartWebhostingService $cartWebhostingService): Response
{
$domain = $request->query->get('domain');
$domainNameComplete = urldecode($domain);
if ($this->getUser()) {
$user = $this->getUser();
}
$domainParts = explode('.', $domainNameComplete);
$domainName = $domainParts[0];
$domainTld = $domainParts[1];
$domainExist = $cartDomainRepository->findOneBy(['domain_name' => $domainName, 'domain_tld' => $domainTld]);
$cartWebhostingExist = $cartWebhostingRepository->findOneBy(['session_id' => $this->sessionId]);
$action = 'app_cart_webhosting_show';
if ($domainExist) {
$cartWebhostingExist = $cartWebhostingRepository->find($domainExist->getCartWebhosting()->getId());
$action = 'app_cart_webhosting_packages, {"id": ' . $cartWebhostingExist->getId() . '}';
}
if (!$cartWebhostingExist) {
$cartWebhosting = new CartWebhosting();
$cartWebhosting->setSessionId($this->sessionId);
$cartWebhosting->setClient($user ?? null);
$cartWebhosting->setMonthlyPrice(0);
$cartWebhosting->setRegDatetime(new DateTime());
$cartWebhostingRepository->add($cartWebhosting, true);
$cartWebhostingExist = $cartWebhosting;
}
if (!$cartWebhostingExist->getProductWebhosting()) {
$action = 'app_cart_webhosting_packages';
}
if (!$domainExist) {
$cartDomain = new CartDomain();
$cartDomain->setCartWebhosting($cartWebhostingExist);
$cartDomain->setSessionId($this->sessionId);
$cartDomain->setDomainName($domainName);
$cartDomain->setDomainTld($domainTld);
$cartDomain->setRegDatetime(new DateTime());
$cartDomain->setDomainPrice(0);
$cartDomainRepository->add($cartDomain, true);
}
if ($cartWebhostingExist->getProductWebhosting()) {
$cartWebhostingService->updateDomainPrices($cartWebhostingExist->getCartDomains(), $cartWebhostingExist->getProductWebhosting());
}
return $this->render('_partials/continue.html.twig', [
'action' => $action,
]);
}
#[Route('/webhosting-packages', name: 'app_cart_webhosting_packages', methods: ['GET', 'POST'])]
public function showWebhostingPackages(Request $request, CartWebhostingRepository $cartWebhostingRepository, ProductWebhostingRepository $productWebhostingRepository): Response
{
$cartWebhosting = $cartWebhostingRepository->findOneBy(['session_id' => $this->sessionId]);
if ($request->query->get('id')) {
$cartWebhosting = $cartWebhostingRepository->find($request->query->get('id'));
}
if ($this->getUser()) {
$cartWebhosting = $cartWebhostingRepository->findOneBy(['client' => $this->getUser()]);
}
$domainsInCart = $cartWebhosting->getCartDomains();
return $this->render('site/webhosting.html.twig', [
'cart_webhosting_id' => $cartWebhosting->getId(),
'product_webhosting' => $productWebhostingRepository->findBy(['is_public' => true, 'is_active' => true]),
'domains_in_cart' => $domainsInCart,
]);
}
#[Route('/edit', name: 'app_cart_webhosting_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, CartWebhostingRepository $cartWebhostingRepository, ProductWebhostingRepository $productWebhostingRepository, CartWebhostingService $cartWebhostingService): JsonResponse
{
$cartWebhostingId = $request->query->get('cart_id');
$webhostingId = $request->query->get('id');
$cartWebhosting = $cartWebhostingRepository->find($cartWebhostingId);
$productWebhosting = $productWebhostingRepository->find($webhostingId);
if ($cartWebhosting && $productWebhosting) {
$cartWebhosting->setProductWebhosting($productWebhosting);
$cartWebhosting->setMonthlyPrice($productWebhosting->getPackagePrice());
$cartWebhosting->setRegDatetime(new DateTime());
$cartWebhostingRepository->add($cartWebhosting, true);
/**
* calculate and update domain prices
*/
if ($cartWebhosting->getCartDomains()) {
$cartWebhostingService->updateDomainPrices($cartWebhosting->getCartDomains(), $productWebhosting);
}
}
//return $this->redirectToRoute('app_cart_webhosting_show', ['id' => $cartWebhostingId], Response::HTTP_SEE_OTHER);
return new JsonResponse(['success' => true]);
}
#[Route('/show', name: 'app_cart_webhosting_show', methods: ['GET', 'POST'])]
public function show(Request $request, CartWebhostingRepository $cartWebhostingRepository, CartWebhostingService $cartWebhostingService, SettingRepository $settingRepository): Response
{
$domainData = [];
$cartWebhostingData = [];
$cartWebhosting = [];
$id = $request->query->get('id');
$sessionId = $this->sessionId;
$settings = $settingRepository->findSettings();
if ($id && $sessionId) {
$cartWebhosting = $cartWebhostingRepository->find($id);
}
if ($sessionId) {
$cartWebhosting = $cartWebhostingRepository->findOneBy(['session_id' => $sessionId]);
}
if ($this->getUser()) {
$cartWebhosting = $cartWebhostingRepository->findOneBy(['client' => $this->getUser()]);
}
if ($cartWebhosting) {
$cartWebhostingData = $cartWebhosting;
$cartWebhostingDomains = $cartWebhosting->getCartDomains();
$productWebhosting = $cartWebhosting->getProductWebhosting() ?? null;
$domainData = $cartWebhostingService->calcDomainPrices($cartWebhostingDomains, $productWebhosting);
}
//dd($domainData);
$this->session->set('oldSessionId', $this->sessionId);
return $this->render('cart_webhosting/show.html.twig', [
'cart_product_webhosting' => $cartWebhostingData,
'cart_domains_webhosting' => $domainData,
'vat_value' => $settings->getVat(),
]);
}
#[Route('/remove/domain', name: 'app_cart_webhosting_remove_domain', methods: ['GET'])]
public function removeDomain(Request $request, CartDomainRepository $cartDomainRepository, CartWebhostingRepository $cartWebhostingRepository, TranslatorInterface $translator): JsonResponse
{
$id = $request->query->get('id');
$sessionId = $this->sessionId;
$amountDomains = 0;
$domainToDelete = null;
if ($sessionId) {
$domains = $cartDomainRepository->findBy(['session_id' => $sessionId]);
$amountDomains = count($domains);
}
if ($amountDomains > 1) {
$domainToDelete = $cartDomainRepository->find($id);
$cartDomainRepository->remove($domainToDelete, true);
}
if ($amountDomains == 1) {
$domainToDelete = $cartDomainRepository->find($id);
$cartDomainRepository->remove($domainToDelete, true);
$webhostingToDelete = $cartWebhostingRepository->find($domainToDelete->getCartWebhosting());
$cartWebhostingRepository->remove($webhostingToDelete, true);
}
if ($domainToDelete) {
$this->addFlash('success', $domainToDelete->getDomainName() . '.' . $domainToDelete->getDomainTld() . ' ' . $translator->trans('lang.removed'));
}
return new JsonResponse(['success' => true]);
}
#[Route('/remove/webhosting', name: 'app_cart_webhosting_remove_webhosting', methods: ['GET'])]
public function removeWebhostingAndDomains(
Request $request,
CartDomainRepository $cartDomainRepository,
CartWebhostingRepository $cartWebhostingRepository,
TranslatorInterface $translator
): JsonResponse {
$sessionId = $this->sessionId;
if ($sessionId) {
$domains = $cartDomainRepository->findBy(['session_id' => $sessionId]);
$webhosting = $cartWebhostingRepository->findOneBy(['session_id' => $sessionId]);
foreach ($domains as $domain) {
$cartDomainRepository->remove($domain, true);
}
$cartWebhostingRepository->remove($webhosting, true);
}
$this->addFlash('success', $translator->trans('lang.allRemoved'));
return new JsonResponse(['success' => true]);
}
}