<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
class DomenyController extends AbstractController {
/**
* Zasób deaktuwyje domenę.
* @Route ("/deactivate-domain/{domainId}", name="deactivate-domain", methods={"POST"})
* @isGranted("ROLE_ADMIN")
* @param \Doctrine\ORM\EntityManagerInterface $EM
* @param int $domainId
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function deactivateDomain (\Doctrine\ORM\EntityManagerInterface $EM, int $domainId): \Symfony\Component\HttpFoundation\JsonResponse {
$domena = $EM->getRepository(\App\Entity\HRDDomain::class)->find ($domainId);
if ($domena) {
$EM->persist($domena);
$domena->setDeactivation(new \DateTime ());
$EM->flush ();
return new \Symfony\Component\HttpFoundation\JsonResponse([ "success" => true ]);
}
return new \Symfony\Component\HttpFoundation\JsonResponse([ "success" => false ]);
}
/**
* Zasób aktuwyje domenę.
* @Route ("/activate-domain/{domainId}", name="activate-domain", methods={"POST"})
* @isGranted("ROLE_ADMIN")
* @param \Doctrine\ORM\EntityManagerInterface $EM
* @param int $domainId
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function activateDomain (\Doctrine\ORM\EntityManagerInterface $EM, int $domainId): \Symfony\Component\HttpFoundation\JsonResponse {
$domena = $EM->getRepository(\App\Entity\HRDDomain::class)->find ($domainId);
if ($domena) {
$EM->persist($domena);
$domena->setDeactivation(null);
$EM->flush ();
return new \Symfony\Component\HttpFoundation\JsonResponse([ "success" => true ]);
}
return new \Symfony\Component\HttpFoundation\JsonResponse([ "success" => false ]);
}
/**
* Zasób wyświetla listę wygasajacych domen w okresie $dni dni.
* @isGranted("ROLE_ADMIN")
* @Route ("/wygasajace-domeny/{dni}", name="wygasajace-domeny", methods={"GET"})
* @param \Doctrine\ORM\EntityManagerInterface $EM
* @param int $dni
* @return Response
*/
public function wygasajaceDomeny (\Doctrine\ORM\EntityManagerInterface $EM, int $dni = 30): Response {
return $this->render ("wygasajace_domeny.html.twig", [ "dni" => $dni, "EM" => $EM, "domains" => $EM->getRepository (\App\Entity\HRDDomain::class)->findAllToExDateDays ($dni) ]);
}
/**
* Funkcja dokonuje odnowienia domeny w HRD
* @Route ("/manuallyReNewDomain", name="manually-reNew-domain", methods={"POST"})
* @isGranted("ROLE_ADMIN")
* @param \Doctrine\ORM\EntityManager $EM
* @param \Symfony\Component\BrowserKit\Request $zadanie
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function manuallyReNewDomain (\Doctrine\ORM\EntityManagerInterface $EM, \Symfony\Component\HttpFoundation\Request $zadanie, \App\Service\TPAYLogger $logger): \Symfony\Component\HttpFoundation\JsonResponse {
$domena = new \App\Entity\HRDDomain();
$domena = $EM->getRepository(\App\Entity\HRDDomain::class)->find (intval ($zadanie->get ("id")));
if ($domena) {
$HRD = \HRDBase\Api\HRDApi::getInstance([
"apiHash" => $EM->getRepository(\App\Entity\Conf::class)->peek("hash"),
"apiLogin" => $EM->getRepository(\App\Entity\Conf::class)->peek("login"),
"apiPass" => $EM->getRepository(\App\Entity\conf::class)->peek("pass")
]);
$HRDTOKEN = $HRD->getToken();
if ($HRDTOKEN) {
if ($domena->reNew($EM, $HRD, 1, $logger)) {
return new \Symfony\Component\HttpFoundation\JsonResponse([ "status" => true ]);
} else {
$logger->log("Nie udało się odnbowić domeny " . $domena->getName());
return new \Symfony\Component\HttpFoundation\JsonResponse([ "status" => false ]);
}
} else {
$logger->log("Nie udało się zalogować do HRD.");
return new \Symfony\Component\HttpFoundation\JsonResponse([ "status" => false ]);
}
} else {
return new \Symfony\Component\HttpFoundation\JsonResponse([ "status" => false ]);
}
}
/**
* Funkcja tworzy nowy dokument na podstawie listy domen, usuwa domeny z ich bieżących proform na odnowienie i ewentualnie puste proformy
* @Route ("/manuallyReNewDomains-createNewDocument", name="manually-reNew-domains-create-doc", methods={"POST"})
* @isGranted("ROLE_ADMIN")
* @param \Doctrine\ORM\EntityManagerInterface $EM
* @param \Symfony\Component\HttpFoundation\Request $zadanie
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function manuallyReNewDomainsCreateDoc (\Doctrine\ORM\EntityManagerInterface $EM, \Symfony\Component\HttpFoundation\Request $zadanie): \Symfony\Component\HttpFoundation\JsonResponse {
$domainsList = [];
foreach ($zadanie->get ("domains") as $domainId) {
$domainsList [] = $EM->getRepository(\App\Entity\HRDDomain::class)->find (intval ($domainId));
}
$domain = new \App\Entity\HRDDomain();
foreach ($domainsList as $domain) {
$currentDoc = $domain->getCurrentDocument($EM);
if ($currentDoc != null) {
if ($currentDoc->getPozycje()->count() == 1) {
$EM->remove($currentDoc);
$EM->flush ();
} else {
$pozycja = new \App\Entity\PozycjaDokumentu();
$out = false;
foreach ($currentDoc->getPozycje() as $pozycja) {
if (($pozycja->getRodzaj() == \App\Entity\PozycjaDokumentu::prOdnowienieDomenyDwaLata) || ($pozycja->getRodzaj() == \App\Entity\PozycjaDokumentu::prOdnowienieDomenyRok) || ($pozycja->getRodzaj() == \App\Entity\PozycjaDokumentu::prOdnowienieDomenyTrzyLata)) {
if ($pozycja->getDomena() == $domain->getName()) {
$EM->persist($currentDoc);
$currentDoc->setNetto($currentDoc->getNetto() - $pozycja->getWartoscnetto());
$currentDoc->setVat($currentDoc->getVat() - $pozycja->getWartoscvat());
$currentDoc->setBrutto($currentDoc->getBrutto() - $pozycja->getWartoscbrutto());
$currentDoc->removePozycje($pozycja);
$EM->flush ();
$out = true;
break;
}
}
if ($out) break;
}
}
}
}
//$dokument = new \App\Entity\Dokument();
$dokument = $EM->getRepository(\App\Entity\Dokument::class)->makeDocumentFromDomainsListForReNew ($domainsList [0]->getHRDUser ()->getUser (), intval ($zadanie->get ("documentType")), $domainsList);
if ($zadanie->get ("payd") == "true") {
$dokument->setDozaplaty(0);
$dokument->setStatus(\App\Entity\Dokument::dsZaplacony);
$EM->persist($dokument);
$EM->flush();
}
return new \Symfony\Component\HttpFoundation\JsonResponse($_POST);
}
/**
* Zasób wyświetla i ustawia konfigurator wyszukiwarki domen
* @Route ("/domainsConfig", name="domains-config")
* @isGranted("ROLE_ADMIN")
* @param \Symfony\Component\HttpFoundation\Request $zadanie
* @param \Doctrine\ORM\EntityManagerInterface $EM
* @return Response
*/
public function domainsSearchConfig (\Symfony\Component\HttpFoundation\Request $zadanie, \Doctrine\ORM\EntityManagerInterface $EM): Response {
if ($zadanie->getMethod() == "POST") {
foreach ($zadanie->get("domeny") as $domena) {
$KD = $EM->getRepository (\App\Entity\KoncowkaDomeny::class)->find ($domena ['id']);
if ($KD) {
$EM->persist ($KD);
$KD->setAktywna($domena ['active'] == "true");
$KD->setGrupa($domena ['group']);
$KD->setPierwsze($domena ['first'] == "true");
$EM->flush ();
}
}
return new Response ("OK");
}
$KD = new \App\Entity\KoncowkaDomeny();
$koncowkiDomen = $EM->getRepository(\App\Entity\KoncowkaDomeny::class)->findAll ();
$groups = [];
foreach ($koncowkiDomen as $KD) {
$index = -1;
foreach ($groups as $N => $G) {
if ($G ['grupa'] == $KD->getGrupa()) {
$index = $N;
break;
}
}
if ($index == -1) {
$NI = [ "grupa" => $KD->getGrupa(), "pierwsze" => [], "drugie" => [], "priorytet" => $KD->getPriorytet() ];
if ($KD->getPierwsze()) {
$NI ['pierwsze'][] = $KD;
} else {
$NI ['drugie'][] = $KD;
}
$groups [] = $NI;
} else {
if ($KD->getPierwsze()) {
$groups [$index]['pierwsze'][] = $KD;
} else {
$groups [$index]['drugie'][] = $KD;
}
}
}
usort ($groups, function ($a, $b) {
if ($a ['priorytet'] == $b ['priorytet']) {
return 0;
} elseif ($a ['priorytet'] < $b ['priorytet']) {
return -1;
} else {
return 1;
}
});
return $this->render("domain-search-config.html.twig", [
"groups" => $groups,
"colwidth" => ((count ($groups) == 0) ? 0 : intval (12 / count ($groups)))
]);
}
/**
* Zasób wyświetla listę domen klienta
* @Route("/mojeDomeny", name="my-domains")
* @IsGranted("ROLE_USER");
* @return Response
*/
public function myDomainsList (): Response {
$domains = [];
foreach ($this->getUser()->getCSA () as $CSA) {
foreach ($CSA->getHRDDomains() as $domain) {
$domains [] = $domain;
}
}
usort ($domains, function ($a, $b) {
if ($a->getExDate()->getTimestamp() < $b->getExDate()->getTimestamp()) {
return -1;
} elseif ($a->getExDate()->getTimestamp() > $b->getExDate()->getTimestamp()) {
return 1;
} else {
return 0;
}
});
return $this->render ("domains-list.html.twig", [
"domains" => $domains
]);
}
/**
* @Route("/domeny", name="domain-search")
* @param \Symfony\Component\HttpFoundation\Request $zadanie
* @param \Doctrine\ORM\EntityManagerInterface $em
* @return Response
*/
public function index(\Symfony\Component\HttpFoundation\Request $zadanie, \Doctrine\ORM\EntityManagerInterface $em): Response {
if ($this->getUser ()) {
if (count ($this->getUser ()->getCsa ()) == 0) {
return $this->render ("wyszukiwarka-domen-brak-csa.html.twig");
}
}
$domainName = $zadanie->get ("domena");
if ($domainName == "") {
return $this->render('wyszukiwarka-domen.html.twig', [
'domena' => $domainName,
'domeny' => [],
'message' => "Proszę podać poszukiwana nazwę domeny."
]);
} else {
$baseDomain = $em->getRepository(\App\Entity\KoncowkaDomeny::class)->findBaseDomain($domainName);
if ($baseDomain) {
//Jest zapytanie o konkretna domenę
$subDomain = substr($domainName, 0, strlen($domainName) - strlen($baseDomain->getNazwa()));
} else {
//Jest zapytanie ogólne
$subDomain = $domainName;
}
if ($subDomain [strlen($subDomain) - 1] == ".")
$subDomain = substr($subDomain, 0, strlen($subDomain) - 1);
if (strpos($subDomain, ".") !== false) {
return $this->render('wyszukiwarka-domen.html.twig', [
'domena' => $domainName,
'domeny' => [],
'message' => "Nazwa właściwa domeny nie może zawierać znaku \".\"."
]);
}
$domeny = $em->getRepository(\App\Entity\KoncowkaDomeny::class)->getInGroups();
$vat = floatval ($em->getRepository(\App\Entity\Conf::class)->peek ("domains_vat")) / 100 + 1;
foreach ($domeny as &$d) {
foreach ($d ['pierwsze'] as &$p) {
$p = [ "nazwa" => $subDomain . "." . $p->getNazwa (), "netto" => ($p->getCennik () ? $p->getCennik()->getRejestracja() : 0), "brutto" => ($p->getCennik () ? round ($p->getCennik()->getRejestracja() * $vat, 2) : 0) ];
}
foreach ($d ['drugie'] as &$p) {
$p = [ "nazwa" => $subDomain . "." . $p->getNazwa (), "netto" => ($p->getCennik () ? $p->getCennik()->getRejestracja() : 0), "brutto" => ($p->getCennik () ? round ($p->getCennik()->getRejestracja() * $vat, 2) : 0) ];
}
}
if ($baseDomain) {
return $this->render('wyszukiwarka-domen.html.twig', [
'poszukiwane' => $baseDomain->getNazwa (),
'domena' => $zadanie->get("domena"),
'domeny' => $domeny
]);
} else {
return $this->render('wyszukiwarka-domen.html.twig', [
'domena' => $zadanie->get("domena"),
'domeny' => $domeny
]);
}
}
}
/**
* Funkcja sprawdza, czy podane domeny mozna zarejestrować
* @Route("/domains-check", name="domains-check-portions", methods={"POST"})
* @param \Doctrine\ORM\EntityManagerInterface $em
* @param \Symfony\Component\HttpFoundation\Request $zadanie
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function checkPortionDomains (\Doctrine\ORM\EntityManagerInterface $em, \Symfony\Component\HttpFoundation\Request $zadanie): \Symfony\Component\HttpFoundation\JsonResponse {
$koszyk = null;
if ($this->getUser ()) {
$koszyk = $this->getUser ()->getKoszyk ();
} else {
if ($zadanie->getSession()->isStarted()) {
$koszyk = $zadanie->getSession()->get ("koszyk");
}
}
$wynik = ['status' => false, 'domains' => $zadanie->get ("domains") ];
$HRD = \HRDBase\Api\HRDApi::getInstance([
"apiHash" => $em->getRepository(\App\Entity\Conf::class)->peek("hash"),
"apiLogin" => $em->getRepository(\App\Entity\Conf::class)->peek("login"),
"apiPass" => $em->getRepository(\App\Entity\conf::class)->peek("pass")
]);
$HRDTOKEN = $HRD->getToken();
if ($HRDTOKEN) {
$lista = [];
foreach ($wynik ['domains'] as $domain) {
$lista [] = $domain ['nazwa'];
}
$wynik ['status'] = true;
try {
$CHECK = $HRD->domainCheck ($lista);
} catch (\Exception $e) {
$wynik ['status'] = false;
$wynik ['message'] = $e->getMessage ();
}
if ($wynik ['status']) {
foreach ($CHECK as $domainName => $domainStatus) {
foreach ($wynik ['domains'] as &$domain) {
if ($domain ['nazwa'] == $domainName) {
$domain ['status'] = $domainStatus;
if ($koszyk) {
$domain ['cart'] = $koszyk->isIn ($domainName);
} else {
$domain ['cart'] = false;
}
break;
}
}
}
}
} else {
$wynik ['message'] = "Nie udało się zalogować do HRD.";
}
return new \Symfony\Component\HttpFoundation\JsonResponse($wynik);
}
}