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

138 lines
4.4 KiB
PHP
Raw Normal View History

<?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 Exception;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface;
use TYPO3\CMS\Frontend\DataProcessing\DatabaseQueryProcessor;
final class BlogPostsDataProvider implements DataProcessorInterface
{
public function __construct(
private readonly DatabaseQueryProcessor $databaseQueryProcessor,
private readonly ConnectionPool $connectionPool
) {
}
public function process(
ContentObjectRenderer $contentObjectRenderer,
array $contentObjectConfiguration,
array $processorConfiguration,
array $processedData
) {
$processedData = $this->databaseQueryProcessor->process(
$contentObjectRenderer,
$contentObjectConfiguration,
array_merge($processorConfiguration, [
'uidInList' => $this->getPageUids($contentObjectRenderer->getRequest()),
]),
$processedData
);
foreach ($processedData['pages'] as &$page) {
$page['description'] = $page['data']['abstract']
. $this->getContent(
$contentObjectRenderer,
$page['data']['uid']
);
}
return $processedData;
}
private function getPageUids(ServerRequestInterface $request): string
{
2024-02-08 08:04:56 +01:00
$categoryUid = (int) ($request->getQueryParams()['category_uid'] ?? 0);
if ($categoryUid === 0) {
return '';
}
return implode(',', $this->getPageUidsWithRelationToCategory($categoryUid));
}
private function getPageUidsWithRelationToCategory(int $categoryUid): array
{
$queryBuilder = $this->connectionPool->getQueryBuilderForTable('pages');
$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)
)
);
2024-02-08 08:04:56 +01:00
return array_map(static function (array $row) {
if (is_numeric($row['uid'])) {
return (int) $row['uid'];
}
throw new Exception('UID was not numeric: ' . var_export($row['uid'], true), 1707325559);
}, $queryBuilder->executeQuery()->fetchAllAssociative());
}
private function getContent(
ContentObjectRenderer $contentObjectRenderer,
int $pageUid
): string {
$colPositions = [
50,
0,
100,
200,
];
$content = '';
foreach ($colPositions as $colPos) {
$content .= $contentObjectRenderer->cObjGetSingle('CONTENT', [
'table' => 'tt_content',
'select.' => [
'orderBy' => 'sorting',
'where' => '{#colPos}=' . $colPos,
'pidInList' => $pageUid,
],
]);
}
return $content;
}
}