forked from laysense/laysite
Prepare LaySite 1.5.1 page routing and template updates
This commit is contained in:
+43
-11
@@ -30,15 +30,26 @@ final class Repository
|
||||
|
||||
public function pages(bool $publishedOnly = false): array
|
||||
{
|
||||
$where = $publishedOnly ? " WHERE pages.status = 'published' AND pages.layout_id IS NULL AND pages.page_type = 'page'" : '';
|
||||
$where = $publishedOnly ? " WHERE pages.status = 'published' AND pages.layout_id IS NULL AND pages.page_type IN ('page', 'home')" : '';
|
||||
|
||||
return $this->pdo->query('SELECT pages.slug, pages.name, pages.title, pages.updated_at, pages.status, pages.version, pages.page_type, d.kind AS layout_kind
|
||||
return $this->pdo->query('SELECT pages.slug, pages.name, pages.title, pages.updated_at, pages.status, pages.version, pages.page_type, pages.layout_id, 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 homepage(): ?array
|
||||
{
|
||||
$result = $this->one("SELECT slug FROM pages WHERE page_type = 'home'
|
||||
AND layout_id IS NULL AND status = 'published' ORDER BY RANDOM() LIMIT 1", []);
|
||||
|
||||
return $result ? $this->page($result['slug']) : null;
|
||||
}
|
||||
|
||||
public function createPage(string $slug, string $name, array $template = [], string $type = 'page'): void
|
||||
{
|
||||
Database::write(static fn (PDO $pdo) => self::insertPage($pdo, $slug, $name, $template, type: $type));
|
||||
Database::write(static function (PDO $pdo) use ($slug, $name, $template, $type): void {
|
||||
$layout = in_array($type, ['header', 'footer'], true) ? LayoutPage::insertPreset($pdo, $type, $name) : null;
|
||||
self::insertPage($pdo, $slug, $name, $template, $layout, $layout === null ? $type : 'page');
|
||||
});
|
||||
}
|
||||
|
||||
public static function insertPage(PDO $pdo, string $slug, string $name, array $template = [], ?int $layoutId = null, string $type = 'page'): void
|
||||
@@ -98,7 +109,7 @@ final class Repository
|
||||
$catalog = $feed['mode'] === 'catalog';
|
||||
$grouped = $network || ($feed['mode'] === 'tabs' && count($feed['categories']) > 1);
|
||||
$archivePath = '/articles/feed/' . $page['slug'] . '/' . $section['key'];
|
||||
$section['archivePath'] = $feed['categories'] && empty($page['layout_kind']) && ($page['page_type'] ?? 'page') === 'page' && $page['status'] === 'published' ? $archivePath : null;
|
||||
$section['archivePath'] = $feed['categories'] && empty($page['layout_kind']) && in_array($page['page_type'] ?? 'page', ['page', 'home'], true) && $page['status'] === 'published' ? $archivePath : null;
|
||||
$section['articleFeed'] = [];
|
||||
$section['networkFeed'] = $network;
|
||||
$section['catalogFeed'] = $catalog;
|
||||
@@ -125,29 +136,50 @@ 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 pages.layout_id IS NULL AND pages.page_type = 'page'
|
||||
WHERE pages.slug = ? AND pages.status = 'published' AND pages.layout_id IS NULL AND pages.page_type IN ('page', 'home')
|
||||
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): array
|
||||
{
|
||||
return Database::write(function (PDO $pdo) use ($slug, $data, $version, $sections, $replaceDraftSections, $sectionOrder): array {
|
||||
$existing = $this->one('SELECT pages.version, pages.page_type, pages.layout_id, d.kind AS layout_kind
|
||||
FROM pages LEFT JOIN design_presets d ON pages.layout_id = d.id WHERE pages.slug = ?', [$slug]);
|
||||
self::checkVersion($existing !== null && $existing['version'] === $version ? 1 : 0);
|
||||
$target = $data['slug'] ?? $slug;
|
||||
if ($target !== $slug && $this->one('SELECT 1 FROM pages WHERE slug = ?', [$target])) {
|
||||
throw new RuntimeException('此页面地址已存在。');
|
||||
}
|
||||
$previousType = $existing['layout_kind'] ?? $existing['page_type'];
|
||||
$type = $data['type'] ?? $previousType;
|
||||
$layout = $existing['layout_id'];
|
||||
if ($type !== $previousType) {
|
||||
$layout = in_array($type, ['header', 'footer'], true) ? LayoutPage::insertPreset($pdo, $type, $data['name']) : null;
|
||||
}
|
||||
$head = $data['head_html'] ?? null;
|
||||
$query = $pdo->prepare('UPDATE pages SET name = ?, title = ?, description = ?, heading = ?, accent = ?, lead = ?,
|
||||
$query = $pdo->prepare('UPDATE pages SET slug = ?, page_type = ?, layout_id = ?, name = ?, title = ?, description = ?, heading = ?, accent = ?, lead = ?,
|
||||
image = ?, alt = ?, image_scale = ?, alignments = ?, actions = ?, quicklinks = ?, visibility = ?, header_id = ?, footer_id = ?, palette_id = ?, status = ?, updated_at = ?,
|
||||
presentation = COALESCE(?, presentation), ' . ($head === null ? '' : 'head_html = ?, ')
|
||||
. 'version = version + 1 WHERE slug = ? AND version = ?');
|
||||
$query->execute([
|
||||
$data['name'], $data['title'], $data['description'], $data['heading'], $data['accent'], $data['lead'],
|
||||
$target, $layout === null ? $type : 'page', $layout, $data['name'], $data['title'], $data['description'], $data['heading'], $data['accent'], $data['lead'],
|
||||
$data['image'], $data['alt'], $data['image_scale'], Database::json($data['alignments']), Database::json($data['actions']), Database::json($data['quicklinks']),
|
||||
Database::json($data['visibility']), $data['header_id'], $data['footer_id'], $data['palette_id'], $data['status'], self::now(),
|
||||
isset($data['presentation']) ? CanvasStyle::encode($data['presentation']) : null,
|
||||
...($head === null ? [] : [$head]), $slug, $version,
|
||||
]);
|
||||
self::checkVersion($query->rowCount());
|
||||
if ($data['status'] === 'draft' && str_starts_with($slug, 'system--')) {
|
||||
SiteDesign::unsetSystemPage($pdo, $slug);
|
||||
if (isset(SystemPage::TYPES[$previousType])) {
|
||||
if ($data['status'] === 'draft' || $previousType !== $type) {
|
||||
SiteDesign::replaceSystemPage($pdo, $slug);
|
||||
} elseif ($target !== $slug) {
|
||||
SiteDesign::replaceSystemPage($pdo, $slug, replacement: $target);
|
||||
}
|
||||
}
|
||||
if ($existing['layout_id'] !== null && $layout !== $existing['layout_id']) {
|
||||
LayoutPage::deletePreset($pdo, $existing['layout_kind'], $existing['layout_id']);
|
||||
}
|
||||
$slug = $target;
|
||||
if ($replaceDraftSections) {
|
||||
$pdo->prepare('DELETE FROM sections WHERE page_slug = ?')->execute([$slug]);
|
||||
}
|
||||
@@ -164,7 +196,7 @@ final class Repository
|
||||
self::writeSectionOrder($pdo, $slug, $sectionOrder);
|
||||
}
|
||||
$page = $this->page($slug);
|
||||
if (isset($data['layoutConfig'])) {
|
||||
if ($type === $previousType && $layout !== null && isset($data['layoutConfig'])) {
|
||||
LayoutPage::saveSettings($pdo, $page, $data['layoutConfig']);
|
||||
}
|
||||
|
||||
@@ -186,7 +218,7 @@ final class Repository
|
||||
if ($page['layout_id'] !== null) {
|
||||
LayoutPage::deletePreset($pdo, $page['layout_kind'], $page['layout_id']);
|
||||
}
|
||||
SiteDesign::unsetSystemPage($pdo, $slug);
|
||||
SiteDesign::replaceSystemPage($pdo, $slug);
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user