<?php
/**
* Created by Elements.at New Media Solutions GmbH
*
*/
namespace App\Twig;
use Pimcore\Model\DataObject\Product;
use Pimcore\Model\DataObject\ProductCategory;
use Pimcore\Model\DataObject\MascusProduct;
use Pimcore\Model\DataObject\Forklift;
use Pimcore;
use Pimcore\Http\Request\Resolver\DocumentResolver;
use Pimcore\Model\DataObject\SiteConfig;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class ConfigHelper extends AbstractExtension
{
public function __construct(
private readonly DocumentResolver $documentResolver,
) {
}
public function getFunctions(): array
{
return [
new TwigFunction('siteConfig', [$this, 'getSiteConfig'], [ 'is_safe' => ['html'] ]),
new TwigFunction('pressConfig', [$this, 'getPressConfig'], [ 'is_safe' => ['html'] ]),
new TwigFunction('allCountrySites', [$this, 'getAllCountrySites'], [ 'is_safe' => ['html'] ]),
new TwigFunction('contactForProduct', [$this, 'getContactForProduct'], [ 'is_safe' => ['html'] ]),
];
}
protected ?SiteConfig $siteConfig = null;
/**
* @return SiteConfig|null
*/
public function getSiteConfig()
{
if ($this->siteConfig === null) {
$siteConfig = null;
try {
$document = $this->documentResolver->getDocument();
if ($document && ($document->getProperty('siteConfig'))) {
$siteConfig = $document->getProperty('siteConfig');
} else {
$siteConfig = Pimcore\Model\Document::getById(1)->getProperty('siteConfig');
}
} catch (\Throwable $exception) {
$siteConfig = \Pimcore\Model\DataObject\SiteConfig::getList()->current();
}
if ($siteConfig == '') {
$siteConfig = SiteConfig::getList()->current();
}
$this->siteConfig = $siteConfig;
}
return $this->siteConfig;
}
protected ?Pimcore\Model\DataObject\PresseBundleConfig $pressConfig = null;
/**
* @return false|Pimcore\Model\DataObject\PresseBundleConfig
*/
public function getPressConfig()
{
if ($this->pressConfig === null) {
$pressConfig = null;
try {
$document = $this->documentResolver->getDocument();
$pressConfig = $document?->getProperty('presse_config');
} catch (\Throwable $exception) {
p_r($exception->getFile() . ' ' . $exception->getLine() . ': ' . $exception->getMessage());
}
if ($pressConfig == '') {
$pressConfigList = new Pimcore\Model\DataObject\PresseBundleConfig\Listing();
$pressConfig = $pressConfigList->current();
}
$this->pressConfig = $pressConfig;
}
return $this->pressConfig;
}
public function getAllCountrySites(): mixed
{
$countries = [];
$siteConfigs = new SiteConfig\Listing();
$siteConfigs->addConditionParam("country IS NOT NULL");
$siteConfigs->addConditionParam("rootDocument__id IS NOT NULL");
$siteConfigs->load();
foreach ($siteConfigs as $siteConfig) {
if ($siteConfig->getRootDocument() && $siteConfig->getRootDocument()->getParent()->isPublished() && !$siteConfig->getRootDocument()->getProperty('seo_noindex')) {
$countries[$siteConfig->getCountry()] = $siteConfig->getRootDocument();
}
}
return $countries;
}
public function getContactForProduct(Product|MascusProduct|Forklift $object): mixed
{
$contactPerson = null;
$document = $this->documentResolver->getDocument();
$siteConfig = $document->getProperty('siteConfig');
$contacts = $object->getContactPersons();
if (!$contacts && !$object instanceof Forklift) {
$contacts = $object->getCategory()?->getContactPersons();
}
foreach ($contacts as $contact) {
foreach ($contact->getConfig() as $config) {
if ($config == $siteConfig) {
$contactPerson = $contact;
break 2;
}
}
}
if ($contactPerson === null && $object instanceof Forklift) {
if ($siteConfig instanceof SiteConfig) {
$contactPerson = $siteConfig->getForkliftContact();
}
}
if ($contactPerson === null && $object instanceof MascusProduct) {
$config = SiteConfig::getByCountry($object->getCountry(), 1);
if ($config->getMascusContact()) {
$contactPerson = $config->getMascusContact();
} else {
foreach (Pimcore\Tool::getValidLanguages() as $language) {
if ($config->getMascusContact($language)) {
$contactPerson = $config->getMascusContact($language);
break;
}
}
}
}
return $contactPerson;
}
}