ds-site/Classes/Frontend/DataProcessing/CategoriesCounts.php
Daniel Siepmann abd08429f5 Migrate DB logic from TS to new Data Processor
This way multiple joins are possible.
As multiple joins are possible, TYPO3 can add pages to the query and
therefore respect the enable fields.
Hidden pages will therefore not be part of the result, fixing
inaccurate count for none published pages with assigned categories.

Also the TypoScript communicates what should happen.
2020-09-25 01:09:36 +02:00

110 lines
3.5 KiB
PHP

<?php
namespace DanielSiepmann\DsSite\Frontend\DataProcessing;
/*
* Copyright (C) 2020 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 TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Database\Query\QueryHelper;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface;
class CategoriesCounts implements DataProcessorInterface
{
/**
* @var QueryBuilder
*/
private $queryBuilder;
public function __construct(QueryBuilder $queryBuilder)
{
$this->queryBuilder = $queryBuilder;
}
public function process(
ContentObjectRenderer $cObj,
array $contentObjectConfiguration,
array $processorConfiguration,
array $processedData
) {
$as = $cObj->stdWrapValue('as', $processorConfiguration, 'categories');
$parent = $cObj->stdWrapValue('parent', $processorConfiguration, '');
$orderBy = $cObj->stdWrapValue('orderBy', $processorConfiguration, 'count desc');
[$orderByField, $orderByDirection] = QueryHelper::parseOrderBy($orderBy)[0];
if ($parent === '') {
throw new \InvalidArgumentException('No "parent" given.', 1600988668);
}
if ($orderByField === null || $orderByDirection === null) {
throw new \Exception('Non correct "orderBy" given. Needs field and "asc" or "desc".', 1600988713);
}
$processedData[$as] = $this->getCategoriesCount(
$parent,
$orderByField,
$orderByDirection
);
return $processedData;
}
private function getCategoriesCount(
int $parentId,
string $orderByField,
string $orderByDirection
): array {
$queryBuilder = clone $this->queryBuilder;
$queryBuilder->select('category.*');
$queryBuilder->addSelectLiteral('count(mm.uid_foreign) as count');
$queryBuilder->from('pages');
$queryBuilder->leftJoin(
'pages',
'sys_category_record_mm',
'mm',
'pages.uid = mm.uid_foreign',
);
$queryBuilder->leftJoin(
'mm',
'sys_category',
'category',
'mm.uid_local = category.uid',
);
$queryBuilder->where(
$queryBuilder->expr()->eq(
'category.parent',
$queryBuilder->createNamedParameter($parentId)
)
);
$queryBuilder->groupBy('category.uid');
$queryBuilder->orderBy($orderByField, $orderByDirection);
$statement = $queryBuilder->execute();
$categories = [];
while ($category = $statement->fetch()) {
$categories[] = [
'data' => $category,
];
}
return $categories;
}
}