75 lines
2.2 KiB
PHP
75 lines
2.2 KiB
PHP
<?php
|
|
|
|
if (!function_exists('websiteContentEnsureEntry')) {
|
|
function websiteContentEnsureEntry(PDO $pdo, array $entry): int
|
|
{
|
|
$title = trim((string)($entry['webseitentitel'] ?? ''));
|
|
if ($title === '') {
|
|
throw new InvalidArgumentException('webseitentitel is required');
|
|
}
|
|
|
|
$stmt = $pdo->prepare("
|
|
SELECT inhaltid
|
|
FROM webseiteninhalt
|
|
WHERE webseitentitel = :webseitentitel
|
|
ORDER BY inhaltid DESC
|
|
LIMIT 1
|
|
");
|
|
$stmt->execute(['webseitentitel' => $title]);
|
|
$existingId = (int)($stmt->fetchColumn() ?: 0);
|
|
|
|
if ($existingId > 0) {
|
|
return $existingId;
|
|
}
|
|
|
|
$insert = $pdo->prepare("
|
|
INSERT INTO webseiteninhalt (webseitentitel, inhalt, beschreibung, url)
|
|
VALUES (:webseitentitel, :inhalt, :beschreibung, :url)
|
|
");
|
|
$insert->execute([
|
|
'webseitentitel' => $title,
|
|
'inhalt' => (string)($entry['inhalt'] ?? ''),
|
|
'beschreibung' => (string)($entry['beschreibung'] ?? ''),
|
|
'url' => (string)($entry['url'] ?? ''),
|
|
]);
|
|
|
|
return (int)$pdo->lastInsertId();
|
|
}
|
|
}
|
|
|
|
if (!function_exists('websiteContentEnsureEntries')) {
|
|
function websiteContentEnsureEntries(PDO $pdo, array $entries): void
|
|
{
|
|
foreach ($entries as $entry) {
|
|
websiteContentEnsureEntry($pdo, $entry);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!function_exists('websiteContentGetByTitle')) {
|
|
function websiteContentGetByTitle(PDO $pdo, string $title, string $fallback = ''): array
|
|
{
|
|
$stmt = $pdo->prepare("
|
|
SELECT inhaltid, webseitentitel, inhalt, beschreibung, url
|
|
FROM webseiteninhalt
|
|
WHERE webseitentitel = :webseitentitel
|
|
ORDER BY inhaltid DESC
|
|
LIMIT 1
|
|
");
|
|
$stmt->execute(['webseitentitel' => $title]);
|
|
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if ($row) {
|
|
return $row;
|
|
}
|
|
|
|
return [
|
|
'inhaltid' => 0,
|
|
'webseitentitel' => $title,
|
|
'inhalt' => $fallback,
|
|
'beschreibung' => '',
|
|
'url' => '',
|
|
];
|
|
}
|
|
}
|