Edit shared headers and footers as page types

This commit is contained in:
2026-11-23 20:58:42 +08:00
parent 636788b65c
commit 9d386dd39c
40 changed files with 725 additions and 937 deletions
+35 -4
View File
@@ -10,6 +10,7 @@ use App\Content\CanvasStyle;
use App\Content\DesignLibrary;
use App\Content\Database;
use App\Content\Html;
use App\Content\LayoutPage;
use App\Content\Passwords;
use App\Content\Repository;
use App\Content\SiteDesign;
@@ -251,6 +252,10 @@ class AdminController
if (!$page) {
return $this->error($request, '找不到此页面。', 404);
}
$site = $repo->site();
$layoutKind = $page['layout_kind'];
$layout = $layoutKind === null ? null : DesignLibrary::find($page['layout_id'], $layoutKind);
$default = $layout && ($site[$layoutKind . '_id'] ?? null) === $layout['id'];
$error = '';
if ($request->method() === 'POST') {
$data = [];
@@ -258,7 +263,10 @@ class AdminController
$data[$field] = $this->input($request, $field);
}
$data['name'] = $request->post('name') === null ? $page['name'] : $this->input($request, 'name');
$data['status'] = $request->post('status') === null || $slug === 'home' ? $page['status'] : $this->input($request, 'status');
if ($layout) {
$data['title'] = $data['name'];
}
$data['status'] = $layout || $request->post('status') === null || $slug === 'home' ? $page['status'] : $this->input($request, 'status');
$scale = $this->input($request, 'image_scale');
$data['image_scale'] = $request->post('image_scale') === null ? (int) $page['image_scale']
: (ctype_digit($scale) ? (int) $scale : -1);
@@ -282,6 +290,28 @@ class AdminController
}
$data['visibility'][$area] = $value === '1';
}
if ($layout) {
$default = $request->post('make_default') === '1';
$provider = null;
if ($layoutKind === 'header' && $request->post('provider_mode') !== null) {
foreach (DesignLibrary::PROVIDER_FIELDS as $name) {
$provider[$name] = $this->input($request, 'provider_' . $name);
}
}
$data['layoutConfig'] = ['default' => $default, 'provider' => $provider, 'secret' => $this->input($request, 'provider_secret', trimValue: false)];
} else {
foreach (['header', 'footer'] as $area) {
$choice = $request->post($area . '_choice');
if ($choice === null) {
continue;
}
$data['visibility'][$area] = $choice !== 'hidden';
if ($choice !== 'hidden') {
$data[$area . '_id'] = DesignLibrary::selection($area, $choice);
}
}
$page = array_replace($page, array_intersect_key($data, ['header_id' => true, 'footer_id' => true]));
}
$page['visibility'] = $data['visibility'];
$data['alignments'] = $request->post('alignments') === null ? $page['alignments']
: $this->alignmentMap(json_decode($this->input($request, 'alignments'), true), ['heading', 'lead', 'actions', 'image']);
@@ -292,7 +322,7 @@ class AdminController
$data['quicklinks'] = $request->post('quicklinks') === null ? $page['quicklinks'] : SiteDesign::parseLinks($this->input($request, 'quicklinks'));
if ($data['name'] === '' || mb_strlen($data['name']) > 100 || !in_array($data['status'], ['draft', 'published'], true)
|| $data['title'] === '' || mb_strlen($data['title']) > 160 ||
($data['status'] === 'published' && $data['description'] === '') || mb_strlen($data['description']) > 300 ||
(!$layout && $data['status'] === 'published' && $data['description'] === '') || mb_strlen($data['description']) > 300 ||
(($data['visibility']['hero'] ?? true) && $data['heading'] === '') || mb_strlen($data['heading'] . $data['accent']) > 110 ||
mb_strlen($data['lead']) > 600 || mb_strlen($data['alt']) > 200 ||
($data['image_scale'] !== 0 && ($data['image_scale'] < 10 || $data['image_scale'] > 100)) ||
@@ -318,8 +348,9 @@ class AdminController
return $this->render($request, 'admin-page', [
'title' => '编辑 ' . $page['name'], 'page' => $page, 'categories' => $repo->categories(), 'wideAdmin' => true, 'error' => $error, 'saved' => $request->get('saved') === '1',
'pageTemplateDrawer' => true, 'designs' => DesignLibrary::all(),
'layoutDefaults' => array_intersect_key($repo->site(), ['header_id' => true, 'footer_id' => true]),
'pageTemplateDrawer' => !$layout, 'designs' => DesignLibrary::all(), 'layout' => $layout, 'layoutDefault' => $default,
'layoutHasMember' => $layoutKind === 'header' && LayoutPage::hasMember($page['sections']),
'layoutDefaults' => array_intersect_key($site, ['header_id' => true, 'footer_id' => true]),
], $error !== '' ? 422 : 200);
}
+30 -69
View File
@@ -22,7 +22,7 @@ final class DesignController extends AdminController
}
return $this->render($request, 'admin-design', [
'title' => '站点设计', 'items' => DesignLibrary::all(), 'site' => (new Repository())->site(),
'title' => '站点设计', 'items' => DesignLibrary::all('palette'),
]);
}
@@ -31,84 +31,42 @@ final class DesignController extends AdminController
if ($failure = $this->guard($request)) {
return $failure;
}
if ($kind !== 'palette') {
if ($key === 'new') {
return View::redirect('/admin/new-page?type=' . $kind);
}
$page = (new Repository())->layoutPage((int) $key);
return $page && $page['layout_kind'] === $kind
? View::redirect('/admin/pages/' . $page['slug']) : $this->error($request, '找不到这项设计配置。', 404);
}
$id = $key === 'new' ? null : (int) $key;
$item = $id === null ? [
'name' => '', 'preset' => array_key_first(DesignLibrary::PRESETS[$kind]),
'payload' => DesignLibrary::defaults($kind), 'version' => 0,
] : DesignLibrary::find($id, $kind);
'name' => '', 'preset' => 'custom', 'payload' => DesignLibrary::defaults('palette'), 'version' => 0,
] : DesignLibrary::find($id, 'palette');
if (!$item) {
return $this->error($request, '找不到这项设计配置。', 404);
return $this->error($request, '找不到这项配色方案。', 404);
}
$error = '';
$field = '';
$secret = '';
$links = SiteDesign::linkText($item['payload']['links'] ?? []);
$site = (new Repository())->site();
$default = $id === null ? $request->get('default') === '1' : ($site[$kind . '_id'] ?? null) === $id;
if ($request->method() === 'POST') {
$item['name'] = $this->input($request, 'name');
$item['version'] = $this->version($request);
foreach (array_keys(DesignLibrary::COLORS) as $color) {
$item['payload'][$color] = $this->input($request, $color);
}
try {
$savedProvider = $item['payload']['provider'] ?? [];
$item['name'] = $this->input($request, 'name');
$item['preset'] = $this->input($request, 'preset');
$item['version'] = $this->version($request);
$default = $request->post('make_default') === '1';
if ($kind === 'palette') {
foreach (DesignLibrary::COLORS as $color => $definition) {
$item['payload'][$color] = $this->input($request, $color);
}
} else {
$url = $this->input($request, 'action_url');
$label = $this->input($request, 'action_label');
$links = $this->input($request, 'links', trimValue: false);
$item['payload'] = [
'components' => $request->post('components', []),
'brand' => $this->input($request, 'brand'), 'logo' => $this->input($request, 'logo'),
'text' => $this->input($request, 'text'), 'legal' => $this->input($request, 'legal'),
'action' => $url === '' && $label === '' ? [] : [[$url, $label, $this->input($request, 'action_style'), $request->post('action_blank') === '1']],
'provider' => $savedProvider,
];
$components = $item['payload']['components'];
if ($kind === 'header' && is_array($components) && DesignLibrary::hasAccount($item['preset'], $components)) {
foreach (['mode', 'issuer', 'authorization', 'token', 'userinfo', 'client_id', 'scope', 'subject_field', 'name_field', 'avatar_field', 'token_auth'] as $field) {
$item['payload']['provider'][$field] = $this->input($request, 'provider_' . $field);
}
$secret = $this->input($request, 'provider_secret', trimValue: false);
}
if ($request->post('canvas_links') !== null) {
$draftLinks = json_decode($this->input($request, 'canvas_links'), true);
if (!is_array($draftLinks) || !array_is_list($draftLinks)
|| array_filter($draftLinks, 'is_array') !== $draftLinks) {
throw new DesignFieldError('links', '导航链接格式无效。');
}
$item['payload']['links'] = $draftLinks;
} else {
try {
$item['payload']['links'] = SiteDesign::parseLinks($links);
} catch (RuntimeException $ex) {
throw new DesignFieldError('links', $ex->getMessage());
}
}
}
$id = DesignLibrary::save($kind, $id, $item['name'], $item['preset'], $item['payload'], $item['version'], $default, $secret);
$id = DesignLibrary::save($kind, $id, $item['name'], 'custom', $item['payload'], $item['version'], false);
return $this->success($request, "/admin/design/$kind/$id?saved=1");
} catch (RuntimeException $ex) {
$error = $this->validationMessage($ex);
$field = $ex instanceof DesignFieldError ? $ex->field : '';
return $this->success($request, "/admin/design/palette/$id?saved=1");
} catch (RuntimeException $exception) {
$error = $this->validationMessage($exception);
$field = $exception instanceof DesignFieldError ? $exception->field : '';
}
}
if ($kind !== 'palette') {
$components = $item['payload']['components'];
$item['payload']['components'] = is_array($components) ? array_values(array_intersect(
array_filter($components, 'is_string'), array_keys(DesignLibrary::COMPONENTS[$kind])
)) : [];
}
return $this->render($request, 'admin-design-edit', [
'title' => '设计配置', 'kind' => $kind, 'key' => $key, 'item' => $item, 'default' => $default,
'error' => $error, 'field' => $field, 'links' => $links, 'reenterSecret' => $error !== '' && $secret !== '',
'layoutEditor' => $kind !== 'palette', 'wideAdmin' => $kind !== 'palette', 'siteName' => $site['name'],
'title' => '配色方案', 'key' => $key, 'item' => $item, 'error' => $error, 'field' => $field,
'saved' => $error === '' && $request->get('saved') === '1',
], $error === '' ? 200 : 422);
}
@@ -149,15 +107,18 @@ final class DesignController extends AdminController
if ($failure = $this->guard($request)) {
return $failure;
}
if ($kind !== 'palette') {
$layout = (new Repository())->layoutPage((int) $id);
return $layout && $layout['layout_kind'] === $kind ? View::redirect('/admin/pages/' . $layout['slug'] . '/preview')
: $this->error($request, '找不到这项设计配置。', 404);
}
if (!DesignLibrary::find((int) $id, $kind)) {
return $this->error($request, '找不到这项设计配置。', 404);
}
$repo = new Repository();
$page = $repo->page('home');
$page[$kind . '_id'] = (int) $id;
if ($kind !== 'palette') {
$page['visibility'][$kind] = true;
}
return (new SiteController())->renderPage($repo, $page, true);
}
+1 -1
View File
@@ -122,7 +122,7 @@ final class McpController
try {
$data = match ($name) {
'list_pages' => $repo->pages(),
'get_page' => $this->read($repo, PageDraft::slug($args['slug'] ?? null, [])),
'get_page' => $this->read($repo, PageDraft::slug($args['slug'] ?? null, [], includeLayouts: true)),
'list_templates' => $this->templates(),
'list_designs' => DesignLibrary::all(),
'create_draft' => $this->create($repo, $args),
+3 -26
View File
@@ -25,7 +25,7 @@ final class SiteController
{
$repo = new Repository();
$page = $repo->page($slug);
if (!$page || $page['status'] !== 'published' || $slug === 'home' && $request->path() !== '/') {
if (!$page || $page['layout_kind'] !== null || $page['status'] !== 'published' || $slug === 'home' && $request->path() !== '/') {
return $this->missing($request);
}
if ($slug === 'articles' && $request->get('page') !== null) {
@@ -46,30 +46,7 @@ final class SiteController
$slug = $page['slug'];
$isHome = $slug === 'home';
$showHero = $page['visibility']['hero'] ?? true;
$categories = $repo->categories();
foreach ($page['sections'] as &$section) {
if (!$section['feed']) {
continue;
}
$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/' . $slug . '/' . $section['key'];
$section['archivePath'] = $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' => $repo->articles($selected, limit: $feed['limit'], directoryOrder: $network || $catalog),
];
}
}
unset($section);
$page = $repo->withSectionFeeds($page);
return View::render('page', [
'site' => $repo->site(),
@@ -240,7 +217,7 @@ final class SiteController
$normal = rtrim($path, '/');
$entry = preg_match('~^/(?:admin|account)(?:/|$)~D', $normal)
&& Route::dispatch($request->method(), $normal)[0] === Dispatcher::FOUND;
$single = preg_match('~^/[a-z0-9-]+$~D', $normal);
$single = preg_match('~^/[a-z0-9]+(?:-[a-z0-9]+)*$~D', $normal);
if ($entry || ($single && $normal !== '/home' && ((new Repository())->page(substr($normal, 1))['status'] ?? null) === 'published')
|| preg_match('~^/articles(?:/[a-z0-9-]+){1,2}$~D', $normal)) {
$query = $request->queryString();
+23 -2
View File
@@ -5,7 +5,9 @@ declare(strict_types=1);
namespace App\Controller;
use App\Content\BlockTemplates;
use App\Content\DesignLibrary;
use App\Content\Html;
use App\Content\LayoutPage;
use App\Content\Media;
use App\Content\PageDraft;
use App\Content\PageTemplates;
@@ -22,16 +24,26 @@ final class StudioController extends AdminController
if ($failure = $this->guard($request)) {
return $failure;
}
$type = $request->get('type');
$data = [
'title' => '新建页面', 'name' => '', 'slug' => '', 'error' => '', 'pageTemplateDrawer' => true,
'openTemplates' => $request->get('templates') === '1',
'type' => is_string($type) && isset(LayoutPage::TYPES[$type]) ? $type : 'page',
];
if ($request->method() === 'POST') {
$data['name'] = $this->input($request, 'name');
$data['slug'] = $this->input($request, 'slug');
$data['type'] = $request->post('type') === null ? 'page' : $this->input($request, 'type');
try {
if (!$this->slug($data['slug']) || in_array($data['slug'], PageDraft::RESERVED, true)
|| $data['name'] === '' || mb_strlen($data['name']) > 100) {
if (!isset(LayoutPage::TYPES[$data['type']]) || $data['name'] === '' || mb_strlen($data['name']) > 100) {
throw new RuntimeException('请填写页面名称并选择页面类型。');
}
if ($data['type'] !== 'page') {
$slug = LayoutPage::create($data['type'], $data['name']);
return $this->success($request, '/admin/pages/' . $slug);
}
if (!$this->slug($data['slug']) || in_array($data['slug'], PageDraft::RESERVED, true)) {
throw new RuntimeException('请填写页面名称及可用的网址标识。');
}
$key = $this->input($request, 'template');
@@ -69,6 +81,15 @@ final class StudioController extends AdminController
}
$repo = new Repository();
$page = $repo->page($slug);
$area = $request->get('area');
if ($page && $page['layout_kind'] === null && in_array($area, ['header', 'footer'], true)) {
try {
$page[$area . '_id'] = DesignLibrary::selection($area, $request->get('choice'));
$page['visibility'][$area] = true;
} catch (RuntimeException $error) {
return $this->error($request, $this->validationMessage($error), 422);
}
}
return $page ? (new SiteController())->renderPage($repo, $page, true, $request->get('canvas') === '1') : $this->error($request, '找不到此页面。', 404);
}