183 lines
8.3 KiB
PHP
183 lines
8.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Content\Assets;
|
|
use App\Content\DesignFieldError;
|
|
use App\Content\DesignLibrary;
|
|
use App\Content\Repository;
|
|
use App\Content\SiteDesign;
|
|
use App\Http\Request;
|
|
use RuntimeException;
|
|
use support\Response;
|
|
|
|
final class DesignController extends AdminController
|
|
{
|
|
public function index(Request $request): Response
|
|
{
|
|
if ($failure = $this->guard($request)) {
|
|
return $failure;
|
|
}
|
|
|
|
return $this->render($request, 'admin-design', [
|
|
'title' => '站点设计', 'items' => DesignLibrary::all(), 'site' => (new Repository())->site(),
|
|
]);
|
|
}
|
|
|
|
public function edit(Request $request, string $kind, string $key): Response
|
|
{
|
|
if ($failure = $this->guard($request)) {
|
|
return $failure;
|
|
}
|
|
$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);
|
|
if (!$item) {
|
|
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') {
|
|
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);
|
|
|
|
return $this->success($request, "/admin/design/$kind/$id?saved=1");
|
|
} catch (RuntimeException $ex) {
|
|
$error = $this->validationMessage($ex);
|
|
$field = $ex instanceof DesignFieldError ? $ex->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'],
|
|
'saved' => $error === '' && $request->get('saved') === '1',
|
|
], $error === '' ? 200 : 422);
|
|
}
|
|
|
|
public function site(Request $request): Response
|
|
{
|
|
if ($failure = $this->guard($request)) {
|
|
return $failure;
|
|
}
|
|
$site = (new Repository())->site();
|
|
$error = '';
|
|
$name = $site['name'];
|
|
$cdn = $site['cdn']['origin'] ?? '';
|
|
$version = (int) ($site['revisions']['general'] ?? 0);
|
|
if ($request->method() === 'POST') {
|
|
try {
|
|
$name = $this->input($request, 'name');
|
|
$cdn = Assets::cdn($this->input($request, 'cdn'));
|
|
if ($name === '' || mb_strlen($name) > 100) {
|
|
throw new RuntimeException('站点名称必须为 1 至 100 个字符。');
|
|
}
|
|
SiteDesign::saveGeneral($name, $cdn, $this->version($request));
|
|
|
|
return $this->success($request, '/admin/settings?saved=1');
|
|
} catch (RuntimeException $ex) {
|
|
$error = $this->validationMessage($ex);
|
|
}
|
|
}
|
|
|
|
return $this->render($request, 'studio-settings', [
|
|
'title' => '站点设置', 'name' => $name, 'cdn' => $cdn, 'version' => $version, 'error' => $error,
|
|
'saved' => $request->get('saved') === '1',
|
|
], $error === '' ? 200 : 422);
|
|
}
|
|
|
|
public function previewDesign(Request $request, string $kind, string $id): Response
|
|
{
|
|
if ($failure = $this->guard($request)) {
|
|
return $failure;
|
|
}
|
|
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);
|
|
}
|
|
|
|
public function palette(Request $request, string $id): Response
|
|
{
|
|
$palette = DesignLibrary::find((int) $id, 'palette');
|
|
if (!$palette) {
|
|
return new Response(404);
|
|
}
|
|
$declarations = [];
|
|
foreach ($palette['payload'] as $variable => $value) {
|
|
$declarations[] = '--' . $variable . ':' . $value;
|
|
}
|
|
|
|
return new Response(200, [
|
|
'Content-Type' => 'text/css; charset=utf-8', 'Cache-Control' => 'public, max-age=60',
|
|
'X-Content-Type-Options' => 'nosniff',
|
|
], ':root, #main-content.page-theme {' . implode(';', $declarations) . '} .page-theme .hero, .hero-home {background:var(--paper)}'
|
|
. ' #main-content .button-primary, #main-content .button-primary:hover {color:var(--button-ink);background:var(--accent)}');
|
|
}
|
|
}
|