Build a full-width CMS canvas and restore featured home products
This commit is contained in:
+31
-16
@@ -65,15 +65,19 @@ final class Repository
|
||||
{
|
||||
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 = ?');
|
||||
image = ?, alt = ?, image_scale = ?, 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['image'], $data['alt'], $data['image_scale'], 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']);
|
||||
if ($section['version'] === null) {
|
||||
self::insertSection($pdo, $slug, $section['key'], $section);
|
||||
} else {
|
||||
self::updateSection($pdo, $slug, $section['key'], $section, $section['version']);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -97,19 +101,28 @@ final class Repository
|
||||
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 {
|
||||
$query = $pdo->prepare('INSERT OR IGNORE INTO sections (page_slug, key, title, lead, style, body, position)
|
||||
SELECT ?, ?, ?, ?, ?, ?, COALESCE(MAX(position), -1) + 1 FROM sections WHERE page_slug = ?');
|
||||
$query->execute([$page, $key, $title, '', $style, $body, $page]);
|
||||
if ($query->rowCount() !== 1) {
|
||||
throw new RuntimeException('此区块锚点已被使用。');
|
||||
}
|
||||
self::insertSection($pdo, $page, $key, compact('title', 'style', 'body') + ['lead' => '']);
|
||||
self::touchPage($pdo, $page, structureChanged: true);
|
||||
});
|
||||
}
|
||||
|
||||
public function arrangeSection(string $page, string $key, string $operation, int $version): void
|
||||
private static function insertSection(PDO $pdo, string $page, string $key, array $data): void
|
||||
{
|
||||
Database::write(function (PDO $pdo) use ($page, $key, $operation, $version): void {
|
||||
$query = $pdo->prepare('INSERT OR IGNORE INTO sections (page_slug, key, title, lead, style, body, position)
|
||||
SELECT ?, ?, ?, ?, ?, ?, COALESCE(MAX(position), -1) + 1 FROM sections WHERE page_slug = ?');
|
||||
$query->execute([$page, $key, $data['title'], $data['lead'], $data['style'], $data['body'], $page]);
|
||||
if ($query->rowCount() !== 1) {
|
||||
throw new RuntimeException('此区块锚点已被使用。');
|
||||
}
|
||||
}
|
||||
|
||||
public function arrangeSection(string $page, string $key, string $operation, int $version, int $pageVersion): void
|
||||
{
|
||||
Database::write(function (PDO $pdo) use ($page, $key, $operation, $version, $pageVersion): void {
|
||||
$current = $pdo->prepare('SELECT version FROM pages WHERE slug = ?');
|
||||
$current->execute([$page]);
|
||||
$revision = $current->fetchColumn();
|
||||
self::checkVersion($revision !== false && (int) $revision === $pageVersion ? 1 : 0);
|
||||
if ($operation === 'delete') {
|
||||
$query = $pdo->prepare('DELETE FROM sections WHERE page_slug = ? AND key = ? AND version = ?');
|
||||
$query->execute([$page, $key, $version]);
|
||||
@@ -118,13 +131,15 @@ final class Repository
|
||||
|
||||
return;
|
||||
}
|
||||
$query = $pdo->prepare('SELECT key FROM sections WHERE page_slug = ? ORDER BY position, key');
|
||||
$query = $pdo->prepare('SELECT key, version FROM sections WHERE page_slug = ? ORDER BY position, key');
|
||||
$query->execute([$page]);
|
||||
$keys = $query->fetchAll(PDO::FETCH_COLUMN);
|
||||
$index = array_search($key, $keys, true);
|
||||
if ($index === false) {
|
||||
$revisions = $query->fetchAll(PDO::FETCH_KEY_PAIR);
|
||||
if (!array_key_exists($key, $revisions)) {
|
||||
throw new RuntimeException('找不到该区块。');
|
||||
}
|
||||
self::checkVersion((int) $revisions[$key] === $version ? 1 : 0);
|
||||
$keys = array_map('strval', array_keys($revisions));
|
||||
$index = array_search($key, $keys, true);
|
||||
$other = $index + ($operation === 'up' ? -1 : 1);
|
||||
if (!isset($keys[$other])) {
|
||||
return;
|
||||
@@ -134,7 +149,7 @@ final class Repository
|
||||
foreach ($keys as $position => $section) {
|
||||
$query->execute([$position, $page, $section]);
|
||||
}
|
||||
self::touchPage($pdo, $page);
|
||||
self::touchPage($pdo, $page, structureChanged: true);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user