Add page publication history and reusable blocks
This commit is contained in:
@@ -13,6 +13,8 @@ use App\Content\Html;
|
||||
use App\Content\LayoutPage;
|
||||
use App\Content\PageHead;
|
||||
use App\Content\PageDraft;
|
||||
use App\Content\PageRevisions;
|
||||
use App\Content\ReusableBlocks;
|
||||
use App\Content\SystemPage;
|
||||
use App\Content\Licenses;
|
||||
use App\Content\Media;
|
||||
@@ -287,7 +289,7 @@ class AdminController
|
||||
|| mb_strlen($data['summary']) > 250 || strlen($data['body']) > 200000
|
||||
|| array_diff($data['categories'], array_keys($categories))
|
||||
|| !in_array($data['body_format'], ['markdown', 'html'], true)
|
||||
|| !in_array($data['status'], ['draft', 'published'], true)) {
|
||||
|| !in_array($data['publicationAction'], ['', 'draft', 'published'], true)) {
|
||||
throw new RuntimeException('请检查标题、网址标识、分类、摘要和正文长度。');
|
||||
}
|
||||
$this->checkArticleCard($data['card']);
|
||||
@@ -343,6 +345,11 @@ class AdminController
|
||||
$layoutKind = $page['layout_kind'];
|
||||
$layout = $layoutKind === null ? null : DesignLibrary::find($page['layout_id'], $layoutKind);
|
||||
$default = $layout && ($site[$layoutKind . '_id'] ?? null) === $layout['id'];
|
||||
$pendingLayout = $layout ? LayoutPage::draftSettings($layout['id']) : null;
|
||||
if ($pendingLayout) {
|
||||
$default = $pendingLayout['default'];
|
||||
$layout['payload']['provider'] = $pendingLayout['provider'] ?? $layout['payload']['provider'];
|
||||
}
|
||||
$headEditor = !$layout && array_key_exists('head_html', $page);
|
||||
$error = '';
|
||||
if ($request->method() === 'POST') {
|
||||
@@ -361,7 +368,8 @@ class AdminController
|
||||
}
|
||||
}
|
||||
$publication = $request->post('status');
|
||||
$data['status'] = $layout || $publication === null ? $page['status'] : $this->input($request, 'status');
|
||||
$data['publicationAction'] = $publication === null ? '' : $this->input($request, 'status');
|
||||
$data['status'] = $page['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);
|
||||
@@ -380,11 +388,6 @@ class AdminController
|
||||
$data['slug'] = $target === $slug ? $slug : PageDraft::slug($target);
|
||||
$data['type'] = $type;
|
||||
$public = in_array($type, ['page', 'home'], true);
|
||||
if (in_array($type, ['header', 'footer'], true)) {
|
||||
$data['status'] = 'published';
|
||||
} elseif ($public && !in_array($previousType, ['page', 'home'], true)) {
|
||||
$data['status'] = 'draft';
|
||||
}
|
||||
if ($head !== null) {
|
||||
if (!$headEditor) {
|
||||
throw new RuntimeException(Database::MIGRATION_REQUIRED);
|
||||
@@ -441,7 +444,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 ||
|
||||
($public && $data['status'] === 'published' && $data['description'] === '') || mb_strlen($data['description']) > 300 ||
|
||||
($public && $data['publicationAction'] === 'published' && $data['description'] === '') || mb_strlen($data['description']) > 300 ||
|
||||
mb_strlen($data['heading'] . $data['accent']) > 110 ||
|
||||
mb_strlen($data['lead']) > 600 || mb_strlen($data['alt']) > 200 ||
|
||||
($data['image_scale'] !== 0 && ($data['image_scale'] < CanvasStyle::MIN_MEDIA_SCALE || $data['image_scale'] > 100)) ||
|
||||
@@ -465,6 +468,7 @@ class AdminController
|
||||
}
|
||||
return $this->json([
|
||||
'saved' => true, 'version' => $saved['version'], 'name' => $saved['name'], 'status' => $saved['status'],
|
||||
'publication' => PageRevisions::publicationInfo(Database::connect(), $saved['slug']),
|
||||
'layoutIds' => array_intersect_key($saved, $originalLayoutIds),
|
||||
'sections' => array_map(static function (array $section) use ($sections): array {
|
||||
$key = $section['key'];
|
||||
@@ -484,13 +488,106 @@ class AdminController
|
||||
'title' => '编辑 ' . $page['name'], 'page' => $page, 'categories' => $repo->categories(), 'wideAdmin' => true, 'canvasSourceEditor' => true, 'error' => $error, 'saved' => $request->get('saved') === '1',
|
||||
'pageTemplateDrawer' => !$layout, 'headEditor' => $headEditor, 'designs' => DesignLibrary::layouts(), 'layout' => $layout, 'layoutDefault' => $default,
|
||||
'layoutBase' => $layout && ($site['layout_defaults'][$layoutKind] ?? null) === $layout['id'],
|
||||
'siteName' => $site['name'], 'layoutHasMember' => $layoutKind === 'header' && LayoutPage::hasMember($page['sections']),
|
||||
'siteName' => $site['name'], 'layoutHasMember' => $layoutKind === 'header' && LayoutPage::hasMember($repo->resolveDraft($page)['sections']),
|
||||
'layoutDefaults' => array_intersect_key($site, ['header_id' => true, 'footer_id' => true]),
|
||||
'palettes' => DesignLibrary::palettes(), 'defaultPaletteId' => $site['palette_id'] ?? null,
|
||||
'originalLayoutIds' => $originalLayoutIds,
|
||||
'originalLayoutIds' => $originalLayoutIds, 'layoutSecretPending' => $pendingLayout['secret_pending'] ?? false,
|
||||
'publication' => PageRevisions::publicationInfo(Database::connect(), $slug),
|
||||
'reusableBlocks' => ReusableBlocks::sources(Database::connect()),
|
||||
], $error !== '' ? 422 : 200);
|
||||
}
|
||||
|
||||
public function copyPage(Request $request, string $slug): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
return $failure;
|
||||
}
|
||||
$repo = new Repository();
|
||||
$page = $repo->page($slug);
|
||||
if (!$page) {
|
||||
return $this->error($request, '找不到此页面。', 404);
|
||||
}
|
||||
$name = mb_substr($page['name'], 0, 95) . '(副本)';
|
||||
$base = rtrim(substr(str_replace('--', '-', $slug), 0, 55), '-');
|
||||
$target = $base . '-copy';
|
||||
for ($suffix = 2; $repo->page($target) || $repo->publishedPage($target); ++$suffix) {
|
||||
$target = $base . '-copy-' . $suffix;
|
||||
}
|
||||
$error = '';
|
||||
if ($request->method() === 'POST') {
|
||||
$name = $this->input($request, 'name');
|
||||
$target = $this->input($request, 'slug');
|
||||
try {
|
||||
if ($name === '' || mb_strlen($name) > 100) {
|
||||
throw new RuntimeException('请填写页面名称(最多 100 个字符)。');
|
||||
}
|
||||
$repo->copyPage($slug, PageDraft::slug($target), $name, $this->version($request));
|
||||
|
||||
return $this->success($request, '/admin/pages/' . $target);
|
||||
} catch (RuntimeException $exception) {
|
||||
$error = $this->validationMessage($exception);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render($request, 'admin-page-copy', [
|
||||
'title' => '复制页面', 'page' => $page, 'name' => $name, 'target' => $target, 'error' => $error,
|
||||
], $error === '' ? 200 : 422);
|
||||
}
|
||||
|
||||
public function pageHistory(Request $request, string $slug): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
return $failure;
|
||||
}
|
||||
$repo = new Repository();
|
||||
$page = $repo->page($slug);
|
||||
if (!$page) {
|
||||
return $this->error($request, '找不到此页面。', 404);
|
||||
}
|
||||
$id = filter_var($request->method() === 'POST' ? $request->post('revision') : $request->get('revision'),
|
||||
FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
|
||||
if ($request->method() === 'POST') {
|
||||
try {
|
||||
if ($id && $repo->restorePage($slug, $id, $this->version($request))) {
|
||||
return $this->success($request, '/admin/pages/' . $slug . '?saved=1');
|
||||
}
|
||||
} catch (RuntimeException $exception) {
|
||||
return $this->error($request, $this->validationMessage($exception), 422);
|
||||
}
|
||||
|
||||
return $this->error($request, '找不到此历史版本。', 404);
|
||||
}
|
||||
$pdo = Database::connect();
|
||||
if ($request->get('revision') !== null) {
|
||||
$saved = $id ? PageRevisions::find($pdo, $slug, $id) : null;
|
||||
|
||||
return $saved ? (new SiteController())->renderPage($repo, $saved, true)
|
||||
: $this->error($request, '找不到此历史版本。', 404);
|
||||
}
|
||||
|
||||
return $this->render($request, 'admin-page-history', [
|
||||
'title' => '版本历史', 'page' => $page, 'revisions' => PageRevisions::history($pdo, $slug),
|
||||
]);
|
||||
}
|
||||
|
||||
public function reusableBlock(Request $request, string $slug): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
return $failure;
|
||||
}
|
||||
$operation = $this->input($request, 'operation');
|
||||
if (!in_array($operation, ['main', 'add', 'detach'], true)) {
|
||||
return $this->error($request, '操作不可用。', 422);
|
||||
}
|
||||
try {
|
||||
$key = (new Repository())->editReusableBlock($slug, $this->version($request), $operation, $this->input($request, 'value'));
|
||||
|
||||
return $this->success($request, '/admin/pages/' . $slug . '?block=' . rawurlencode($key) . '#blocks');
|
||||
} catch (RuntimeException $exception) {
|
||||
return $this->error($request, $this->validationMessage($exception), 422);
|
||||
}
|
||||
}
|
||||
|
||||
public function deletePage(Request $request, string $slug): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
@@ -516,6 +613,10 @@ class AdminController
|
||||
if (!$section) {
|
||||
return $this->error($request, '找不到此区块。', 404);
|
||||
}
|
||||
if (!empty($section['mirror_id'])) {
|
||||
return $request->method() === 'POST' ? $this->error($request, '镜像区块只读。', 422)
|
||||
: $this->success($request, '/admin/pages/' . $slug . '?block=' . rawurlencode($key) . '#blocks');
|
||||
}
|
||||
$savedStyle = $section['style'];
|
||||
$categories = $repo->categories();
|
||||
$error = '';
|
||||
|
||||
Reference in New Issue
Block a user