2021-07-13 15:50:14 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2023-11-09 10:27:43 +01:00
|
|
|
namespace WerkraumMedia\Events\Domain\Repository;
|
2021-07-13 15:50:14 +02: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
use TYPO3\CMS\Core\Database\ConnectionPool;
|
|
|
|
use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper;
|
|
|
|
use TYPO3\CMS\Extbase\Persistence\Repository;
|
2023-11-09 10:27:43 +01:00
|
|
|
use WerkraumMedia\Events\Domain\Model\Category;
|
2021-07-13 15:50:14 +02:00
|
|
|
|
2023-11-27 10:04:42 +01:00
|
|
|
final class CategoryRepository extends Repository
|
2021-07-13 15:50:14 +02:00
|
|
|
{
|
2023-11-27 10:04:42 +01:00
|
|
|
public function __construct(
|
|
|
|
private readonly ConnectionPool $connectionPool,
|
|
|
|
private readonly DataMapper $dataMapper,
|
|
|
|
) {
|
|
|
|
parent::__construct();
|
2021-07-13 15:50:14 +02:00
|
|
|
}
|
|
|
|
|
2021-09-07 09:52:14 +02:00
|
|
|
/**
|
|
|
|
* @return array<Category>
|
|
|
|
*/
|
2022-07-05 14:08:14 +02:00
|
|
|
public function findAllCurrentlyAssigned(
|
|
|
|
int $parentUid = 0,
|
|
|
|
string $relation = 'categories'
|
|
|
|
): array {
|
2021-07-13 15:50:14 +02:00
|
|
|
$qb = $this->connectionPool->getQueryBuilderForTable('tx_events_domain_model_event');
|
|
|
|
$qb->select('category.*');
|
|
|
|
$qb->from('tx_events_domain_model_event', 'event');
|
|
|
|
|
|
|
|
$qb->leftJoin(
|
|
|
|
'event',
|
|
|
|
'sys_category_record_mm',
|
|
|
|
'mm',
|
|
|
|
'event.uid = mm.uid_foreign'
|
|
|
|
. ' AND mm.tablenames = ' . $qb->createNamedParameter('tx_events_domain_model_event')
|
2022-07-05 14:08:14 +02:00
|
|
|
. ' AND mm.fieldname = ' . $qb->createNamedParameter($relation)
|
2021-07-13 15:50:14 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$qb->leftJoin(
|
|
|
|
'mm',
|
|
|
|
'sys_category',
|
|
|
|
'category',
|
|
|
|
'category.uid = mm.uid_local'
|
|
|
|
. ' AND mm.tablenames = ' . $qb->createNamedParameter('tx_events_domain_model_event')
|
2022-07-05 14:08:14 +02:00
|
|
|
. ' AND mm.fieldname = ' . $qb->createNamedParameter($relation)
|
2021-07-13 15:50:14 +02:00
|
|
|
);
|
|
|
|
|
2021-09-08 12:32:51 +02:00
|
|
|
$qb->where($qb->expr()->neq('category.uid', $qb->createNamedParameter(0)));
|
2022-07-05 14:08:14 +02:00
|
|
|
if ($parentUid > 0) {
|
|
|
|
$qb->andWhere($qb->expr()->eq('category.parent', $qb->createNamedParameter($parentUid)));
|
|
|
|
}
|
2021-07-13 15:50:14 +02:00
|
|
|
$qb->orderBy('category.title', 'asc');
|
|
|
|
$qb->groupBy('category.uid');
|
|
|
|
|
|
|
|
return $this->dataMapper->map(
|
|
|
|
Category::class,
|
2023-11-27 10:04:42 +01:00
|
|
|
$qb->executeQuery()->fetchAll()
|
2021-07-13 15:50:14 +02:00
|
|
|
);
|
|
|
|
}
|
2022-07-05 14:08:14 +02:00
|
|
|
|
|
|
|
public function findOneForImport(
|
|
|
|
Category $parentCategory,
|
|
|
|
int $pid,
|
|
|
|
string $title
|
|
|
|
): ?Category {
|
|
|
|
$query = $this->createQuery();
|
|
|
|
|
|
|
|
$query->getQuerySettings()->setStoragePageIds([$pid]);
|
|
|
|
// Necessary as enableFieldsToBeIgnored would not be respected.
|
|
|
|
// Both combined lead to only ignoring defined enable fields.
|
|
|
|
$query->getQuerySettings()->setIgnoreEnableFields(true);
|
|
|
|
$query->getQuerySettings()->setEnableFieldsToBeIgnored(['disabled']);
|
|
|
|
|
2023-11-27 10:04:42 +01:00
|
|
|
$query->matching($query->logicalAnd(
|
2022-07-05 14:08:14 +02:00
|
|
|
$query->equals('parent', $parentCategory),
|
2023-06-07 08:56:42 +02:00
|
|
|
$query->equals('title', $title),
|
2023-11-27 10:04:42 +01:00
|
|
|
));
|
2022-07-05 14:08:14 +02:00
|
|
|
|
|
|
|
$query->setLimit(1);
|
|
|
|
|
|
|
|
return $query->execute()->getFirst();
|
|
|
|
}
|
2021-07-13 15:50:14 +02:00
|
|
|
}
|