<?php
/**
* This file is part of the Pimcore X Installation by
* ercas GmbH & CO. KG <https://www.ercasdieagentur.de>
*
* @license GPLv3
*/
namespace App\EventListener;
use Pimcore\Http\Request\Resolver\SiteResolver;
use Pimcore\Model\Document;
use Pimcore\Model\Document\Service;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;
class FrontendMapperListener
{
protected SiteResolver $siteResolver;
protected Service $documentService;
public function __construct(SiteResolver $siteResolver)
{
$this->siteResolver = $siteResolver;
$this->documentService = new Service();
}
public function onKernelRequest(RequestEvent $event): void
{
if (is_null($event?->getRequest()?->get('routeDocument'))) {
return;
}
if ($target = $event?->getRequest()?->get('contentDocument')) {
if ($event->getRequest()->get('_pimcore_frontend_request') && $redirectPage = $target->getProperty('front_page_map')) {
$translations = $this->documentService->getTranslations($redirectPage);
$preferredRequestLanguage = $event->getRequest()->getPreferredLanguage(array_keys($translations));
if ($redirectPage->getProperty('language') !== $preferredRequestLanguage) {
$redirectPage = Document::getById($translations[$preferredRequestLanguage]);
}
if ($event->getRequest()->get('pimcore_editmode')) {
$html = '<center style="font: 400 13px/20px \'Open Sans\', \'Helvetica Neue\', helvetica, arial, verdana, sans-serif;">
<img src="/bundles/pimcoreadmin/img/flat-color-icons/link.svg" id="admin-redirect-icon">
<p>This page is a hard redirect to</p>
<strong>%s</strong>
<p>and can not be displayed!</p>
</center>';
$css = '<style>
#admin-redirect-icon {
width: 96px;
height: 96px;
padding: 20px;
border: 3px solid #f24337;
border-radius: 50%;
box-sizing: border-box;
margin-top: 128px;
animation: rotate infinite 10s;
}
@keyframes rotate {
0% { transform: rotate(0); }
90% { transform: rotate(0); }
100% { transform: rotate(360deg); }
}
</style>';
$response = new Response(sprintf($html, $redirectPage->getFullPath()).$css);
$event->setResponse($response);
} else {
$response = new RedirectResponse($redirectPage->getFullPath());
$event->setResponse($response);
}
}
}
}
}