Bugfix
This commit is contained in:
@@ -2,57 +2,37 @@
|
|||||||
|
|
||||||
namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
|
namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
|
||||||
|
|
||||||
|
use Contao\System;
|
||||||
use MummertMedia\ContaoMeilisearchBundle\Service\MeilisearchImageHelper;
|
use MummertMedia\ContaoMeilisearchBundle\Service\MeilisearchImageHelper;
|
||||||
|
|
||||||
class IndexPageListener
|
class IndexPageListener
|
||||||
{
|
{
|
||||||
public function __construct(
|
private ?MeilisearchImageHelper $imageHelper = null;
|
||||||
private readonly MeilisearchImageHelper $imageHelper,
|
|
||||||
) {}
|
private function getImageHelper(): MeilisearchImageHelper
|
||||||
|
{
|
||||||
|
if ($this->imageHelper === null) {
|
||||||
|
$this->imageHelper = System::getContainer()
|
||||||
|
->get(MeilisearchImageHelper::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->imageHelper;
|
||||||
|
}
|
||||||
|
|
||||||
public function onIndexPage(string $content, array &$data, array &$set): void
|
public function onIndexPage(string $content, array &$data, array &$set): void
|
||||||
{
|
{
|
||||||
$debug = (PHP_SAPI === 'cli');
|
|
||||||
|
|
||||||
if ($debug) {
|
|
||||||
fwrite(STDERR, "\n=============================\n");
|
|
||||||
fwrite(STDERR, "INDEXPAGE LISTENER\n");
|
|
||||||
fwrite(STDERR, "URL: " . ($set['url'] ?? $data['url'] ?? '[unknown]') . "\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Marker vorhanden?
|
// Marker vorhanden?
|
||||||
if (!str_contains($content, 'MEILISEARCH_JSON')) {
|
if (!str_contains($content, 'MEILISEARCH_JSON')) {
|
||||||
if ($debug) {
|
|
||||||
fwrite(STDERR, "❌ MEILISEARCH_JSON not found\n");
|
|
||||||
fwrite(STDERR, "=============================\n");
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($debug) {
|
|
||||||
fwrite(STDERR, "✔ MEILISEARCH_JSON marker found\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
// JSON aus Kommentar extrahieren + parsen
|
|
||||||
$parsed = $this->extractMeilisearchJson($content);
|
$parsed = $this->extractMeilisearchJson($content);
|
||||||
|
|
||||||
if ($parsed === null) {
|
if ($parsed === null) {
|
||||||
if ($debug) {
|
|
||||||
fwrite(STDERR, "❌ JSON could not be parsed\n");
|
|
||||||
fwrite(STDERR, "=============================\n");
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($debug) {
|
|
||||||
fwrite(STDERR, "✔ JSON parsed successfully:\n");
|
|
||||||
fwrite(STDERR, print_r($parsed, true));
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* =====================
|
|
||||||
* PRIORITY
|
* PRIORITY
|
||||||
* =====================
|
|
||||||
*/
|
*/
|
||||||
$priority =
|
$priority =
|
||||||
$parsed['event']['priority'] ?? null ??
|
$parsed['event']['priority'] ?? null ??
|
||||||
@@ -61,48 +41,32 @@ class IndexPageListener
|
|||||||
|
|
||||||
if ($priority !== null && $priority !== '') {
|
if ($priority !== null && $priority !== '') {
|
||||||
$set['priority'] = (int) $priority;
|
$set['priority'] = (int) $priority;
|
||||||
|
|
||||||
if ($debug) {
|
|
||||||
fwrite(STDERR, "✔ priority set to: {$set['priority']}\n");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* =====================
|
|
||||||
* KEYWORDS
|
* KEYWORDS
|
||||||
* =====================
|
|
||||||
*/
|
*/
|
||||||
$keywordSources = [
|
$kw = [];
|
||||||
|
foreach ([
|
||||||
$parsed['event']['keywords'] ?? null,
|
$parsed['event']['keywords'] ?? null,
|
||||||
$parsed['news']['keywords'] ?? null,
|
$parsed['news']['keywords'] ?? null,
|
||||||
$parsed['page']['keywords'] ?? null,
|
$parsed['page']['keywords'] ?? null,
|
||||||
];
|
] as $s) {
|
||||||
|
if (is_string($s)) {
|
||||||
$kw = [];
|
foreach (preg_split('/\s+/', trim($s)) as $p) {
|
||||||
foreach ($keywordSources as $s) {
|
|
||||||
if (!is_string($s) || trim($s) === '') {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (preg_split('/\s+/', trim($s)) ?: [] as $p) {
|
|
||||||
if ($p !== '') {
|
if ($p !== '') {
|
||||||
$kw[] = $p;
|
$kw[] = $p;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ($kw) {
|
if ($kw) {
|
||||||
$set['keywords'] = implode(' ', array_unique($kw));
|
$set['keywords'] = implode(' ', array_unique($kw));
|
||||||
|
|
||||||
if ($debug) {
|
|
||||||
fwrite(STDERR, "✔ keywords set to: {$set['keywords']}\n");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* =====================
|
|
||||||
* IMAGEPATH
|
* IMAGEPATH
|
||||||
* =====================
|
|
||||||
*/
|
*/
|
||||||
$image =
|
$image =
|
||||||
$parsed['event']['searchimage'] ?? null ??
|
$parsed['event']['searchimage'] ?? null ??
|
||||||
@@ -110,71 +74,25 @@ class IndexPageListener
|
|||||||
$parsed['page']['searchimage'] ?? null ??
|
$parsed['page']['searchimage'] ?? null ??
|
||||||
$parsed['custom']['searchimage'] ?? null;
|
$parsed['custom']['searchimage'] ?? null;
|
||||||
|
|
||||||
if ($debug) {
|
|
||||||
fwrite(STDERR, "Resolved image UUID: " . var_export($image, true) . "\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (is_string($image) && $image !== '') {
|
if (is_string($image) && $image !== '') {
|
||||||
if ($debug) {
|
$path = $this->getImageHelper()->getImagePathFromUuid($image);
|
||||||
fwrite(STDERR, "→ Calling MeilisearchImageHelper\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
$path = $this->imageHelper->getImagePathFromUuid($image);
|
|
||||||
|
|
||||||
if ($debug) {
|
|
||||||
fwrite(STDERR, "← Image helper returned: " . var_export($path, true) . "\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($path !== null) {
|
if ($path !== null) {
|
||||||
$set['imagepath'] = $path;
|
$set['imagepath'] = $path;
|
||||||
|
|
||||||
if ($debug) {
|
|
||||||
fwrite(STDERR, "✔ imagepath set to: {$set['imagepath']}\n");
|
|
||||||
}
|
|
||||||
} elseif ($debug) {
|
|
||||||
fwrite(STDERR, "❌ image helper returned NULL\n");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* =====================
|
|
||||||
* STARTDATE
|
* STARTDATE
|
||||||
* =====================
|
|
||||||
*/
|
*/
|
||||||
$date =
|
$date =
|
||||||
$parsed['event']['date'] ?? null ??
|
$parsed['event']['date'] ?? null ??
|
||||||
$parsed['news']['date'] ?? null;
|
$parsed['news']['date'] ?? null;
|
||||||
|
|
||||||
if ($debug) {
|
if (is_string($date)) {
|
||||||
fwrite(STDERR, "Resolved date: " . var_export($date, true) . "\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (is_string($date) && $date !== '') {
|
|
||||||
$ts = strtotime($date);
|
$ts = strtotime($date);
|
||||||
|
|
||||||
if ($ts !== false) {
|
if ($ts !== false) {
|
||||||
$set['startDate'] = $ts;
|
$set['startDate'] = $ts;
|
||||||
|
|
||||||
if ($debug) {
|
|
||||||
fwrite(STDERR, "✔ startDate set to timestamp: {$set['startDate']}\n");
|
|
||||||
}
|
}
|
||||||
} elseif ($debug) {
|
|
||||||
fwrite(STDERR, "❌ strtotime failed\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($debug) {
|
|
||||||
fwrite(STDERR, "---- FINAL \$set ----\n");
|
|
||||||
fwrite(
|
|
||||||
STDERR,
|
|
||||||
print_r([
|
|
||||||
'priority' => $set['priority'] ?? null,
|
|
||||||
'keywords' => $set['keywords'] ?? null,
|
|
||||||
'imagepath' => $set['imagepath'] ?? null,
|
|
||||||
'startDate' => $set['startDate'] ?? null,
|
|
||||||
], true)
|
|
||||||
);
|
|
||||||
fwrite(STDERR, "=============================\n");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user