ds-site/Classes/Frontend/RssFeed/SitemapDataProvider.php

106 lines
3.5 KiB
PHP
Raw Normal View History

2021-02-12 11:42:35 +01:00
<?php
declare(strict_types=1);
2021-02-12 11:42:35 +01:00
/*
* Copyright (C) 2021 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
namespace DanielSiepmann\DsSite\Frontend\RssFeed;
2021-02-12 11:42:35 +01:00
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Seo\XmlSitemap\RecordsXmlSitemapDataProvider;
/**
* Provides dynamic for additionalWhere,
* to only list pages for current requested category.
*/
class SitemapDataProvider extends RecordsXmlSitemapDataProvider
{
public function __construct(
ServerRequestInterface $request,
string $key,
array $config = [],
ContentObjectRenderer $cObj = null
) {
$categoryUid = intval($request->getQueryParams()['category_uid'] ?? 0);
2021-02-12 11:42:35 +01:00
if ($categoryUid > 0) {
$pageUids = $this->getPageUidsWithRelationToCategory($categoryUid);
$config['additionalWhere'] = ($config['additionalWhere'] ?? '')
. $this->createAdditionalWhereForPageUids($pageUids)
;
}
parent::__construct($request, $key, $config, $cObj);
}
2023-08-03 22:14:33 +02:00
public function generateItems(): void
{
parent::generateItems();
$contentRendering = new ContentRendering($this->cObj);
foreach ($this->items as $key => $item) {
$this->items[$key] = $contentRendering->extend($item);
}
}
2021-02-12 11:42:35 +01:00
private function getPageUidsWithRelationToCategory(int $categoryUid): array
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('pages');
/* @var QueryBuilder $queryBuilder */
$queryBuilder->select('uid');
$queryBuilder->from('pages');
$queryBuilder->leftJoin(
'pages',
'sys_category_record_mm',
'mm',
'pages.uid = mm.uid_foreign'
);
$queryBuilder->where(
$queryBuilder->expr()->eq(
'mm.tablenames',
$queryBuilder->createNamedParameter('pages')
),
$queryBuilder->expr()->eq(
'mm.fieldname',
$queryBuilder->createNamedParameter('categories')
),
$queryBuilder->expr()->in(
'mm.uid_local',
$queryBuilder->createNamedParameter($categoryUid)
)
);
return array_map(function (array $row) {
return (int) $row['uid'];
2022-12-06 08:18:42 +01:00
}, $queryBuilder->execute()->fetchAll());
2021-02-12 11:42:35 +01:00
}
private function createAdditionalWhereForPageUids(array $pageUids): string
{
return ' AND uid IN(' . implode(',', $pageUids) . ')';
}
}