diff --git a/src/EventListener/IndexPageListener.php b/src/EventListener/IndexPageListener.php index d4a4523..97278e5 100644 --- a/src/EventListener/IndexPageListener.php +++ b/src/EventListener/IndexPageListener.php @@ -2,126 +2,135 @@ namespace MummertMedia\ContaoMeilisearchBundle\EventListener; +use Contao\Database; + class IndexPageListener { - public function onIndexPage(string $content, array &$data, array &$set): void + public function onIndexPage(array &$data, array $set, array $page): void { + // -------------------------------------------------- + // DEBUG START + // -------------------------------------------------- if (PHP_SAPI === 'cli') { echo "\n=============================\n"; echo "INDEXPAGE HOOK START\n"; - echo "URL: " . ($set['url'] ?? '[no url]') . "\n"; + echo "URL: {$set['url']}\n"; } - // 1. Marker vorhanden? - if (!str_contains($content, 'MEILISEARCH')) { - if (PHP_SAPI === 'cli') { - echo "❌ MEILISEARCH marker NOT found in content\n"; - } - return; - } - - if (PHP_SAPI === 'cli') { - echo "✅ MEILISEARCH marker found\n"; - } - - // 2. Marker extrahieren - $markers = $this->extractMarkers($content); - - if (PHP_SAPI === 'cli') { - echo "---- PARSED MARKERS ----\n"; - var_dump($markers); - echo "------------------------\n"; - } - - if ($markers === []) { - if (PHP_SAPI === 'cli') { - echo "❌ Marker array EMPTY after parsing\n"; - } - return; - } - - // 3. PRIORITY - if (isset($markers['event.priority'])) { - $data['priority'] = (int) $markers['event.priority']; - } elseif (isset($markers['news.priority'])) { - $data['priority'] = (int) $markers['news.priority']; - } elseif (isset($markers['page.priority'])) { - $data['priority'] = (int) $markers['page.priority']; - } - - // 4. KEYWORDS - $keywords = []; - foreach (['event.keywords', 'news.keywords', 'page.keywords'] as $key) { - if (!empty($markers[$key])) { - $keywords = array_merge( - $keywords, - preg_split('/\s+/', trim($markers[$key])) ?: [] - ); - } - } - - if ($keywords) { - $data['keywords'] = implode(' ', array_unique($keywords)); - } - - // 5. IMAGEPATH - foreach ( - ['event.searchimage', 'news.searchimage', 'page.searchimage', 'custom.searchimage'] - as $key + // -------------------------------------------------- + // 1. MEILISEARCH_JSON aus HTML extrahieren + // -------------------------------------------------- + if ( + !isset($set['content']) || + !preg_match('#MEILISEARCH_JSON\s*(\{.*?\})#s', $set['content'], $m) ) { - if (!empty($markers[$key])) { - $data['imagepath'] = trim($markers[$key]); - break; + if (PHP_SAPI === 'cli') { + echo "❌ Kein MEILISEARCH_JSON gefunden\n"; + echo "INDEXPAGE HOOK END\n"; + echo "=============================\n"; } + return; } - // 6. STARTDATE - foreach (['event.date', 'news.date'] as $key) { - if (!empty($markers[$key])) { - $ts = strtotime($markers[$key]); - if ($ts !== false) { - $data['startDate'] = $ts; - } - break; + $meta = json_decode($m[1], true); + + if (!is_array($meta)) { + if (PHP_SAPI === 'cli') { + echo "❌ MEILISEARCH_JSON ist kein valides JSON\n"; + echo "INDEXPAGE HOOK END\n"; + echo "=============================\n"; } + return; } - // 7. FINAL STATE if (PHP_SAPI === 'cli') { - echo "---- FINAL \$data ----\n"; - var_dump([ - 'priority' => $data['priority'] ?? null, - 'keywords' => $data['keywords'] ?? null, - 'imagepath' => $data['imagepath'] ?? null, - 'startDate' => $data['startDate'] ?? null, - ]); + echo "✅ MEILISEARCH_JSON gefunden\n"; + echo "---- RAW JSON ----\n"; + var_dump($meta); + echo "------------------\n"; + } - echo "---- RAW \$data ----\n"; - var_dump($data); + // -------------------------------------------------- + // 2. Sauberes Mapping (klar definierte Priorität) + // -------------------------------------------------- + $priority = + $meta['event']['priority'] + ?? $meta['news']['priority'] + ?? $meta['page']['priority'] + ?? null; - echo "---- RAW \$set ----\n"; - var_dump($set); + $keywords = + $meta['event']['keywords'] + ?? $meta['news']['keywords'] + ?? $meta['page']['keywords'] + ?? null; + $imagepath = + $meta['custom']['searchimage'] + ?? $meta['event']['searchimage'] + ?? $meta['news']['searchimage'] + ?? $meta['page']['searchimage'] + ?? null; + + $startDate = + $meta['event']['date'] + ?? $meta['news']['date'] + ?? null; + + // -------------------------------------------------- + // 3. Daten vorbereiten + // -------------------------------------------------- + $update = []; + + if ($priority !== null) { + $update['priority'] = (int) $priority; + } + + if ($keywords !== null) { + $update['keywords'] = trim((string) $keywords); + } + + if ($imagepath !== null) { + $update['imagepath'] = (string) $imagepath; + } + + if ($startDate !== null) { + // ISO-Datum → UNIX-Timestamp + $ts = strtotime($startDate); + if ($ts !== false) { + $update['startDate'] = $ts; + } + } + + if (PHP_SAPI === 'cli') { + echo "---- FINAL UPDATE ----\n"; + var_dump($update); + echo "----------------------\n"; + } + + if (!$update) { + if (PHP_SAPI === 'cli') { + echo "ℹ️ Keine Felder zu aktualisieren\n"; + echo "INDEXPAGE HOOK END\n"; + echo "=============================\n"; + } + return; + } + + // -------------------------------------------------- + // 4. tl_search aktualisieren + // -------------------------------------------------- + Database::getInstance() + ->prepare( + 'UPDATE tl_search %s WHERE url=?' + ) + ->set($update) + ->execute($set['url']); + + if (PHP_SAPI === 'cli') { + echo "✅ tl_search aktualisiert\n"; echo "INDEXPAGE HOOK END\n"; echo "=============================\n"; } } - - private function extractMarkers(string $content): array - { - if (!preg_match('//s', $content, $m)) { - return []; - } - - $markers = []; - foreach (preg_split('/\R/', trim($m[1])) as $line) { - if (!str_contains($line, '=')) { - continue; - } - [$k, $v] = explode('=', $line, 2); - $markers[trim($k)] = trim($v); - } - - return $markers; - } } \ No newline at end of file