<?php
namespace App\Service;
use App\Entity\ContactConfig;
use App\Entity\Maintenance;
use App\Repository\ContactConfigRepository;
use App\Repository\ContributorConfigRepository;
use App\Repository\MentionLegalRepository;
use App\Service\ContributorConfig;
use App\Entity\SiteConfig as SiteConfigEntity;
use App\Entity\SiteCustomization as SiteCustomizationEntity;
use App\Entity\SocialNetwork as SocialNetworkEntity;
use App\Repository\MaintenanceRepository;
use App\Repository\SiteConfigRepository;
use App\Repository\SiteCustomizationRepository;
use App\Repository\SocialNetworkRepository;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Vich\UploaderBundle\Templating\Helper\UploaderHelper;
class SiteConfig extends ContributorConfig
{
private ?SiteConfigEntity $config;
private ?SocialNetworkEntity $socialNetwork;
private ?SiteCustomizationEntity $customization;
private ?Maintenance $maintenance;
private RequestStack $requestStack;
private ?ContactConfig $contactConfig;
private ?\App\Entity\MentionLegal $mentionLegal;
// Add these properties to store the repositories
private SiteConfigRepository $siteConfigRepository;
private SocialNetworkRepository $socialNetworkRepository;
private SiteCustomizationRepository $siteCustomizationRepository;
public function __construct(
private UploaderHelper $vich,
SiteConfigRepository $siteConfigRepository,
SocialNetworkRepository $socialNetworkRepository,
SiteCustomizationRepository $siteCustomizationRepository,
MaintenanceRepository $maintenanceRepository,
RequestStack $requestStack,
ContributorConfigRepository $contributorConfigRepo,
ContactConfigRepository $contactConfigRepo,
private ContainerBagInterface $params,
MentionLegalRepository $mentionLegalRepository
) {
// Store the repositories
$this->siteConfigRepository = $siteConfigRepository;
$this->socialNetworkRepository = $socialNetworkRepository;
$this->siteCustomizationRepository = $siteCustomizationRepository;
$this->requestStack = $requestStack;
$this->maintenance = $maintenanceRepository->findOneBy([]);
$this->contactConfig = $contactConfigRepo->findOneBy([]);
$this->mentionLegal = $mentionLegalRepository->findOneBy([]);
// Initialize with default configs first
$this->config = $siteConfigRepository->findOneBy([]);
$this->socialNetwork = $socialNetworkRepository->findOneBy([]);
$this->customization = $siteCustomizationRepository->findOneBy([]);
parent::__construct($contributorConfigRepo, $requestStack, $params);
}
private function getInstanceId(): ?int
{
try {
$session = $this->requestStack->getSession();
return $session->get('selected_instance_id');
} catch (\RuntimeException $e) {
// Session not available yet
return null;
}
}
public function getConfigGenerale(): SiteConfigEntity
{
$configGenerale = $this->siteConfigRepository->findOneBy(['instance' => null]);
return $configGenerale;
}
public function getConfig(): SiteConfigEntity
{
$instanceId = $this->getInstanceId();
if ($instanceId && (!$this->config->getInstance() || $this->config->getInstance()->getId() !== $instanceId)) {
$instanceConfig = $this->siteConfigRepository->findOneBy(['instance' => $instanceId]);
if ($instanceConfig) {
$this->config = $instanceConfig;
}
}
return $this->config;
}
// public function getSocialNetwork(): SocialNetworkEntity
// {
// $instanceId = $this->getInstanceId();
// if ($instanceId && (!$this->socialNetwork->getInstance() || $this->socialNetwork->getInstance()->getId() !== $instanceId)) {
// $instanceSocial = $this->socialNetworkRepository->findOneBy(['instance' => $instanceId]);
// if ($instanceSocial) {
// $this->socialNetwork = $instanceSocial;
// }
// }
// return $this->socialNetwork;
// }
public function getCustomization(): SiteCustomizationEntity
{
$instanceId = $this->getInstanceId();
if ($instanceId && (!$this->customization->getInstance() || $this->customization->getInstance()->getId() !== $instanceId)) {
$instanceCustom = $this->siteCustomizationRepository->findOneBy(['instance' => $instanceId]);
if ($instanceCustom) {
$this->customization = $instanceCustom;
}
}
return $this->customization;
}
public function getEntityId(): int
{
return $this->config->getId();
}
public function getMaintenanceMode(): bool
{
return $this->getConfigGenerale()->getMaintenanceMode();
}
public function isAccountProfile(): bool
{
return $this->config->isAccountProfile();
}
public function isCockpitActive(): bool
{
return $this->getConfigGenerale()->isCockpitActive();
}
public function isHamburgerMenu(): bool
{
return $this->config->isHamburgerMenu();
}
public function isIsSearchForm(): bool
{
return $this->config->isIsSearchForm();
}
public function getEnableNewsletter(): bool
{
return $this->config->getEnableNewsletter();
}
public function getContactEmail(): string
{
return $this->config->getContactEmail();
}
public function getAdminEmail(): string
{
return $this->config->getAdminEmail();
}
public function getBlogPostLifeTimeDays(): ?string
{
return $this->config->getBlogPostLifeTimeDays();
}
public function isBlogPostLifeTime(): ?bool
{
return $this->config->isBlogPostLifeTime();
}
public function isPublicFrontOffice(): ?bool
{
return $this->getConfigGenerale()->isPublicFrontOffice();
}
private function getPhoneNumber(): array
{
$maybePhone = $this->config->getPhoneNumber();
if (empty($maybePhone)) {
return ['', ''];
}
while (count($maybePhone) < 2) {
$maybePhone[] = '';
}
return $maybePhone;
}
private function getPostalAddress(): array
{
$maybeAddress = $this->config->getPostalAddress();
if (empty($maybeAddress)) {
return ['', ''];
}
while (count($maybeAddress) < 2) {
$maybeAddress[] = '';
}
return $maybeAddress;
}
public function getContactInfo(): array
{
return [
'email' => $this->config->getContactEmail(),
'postal' => $this->getPostalAddress(),
'phone' => $this->getPhoneNumber(),
];
}
/**
* @return array{twitter: ?string, facebook: ?string, instagram: ?string}
*/
public function getSocialLinks(): array
{
return [
'twitter' => $this->socialNetwork->getTwitter(),
'facebook' => $this->socialNetwork->getFacebook(),
'instagram' => $this->socialNetwork->getInstagram(),
'youtube' => $this->socialNetwork->getYoutube(),
'linkedin' => $this->socialNetwork->getLinkedin(),
'tiktok' => $this->socialNetwork->getTiktok(),
'twitch' => $this->socialNetwork->getTwitch(),
];
}
public function getSocialNetworkSlogan(): ?string
{
return $this->socialNetwork->getSlogan();
}
public function getSocialNetworkHashtag(): ?string
{
return $this->socialNetwork->getHashtag();
}
public function getLogoUrl(): ?string
{
return $this->vich->asset($this->config, 'logoFile');
}
public function getFaviconUrl(): ?string
{
return $this->vich->asset($this->config, 'faviconFile');
}
public function getAuthor(): ?string
{
return $this->config->getMetaAuthor();
}
public function getDescription(): ?string
{
return $this->config->getMetaDescription();
}
public function getSiteName(): ?string
{
return $this->config->getSiteName();
}
public function getLocation(): array
{
return [
'lat' => $this->config->getLocLat() ?? 0,
'lon' => $this->config->getLocLon() ?? 0,
];
}
public function getTermsOfService(?string $locale = null): ?string
{
return $this->mentionLegal->getTermsOfService($locale);
}
public function getSiteCustomization(): ?SiteCustomizationEntity
{
return $this->customization;
}
public function getCopyright(): ?string
{
return $this->config->getCopyright();
}
public function getIp(): ?string
{
$request = $this->requestStack->getCurrentRequest();
return $request->getClientIp();
}
public function getMaintenance()
{
return $this->maintenance;
}
public function isShared(): bool
{
return $this->socialNetwork->isShared();
}
public function isSharedTwitter(): ?bool
{
return $this->socialNetwork->getIsSharedTwitter();
}
public function isSharedFacebook(): ?bool
{
return $this->socialNetwork->getIsSharedFacebook();
}
public function isSharedInstagram(): ?bool
{
return $this->socialNetwork->getIsSharedInstagram();
}
public function isSharedYoutube(): ?bool
{
return $this->socialNetwork->getIsSharedYoutube();
}
public function isSharedLinkedin(): ?bool
{
return $this->socialNetwork->getIsSharedLinkedin();
}
public function isSharedTiktok(): ?bool
{
return $this->socialNetwork->getIsSharedTiktok();
}
public function isSharedTwich(): ?bool
{
return $this->socialNetwork->getIsSharedTwich();
}
public function isSharedSms(): ?bool
{
return $this->socialNetwork->isSharedSms();
}
public function isSharedWhatsapp(): ?bool
{
return $this->socialNetwork->isSharedWhatsapp();
}
public function isSharedEmail(): ?bool
{
return $this->socialNetwork->isSharedEmail();
}
public function isSharedMessagerie(): ?bool
{
return $this->socialNetwork->isSharedMessagerie();
}
public function getMessagerieSharingLabel(): ?string
{
return $this->socialNetwork->getMessagerieSharingLabel();
}
public function getActivedItmConnect(): ?bool
{
return $this->getConfigGenerale()->getActivedItmConnect();
}
public function getActivedNotification(): ?bool
{
return $this->getConfigGenerale()->getActivedNotification();
}
public function getActivedContribution(): ?bool
{
return $this->getConfigGenerale()->getActivedContribution();
}
public function getActivedViewPdf(): bool
{
return $this->getConfigGenerale()->getActivedViewPdf();
}
public function isContactActivated(): ?bool
{
return $this->contactConfig->getIsActivated();
}
public function getContactTemplateId(): ?Int
{
return $this->config->getContactTemplateId();
}
public function getContributionTemplateId(): ?Int
{
return $this->config->getContributionTemplateId();
}
public function getIsTrackingForced(): ?bool
{
return $this->config->getIsTrackingForced();
}
public function setisTrackingForced(?bool $isTrackingForced): self
{
$this->config->setisTrackingForced($isTrackingForced);
return $this;
}
public function isAnonymizationActive(): bool
{
$config = $this->siteConfigRepository->findOneBy([]);
return $config ? $config->isAnonymizationActive() : false;
}
}