src/Service/SiteConfig.php line 73

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\ContactConfig;
  4. use App\Entity\Maintenance;
  5. use App\Repository\ContactConfigRepository;
  6. use App\Repository\ContributorConfigRepository;
  7. use App\Repository\MentionLegalRepository;
  8. use App\Service\ContributorConfig;
  9. use App\Entity\SiteConfig as SiteConfigEntity;
  10. use App\Entity\SiteCustomization as SiteCustomizationEntity;
  11. use App\Entity\SocialNetwork as SocialNetworkEntity;
  12. use App\Repository\MaintenanceRepository;
  13. use App\Repository\SiteConfigRepository;
  14. use App\Repository\SiteCustomizationRepository;
  15. use App\Repository\SocialNetworkRepository;
  16. use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\RequestStack;
  19. use Vich\UploaderBundle\Templating\Helper\UploaderHelper;
  20. class SiteConfig extends ContributorConfig
  21. {
  22.    
  23.     private ?SiteConfigEntity $config;
  24.     private ?SocialNetworkEntity $socialNetwork;
  25.     private ?SiteCustomizationEntity $customization;
  26.     private ?Maintenance $maintenance;
  27.     private RequestStack $requestStack;
  28.     private ?ContactConfig $contactConfig;
  29.     private ?\App\Entity\MentionLegal $mentionLegal;
  30.     
  31.     // Add these properties to store the repositories
  32.     private SiteConfigRepository $siteConfigRepository;
  33.     private SocialNetworkRepository $socialNetworkRepository;
  34.     private SiteCustomizationRepository $siteCustomizationRepository;
  35.     public function __construct(
  36.         private UploaderHelper $vich,
  37.         SiteConfigRepository $siteConfigRepository,
  38.         SocialNetworkRepository $socialNetworkRepository,
  39.         SiteCustomizationRepository $siteCustomizationRepository,
  40.         MaintenanceRepository $maintenanceRepository,
  41.         RequestStack $requestStack,
  42.         ContributorConfigRepository $contributorConfigRepo,
  43.         ContactConfigRepository $contactConfigRepo,
  44.         private ContainerBagInterface $params,
  45.         MentionLegalRepository $mentionLegalRepository
  46.     ) {
  47.         // Store the repositories
  48.         $this->siteConfigRepository $siteConfigRepository;
  49.         $this->socialNetworkRepository $socialNetworkRepository;
  50.         $this->siteCustomizationRepository $siteCustomizationRepository;
  51.         
  52.         $this->requestStack $requestStack;
  53.         $this->maintenance $maintenanceRepository->findOneBy([]);
  54.         $this->contactConfig $contactConfigRepo->findOneBy([]);
  55.         $this->mentionLegal $mentionLegalRepository->findOneBy([]);
  56.         
  57.         // Initialize with default configs first
  58.         $this->config $siteConfigRepository->findOneBy([]);
  59.         $this->socialNetwork $socialNetworkRepository->findOneBy([]);
  60.         $this->customization $siteCustomizationRepository->findOneBy([]);
  61.         
  62.         parent::__construct($contributorConfigRepo$requestStack$params);
  63.     }
  64.     
  65.     private function getInstanceId(): ?int
  66.     {
  67.         try {
  68.             $session $this->requestStack->getSession();
  69.             return $session->get('selected_instance_id');
  70.         } catch (\RuntimeException $e) {
  71.             // Session not available yet
  72.             return null;
  73.         }
  74.     }
  75.     public function getConfigGenerale(): SiteConfigEntity 
  76.     {
  77.         $configGenerale $this->siteConfigRepository->findOneBy(['instance' => null]);
  78.         return $configGenerale;
  79.     }
  80.     public function getConfig(): SiteConfigEntity
  81.     {
  82.         $instanceId $this->getInstanceId();
  83.         if ($instanceId && (!$this->config->getInstance() || $this->config->getInstance()->getId() !== $instanceId)) {
  84.             $instanceConfig $this->siteConfigRepository->findOneBy(['instance' => $instanceId]);
  85.             if ($instanceConfig) {
  86.                 $this->config $instanceConfig;
  87.             }
  88.         }
  89.         return $this->config;
  90.     }
  91.     // public function getSocialNetwork(): SocialNetworkEntity
  92.     // {
  93.     //     $instanceId = $this->getInstanceId();
  94.     //     if ($instanceId && (!$this->socialNetwork->getInstance() || $this->socialNetwork->getInstance()->getId() !== $instanceId)) {
  95.     //         $instanceSocial = $this->socialNetworkRepository->findOneBy(['instance' => $instanceId]);
  96.     //         if ($instanceSocial) {
  97.     //             $this->socialNetwork = $instanceSocial;
  98.     //         }
  99.     //     }
  100.     //     return $this->socialNetwork;
  101.     // }
  102.     public function getCustomization(): SiteCustomizationEntity
  103.     {
  104.         $instanceId $this->getInstanceId();
  105.         if ($instanceId && (!$this->customization->getInstance() || $this->customization->getInstance()->getId() !== $instanceId)) {
  106.             $instanceCustom $this->siteCustomizationRepository->findOneBy(['instance' => $instanceId]);
  107.             if ($instanceCustom) {
  108.                 $this->customization $instanceCustom;
  109.             }
  110.         }
  111.         return $this->customization;
  112.     }
  113.     public function getEntityId(): int
  114.     {
  115.         return $this->config->getId();
  116.     }
  117.     public function getMaintenanceMode(): bool
  118.     {
  119.         return $this->getConfigGenerale()->getMaintenanceMode();
  120.     }
  121.     public function isAccountProfile(): bool
  122.     {
  123.         return $this->config->isAccountProfile();
  124.     }
  125.     public function isCockpitActive(): bool
  126.     {
  127.         return $this->getConfigGenerale()->isCockpitActive();
  128.     }
  129.     public function isHamburgerMenu(): bool
  130.     {
  131.         return $this->config->isHamburgerMenu();
  132.     }
  133.     public function isIsSearchForm(): bool
  134.     {
  135.         return $this->config->isIsSearchForm();
  136.     }
  137.     public function getEnableNewsletter(): bool
  138.     {
  139.         return $this->config->getEnableNewsletter();
  140.     }
  141.     public function getContactEmail(): string
  142.     {
  143.         return $this->config->getContactEmail();
  144.     }
  145.     public function getAdminEmail(): string
  146.     {
  147.         return $this->config->getAdminEmail();
  148.     }
  149.     public function getBlogPostLifeTimeDays(): ?string
  150.     {
  151.         return $this->config->getBlogPostLifeTimeDays();
  152.     }
  153.     public function isBlogPostLifeTime(): ?bool
  154.     {
  155.         return $this->config->isBlogPostLifeTime();
  156.     }
  157.     public function isPublicFrontOffice(): ?bool
  158.     {
  159.         return $this->getConfigGenerale()->isPublicFrontOffice();
  160.     }
  161.     private function getPhoneNumber(): array
  162.     {
  163.         $maybePhone $this->config->getPhoneNumber();
  164.         if (empty($maybePhone)) {
  165.             return [''''];
  166.         }
  167.         while (count($maybePhone) < 2) {
  168.             $maybePhone[] = '';
  169.         }
  170.         return $maybePhone;
  171.     }
  172.     private function getPostalAddress(): array
  173.     {
  174.         $maybeAddress $this->config->getPostalAddress();
  175.         if (empty($maybeAddress)) {
  176.             return [''''];
  177.         }
  178.         while (count($maybeAddress) < 2) {
  179.             $maybeAddress[] = '';
  180.         }
  181.         return $maybeAddress;
  182.     }
  183.     public function getContactInfo(): array
  184.     {
  185.         return [
  186.             'email' => $this->config->getContactEmail(),
  187.             'postal' => $this->getPostalAddress(),
  188.             'phone' => $this->getPhoneNumber(),
  189.         ];
  190.     }
  191.     /**
  192.      * @return array{twitter: ?string, facebook: ?string, instagram: ?string}
  193.      */
  194.     public function getSocialLinks(): array
  195.     {
  196.         return [
  197.             'twitter' => $this->socialNetwork->getTwitter(),
  198.             'facebook' => $this->socialNetwork->getFacebook(),
  199.             'instagram' => $this->socialNetwork->getInstagram(),
  200.             'youtube' => $this->socialNetwork->getYoutube(),
  201.             'linkedin' => $this->socialNetwork->getLinkedin(),
  202.             'tiktok' => $this->socialNetwork->getTiktok(),
  203.             'twitch' => $this->socialNetwork->getTwitch(),
  204.         ];
  205.     }
  206.     public function getSocialNetworkSlogan(): ?string
  207.     {
  208.         return $this->socialNetwork->getSlogan();
  209.     }
  210.     public function getSocialNetworkHashtag(): ?string
  211.     {
  212.         return $this->socialNetwork->getHashtag();
  213.     }
  214.     public function getLogoUrl(): ?string
  215.     {
  216.         return $this->vich->asset($this->config'logoFile');
  217.     }
  218.     public function getFaviconUrl(): ?string
  219.     {
  220.         return $this->vich->asset($this->config'faviconFile');
  221.     }
  222.     public function getAuthor(): ?string
  223.     {
  224.         return $this->config->getMetaAuthor();
  225.     }
  226.     public function getDescription(): ?string
  227.     {
  228.         return $this->config->getMetaDescription();
  229.     }
  230.     public function getSiteName(): ?string
  231.     {
  232.         return $this->config->getSiteName();
  233.     }
  234.     public function getLocation(): array
  235.     {
  236.         return [
  237.             'lat' => $this->config->getLocLat() ?? 0,
  238.             'lon' => $this->config->getLocLon() ?? 0,
  239.         ];
  240.     }
  241.     public function getTermsOfService(?string $locale null): ?string
  242.     {
  243.         return $this->mentionLegal->getTermsOfService($locale);
  244.     }
  245.     public function getSiteCustomization(): ?SiteCustomizationEntity
  246.     {
  247.         return $this->customization;
  248.     }
  249.     public function getCopyright(): ?string
  250.     {
  251.         return $this->config->getCopyright();
  252.     }
  253.     public function getIp(): ?string
  254.     {
  255.         $request $this->requestStack->getCurrentRequest();
  256.         return $request->getClientIp();
  257.     }
  258.     public function getMaintenance()
  259.     {
  260.         return $this->maintenance;
  261.     }
  262.     public function isShared(): bool
  263.     {
  264.         return $this->socialNetwork->isShared();
  265.     }
  266.     public function isSharedTwitter(): ?bool
  267.     {
  268.         return $this->socialNetwork->getIsSharedTwitter();
  269.     }
  270.     public function isSharedFacebook(): ?bool
  271.     {
  272.         return $this->socialNetwork->getIsSharedFacebook();
  273.     }
  274.     public function isSharedInstagram(): ?bool
  275.     {
  276.         return $this->socialNetwork->getIsSharedInstagram();
  277.     }
  278.     public function isSharedYoutube(): ?bool
  279.     {
  280.         return $this->socialNetwork->getIsSharedYoutube();
  281.     }
  282.     public function isSharedLinkedin(): ?bool
  283.     {
  284.         return $this->socialNetwork->getIsSharedLinkedin();
  285.     }
  286.     public function isSharedTiktok(): ?bool
  287.     {
  288.         return $this->socialNetwork->getIsSharedTiktok();
  289.     }
  290.     public function isSharedTwich(): ?bool
  291.     {
  292.         return $this->socialNetwork->getIsSharedTwich();
  293.     }
  294.     public function isSharedSms(): ?bool
  295.     {
  296.         return $this->socialNetwork->isSharedSms();
  297.     }
  298.     public function isSharedWhatsapp(): ?bool
  299.     {
  300.         return $this->socialNetwork->isSharedWhatsapp();
  301.     }
  302.     public function isSharedEmail(): ?bool
  303.     {
  304.         return $this->socialNetwork->isSharedEmail();
  305.     }
  306.     public function isSharedMessagerie(): ?bool
  307.     {
  308.         return $this->socialNetwork->isSharedMessagerie();
  309.     }
  310.     public function getMessagerieSharingLabel(): ?string
  311.     {
  312.         return $this->socialNetwork->getMessagerieSharingLabel();
  313.     }
  314.     public function getActivedItmConnect(): ?bool
  315.     {
  316.         return $this->getConfigGenerale()->getActivedItmConnect();
  317.     }
  318.     public function getActivedNotification(): ?bool
  319.     {
  320.         return $this->getConfigGenerale()->getActivedNotification();
  321.     }
  322.     public function getActivedContribution(): ?bool
  323.     {
  324.         return $this->getConfigGenerale()->getActivedContribution();
  325.     }
  326.     public function getActivedViewPdf(): bool
  327.     {
  328.         return $this->getConfigGenerale()->getActivedViewPdf();
  329.     }
  330.     public function isContactActivated(): ?bool
  331.     {
  332.         return $this->contactConfig->getIsActivated();
  333.     }
  334.     public function getContactTemplateId(): ?Int
  335.     {
  336.         return $this->config->getContactTemplateId();
  337.     }
  338.     public function getContributionTemplateId(): ?Int
  339.     {
  340.         return $this->config->getContributionTemplateId();
  341.     }
  342.     public function getIsTrackingForced(): ?bool
  343.     {
  344.         return $this->config->getIsTrackingForced();
  345.     }
  346.     public function setisTrackingForced(?bool $isTrackingForced): self
  347.     {
  348.         $this->config->setisTrackingForced($isTrackingForced);
  349.         return $this;
  350.     }
  351.     public function isAnonymizationActive(): bool
  352.     {
  353.         $config $this->siteConfigRepository->findOneBy([]);
  354.         return $config $config->isAnonymizationActive() : false;
  355.     }
  356. }