<?php
namespace App\Controller;
use Exception;
use Pimcore\Model\Document;
use Pimcore\Model\Document\Page;
use Pimcore\Model\Site;
use Pimcore\Model\DataObject;
use Pimcore\Tool;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class SitemapController extends AbstractController
{
// --------------------------------
// VARIABLES
// --------------------------------
private $sitemapElements = [];
// --------------------------------
// SITEMAP
// --------------------------------
/**
* @Route("/sitemap.xml", name="sitemap")
*
* @return Response
*/
public function xmlAction(TranslatorInterface $translator)
{
try {
$rootDoc = Document::getByPath('/');
$baseUrl = Tool::getHostUrl();
if (Site::isSiteRequest()) {
$current = Site::getCurrentSite();
$rootDoc = $current->getRootDocument();
}
$this->recursiveSitemap($rootDoc, $baseUrl);
// detail pages
// $this->newsDetails($translator, $baseUrl);
array_unshift($this->sitemapElements, '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">');
array_unshift($this->sitemapElements, '<?xml version="1.0" encoding="UTF-8" ?>');
array_push($this->sitemapElements, '</urlset>');
return new Response(trim(implode(PHP_EOL, $this->sitemapElements)), 200, ['Content-Type' => 'text/xml']);
} catch (Exception $e) {
return new Response('', 404);
}
}
// --------------------------------
// RECURSIVE SITEMAP
// --------------------------------
/**
* @param Document $doc
* @param $baseUrl
*/
private function recursiveSitemap(Document $doc, $baseUrl)
{
if ($doc->hasChildren()) {
foreach ($doc->getChildren() as $child) {
if ($child instanceof Document) {
if ($child->hasProperty('sitemap_exclude') && $child->getProperty('sitemap_exclude')) {
continue;
}
if (!$child->isPublished()) {
continue;
}
if ($child instanceof Page) {
array_push($this->sitemapElements, '<url><loc>' . $baseUrl . $child->getFullPath() . '</loc><lastmod>' . date('Y-m-d', $child->getModificationDate()) . '</lastmod></url>');
}
$this->recursiveSitemap($child, $baseUrl);
}
}
}
}
// --------------------------------
// NEWS DETAIL PAGES
// --------------------------------
/**
* @param $baseUrl
*/
/*private function newsDetails($translator, $baseUrl) {
// objects
$list = new DataObject\News\Listing();
$list->setOrderKey('date');
$list->setOrder('desc');
$list->load();
foreach ($list as $detail) {
// languages
$siteLanguages = \Pimcore\Tool::getValidLanguages();
// last modification date
$detailLastMod = $detail->getModificationDate();
foreach ($siteLanguages as $siteLanguage) {
$subPath = $translator->trans(
'news-detail-route-path',
[],
'messages',
$siteLanguage
);
$detailLink = $baseUrl . '/' . $siteLanguage . '/' . $subPath . '/' . HelperController::toUri($detail->getTitle($siteLanguage)) . '_n' . $detail->getId();
// add link to sitemap
array_push($this->sitemapElements, '<url><loc>' . $detailLink . '</loc><lastmod>' . date('Y-m-d', $detailLastMod) . '</lastmod></url>');
}
}
}*/
}