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

95 lines
3.2 KiB
PHP

<?php
declare(strict_types=1);
/*
* 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;
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);
if ($categoryUid > 0) {
$pageUids = $this->getPageUidsWithRelationToCategory($categoryUid);
$config['additionalWhere'] = ($config['additionalWhere'] ?? '')
. $this->createAdditionalWhereForPageUids($pageUids)
;
}
parent::__construct($request, $key, $config, $cObj);
}
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'];
}, $queryBuilder->execute()->fetchAll());
}
private function createAdditionalWhereForPageUids(array $pageUids): string
{
return ' AND uid IN(' . implode(',', $pageUids) . ')';
}
}