<?php
namespace App\Service;
use App\Entity\Advertiser;
use App\Repository\AdvertiserRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Yaml\Yaml;
class AdvertisingManagement
{
private AdvertiserRepository $advertiserRepository;
private EntityManagerInterface $entityManager;
private RequestStack $requestStack;
private ?Advertiser $currentAdvertiser = null;
private ?Advertiser $defaultAdvertiser = null;
private string $staciSubDomain;
private string $cookieDomain;
public function __construct(AdvertiserRepository $advertiserRepository, EntityManagerInterface $entityManager, RequestStack $requestStack, string $staciSubDomain, string $cookieDomain)
{
$this->advertiserRepository = $advertiserRepository;
$this->entityManager = $entityManager;
$this->requestStack = $requestStack;
$this->staciSubDomain = $staciSubDomain;
$this->cookieDomain = $cookieDomain;
}
public function getAbsoluteUrl(string $subdomain): string
{
$defaultAdvertiser = $this->getDefaultAdvertiser();
$url = '';
if ($subdomain === $defaultAdvertiser->getSubdomain()) {
$url = preg_replace('!^.!', '', $this->cookieDomain);
} else {
$url = $subdomain . $this->cookieDomain;
}
if ($this->requestStack->getCurrentRequest() === null || empty($this->requestStack->getCurrentRequest()->server->get('REQUEST_SCHEME')) || $this->requestStack->getCurrentRequest()->server->get('HTTPS') === 'on') {
$scheme = 'https';
} else {
$scheme = $this->requestStack->getCurrentRequest()->server->get('REQUEST_SCHEME');
}
return $scheme . '://' . $url;
}
public function getSubdomain(): string
{
if ($this->requestStack->getCurrentRequest() instanceof Request && !empty($this->requestStack->getCurrentRequest()->server->get('HTTP_HOST'))) {
$host = explode('.', (string) $this->requestStack->getCurrentRequest()->server->get('HTTP_HOST'));
$subdomain = $host[0];
}
/*
elseif ($this->requestStack->getMainRequest() !== null) {
$host = explode('.', $this->requestStack->getCurrentRequest()->server->get('HTTP_HOST'));
$subdomain = $host[0];
}*/
else {
$subdomain = '';
}
return $subdomain;
}
public function getDefaultAdvertiser(): ?Advertiser
{
if (!$this->defaultAdvertiser instanceof Advertiser) {
$this->defaultAdvertiser = $this->advertiserRepository->findOneBy([
'subdomain' => $this->staciSubDomain,
]);
}
return $this->defaultAdvertiser;
}
public function getDefaultSubdomain(): ?string
{
$defaultAdvertiser = $this->getDefaultAdvertiser();
return $defaultAdvertiser->getSubdomain();
}
public function getCurrentAdvertiser(): ?Advertiser
{
$advertiser = null;
if ($this->requestStack->getMainRequest() !== null && $this->requestStack->getMainRequest()->hasSession() && null !== $this->requestStack->getMainRequest()->getSession()->get('advertiser')) {
$advertiserSession = $this->requestStack->getSession()->get('advertiser');
return $this->entityManager->getRepository(Advertiser::class)
->find($advertiserSession);
}
if (!$this->currentAdvertiser instanceof Advertiser) {
$this->currentAdvertiser = $this->advertiserRepository->findOneBy([
'subdomain' => $this->getSubdomain(),
]);
$advertiser = $this->currentAdvertiser;
} else {
$advertiser = $this->currentAdvertiser;
}
return $advertiser;
}
public function getCurrentAdvertiserByDomain(): ?Advertiser
{
return $this->advertiserRepository->findOneBy([
'subdomain' => $this->getSubdomain(),
]);
}
public function getAdvertisers(): array
{
return $this->advertiserRepository->findBy(['status' => true], ['title' => 'ASC']);
}
public function isDefaultAdvertiser(Advertiser $advertiser): bool
{
$defaultAdvertiser = $this->getDefaultAdvertiser();
if ($defaultAdvertiser->getId() === $advertiser->getId()) {
return true;
}
return false;
}
public function getUserFilterListParams(Advertiser $advertiser, Request $request): array
{
$params = [
'email' => '',
'role' => '',
'businessUnit' => '',
'region' => '',
'username' => '',
'lastname' => '',
'firstname' => '',
];
$userFilterList = $request->query->get('user_filter_list');
foreach ($params as $key => $value) {
if (\is_array($userFilterList) && null !== $userFilterList[$key] && '' !== $userFilterList[$key]) {
$params[$key] = $userFilterList[$key];
// $exportPath .= '&email='.$request->query->get('user_filter_list')['email'];
}
}
return $params;
}
public function getElfinderInstance(): string
{
$config = 'default';
$advertiser = $this->getCurrentAdvertiser();
$ref = $advertiser->getRef();
$filePath = sprintf('%s/config/packages/fm_elfinder.yaml', __DIR__ . '/../..');
$fileContent = Yaml::parseFile($filePath);
if (isset($fileContent['fm_elfinder']['instances'][$ref])) {
$config = $ref;
}
return $config;
}
/**
* @param string $type
*/
public function getCkeditorInstance($type): string
{
$configName = 'default_' . $type . '_config';
$advertiser = $this->getCurrentAdvertiser();
$refConfig = $advertiser->getRef() . '_' . $type . '_config';
$filePath = sprintf('%s/config/packages/fos_ckeditor.yaml', __DIR__ . '/../..');
$fileContent = Yaml::parseFile($filePath);
if (isset($fileContent['fos_ck_editor']['configs'][$refConfig])) {
$configName = $refConfig;
}
return $configName;
}
}