<?php
namespace App\Twig;
use App\Entity\Gos\Language;
use App\Entity\Gos\PortalSettings;
use App\Entity\Gos\Uniqskills\Cooperator;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class CompanySettingsExtension extends AbstractExtension
{
private EntityManagerInterface $em;
private ?Request $request;
public function __construct(EntityManagerInterface $em, RequestStack $requestStack)
{
$this->em = $em;
$this->request = $requestStack->getCurrentRequest();
}
public function getFunctions(): array
{
return [
new TwigFunction('getFmCompanySettings', [$this, 'getFmCompanySettings']),
new TwigFunction('getFooterCooperators', [$this, 'getFooterCooperators']),
new TwigFunction('getFooterCooperatorsHeader', [$this, 'getFooterCooperatorsHeader'])
];
}
public function getFmCompanySettings()
{
$domain = $this->request->getSession()->get('domain');
$portalSettings = $this->em->getRepository(PortalSettings::class)->find($domain);
return $portalSettings->getFmCompanySettings();
}
public function getFooterCooperators(): array
{
/** @var Language $currentLanguage */
$currentLanguage = $this->em->getRepository(Language::class)->findOneBy(['code' => $this->request->getLocale()]);
$cooperators = $this->em->getRepository(Cooperator::class)->findBy(['visibleInFooter' => true, 'notForUniqskills' => false]);
$newCooperators = [];
foreach ($cooperators as $cooperator)
{
foreach ($cooperator->getLanguages() as $language)
{
if($currentLanguage && $language)
{
if ($language->getId() == $currentLanguage->getId())
{
$newCooperators[] = $cooperator;
}
}
}
}
return $newCooperators;
}
public function getFooterCooperatorsHeader()
{
$domain = $this->request->getSession()->get('domain');
if (!$domain) return false;
$portalSettings = $this->em->getRepository(PortalSettings::class)->find($domain);
$companySettings = $portalSettings->getFmCompanySettings();
$rows = $companySettings->getTextFooterCooperatorHeaders();
if (isset($rows[$this->request->getLocale()]))
{
return $rows[$this->request->getLocale()];
}
return false;
}
}