. */ use Alledia\OSMap\Factory; use Alledia\OSMap\Helper\General; use Alledia\OSMap\Sitemap\Item; use Alledia\OSMap\Sitemap\Standard; use Joomla\CMS\Application\SiteApplication; use Joomla\CMS\Component\ComponentHelper; use Joomla\CMS\Language\Text; use Joomla\CMS\MVC\View\HtmlView; use Joomla\CMS\Router\Route; use Joomla\CMS\Uri\Uri; use Joomla\Registry\Registry; // phpcs:disable PSR1.Files.SideEffects defined('_JEXEC') or die(); // phpcs:enable PSR1.Files.SideEffects // phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace class OsmapViewXml extends HtmlView { /** * @var SiteApplication */ protected $app = null; /** * @var string */ protected $type = null; /** * @var Registry */ protected $params = null; /** * @var Registry */ protected $osmapParams = null; /** * @var Standard */ protected $sitemap = null; /** * @var string */ protected $language = null; /** * @var DateTime */ protected $newsCutoff = null; /** * @var int */ protected $newsLimit = 1000; /** * @inheritDoc * @throws Exception */ public function __construct($config = []) { parent::__construct($config); $this->app = Factory::getApplication(); } /** * @inheritDoc */ public function display($tpl = null) { $document = $this->app->getDocument(); if ($document->getType() != 'xml') { // There are ways to get here with a non-xml document, so we have to redirect $uri = Uri::getInstance(); $uri->setVar('format', 'xml'); $this->app->redirect($uri); // Not strictly necessary, but makes the point :) return; } $this->type = General::getSitemapTypeFromInput(); $this->sitemap = Factory::getSitemap($this->app->input->getInt('id'), $this->type); if (!$this->sitemap->isPublished) { throw new Exception(Text::_('COM_OSMAP_MSG_SITEMAP_IS_UNPUBLISHED'), 404); } $this->params = $this->app->getParams(); $this->osmapParams = ComponentHelper::getParams('com_osmap'); $this->language = $document->getLanguage(); $this->newsCutoff = new DateTime('-' . $this->sitemap->newsDateLimit . ' days'); if ($this->params->get('debug', 0)) { $document->setMimeEncoding('text/plain'); } parent::display($tpl); } /** * @return string */ protected function addStylesheet(): string { if ($this->params->get('add_styling', 1)) { $query = [ 'option' => 'com_osmap', 'view' => 'xsl', 'format' => 'xsl', 'layout' => $this->type, 'id' => $this->sitemap->id, ]; if ($itemId = $this->app->input->getInt('Itemid')) { $query['Itemid'] = $itemId; } return sprintf( '', Route::_('index.php?' . http_build_query($query)) ); } return ''; } /** * @param Item $node * * @return ?DateTime */ protected function isNewsPublication(Item $node): ?DateTime { try { $publicationDate = ( !empty($node->publishUp) && $node->publishUp != Factory::getDbo()->getNullDate() && $node->publishUp != -1 ) ? $node->publishUp : null; if ($publicationDate) { $publicationDate = new DateTime($publicationDate); if ($this->newsCutoff <= $publicationDate) { $this->newsLimit--; if ($this->newsLimit >= 0) { return $publicationDate; } } } } catch (Throwable $error) { // Don't care } return null; } }