Add event teaser data attribute listener
This commit is contained in:
@@ -0,0 +1,104 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace MummertMedia\EventManagerBundle\EventListener;
|
||||||
|
|
||||||
|
use Contao\CalendarEventsModel;
|
||||||
|
use Contao\CoreBundle\DependencyInjection\Attribute\AsHook;
|
||||||
|
use Contao\Module;
|
||||||
|
use Doctrine\DBAL\Connection;
|
||||||
|
use Doctrine\DBAL\Exception;
|
||||||
|
use Doctrine\DBAL\ParameterType;
|
||||||
|
|
||||||
|
#[AsHook('getAllEvents')]
|
||||||
|
class EventTeaserDataAttributesListener
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly Connection $connection,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __invoke(array $events, array $calendars, int $start, int $end, Module $module): array
|
||||||
|
{
|
||||||
|
foreach ($events as $dayKey => $eventsPerDay) {
|
||||||
|
foreach ($eventsPerDay as $timeKey => $eventsPerTime) {
|
||||||
|
foreach ($eventsPerTime as $index => $event) {
|
||||||
|
if (!isset($event['id'])) {
|
||||||
|
$events[$dayKey][$timeKey][$index]['location_id'] = null;
|
||||||
|
$events[$dayKey][$timeKey][$index]['event_tags'] = '';
|
||||||
|
$events[$dayKey][$timeKey][$index]['event_org'] = '';
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$eventId = (int) $event['id'];
|
||||||
|
|
||||||
|
if (isset($event['location_id']) && '' !== (string) $event['location_id']) {
|
||||||
|
$events[$dayKey][$timeKey][$index]['location_id'] = (int) $event['location_id'];
|
||||||
|
} else {
|
||||||
|
$eventModel = CalendarEventsModel::findById($eventId);
|
||||||
|
$events[$dayKey][$timeKey][$index]['location_id'] = $eventModel ? (int) $eventModel->location_id : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$events[$dayKey][$timeKey][$index]['event_tags'] = implode(',', $this->fetchTagIds($eventId));
|
||||||
|
$events[$dayKey][$timeKey][$index]['event_org'] = implode(',', $this->fetchOrganizationIds($eventId));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $events;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return list<int> */
|
||||||
|
private function fetchTagIds(int $eventId): array
|
||||||
|
{
|
||||||
|
if ($eventId <= 0) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$ids = $this->connection->createQueryBuilder()
|
||||||
|
->select('tag_id')
|
||||||
|
->from('tl_tags_rel')
|
||||||
|
->where('ptable = :ptable')
|
||||||
|
->andWhere('field = :field')
|
||||||
|
->andWhere('pid = :pid')
|
||||||
|
->setParameter('ptable', 'tl_calendar_events')
|
||||||
|
->setParameter('field', 'tags')
|
||||||
|
->setParameter('pid', $eventId, ParameterType::INTEGER)
|
||||||
|
->orderBy('tag_id', 'ASC')
|
||||||
|
->executeQuery()
|
||||||
|
->fetchFirstColumn();
|
||||||
|
|
||||||
|
return array_values(array_unique(array_map('intval', $ids)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return list<int> */
|
||||||
|
private function fetchOrganizationIds(int $eventId): array
|
||||||
|
{
|
||||||
|
if ($eventId <= 0) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$tables = ['tl_events_organization', 'tl_calendar_events_organization'];
|
||||||
|
|
||||||
|
foreach ($tables as $table) {
|
||||||
|
try {
|
||||||
|
$ids = $this->connection->createQueryBuilder()
|
||||||
|
->select('organization_id')
|
||||||
|
->from($table)
|
||||||
|
->where('event_id = :eventId')
|
||||||
|
->setParameter('eventId', $eventId, ParameterType::INTEGER)
|
||||||
|
->orderBy('organization_id', 'ASC')
|
||||||
|
->executeQuery()
|
||||||
|
->fetchFirstColumn();
|
||||||
|
|
||||||
|
return array_values(array_unique(array_map('intval', $ids)));
|
||||||
|
} catch (Exception) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user