Add event full template data hook
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace MummertMedia\EventManagerBundle\EventListener;
|
||||
|
||||
use Contao\CoreBundle\DependencyInjection\Attribute\AsHook;
|
||||
use Contao\Template;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Doctrine\DBAL\ParameterType;
|
||||
|
||||
#[AsHook('parseTemplate', method: 'onParseTemplate')]
|
||||
class EventFullTemplateDataListener
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Connection $connection,
|
||||
) {
|
||||
}
|
||||
|
||||
public function onParseTemplate(Template $template): void
|
||||
{
|
||||
$templateName = (string) $template->getName();
|
||||
|
||||
if (!str_starts_with($templateName, 'event_full')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$data = $template->getData();
|
||||
$eventId = (int) ($data['id'] ?? 0);
|
||||
|
||||
if ($eventId <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$locationId = isset($data['location_id']) ? (int) $data['location_id'] : 0;
|
||||
|
||||
$template->location_fields = $this->fetchLocationFields($locationId);
|
||||
$template->organization_fields = $this->fetchOrganizationFields($eventId);
|
||||
$template->tag_fields = $this->fetchTagFields($eventId);
|
||||
}
|
||||
|
||||
/** @return array<string, string> */
|
||||
private function fetchLocationFields(int $locationId): array
|
||||
{
|
||||
if ($locationId <= 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$row = $this->connection->createQueryBuilder()
|
||||
->select('*')
|
||||
->from('tl_location')
|
||||
->where('id = :id')
|
||||
->setParameter('id', $locationId, ParameterType::INTEGER)
|
||||
->setMaxResults(1)
|
||||
->executeQuery()
|
||||
->fetchAssociative();
|
||||
|
||||
if (false === $row || null === $row) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$fields = [];
|
||||
|
||||
foreach ($row as $column => $value) {
|
||||
$fields['location_'.$column] = $this->toString($value);
|
||||
}
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
/** @return array<string, string> */
|
||||
private function fetchOrganizationFields(int $eventId): array
|
||||
{
|
||||
$rows = $this->connection->createQueryBuilder()
|
||||
->select('o.*')
|
||||
->from('tl_calendar_events_organization', 'ceo')
|
||||
->innerJoin('ceo', 'tl_organization', 'o', 'o.id = ceo.organization_id')
|
||||
->where('ceo.event_id = :eventId')
|
||||
->setParameter('eventId', $eventId, ParameterType::INTEGER)
|
||||
->orderBy('ceo.organization_id', 'ASC')
|
||||
->executeQuery()
|
||||
->fetchAllAssociative();
|
||||
|
||||
$fields = [];
|
||||
$index = 1;
|
||||
|
||||
foreach ($rows as $row) {
|
||||
foreach ($row as $column => $value) {
|
||||
$fields['organization'.$index.'_'.$column] = $this->toString($value);
|
||||
}
|
||||
|
||||
++$index;
|
||||
}
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
/** @return array<string, string> */
|
||||
private function fetchTagFields(int $eventId): array
|
||||
{
|
||||
$rows = $this->connection->createQueryBuilder()
|
||||
->select('t.*')
|
||||
->from('tl_tags_rel', 'r')
|
||||
->innerJoin('r', 'tl_tags', 't', 't.id = r.tag_id')
|
||||
->where('r.ptable = :ptable')
|
||||
->andWhere('r.field = :field')
|
||||
->andWhere('r.pid = :pid')
|
||||
->setParameter('ptable', 'tl_calendar_events')
|
||||
->setParameter('field', 'tags')
|
||||
->setParameter('pid', $eventId, ParameterType::INTEGER)
|
||||
->orderBy('r.tag_id', 'ASC')
|
||||
->executeQuery()
|
||||
->fetchAllAssociative();
|
||||
|
||||
$fields = [];
|
||||
$index = 1;
|
||||
|
||||
foreach ($rows as $row) {
|
||||
$title = '';
|
||||
|
||||
if (isset($row['title']) && '' !== (string) $row['title']) {
|
||||
$title = (string) $row['title'];
|
||||
} elseif (isset($row['tag'])) {
|
||||
$title = (string) $row['tag'];
|
||||
}
|
||||
|
||||
$fields['tag'.$index.'_title'] = $title;
|
||||
++$index;
|
||||
}
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
private function toString(mixed $value): string
|
||||
{
|
||||
if (null === $value) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (\is_bool($value)) {
|
||||
return $value ? '1' : '0';
|
||||
}
|
||||
|
||||
if (\is_scalar($value)) {
|
||||
return (string) $value;
|
||||
}
|
||||
|
||||
return json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) ?: '';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user