diff --git a/src/EventListener/EventFullTemplateDataListener.php b/src/EventListener/EventFullTemplateDataListener.php index 7b7a6ef..bb8a391 100644 --- a/src/EventListener/EventFullTemplateDataListener.php +++ b/src/EventListener/EventFullTemplateDataListener.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace MummertMedia\EventManagerBundle\EventListener; use Contao\CoreBundle\DependencyInjection\Attribute\AsHook; +use Contao\StringUtil; use Contao\Template; use Doctrine\DBAL\Connection; use Doctrine\DBAL\ParameterType; @@ -34,13 +35,29 @@ class EventFullTemplateDataListener $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); + $locationRow = $this->fetchLocationRow($locationId); + $organizations = $this->fetchOrganizations($eventId); + $tags = $this->fetchTags($eventId); + + $template->location_row = $locationRow; + $template->organizations = $organizations; + $template->tags = $tags; + $template->has_tag_21 = \in_array(21, array_map(static fn (array $tag): int => (int) ($tag['id'] ?? 0), $tags), true); + + if ($locationRow && '' !== ($locationRow['lng'] ?? '') && '' !== ($locationRow['lat'] ?? '') && 58 !== (int) ($locationRow['id'] ?? 0)) { + $this->registerMapAssets(); + $template->show_location_map = true; + } else { + $template->show_location_map = false; + } + + $template->location_fields = $this->toFlatLocationFields($locationRow); + $template->organization_fields = $this->toFlatOrganizationFields($organizations); + $template->tag_fields = $this->toFlatTagFields($tags); } /** @return array */ - private function fetchLocationFields(int $locationId): array + private function fetchLocationRow(int $locationId): array { if ($locationId <= 0) { return []; @@ -59,17 +76,21 @@ class EventFullTemplateDataListener return []; } - $fields = []; + $normalized = []; foreach ($row as $column => $value) { - $fields['location_'.$column] = $this->toString($value); + $normalized[$column] = $this->toString($value); } - return $fields; + if ('' !== ($normalized['image'] ?? '')) { + $normalized['image_uuid'] = $this->normalizeUuid($normalized['image']); + } + + return $normalized; } - /** @return array */ - private function fetchOrganizationFields(int $eventId): array + /** @return list> */ + private function fetchOrganizations(int $eventId): array { $rows = $this->connection->createQueryBuilder() ->select('o.*') @@ -81,22 +102,27 @@ class EventFullTemplateDataListener ->executeQuery() ->fetchAllAssociative(); - $fields = []; - $index = 1; + $organizations = []; foreach ($rows as $row) { + $normalized = []; + foreach ($row as $column => $value) { - $fields['organization'.$index.'_'.$column] = $this->toString($value); + $normalized[$column] = $this->toString($value); } - ++$index; + if ('' !== ($normalized['logo'] ?? '')) { + $normalized['logo_uuid'] = $this->normalizeUuid($normalized['logo']); + } + + $organizations[] = $normalized; } - return $fields; + return $organizations; } - /** @return array */ - private function fetchTagFields(int $eventId): array + /** @return list> */ + private function fetchTags(int $eventId): array { $rows = $this->connection->createQueryBuilder() ->select('t.*') @@ -112,8 +138,7 @@ class EventFullTemplateDataListener ->executeQuery() ->fetchAllAssociative(); - $fields = []; - $index = 1; + $tags = []; foreach ($rows as $row) { $title = ''; @@ -124,13 +149,103 @@ class EventFullTemplateDataListener $title = (string) $row['tag']; } - $fields['tag'.$index.'_title'] = $title; + $tags[] = [ + 'id' => (string) ((int) ($row['id'] ?? 0)), + 'title' => $title, + 'tag' => (string) ($row['tag'] ?? ''), + ]; + } + + return $tags; + } + + /** @param array $locationRow + * @return array + */ + private function toFlatLocationFields(array $locationRow): array + { + $fields = []; + + foreach ($locationRow as $column => $value) { + $fields['location_'.$column] = $value; + } + + return $fields; + } + + /** @param list> $organizations + * @return array + */ + private function toFlatOrganizationFields(array $organizations): array + { + $fields = []; + $index = 1; + + foreach ($organizations as $organization) { + foreach ($organization as $column => $value) { + $fields['organization'.$index.'_'.$column] = $value; + } + ++$index; } return $fields; } + /** @param list> $tags + * @return array + */ + private function toFlatTagFields(array $tags): array + { + $fields = []; + $index = 1; + + foreach ($tags as $tag) { + $fields['tag'.$index.'_title'] = (string) ($tag['title'] ?? ''); + ++$index; + } + + return $fields; + } + + private function normalizeUuid(string $value): string + { + $trimmed = trim($value); + + if (preg_match('/^[0-9a-fA-F-]{36}$/', $trimmed)) { + return strtolower($trimmed); + } + + if (16 === strlen($trimmed)) { + return StringUtil::binToUuid($trimmed); + } + + return $trimmed; + } + + private function registerMapAssets(): void + { + $GLOBALS['TL_JAVASCRIPT'] ??= []; + $GLOBALS['TL_CSS'] ??= []; + + $jsAssets = [ + 'https://maps.mummert.media/libraries/maplibre-gl.js', + 'https://maps.mummert.media/libraries/pmtiles.js', + ]; + + foreach ($jsAssets as $asset) { + if (!\in_array($asset, $GLOBALS['TL_JAVASCRIPT'], true)) { + $GLOBALS['TL_JAVASCRIPT'][] = $asset; + } + } + + $cssAsset = 'https://maps.mummert.media/libraries/maplibre-gl.css'; + + if (!\in_array($cssAsset, $GLOBALS['TL_CSS'], true)) { + $GLOBALS['TL_CSS'][] = $cssAsset; + } + } + private function toString(mixed $value): string { if (null === $value) {