Enable in-place editing for page sections and media

This commit is contained in:
2026-11-21 21:06:34 +09:00
parent a910f03929
commit faba214971
11 changed files with 505 additions and 80 deletions
+29 -19
View File
@@ -61,30 +61,39 @@ final class Repository
return $this->one('SELECT * FROM sections WHERE page_slug = ? AND key = ?', [$page, $key]);
}
public function savePage(string $slug, array $data, int $version): void
public function savePage(string $slug, array $data, int $version, array $sections = []): void
{
$query = $this->pdo->prepare('UPDATE pages SET name = ?, title = ?, description = ?, heading = ?, accent = ?, lead = ?,
image = ?, alt = ?, actions = ?, quicklinks = ?, status = ?, updated_at = ?, version = version + 1 WHERE slug = ? AND version = ?');
$query->execute([
$data['name'], $data['title'], $data['description'], $data['heading'], $data['accent'], $data['lead'],
$data['image'], $data['alt'], Database::json($data['actions']), Database::json($data['quicklinks']),
$data['status'], self::now(), $slug, $version,
]);
self::checkVersion($query->rowCount());
Database::write(function (PDO $pdo) use ($slug, $data, $version, $sections): void {
$query = $pdo->prepare('UPDATE pages SET name = ?, title = ?, description = ?, heading = ?, accent = ?, lead = ?,
image = ?, alt = ?, actions = ?, quicklinks = ?, status = ?, updated_at = ?, version = version + 1 WHERE slug = ? AND version = ?');
$query->execute([
$data['name'], $data['title'], $data['description'], $data['heading'], $data['accent'], $data['lead'],
$data['image'], $data['alt'], Database::json($data['actions']), Database::json($data['quicklinks']),
$data['status'], self::now(), $slug, $version,
]);
self::checkVersion($query->rowCount());
foreach ($sections as $section) {
self::updateSection($pdo, $slug, $section['key'], $section, $section['version']);
}
});
}
public function saveSection(string $page, string $key, array $data, int $version): void
{
Database::write(function (PDO $pdo) use ($page, $key, $data, $version): void {
$query = $pdo->prepare('UPDATE sections SET title = ?, lead = ?, body = ?, style = ?, version = version + 1
WHERE page_slug = ? AND key = ? AND version = ?');
$query->execute([$data['title'], $data['lead'], $data['body'], $data['style'], $page, $key, $version]);
self::checkVersion($query->rowCount());
$query = $pdo->prepare('UPDATE pages SET updated_at = ? WHERE slug = ?');
$query->execute([self::now(), $page]);
self::updateSection($pdo, $page, $key, $data, $version);
self::touchPage($pdo, $page);
});
}
private static function updateSection(PDO $pdo, string $page, string $key, array $data, int $version): void
{
$query = $pdo->prepare('UPDATE sections SET title = ?, lead = ?, body = ?, style = ?, version = version + 1
WHERE page_slug = ? AND key = ? AND version = ?');
$query->execute([$data['title'], $data['lead'], $data['body'], $data['style'], $page, $key, $version]);
self::checkVersion($query->rowCount());
}
public function createSection(string $page, string $key, string $title, string $style, string $body): void
{
Database::write(function (PDO $pdo) use ($page, $key, $title, $style, $body): void {
@@ -94,7 +103,7 @@ final class Repository
if ($query->rowCount() !== 1) {
throw new RuntimeException('此区块锚点已被使用。');
}
self::touchPage($pdo, $page);
self::touchPage($pdo, $page, structureChanged: true);
});
}
@@ -105,7 +114,7 @@ final class Repository
$query = $pdo->prepare('DELETE FROM sections WHERE page_slug = ? AND key = ? AND version = ?');
$query->execute([$page, $key, $version]);
self::checkVersion($query->rowCount());
self::touchPage($pdo, $page);
self::touchPage($pdo, $page, structureChanged: true);
return;
}
@@ -129,9 +138,10 @@ final class Repository
});
}
private static function touchPage(PDO $pdo, string $slug): void
private static function touchPage(PDO $pdo, string $slug, bool $structureChanged = false): void
{
$pdo->prepare('UPDATE pages SET updated_at = ? WHERE slug = ?')->execute([self::now(), $slug]);
$revision = $structureChanged ? ', version = version + 1' : '';
$pdo->prepare('UPDATE pages SET updated_at = ?' . $revision . ' WHERE slug = ?')->execute([self::now(), $slug]);
}
public function articles(?string $category = null, ?string $tag = null, bool $public = true, int $limit = 30, int $offset = 0): array