This commit is contained in:
orasche committed 2026-08-13 12:49:10 +02:00
1 parent 48da8bccd8
commit 8d3ac08a37
22 files changed
+1127 -350

No files matched your search

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Evomedien\Vitec\UserFunc;
use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\ParameterType;
use TYPO3\CMS\Core\Attribute\AsAllowedCallable;
use TYPO3\CMS\Core\Database\ConnectionPool;
@@ -20,12 +21,23 @@ use TYPO3\CMS\Extbase\Service\ImageService;
* today or later), soonest first. FlexForm options: showpast (append past
* events, most recent first, under "pastEvents"), limit, debug.
*
* Layout `regions` additionally emits "regions": the regions that actually have
* an upcoming event, each with the image of its next one. See fetchRegions().
*
* `render()` = TypoScript entry (cObj->data or page discovery).
* `renderForRecord()` = one specific tt_content row; reused by
* ContainerChildrenProcessor / ContentElementResolver. Exception-safe.
*/
class EventlistJsonRenderer
{
/**
* Parent of the region categories. Hard-coded like the other taxonomy roots
* in this extension (Annex B-5 tracks them); making it a FlexForm setting
* would put a raw uid in front of editors for a value that belongs to the
* content model, not to a single content element.
*/
private const REGION_PARENT_CATEGORY = 104;
#[AsAllowedCallable]
public function render(string $content, array $conf): string
{
@@ -101,6 +113,10 @@ class EventlistJsonRenderer
],
];
if ($layout === 'regions') {
$response['regions'] = $this->fetchRegions($daysInAdvance);
}
if ($showPast) {
$past = $this->fetchEvents(false, $limit);
$response['pastEvents'] = array_map(fn($e) => $this->serializeEvent($e), $past);
@@ -111,6 +127,9 @@ class EventlistJsonRenderer
'upcomingCount' => count($upcoming),
'settings' => $settings,
];
if (isset($response['regions'])) {
$response['debug']['regionCount'] = count($response['regions']);
}
}
return json_encode($response);
@@ -181,6 +200,215 @@ class EventlistJsonRenderer
return $qb->executeQuery()->fetchAllAssociative();
}
/**
* Regions for the `regions` layout: the categories below
* REGION_PARENT_CATEGORY that have at least one upcoming event, each
* carrying the image of its next one as the card logo.
*
* A region without an upcoming event is left out entirely -- that is the
* whole point of the layout, so `showpast` does not resurrect it. `limit`
* stays out too: it caps events, and applying it before the grouping would
* drop whole regions from the carousel without the editor seeing why.
*
* Order is the backend sorting of the categories, so the editor arranges the
* carousel by dragging them; `nextEvent.eventstart` is in the payload for a
* frontend that would rather sort chronologically.
*
* @return array<int,array<string,mixed>>
*/
private function fetchRegions(int $daysInAdvance): array
{
$regions = $this->fetchRegionCategories();
if ($regions === []) {
return [];
}
// Every category below a region -- the region itself included -- points
// at that region, so an event filed under a sub-category still counts.
$regionByCategory = [];
foreach ($regions as $regionUid => $region) {
foreach ($region['categoryUids'] as $categoryUid) {
$regionByCategory[$categoryUid] = $regionUid;
}
}
$rows = $this->fetchUpcomingEventsByCategory(array_keys($regionByCategory), $daysInAdvance);
// One row per event/category pair, sorted by eventstart -- so the first
// row seen for a region is its next event, and the ??= keeps it.
$eventsPerRegion = [];
foreach ($rows as $row) {
$regionUid = $regionByCategory[(int)$row['uid_local']] ?? null;
if ($regionUid === null) {
continue;
}
$eventsPerRegion[$regionUid][(int)$row['uid']] ??= $row;
}
$result = [];
foreach ($regions as $regionUid => $region) {
$regionEvents = $eventsPerRegion[$regionUid] ?? [];
if ($regionEvents === []) {
continue;
}
$next = reset($regionEvents);
$start = (int)($next['eventstart'] ?? 0);
$end = (int)($next['eventend'] ?? 0);
$result[] = [
'uid' => $regionUid,
'title' => $region['title'],
// Plain text, not RTE: sys_category descriptions are named as
// out of scope for the richtext conversion in Clause 9.11.
'description' => $region['description'],
'eventCount' => count($regionEvents),
'image' => $this->getEventImage((int)$next['uid']),
'nextEvent' => [
'uid' => (int)$next['uid'],
'title' => (string)($next['title'] ?? ''),
'slug' => (string)($next['slug'] ?? ''),
'eventstart' => $start > 0 ? date('Y-m-d', $start) : null,
'eventend' => $end > 0 ? date('Y-m-d', $end) : null,
'eventurl' => (string)($next['eventurl'] ?? ''),
],
];
}
return $result;
}
/**
* The region categories, keyed by uid and in backend sorting order, each
* with its own uid plus every descendant.
*
* sys_category is read in one go rather than per level: the table is small,
* and CustomerlogosJsonRenderer and MarketListJsonRenderer make the same
* trade-off.
*
* @return array<int,array{title:string,description:string,categoryUids:int[]}>
*/
private function fetchRegionCategories(): array
{
$qb = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('sys_category');
$rows = $qb
->select('uid', 'title', 'description', 'parent')
->from('sys_category')
->where(
$qb->expr()->eq('deleted', 0),
$qb->expr()->eq('hidden', 0)
)
->orderBy('sorting', 'ASC')
->executeQuery()
->fetchAllAssociative();
$byUid = [];
$childrenByParent = [];
foreach ($rows as $row) {
$uid = (int)$row['uid'];
$byUid[$uid] = $row;
$childrenByParent[(int)($row['parent'] ?? 0)][] = $uid;
}
$regions = [];
foreach ($childrenByParent[self::REGION_PARENT_CATEGORY] ?? [] as $regionUid) {
$regions[$regionUid] = [
'title' => (string)($byUid[$regionUid]['title'] ?? ''),
'description' => (string)($byUid[$regionUid]['description'] ?? ''),
'categoryUids' => $this->collectCategoryBranch($regionUid, $childrenByParent),
];
}
return $regions;
}
/**
* A category plus every descendant, breadth first. Iterative and
* seen-guarded: a parent pointing back up the tree would otherwise loop.
*
* @param array<int,int[]> $childrenByParent
* @return int[]
*/
private function collectCategoryBranch(int $uid, array $childrenByParent): array
{
$collected = [$uid];
$seen = [$uid => true];
$queue = [$uid];
while ($queue !== []) {
$current = array_shift($queue);
foreach ($childrenByParent[$current] ?? [] as $childUid) {
if (isset($seen[$childUid])) {
continue;
}
$seen[$childUid] = true;
$collected[] = $childUid;
$queue[] = $childUid;
}
}
return $collected;
}
/**
* Upcoming events assigned to any of the given categories, soonest first.
* One row per event/category pair, so an event in two regions appears in
* both. The "upcoming" test is the one fetchEvents() uses.
*
* @param int[] $categoryUids
* @return array<int,array<string,mixed>>
*/
private function fetchUpcomingEventsByCategory(array $categoryUids, int $daysInAdvance): array
{
if ($categoryUids === []) {
return [];
}
$todayMidnight = strtotime('today');
$qb = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tx_vitec_domain_model_event');
$expr = $qb->expr();
$qb
->select('e.uid', 'e.title', 'e.slug', 'e.eventstart', 'e.eventend', 'e.eventurl')
->addSelect('mm.uid_local')
->from('tx_vitec_domain_model_event', 'e')
->join(
'e',
'sys_category_record_mm',
'mm',
'mm.uid_foreign = e.uid AND mm.tablenames = ' .
$qb->createNamedParameter('tx_vitec_domain_model_event', ParameterType::STRING) .
' AND mm.fieldname = ' .
$qb->createNamedParameter('categories', ParameterType::STRING)
)
->where(
$expr->in('mm.uid_local', $qb->createNamedParameter($categoryUids, ArrayParameterType::INTEGER)),
$expr->eq('e.deleted', 0),
$expr->eq('e.hidden', 0),
$expr->eq('e.hideonwebsite', 0),
$expr->or(
$expr->gte('e.eventend', $qb->createNamedParameter($todayMidnight, ParameterType::INTEGER)),
$expr->and(
$expr->eq('e.eventend', 0),
$expr->gte('e.eventstart', $qb->createNamedParameter($todayMidnight, ParameterType::INTEGER))
)
)
)
->orderBy('e.eventstart', 'ASC');
if ($daysInAdvance > 0) {
$qb->andWhere(
$expr->lt('e.eventstart', $qb->createNamedParameter($todayMidnight + ($daysInAdvance * 86400), ParameterType::INTEGER))
);
}
return $qb->executeQuery()->fetchAllAssociative();
}
/**
* @param array<string,mixed> $event
* @return array<string,mixed>