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

100 lines
3.3 KiB
PHP
Raw Normal View History

2021-02-12 11:42:35 +01:00
<?php
namespace DanielSiepmann\DsSite\Frontend\RssFeed;
/*
* 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.
*/
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_id'] ?? 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);
}
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)
)
);
$result = $queryBuilder->execute()->fetchAll();
if ($result === false) {
return [];
}
return array_map(function (array $row) {
return (int) $row['uid'];
}, $result);
}
private function createAdditionalWhereForPageUids(array $pageUids): string
{
return ' AND uid IN(' . implode(',', $pageUids) . ')';
}
}