src/Twig/ConfigHelper.php line 42

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by Elements.at New Media Solutions GmbH
  4.  *
  5.  */
  6. namespace App\Twig;
  7. use Pimcore\Model\DataObject\Product;
  8. use Pimcore\Model\DataObject\ProductCategory;
  9. use Pimcore\Model\DataObject\MascusProduct;
  10. use Pimcore\Model\DataObject\Forklift;
  11. use Pimcore;
  12. use Pimcore\Http\Request\Resolver\DocumentResolver;
  13. use Pimcore\Model\DataObject\SiteConfig;
  14. use Twig\Extension\AbstractExtension;
  15. use Twig\TwigFunction;
  16. class ConfigHelper extends AbstractExtension
  17. {
  18.     public function __construct(
  19.         private readonly DocumentResolver $documentResolver,
  20.     ) {
  21.     }
  22.     public function getFunctions(): array
  23.     {
  24.         return [
  25.             new TwigFunction('siteConfig', [$this'getSiteConfig'], [ 'is_safe' => ['html'] ]),
  26.             new TwigFunction('pressConfig', [$this'getPressConfig'], [ 'is_safe' => ['html'] ]),
  27.             new TwigFunction('allCountrySites', [$this'getAllCountrySites'], [ 'is_safe' => ['html'] ]),
  28.             new TwigFunction('contactForProduct', [$this'getContactForProduct'], [ 'is_safe' => ['html'] ]),
  29.         ];
  30.     }
  31.     protected ?SiteConfig $siteConfig null;
  32.     /**
  33.      * @return SiteConfig|null
  34.      */
  35.     public function getSiteConfig()
  36.     {
  37.         if ($this->siteConfig === null) {
  38.             $siteConfig null;
  39.             try {
  40.                 $document $this->documentResolver->getDocument();
  41.                 if ($document && ($document->getProperty('siteConfig'))) {
  42.                     $siteConfig $document->getProperty('siteConfig');
  43.                 } else {
  44.                     $siteConfig Pimcore\Model\Document::getById(1)->getProperty('siteConfig');
  45.                 }
  46.             } catch (\Throwable $exception) {
  47.                 $siteConfig \Pimcore\Model\DataObject\SiteConfig::getList()->current();
  48.             }
  49.             if ($siteConfig == '') {
  50.                 $siteConfig SiteConfig::getList()->current();
  51.             }
  52.             $this->siteConfig $siteConfig;
  53.         }
  54.         return $this->siteConfig;
  55.     }
  56.     protected ?Pimcore\Model\DataObject\PresseBundleConfig $pressConfig null;
  57.     /**
  58.      * @return false|Pimcore\Model\DataObject\PresseBundleConfig
  59.      */
  60.     public function getPressConfig()
  61.     {
  62.         if ($this->pressConfig === null) {
  63.             $pressConfig null;
  64.             try {
  65.                 $document $this->documentResolver->getDocument();
  66.                 $pressConfig $document?->getProperty('presse_config');
  67.             } catch (\Throwable $exception) {
  68.                 p_r($exception->getFile() . ' ' $exception->getLine() . ': ' $exception->getMessage());
  69.             }
  70.             if ($pressConfig == '') {
  71.                 $pressConfigList = new Pimcore\Model\DataObject\PresseBundleConfig\Listing();
  72.                 $pressConfig $pressConfigList->current();
  73.             }
  74.             $this->pressConfig $pressConfig;
  75.         }
  76.         return $this->pressConfig;
  77.     }
  78.     public function getAllCountrySites(): mixed
  79.     {
  80.         $countries = [];
  81.         $siteConfigs = new SiteConfig\Listing();
  82.         $siteConfigs->addConditionParam("country IS NOT NULL");
  83.         $siteConfigs->addConditionParam("rootDocument__id IS NOT NULL");
  84.         $siteConfigs->load();
  85.         foreach ($siteConfigs as $siteConfig) {
  86.             if ($siteConfig->getRootDocument() && $siteConfig->getRootDocument()->getParent()->isPublished() && !$siteConfig->getRootDocument()->getProperty('seo_noindex')) {
  87.                 $countries[$siteConfig->getCountry()] = $siteConfig->getRootDocument();
  88.             }
  89.         }
  90.         return $countries;
  91.     }
  92.     public function getContactForProduct(Product|MascusProduct|Forklift $object): mixed
  93.     {
  94.         $contactPerson null;
  95.         $document $this->documentResolver->getDocument();
  96.         $siteConfig $document->getProperty('siteConfig');
  97.         $contacts $object->getContactPersons();
  98.         if (!$contacts && !$object instanceof Forklift) {
  99.             $contacts $object->getCategory()?->getContactPersons();
  100.         }
  101.         foreach ($contacts as $contact) {
  102.             foreach ($contact->getConfig() as $config) {
  103.                 if ($config == $siteConfig) {
  104.                     $contactPerson $contact;
  105.                     break 2;
  106.                 }
  107.             }
  108.         }
  109.         if ($contactPerson === null && $object instanceof Forklift) {
  110.             if ($siteConfig instanceof SiteConfig) {
  111.                 $contactPerson $siteConfig->getForkliftContact();
  112.             }
  113.         }
  114.         if ($contactPerson === null && $object instanceof MascusProduct) {
  115.            $config SiteConfig::getByCountry($object->getCountry(), 1);
  116.            if ($config->getMascusContact()) {
  117.                $contactPerson $config->getMascusContact();
  118.            } else {
  119.                foreach (Pimcore\Tool::getValidLanguages() as $language) {
  120.                    if ($config->getMascusContact($language)) {
  121.                        $contactPerson $config->getMascusContact($language);
  122.                        break;
  123.                    }
  124.                }
  125.            }
  126.         }
  127.         return $contactPerson;
  128.     }
  129. }