Migrate Laysense to upstream LaySite 1.4.3 with external site data
This commit is contained in:
@@ -4,13 +4,15 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Content\Assets;
|
||||
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;
|
||||
use App\Content\Repository;
|
||||
use App\Content\SiteDesign;
|
||||
use App\Content\SystemPage;
|
||||
use App\Http\Request;
|
||||
use RuntimeException;
|
||||
use support\Response;
|
||||
@@ -23,16 +25,41 @@ final class StudioController extends AdminController
|
||||
if ($failure = $this->guard($request)) {
|
||||
return $failure;
|
||||
}
|
||||
$types = LayoutPage::TYPES + SystemPage::TYPES;
|
||||
$type = $request->get('type');
|
||||
$data = [
|
||||
'title' => '新建页面', 'name' => '', 'slug' => '', 'error' => '', 'pageTemplateDrawer' => true,
|
||||
'openTemplates' => $request->get('templates') === '1',
|
||||
'type' => is_string($type) && isset($types[$type]) ? $type : 'page',
|
||||
'types' => $types,
|
||||
];
|
||||
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'], ['admin', 'assets', 'home', 'network', 'news', 'main', 'favicon'], true)
|
||||
|| $data['name'] === '' || mb_strlen($data['name']) > 100) {
|
||||
if (!isset($types[$data['type']]) || $data['name'] === '' || mb_strlen($data['name']) > 100) {
|
||||
throw new RuntimeException('请填写页面名称并选择页面类型。');
|
||||
}
|
||||
if (isset(SystemPage::TYPES[$data['type']])) {
|
||||
$type = $data['type'];
|
||||
$slug = 'system--' . $type . '-' . bin2hex(random_bytes(8));
|
||||
$maintenance = $type === 'maintenance';
|
||||
(new Repository())->createPage($slug, $data['name'], [
|
||||
'heading' => $maintenance ? '站点维护中' : '页面未找到',
|
||||
'lead' => $maintenance ? '我们正在维护站点,请稍后再访问。' : '你访问的页面可能已移动或删除。',
|
||||
'actions' => $maintenance ? [] : [['/', '返回首页', 'solid', false]],
|
||||
'visibility' => $maintenance ? ['header' => false, 'footer' => false] : [],
|
||||
], $type);
|
||||
|
||||
return $this->success($request, '/admin/pages/' . $slug);
|
||||
}
|
||||
if (in_array($data['type'], ['header', 'footer'], true)) {
|
||||
$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');
|
||||
@@ -40,6 +67,7 @@ final class StudioController extends AdminController
|
||||
if ($template === null) {
|
||||
throw new RuntimeException('找不到所选整页模板。');
|
||||
}
|
||||
$template['title'] = $data['name'];
|
||||
(new Repository())->createPage($data['slug'], $data['name'], $template);
|
||||
|
||||
return $this->success($request, '/admin/pages/' . $data['slug']);
|
||||
@@ -69,6 +97,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);
|
||||
}
|
||||
@@ -110,39 +147,6 @@ final class StudioController extends AdminController
|
||||
}
|
||||
}
|
||||
|
||||
public function settings(Request $request, string $part): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
return $failure;
|
||||
}
|
||||
$site = (new Repository())->site();
|
||||
$defaults = match ($part) {
|
||||
'header' => SiteDesign::DEFAULT_HEADER,
|
||||
'footer' => SiteDesign::DEFAULT_FOOTER,
|
||||
'cdn' => ['origin' => ''],
|
||||
};
|
||||
$config = $part === 'footer' ? SiteDesign::footer($site) : ($site[$part] ?? []) + $defaults;
|
||||
$version = $site['revisions'][$part] ?? 0;
|
||||
$error = '';
|
||||
if ($request->method() === 'POST') {
|
||||
try {
|
||||
$config = $part === 'footer' && $request->post('operation') === 'restore-footer'
|
||||
? $defaults : $this->settingsInput($request, $part, $defaults, $config);
|
||||
SiteDesign::saveSettings($part, $config, $this->version($request));
|
||||
|
||||
return $this->success($request, '/admin/settings/' . $part . '?saved=1');
|
||||
} catch (RuntimeException $exception) {
|
||||
$error = $this->validationMessage($exception);
|
||||
$version = $this->version($request);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render($request, 'studio-settings', [
|
||||
'part' => $part, 'config' => $config, 'revision' => $version, 'error' => $error, 'saved' => $request->get('saved') === '1',
|
||||
'title' => ['header' => '页头', 'footer' => '页脚', 'cdn' => '静态资源 CDN'][$part],
|
||||
], $error === '' ? 200 : 422);
|
||||
}
|
||||
|
||||
public function media(Request $request): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
@@ -198,44 +202,6 @@ final class StudioController extends AdminController
|
||||
return $this->success($request, '/admin/media');
|
||||
}
|
||||
|
||||
private function settingsInput(Request $request, string $part, array $defaults, array $current): array
|
||||
{
|
||||
if ($part === 'cdn') {
|
||||
return ['origin' => Assets::cdn($this->input($request, 'origin'))];
|
||||
}
|
||||
$result = [];
|
||||
$hasResources = $request->post('resources') !== null;
|
||||
foreach ($defaults as $key => $default) {
|
||||
if ($part === 'footer' && !$hasResources && in_array($key, ['resources', 'resources_title'], true)) {
|
||||
$result[$key] = $current[$key];
|
||||
continue;
|
||||
}
|
||||
$input = $this->input($request, $key);
|
||||
if (is_array($default)) {
|
||||
$result[$key] = SiteDesign::parseLinks($input);
|
||||
if ($key === 'button') {
|
||||
if (count($result[$key]) > 1) {
|
||||
throw new RuntimeException('页头按钮只接受一条链接。');
|
||||
}
|
||||
$result[$key] = $result[$key][0] ?? [];
|
||||
}
|
||||
} else {
|
||||
if (mb_strlen($input) > 1000) {
|
||||
throw new RuntimeException('设置字段过长。');
|
||||
}
|
||||
$result[$key] = $input;
|
||||
}
|
||||
}
|
||||
if ($result['logo'] !== '' && !Html::url($result['logo'], true)) {
|
||||
throw new RuntimeException('请输入有效的标志图片地址。');
|
||||
}
|
||||
if ($part === 'footer' && ($result['name'] === '' || ($result['email'] !== '' && !filter_var($result['email'], FILTER_VALIDATE_EMAIL)))) {
|
||||
throw new RuntimeException('请填写公司名称及有效的联系邮箱。');
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function slug(string $value): bool
|
||||
{
|
||||
return strlen($value) <= 70 && preg_match('/^[a-z0-9]+(?:-[a-z0-9]+)*$/D', $value) === 1;
|
||||
|
||||
Reference in New Issue
Block a user