<?php
/**
* Created by Elements.at New Media Solutions GmbH
*
*/
namespace App\Twig;
use Elements\Bundle\SeoHelperBundle\Service\PrettyUrl;
use Pimcore\Http\Request\Resolver\DocumentResolver;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\Folder;
use Pimcore\Model\Document;
use Pimcore\Model\Document\Page;
use Pimcore\Translation\Translator;
use Pimcore\Twig\Extension\Templating\Navigation;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\RouterInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class NavigationHelper extends AbstractExtension
{
public function __construct(
private PrettyUrl $prettyUrl,
private Navigation $navigation,
private Translator $translator,
private RouterInterface $router,
private RequestStack $requestStack,
private DocumentResolver $documentResolver,
) {
}
public function getFunctions(): array
{
return [
new TwigFunction('metaNavigation', [$this, 'getMetaNavigation'], [ 'is_safe' => ['html'] ]),
new TwigFunction('mainNavigation', [$this, 'getMainNavigation'], [ 'is_safe' => ['html'] ]),
new TwigFunction('languageNavigation', [$this, 'getLanguageNavigation'], [ 'is_safe' => ['html'] ]),
new TwigFunction('getSiteForDocument', [$this, 'getSiteForDocument'], [ 'is_safe' => ['html'] ]),
];
}
/**
* @param Document $activeDocument
*
* @return string
*
* @throws \Exception
*/
public function getMetaNavigation(Document $activeDocument): string
{
$rootNode = $activeDocument->getProperty('languageRoot') ?? Document::getById(1);
$mainNavStartNode = $rootNode ?: Page::getById(1);
$mainNavigation = $this->navigation->build([
'active' => $activeDocument,
'root' => $mainNavStartNode,
'pageCallback' => function ($page, $document) {
$page->setCustomSetting('exclude', $document->getProperty('navigation_exclude'));
$page->setCustomSetting('resort', $document->getProperty('resort') ?: 'false');
$page->setCustomSetting('document', $document);
},
]);
$this->navigation->menu()->setTemplate('navigation/partials/navbar-meta-links.html.twig');
return $this->navigation->menu()->render($mainNavigation);
}
/**
* @param Document $activeDocument
*
* @return string
*
* @throws \Exception
*/
public function getMainNavigation(Document $activeDocument): string
{
$rootNode = $activeDocument->getProperty('navRoot') ?? Document::getById(1);
$mainNavStartNode = $rootNode ?: Page::getById(1);
$mainNavigation = $this->navigation->build([
'active' => $activeDocument,
'root' => $mainNavStartNode,
'pageCallback' => function ($page, $document) {
$page->setCustomSetting('exclude', $document->getProperty('navigation_exclude'));
$page->setCustomSetting('bigSubnav', $document->getProperty('bigSubnav'));
$page->setCustomSetting('hasBrand', $document->getProperty('hasBrand'));
$page->setCustomSetting('document', $document);
},
]);
$this->navigation->menu()->setTemplate('navigation/partials/navbar-main.html.twig');
return $this->navigation->menu()->render($mainNavigation);
}
/**
* @param Document|null $document
*
* @return mixed
*
*/
public function getLanguageNavigation(Document $document = null)
{
$request = $this->requestStack->getCurrentRequest();
if ($request == '') {
return [];
}
if ($document == null) {
$document = $this->documentResolver->getDocument();
}
$requestLanguage = $request->getLocale();
$service = new \Pimcore\Model\Document\Service;
$translations = $service->getTranslations($document);
if (empty($translations)) {
$translations[$request->getLocale()] = $document->getId();
}
$links = [ ];
try {
$route = $this->router->match($request->getPathInfo());
} catch (\Symfony\Component\Routing\Exception\ResourceNotFoundException $e) {
$route = [];
}
$routename = $route['_route'] ?? '';
$id = $request->get('id', '');
if ($id instanceof DataObject) {
$obj = $id;
$id = $id->getId();
} else {
$obj = \Pimcore\Model\DataObject::getById($id);
}
foreach (\Pimcore\Tool::getValidLanguages() as $language) {
if (isset($translations[$language])) {
$target = '/' . $language;
$localizedDocument = \Pimcore\Model\Document::getById($translations[$language]);
if ($localizedDocument) {
$target = $localizedDocument->getFullPath();
if (!$localizedDocument->isPublished()) {
continue;
}
}
if ($routename != '' && $obj != '' && $id != '') {
if (!$obj instanceof Folder && $obj->getClass()->getLinkGenerator() != '') {
$target = $obj->getClass()->getLinkGenerator()->generate($obj, ['language' => $language, 'document' => $localizedDocument ?: $document, 'toLanguage' => $language]);
} elseif (method_exists($obj, 'getName')) {
$n = $obj->getName('en');
if ($obj->getName($language) != '') {
$n = $obj->getName($language);
}
if ($n != '') {
$linkConfig = [
'path' => $target,
'name' => $n,
'id' => $obj->getId(),
];
$target = $this->prettyUrl->prettyUrl($linkConfig, $routename, false, true, $language);
}
} elseif (method_exists($obj, 'getTitle')) {
$n = $obj->getTitle('en');
if ($obj->getTitle($language) != '') {
$n = $obj->getTitle($language);
}
if ($n != '') {
$linkConfig = [
'path' => $target,
'name' => $n,
'id' => $obj->getId(),
];
$target = $this->prettyUrl->prettyUrl($linkConfig, $routename, false, true, $language);
}
} else {
$target = '';
}
}
$target = preg_replace("/^\/(.*)\/\//", '/', $target);
if ($target != '') {
$links[$target]['text'] = $this->translator->trans('global.language.' . $language, [], null, $requestLanguage);
$links[$target]['lang'] = $language;
}
}
}
return $links;
}
public function getSiteForDocument($document): ?string
{
return \Pimcore\Tool\Frontend::getSiteForDocument($document)?->getMainDomain();
}
}