This commit is contained in:
@@ -217,6 +217,136 @@ final class ListingCatalog
|
||||
return $stmt->fetchAll(\PDO::FETCH_ASSOC) ?: [];
|
||||
}
|
||||
|
||||
public function ensureCategoryForInput(string $input, string $group = 'general'): ?array
|
||||
{
|
||||
$this->ensureSchema();
|
||||
$input = trim($input);
|
||||
if ($input === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$slug = $this->slugify($input);
|
||||
$stmt = $this->pdo->prepare(
|
||||
'SELECT id, slug, title, category_group, sort_order
|
||||
FROM listing_categories
|
||||
WHERE slug = :slug OR LOWER(title) = LOWER(:title)
|
||||
ORDER BY sort_order ASC, id ASC
|
||||
LIMIT 1'
|
||||
);
|
||||
$stmt->execute([
|
||||
'slug' => $slug,
|
||||
'title' => $input,
|
||||
]);
|
||||
$existing = $stmt->fetch(\PDO::FETCH_ASSOC);
|
||||
if ($existing) {
|
||||
$existing['is_new'] = false;
|
||||
return $existing;
|
||||
}
|
||||
|
||||
$insert = $this->pdo->prepare(
|
||||
'INSERT INTO listing_categories (slug, title, category_group, sort_order)
|
||||
VALUES (:slug, :title, :groupName, 999)'
|
||||
);
|
||||
$insert->execute([
|
||||
'slug' => $slug,
|
||||
'title' => $input,
|
||||
'groupName' => in_array($group, ['general', 'event', 'place', 'food', 'family', 'partner'], true) ? $group : 'general',
|
||||
]);
|
||||
|
||||
return [
|
||||
'id' => (int)$this->pdo->lastInsertId(),
|
||||
'slug' => $slug,
|
||||
'title' => $input,
|
||||
'category_group' => $group,
|
||||
'sort_order' => 999,
|
||||
'is_new' => true,
|
||||
];
|
||||
}
|
||||
|
||||
public function listCategoryReviewItems(): array
|
||||
{
|
||||
$this->ensureSchema();
|
||||
$stmt = $this->pdo->query(
|
||||
'SELECT c.id, c.slug, c.title, c.category_group, c.sort_order,
|
||||
(SELECT COUNT(*) FROM listing_category_map m WHERE m.category_id = c.id) AS listing_count,
|
||||
(SELECT COUNT(*) FROM listing_places p WHERE p.place_kind = c.slug) AS place_count
|
||||
FROM listing_categories c
|
||||
ORDER BY
|
||||
CASE WHEN c.sort_order >= 900 THEN 0 ELSE 1 END,
|
||||
c.sort_order ASC,
|
||||
c.title ASC'
|
||||
);
|
||||
$rows = $stmt ? ($stmt->fetchAll(\PDO::FETCH_ASSOC) ?: []) : [];
|
||||
|
||||
$eventStmt = $this->pdo->query('SELECT category_slug, COUNT(*) AS total FROM events WHERE category_slug IS NOT NULL AND category_slug <> "" GROUP BY category_slug');
|
||||
$eventCounts = [];
|
||||
foreach (($eventStmt ? $eventStmt->fetchAll(\PDO::FETCH_ASSOC) : []) as $row) {
|
||||
$eventCounts[(string)$row['category_slug']] = (int)$row['total'];
|
||||
}
|
||||
|
||||
foreach ($rows as &$row) {
|
||||
$row['event_count'] = $eventCounts[(string)$row['slug']] ?? 0;
|
||||
$row['needs_review'] = ((int)($row['sort_order'] ?? 0)) >= 900;
|
||||
}
|
||||
unset($row);
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
public function mergeCategories(string $sourceSlug, string $targetSlug): void
|
||||
{
|
||||
$this->ensureSchema();
|
||||
$sourceSlug = trim($sourceSlug);
|
||||
$targetSlug = trim($targetSlug);
|
||||
if ($sourceSlug === '' || $targetSlug === '' || $sourceSlug === $targetSlug) {
|
||||
throw new \RuntimeException('Bitte zwei unterschiedliche Kategorien auswählen.');
|
||||
}
|
||||
|
||||
$lookup = $this->pdo->prepare('SELECT id, slug FROM listing_categories WHERE slug = :slug LIMIT 1');
|
||||
$lookup->execute(['slug' => $sourceSlug]);
|
||||
$source = $lookup->fetch(\PDO::FETCH_ASSOC);
|
||||
$lookup->execute(['slug' => $targetSlug]);
|
||||
$target = $lookup->fetch(\PDO::FETCH_ASSOC);
|
||||
if (!$source || !$target) {
|
||||
throw new \RuntimeException('Kategorie nicht gefunden.');
|
||||
}
|
||||
|
||||
$sourceId = (int)$source['id'];
|
||||
$targetId = (int)$target['id'];
|
||||
|
||||
$this->pdo->beginTransaction();
|
||||
try {
|
||||
$mapRows = $this->pdo->prepare('SELECT listing_id FROM listing_category_map WHERE category_id = :categoryId');
|
||||
$mapRows->execute(['categoryId' => $sourceId]);
|
||||
$listingIds = $mapRows->fetchAll(\PDO::FETCH_COLUMN) ?: [];
|
||||
$insertMap = $this->pdo->prepare(
|
||||
'INSERT IGNORE INTO listing_category_map (listing_id, category_id) VALUES (:listingId, :categoryId)'
|
||||
);
|
||||
foreach ($listingIds as $listingId) {
|
||||
$insertMap->execute([
|
||||
'listingId' => (int)$listingId,
|
||||
'categoryId' => $targetId,
|
||||
]);
|
||||
}
|
||||
$this->pdo->prepare('DELETE FROM listing_category_map WHERE category_id = :categoryId')->execute(['categoryId' => $sourceId]);
|
||||
$this->pdo->prepare('UPDATE listing_places SET place_kind = :target WHERE place_kind = :source')->execute([
|
||||
'target' => $targetSlug,
|
||||
'source' => $sourceSlug,
|
||||
]);
|
||||
$this->pdo->prepare('UPDATE events SET category_slug = :target WHERE category_slug = :source')->execute([
|
||||
'target' => $targetSlug,
|
||||
'source' => $sourceSlug,
|
||||
]);
|
||||
$this->pdo->prepare('DELETE FROM listing_categories WHERE id = :id')->execute(['id' => $sourceId]);
|
||||
$this->pdo->commit();
|
||||
} catch (\Throwable $e) {
|
||||
if ($this->pdo->inTransaction()) {
|
||||
$this->pdo->rollBack();
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
public function listDashboardEntries(int $userId): array
|
||||
{
|
||||
$this->ensureSchema();
|
||||
@@ -302,8 +432,8 @@ final class ListingCatalog
|
||||
$openingHoursNote = trim((string)($data['opening_hours_note'] ?? ''));
|
||||
$openingHoursRows = $this->normalizeOpeningHoursRows($data);
|
||||
$visibility = (string)($data['visibility'] ?? 'public');
|
||||
$placeKind = trim((string)($data['category_slug'] ?? ''));
|
||||
$customCategoryTitle = trim((string)($data['category_custom_title'] ?? ''));
|
||||
$categoryInput = trim((string)($data['category_input'] ?? $data['category_slug'] ?? ''));
|
||||
$placeKind = $categoryInput;
|
||||
$street = trim((string)($data['street'] ?? ''));
|
||||
$zip = trim((string)($data['zip'] ?? ''));
|
||||
$city = trim((string)($data['city'] ?? ''));
|
||||
@@ -332,10 +462,14 @@ final class ListingCatalog
|
||||
if ($entryType === 'editorial_event' && $recurrenceUntil === '') {
|
||||
throw new \RuntimeException('Bitte gib für Veranstaltungen ein Gültig-bis-Datum an.');
|
||||
}
|
||||
$startsAt = $this->normalizeDateForStorage($startsAt, false);
|
||||
$recurrenceUntil = $this->normalizeDateForStorage($recurrenceUntil, true);
|
||||
|
||||
$this->pdo->beginTransaction();
|
||||
try {
|
||||
[$placeKind, $categoryId] = $this->resolveCategory($placeKind, $customCategoryTitle);
|
||||
$category = $this->ensureCategoryForInput($categoryInput, $entryType === 'place' ? 'place' : 'event');
|
||||
$placeKind = (string)($category['slug'] ?? '');
|
||||
$categoryId = isset($category['id']) ? (int)$category['id'] : null;
|
||||
|
||||
if ($listingId !== null && $listingId > 0) {
|
||||
$existing = $this->getDashboardEntry($userId, $listingId);
|
||||
@@ -536,31 +670,6 @@ final class ListingCatalog
|
||||
return ['single', null];
|
||||
}
|
||||
|
||||
private function resolveCategory(string $slug, string $customTitle): array
|
||||
{
|
||||
$slug = trim($slug);
|
||||
$customTitle = trim($customTitle);
|
||||
if ($customTitle !== '') {
|
||||
$slug = $this->slugify($customTitle);
|
||||
$stmt = $this->pdo->prepare(
|
||||
'INSERT INTO listing_categories (slug, title, category_group, sort_order)
|
||||
VALUES (:slug, :title, "general", 999)
|
||||
ON DUPLICATE KEY UPDATE title = VALUES(title), updated_at = CURRENT_TIMESTAMP'
|
||||
);
|
||||
$stmt->execute([
|
||||
'slug' => $slug,
|
||||
'title' => $customTitle,
|
||||
]);
|
||||
}
|
||||
if ($slug === '') {
|
||||
return ['', null];
|
||||
}
|
||||
$stmt = $this->pdo->prepare('SELECT id FROM listing_categories WHERE slug = :slug LIMIT 1');
|
||||
$stmt->execute(['slug' => $slug]);
|
||||
$categoryId = $stmt->fetchColumn();
|
||||
return [$slug, $categoryId !== false ? (int)$categoryId : null];
|
||||
}
|
||||
|
||||
private function normalizeOpeningHoursRows(array $data): array
|
||||
{
|
||||
$labels = (array)($data['opening_day_label'] ?? []);
|
||||
@@ -586,32 +695,19 @@ final class ListingCatalog
|
||||
|
||||
private function normalizePriceRows(array $data): array
|
||||
{
|
||||
$labels = (array)($data['price_label'] ?? []);
|
||||
$audiences = (array)($data['price_audience'] ?? []);
|
||||
$types = (array)($data['price_type'] ?? []);
|
||||
$amounts = (array)($data['price_amount'] ?? []);
|
||||
$secondaryAmounts = (array)($data['price_amount_secondary'] ?? []);
|
||||
$ageFroms = (array)($data['price_age_from'] ?? []);
|
||||
$ageTos = (array)($data['price_age_to'] ?? []);
|
||||
$notes = (array)($data['price_note'] ?? []);
|
||||
$count = max(count($labels), count($audiences), count($types), count($amounts), count($secondaryAmounts), count($ageFroms), count($ageTos), count($notes));
|
||||
$count = max(count($amounts), count($ageFroms), count($ageTos));
|
||||
$rows = [];
|
||||
for ($index = 0; $index < $count; $index++) {
|
||||
$label = trim((string)($labels[$index] ?? ''));
|
||||
$audience = trim((string)($audiences[$index] ?? 'general'));
|
||||
$priceType = trim((string)($types[$index] ?? 'fixed'));
|
||||
$amount = trim((string)($amounts[$index] ?? ''));
|
||||
$secondary = trim((string)($secondaryAmounts[$index] ?? ''));
|
||||
$ageFrom = trim((string)($ageFroms[$index] ?? ''));
|
||||
$ageTo = trim((string)($ageTos[$index] ?? ''));
|
||||
$note = trim((string)($notes[$index] ?? ''));
|
||||
if ($label === '' && $amount === '' && $secondary === '' && $note === '' && $ageFrom === '' && $ageTo === '') {
|
||||
if ($amount === '' && $ageFrom === '' && $ageTo === '') {
|
||||
continue;
|
||||
}
|
||||
$noteParts = [];
|
||||
if ($note !== '') {
|
||||
$noteParts[] = $note;
|
||||
}
|
||||
if ($ageFrom !== '' || $ageTo !== '') {
|
||||
$ageText = [];
|
||||
if ($ageFrom !== '') {
|
||||
@@ -623,11 +719,11 @@ final class ListingCatalog
|
||||
$noteParts[] = implode(', ', $ageText);
|
||||
}
|
||||
$rows[] = [
|
||||
'label' => $label !== '' ? $label : 'Preis',
|
||||
'audience' => in_array($audience, ['general', 'adult', 'child', 'family', 'group'], true) ? $audience : 'general',
|
||||
'price_type' => in_array($priceType, ['free', 'fixed', 'from', 'up_to', 'range', 'request'], true) ? $priceType : 'fixed',
|
||||
'label' => 'Preis',
|
||||
'audience' => 'general',
|
||||
'price_type' => $amount === '' ? 'request' : 'fixed',
|
||||
'amount' => $amount !== '' ? (float)str_replace(',', '.', $amount) : null,
|
||||
'amount_secondary' => $secondary !== '' ? (float)str_replace(',', '.', $secondary) : null,
|
||||
'amount_secondary' => null,
|
||||
'note' => $noteParts !== [] ? implode(' | ', $noteParts) : null,
|
||||
];
|
||||
}
|
||||
@@ -648,6 +744,18 @@ final class ListingCatalog
|
||||
return is_array($decoded) ? $decoded : [];
|
||||
}
|
||||
|
||||
private function normalizeDateForStorage(string $value, bool $endOfDay): string
|
||||
{
|
||||
$value = trim($value);
|
||||
if ($value === '') {
|
||||
return '';
|
||||
}
|
||||
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $value) === 1) {
|
||||
return $value . ($endOfDay ? ' 23:59:59' : ' 00:00:00');
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
private function slugify(string $value): string
|
||||
{
|
||||
$value = mb_strtolower(trim($value));
|
||||
|
||||
Reference in New Issue
Block a user