<?php
namespace App\Twig\Extension;
use Pimcore\Model\DataObject\Sponsor;
use Pimcore\Model\Document;
use Pimcore\Twig\Extension\Templating\Navigation;
use Pimcore\Twig\Extension\Templating\Placeholder;
use Twig\TwigFunction;
class UtilsExtension extends \Twig\Extension\AbstractExtension
{
/**
* @var Navigation
*/
protected $navigationHelper;
/**
* @var Placeholder
*/
protected $placeholderHelper;
protected $breadcrumbActive = '';
public function __construct(Navigation $navigationHelper, Placeholder $placeholderHelper)
{
$this->navigationHelper = $navigationHelper;
$this->placeholderHelper = $placeholderHelper;
}
public function getFunctions()
{
return [
new TwigFunction('json_decode', [$this, 'getJsonDecode']),
new TwigFunction('md5_file', [$this, 'getMd5File']),
new TwigFunction('app_navigation_breadcrumbs', [$this, 'buildBreadcrumbs']),
new TwigFunction('renderImageClass', [$this, 'renderImageClass']),
new TwigFunction('sponsorList', [$this, 'sponsorList']),
];
}
public function sponsorList($maxItems = 5)
{
$list = new Sponsor\Listing();
$list->addConditionParam('active', true);
if ($list->count() > $maxItems) {
// 5 zufällige auswählen
$items = [];
while (count($items) < $maxItems) {
foreach ($list as $sponsor) {
if ((bool) random_int(0, 1)) {
$items[$sponsor->getId()] = $sponsor;
}
}
}
return $items;
}
return $list;
}
public function renderImageClass(string $content)
{
$content = preg_replace('/(style=".*float:right.*")/', '$1 class="imageRight"', $content);
$content = preg_replace('/(style=".*float:left.*")/', '$1 class="imageLeft"', $content);
$content = preg_replace('/(src="fileadmin)/', 'src="/fileadmin', $content);
return $content;
}
public function getJsonDecode($jsonString)
{
return json_decode($jsonString);
}
public function getMd5File($file)
{
if (file_exists(PIMCORE_WEB_ROOT . $file)) {
return md5_file(PIMCORE_WEB_ROOT . $file);
}
return time();
}
public function buildBreadcrumbs(Document $document)
{
$breadCrumbs = [];
$additionalBreadCrumbs = $this->placeholderHelper->__invoke('addBreadcrumb');
if (count($additionalBreadCrumbs->getArrayCopy())) {
$breadCrumbs = array_reverse($additionalBreadCrumbs->getArrayCopy());
foreach ($breadCrumbs as $index => $breadcrumbItem) {
$breadCrumbs[$index]['active'] = 0 === $index;
}
} else {
$breadCrumbs[] = [
'id' => $document->getId(),
'url' => $document->getRealFullPath(),
'label' => $document->hasProperty('navigation_name') && '' !== trim($document->getProperty('navigation_name')) ? $document->getProperty('navigation_name') : $document->getTittle(),
'active' => true,
];
}
$parent = $document->getParent();
while ($parent) {
$breadCrumbs[] = [
'id' => $parent->getId(),
'url' => $parent->getRealFullPath(),
'label' => $parent->hasProperty('navigation_name') && '' !== trim($parent->getProperty('navigation_name')) ? $parent->getProperty('navigation_name') : $parent->getTitle(),
'active' => false,
];
$parent = $parent->getParent();
}
$breadCrumbs = array_reverse($breadCrumbs);
return $breadCrumbs;
}
}