forked from laysense/laysite
Edit shared headers and footers as page types
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user