Edit shared headers and footers as page types
This commit is contained in:
+68
-21
@@ -30,35 +30,41 @@ final class Repository
|
||||
|
||||
public function pages(bool $publishedOnly = false): array
|
||||
{
|
||||
$where = $publishedOnly ? " WHERE status = 'published'" : '';
|
||||
$where = $publishedOnly ? " WHERE pages.status = 'published' AND pages.layout_id IS NULL" : '';
|
||||
|
||||
return $this->pdo->query('SELECT slug, name, title, updated_at, status, version FROM pages' . $where . ' ORDER BY rowid')->fetchAll();
|
||||
return $this->pdo->query('SELECT pages.slug, pages.name, pages.title, pages.updated_at, pages.status, pages.version, d.kind AS layout_kind
|
||||
FROM pages LEFT JOIN design_presets d ON pages.layout_id = d.id' . $where . ' ORDER BY pages.rowid')->fetchAll();
|
||||
}
|
||||
|
||||
public function createPage(string $slug, string $name, array $template = []): void
|
||||
{
|
||||
Database::write(function (PDO $pdo) use ($slug, $name, $template): void {
|
||||
$query = $pdo->prepare("INSERT OR IGNORE INTO pages (slug, name, title, description, heading, accent,
|
||||
lead, image, alt, actions, quicklinks, theme, visibility, header_id, footer_id, palette_id, status, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'draft', ?)");
|
||||
$query->execute([
|
||||
$slug, $name, $template['title'] ?? $name, $template['description'] ?? '', $template['heading'] ?? $name,
|
||||
$template['accent'] ?? '', $template['lead'] ?? '', $template['image'] ?? '', $template['alt'] ?? '',
|
||||
Database::json($template['actions'] ?? []), Database::json($template['quicklinks'] ?? []),
|
||||
$template['theme'] ?? '', Database::json($template['visibility'] ?? []), $template['header_id'] ?? null, $template['footer_id'] ?? null, $template['palette_id'] ?? null, self::now(),
|
||||
]);
|
||||
if ($query->rowCount() !== 1) {
|
||||
throw new RuntimeException('此页面地址已存在。');
|
||||
}
|
||||
foreach ($template['sections'] ?? [] as $section) {
|
||||
self::insertSection($pdo, $slug, $section['key'], $section);
|
||||
}
|
||||
});
|
||||
Database::write(static fn (PDO $pdo) => self::insertPage($pdo, $slug, $name, $template));
|
||||
}
|
||||
|
||||
public static function insertPage(PDO $pdo, string $slug, string $name, array $template = [], ?int $layoutId = null): void
|
||||
{
|
||||
$query = $pdo->prepare("INSERT OR IGNORE INTO pages (slug, name, title, description, heading, accent,
|
||||
lead, image, alt, actions, quicklinks, theme, visibility, header_id, footer_id, palette_id, layout_id, status, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
||||
$query->execute([
|
||||
$slug, $name, $template['title'] ?? $name, $template['description'] ?? '', $template['heading'] ?? $name,
|
||||
$template['accent'] ?? '', $template['lead'] ?? '', $template['image'] ?? '', $template['alt'] ?? '',
|
||||
Database::json($template['actions'] ?? []), Database::json($template['quicklinks'] ?? []),
|
||||
$template['theme'] ?? '', Database::json($template['visibility'] ?? []), $template['header_id'] ?? null,
|
||||
$template['footer_id'] ?? null, $template['palette_id'] ?? null, $layoutId, $layoutId === null ? 'draft' : 'published', self::now(),
|
||||
]);
|
||||
if ($query->rowCount() !== 1) {
|
||||
throw new RuntimeException('此页面地址已存在。');
|
||||
}
|
||||
foreach ($template['sections'] ?? [] as $section) {
|
||||
self::insertSection($pdo, $slug, $section['key'], $section);
|
||||
}
|
||||
}
|
||||
|
||||
public function page(string $slug): ?array
|
||||
{
|
||||
$page = $this->one('SELECT * FROM pages WHERE slug = ?', [$slug]);
|
||||
$page = $this->one('SELECT pages.*, d.kind AS layout_kind FROM pages
|
||||
LEFT JOIN design_presets d ON pages.layout_id = d.id WHERE pages.slug = ?', [$slug]);
|
||||
if (!$page) {
|
||||
return null;
|
||||
}
|
||||
@@ -72,6 +78,44 @@ final class Repository
|
||||
return $page;
|
||||
}
|
||||
|
||||
public function layoutPage(int $id): ?array
|
||||
{
|
||||
$row = $this->one('SELECT slug FROM pages WHERE layout_id = ?', [$id]);
|
||||
|
||||
return $row ? $this->page($row['slug']) : null;
|
||||
}
|
||||
|
||||
public function withSectionFeeds(array $page): array
|
||||
{
|
||||
$categories = null;
|
||||
foreach ($page['sections'] as &$section) {
|
||||
if (!$section['feed']) {
|
||||
continue;
|
||||
}
|
||||
$categories ??= $this->categories();
|
||||
$feed = json_decode($section['feed'], true, flags: JSON_THROW_ON_ERROR);
|
||||
$network = $feed['mode'] === 'network';
|
||||
$catalog = $feed['mode'] === 'catalog';
|
||||
$grouped = $network || ($feed['mode'] === 'tabs' && count($feed['categories']) > 1);
|
||||
$archivePath = '/articles/feed/' . $page['slug'] . '/' . $section['key'];
|
||||
$section['archivePath'] = empty($page['layout_kind']) && $page['status'] === 'published' ? $archivePath : null;
|
||||
$section['articleFeed'] = [];
|
||||
$section['networkFeed'] = $network;
|
||||
$section['catalogFeed'] = $catalog;
|
||||
$section['feedLimit'] = $feed['limit'];
|
||||
foreach ($grouped ? $feed['categories'] : [''] as $category) {
|
||||
$selected = $category === '' ? $feed['categories'] : [$category];
|
||||
$section['articleFeed'][] = [
|
||||
'categories' => $selected, 'label' => $categories[$category] ?? '',
|
||||
'articles' => $this->articles($selected, limit: $feed['limit'], directoryOrder: $network || $catalog),
|
||||
];
|
||||
}
|
||||
}
|
||||
unset($section);
|
||||
|
||||
return $page;
|
||||
}
|
||||
|
||||
public function section(string $page, string $key): ?array
|
||||
{
|
||||
return $this->one('SELECT * FROM sections WHERE page_slug = ? AND key = ?', [$page, $key]);
|
||||
@@ -81,7 +125,7 @@ final class Repository
|
||||
{
|
||||
return $this->one("SELECT sections.title, sections.feed FROM sections
|
||||
JOIN pages ON pages.slug = sections.page_slug
|
||||
WHERE pages.slug = ? AND pages.status = 'published' AND sections.key = ? AND sections.feed IS NOT NULL", [$page, $key]);
|
||||
WHERE pages.slug = ? AND pages.status = 'published' AND pages.layout_id IS NULL AND sections.key = ? AND sections.feed IS NOT NULL", [$page, $key]);
|
||||
}
|
||||
|
||||
public function savePage(string $slug, array $data, int $version, array $sections = [], bool $replaceDraftSections = false, ?array $sectionOrder = null): void
|
||||
@@ -111,6 +155,9 @@ final class Repository
|
||||
if ($sectionOrder !== null) {
|
||||
self::writeSectionOrder($pdo, $slug, $sectionOrder);
|
||||
}
|
||||
if (isset($data['layoutConfig'])) {
|
||||
LayoutPage::saveSettings($pdo, $this->page($slug), $data['layoutConfig']);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user