Add canvas element tools and categorized article editing
This commit is contained in:
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Content\BlockTemplates;
|
||||
use App\Content\Database;
|
||||
use App\Content\Html;
|
||||
use App\Content\Passwords;
|
||||
@@ -125,22 +126,58 @@ class AdminController
|
||||
return $failure;
|
||||
}
|
||||
$page = max(1, min(10000, (int) $request->get('page', 1)));
|
||||
$articles = (new Repository())->articles(public: false, limit: 31, offset: ($page - 1) * 30);
|
||||
$repo = new Repository();
|
||||
$categories = $repo->categories();
|
||||
$category = $request->get('category');
|
||||
$category = is_string($category) && isset($categories[$category]) ? $category : null;
|
||||
$articles = $repo->articles($category, public: false, limit: 31, offset: ($page - 1) * 30);
|
||||
|
||||
return $this->render($request, 'admin-articles', [
|
||||
'title' => '文章与动态', 'articles' => array_slice($articles, 0, 30), 'pageNumber' => $page, 'hasNext' => count($articles) > 30,
|
||||
'title' => '文章与动态', 'articles' => array_slice($articles, 0, 30), 'pageNumber' => $page,
|
||||
'hasNext' => count($articles) > 30, 'categories' => $categories, 'category' => $category,
|
||||
]);
|
||||
}
|
||||
|
||||
public function articleCategories(Request $request): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
return $failure;
|
||||
}
|
||||
$repo = new Repository();
|
||||
$name = $slug = $error = '';
|
||||
if ($request->method() === 'POST') {
|
||||
$slug = $this->input($request, 'slug');
|
||||
$name = $this->input($request, 'name');
|
||||
try {
|
||||
if (!preg_match('/^[a-z0-9]+(?:-[a-z0-9]+)*$/D', $slug) || strlen($slug) > 60 || $name === '' || mb_strlen($name) > 60) {
|
||||
throw new RuntimeException('请填写分类名称及网址标识。');
|
||||
}
|
||||
$repo->createCategory($slug, $name);
|
||||
|
||||
return $this->success($request, '/admin/article-categories?saved=1');
|
||||
} catch (RuntimeException $exception) {
|
||||
$error = $this->validationMessage($exception);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render($request, 'admin-article-categories', [
|
||||
'title' => '文章分类', 'categories' => $repo->categories(), 'name' => $name, 'slug' => $slug,
|
||||
'error' => $error, 'saved' => $request->get('saved') === '1',
|
||||
], $error === '' ? 200 : 422);
|
||||
}
|
||||
|
||||
public function article(Request $request, ?string $id = null): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
return $failure;
|
||||
}
|
||||
$repo = new Repository();
|
||||
$categories = $repo->categories();
|
||||
$preset = $request->get('category');
|
||||
$preset = is_string($preset) && isset($categories[$preset]) ? $preset : 'news';
|
||||
$article = $id !== null ? $repo->articleById((int) $id) : [
|
||||
'title' => '', 'slug' => '', 'summary' => '', 'category' => 'news', 'body' => '',
|
||||
'tags' => [], 'status' => 'draft', 'version' => 0,
|
||||
'title' => '', 'slug' => '', 'summary' => '', 'body' => '', 'body_format' => 'markdown',
|
||||
'categories' => [$preset => $categories[$preset]], 'card' => [], 'tags' => [], 'status' => 'draft', 'version' => 0,
|
||||
];
|
||||
if (!$article) {
|
||||
return $this->error($request, '找不到这篇文章。', 404);
|
||||
@@ -148,16 +185,26 @@ class AdminController
|
||||
$error = '';
|
||||
if ($request->method() === 'POST') {
|
||||
$data = $this->articleInput($request);
|
||||
$article = array_replace($article, $data, ['version' => $this->version($request)]);
|
||||
$article = array_replace($article, $data, [
|
||||
'categories' => array_intersect_key($categories, array_flip($data['categories'])),
|
||||
'version' => $this->version($request),
|
||||
]);
|
||||
try {
|
||||
if (mb_strlen($data['title']) < 1 || mb_strlen($data['title']) > 120
|
||||
|| !preg_match('/^[a-z0-9]+(?:-[a-z0-9]+)*$/D', $data['slug']) || strlen($data['slug']) > 100
|
||||
|| mb_strlen($data['summary']) > 250 || strlen($data['body']) > 200000
|
||||
|| !isset(Repository::CATEGORIES[$data['category']])
|
||||
|| $data['categories'] === [] || array_diff($data['categories'], array_keys($categories))
|
||||
|| !in_array($data['body_format'], ['markdown', 'html'], true)
|
||||
|| !in_array($data['status'], ['draft', 'published'], true)) {
|
||||
throw new RuntimeException('请检查标题、网址标识、分类、摘要和正文长度。');
|
||||
}
|
||||
if ($data['status'] === 'published' && trim(Html::markdown($data['body'])) === '') {
|
||||
$this->checkArticleCard($data['card']);
|
||||
if ($data['body_format'] === 'html') {
|
||||
$data['body'] = Html::sanitize($data['body']);
|
||||
}
|
||||
if ($data['status'] === 'published' && trim(Html::articleBody($data)) === ''
|
||||
&& (!array_intersect($data['categories'], array_keys(Repository::DIRECTORIES))
|
||||
|| ($data['summary'] === '' && empty($data['card']['url']) && empty($data['card']['image'])))) {
|
||||
throw new RuntimeException('发布前请填写文章正文。');
|
||||
}
|
||||
$saved = $repo->saveArticle($id === null ? null : (int) $id, $data, $this->version($request));
|
||||
@@ -170,7 +217,7 @@ class AdminController
|
||||
|
||||
return $this->render($request, 'admin-article', [
|
||||
'title' => $id === null ? '新建文章' : '编辑文章', 'article' => $article, 'error' => $error,
|
||||
'articleId' => $id, 'saved' => $request->get('saved') === '1',
|
||||
'articleId' => $id, 'categories' => $categories, 'articleEditor' => true, 'saved' => $request->get('saved') === '1',
|
||||
], $error !== '' ? 422 : 200);
|
||||
}
|
||||
|
||||
@@ -214,7 +261,7 @@ class AdminController
|
||||
($data['status'] === 'published' && $data['description'] === '') || mb_strlen($data['description']) > 300 ||
|
||||
$data['heading'] === '' || mb_strlen($data['heading'] . $data['accent']) > 110 ||
|
||||
mb_strlen($data['lead']) > 600 || mb_strlen($data['alt']) > 200 ||
|
||||
!in_array($data['image_scale'], [0, 25, 40, 50, 60, 75, 85, 100], true) ||
|
||||
($data['image_scale'] !== 0 && ($data['image_scale'] < 10 || $data['image_scale'] > 100)) ||
|
||||
strlen($data['image']) > 1000 || ($data['image'] !== '' && !Html::url($data['image'], true)) || !$validButtons) {
|
||||
throw new RuntimeException('请检查页面标题、说明、图片地址和按钮链接。');
|
||||
}
|
||||
@@ -243,15 +290,20 @@ class AdminController
|
||||
return $this->error($request, '找不到此区块。', 404);
|
||||
}
|
||||
$savedStyle = $section['style'];
|
||||
$categories = $repo->categories();
|
||||
$error = '';
|
||||
if ($request->method() === 'POST') {
|
||||
$data = [
|
||||
'title' => $this->input($request, 'title'), 'lead' => $this->input($request, 'lead'),
|
||||
'body' => $this->input($request, 'body'),
|
||||
'style' => $request->post('style') === null ? $section['style'] : $this->input($request, 'style'),
|
||||
'feed' => $section['feed'],
|
||||
];
|
||||
$section = array_replace($section, $data, ['version' => $this->version($request)]);
|
||||
try {
|
||||
if ($request->post('feed_mode') !== null) {
|
||||
$data['feed'] = $section['feed'] = $this->feedInput($request, $categories);
|
||||
}
|
||||
$this->checkSection($data, $savedStyle);
|
||||
$data['body'] = Html::sanitize($data['body']);
|
||||
$repo->saveSection($slug, $key, $data, $this->version($request));
|
||||
@@ -263,7 +315,7 @@ class AdminController
|
||||
}
|
||||
|
||||
return $this->render($request, 'admin-section', [
|
||||
'title' => '编辑区块', 'section' => $section, 'savedStyle' => $savedStyle, 'error' => $error, 'saved' => $request->get('saved') === '1',
|
||||
'title' => '编辑区块', 'section' => $section, 'categories' => $categories, 'savedStyle' => $savedStyle, 'error' => $error, 'saved' => $request->get('saved') === '1',
|
||||
], $error !== '' ? 422 : 200);
|
||||
}
|
||||
|
||||
@@ -305,6 +357,9 @@ class AdminController
|
||||
if (isset($fields['body'])) {
|
||||
$section['body'] = Html::sanitize($section['body']);
|
||||
}
|
||||
if ($isNew && ($change['template'] ?? null) === 'articles') {
|
||||
$section['feed'] = BlockTemplates::ARTICLE_FEED;
|
||||
}
|
||||
$section['version'] = $version;
|
||||
$updates[$key] = $section;
|
||||
}
|
||||
@@ -423,9 +478,14 @@ class AdminController
|
||||
private function articleInput(Request $request): array
|
||||
{
|
||||
$data = [];
|
||||
foreach (['title', 'slug', 'category', 'summary', 'body', 'status'] as $key) {
|
||||
foreach (['title', 'slug', 'summary', 'body', 'status'] as $key) {
|
||||
$data[$key] = $this->input($request, $key);
|
||||
}
|
||||
$data['body_format'] = $this->input($request, 'body_format');
|
||||
$data['categories'] = $this->selectedCategories($request->post('categories'));
|
||||
foreach (['image', 'alt', 'url', 'label', 'extra_url', 'extra_label', 'category_label', 'position'] as $field) {
|
||||
$data['card'][$field] = $this->input($request, 'card_' . $field);
|
||||
}
|
||||
$tags = preg_split('/[,,\r\n]+/u', $this->input($request, 'tags'));
|
||||
$data['tags'] = array_values(array_unique(array_slice(array_filter(
|
||||
array_map('trim', $tags),
|
||||
@@ -435,6 +495,45 @@ class AdminController
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function checkArticleCard(array &$card): void
|
||||
{
|
||||
foreach (['image', 'url', 'extra_url'] as $field) {
|
||||
if (!empty($card[$field]) && (strlen($card[$field]) > 2000 || !Html::url($card[$field], $field === 'image'))) {
|
||||
throw new RuntimeException('请检查目录卡片的媒体地址和链接。');
|
||||
}
|
||||
}
|
||||
foreach (['alt', 'label', 'extra_label', 'category_label'] as $field) {
|
||||
if (mb_strlen($card[$field] ?? '') > 120) {
|
||||
throw new RuntimeException('目录卡片的文字不得超过 120 字。');
|
||||
}
|
||||
}
|
||||
$position = filter_var(($card['position'] ?? '') === '' ? 0 : $card['position'], FILTER_VALIDATE_INT, ['options' => ['min_range' => 0, 'max_range' => 9999]]);
|
||||
if ($position === false) {
|
||||
throw new RuntimeException('目录排序应为 0 到 9999 的整数。');
|
||||
}
|
||||
$card['position'] = $position;
|
||||
}
|
||||
|
||||
private function selectedCategories(mixed $input): array
|
||||
{
|
||||
return is_array($input) ? array_values(array_unique(array_filter($input, 'is_string'))) : [];
|
||||
}
|
||||
|
||||
private function feedInput(Request $request, array $available): ?string
|
||||
{
|
||||
$mode = $this->input($request, 'feed_mode');
|
||||
if ($mode === 'off') {
|
||||
return null;
|
||||
}
|
||||
$categories = $this->selectedCategories($request->post('feed_categories'));
|
||||
$limit = filter_var($this->input($request, 'feed_limit'), FILTER_VALIDATE_INT, ['options' => ['min_range' => 1, 'max_range' => 30]]);
|
||||
if (!in_array($mode, ['merged', 'tabs'], true) || !$categories || array_diff($categories, array_keys($available)) || $limit === false) {
|
||||
throw new RuntimeException('请选择文章分类,并将展示数量设为 1 至 30。');
|
||||
}
|
||||
|
||||
return Database::json(compact('categories', 'mode', 'limit'));
|
||||
}
|
||||
|
||||
protected function validationMessage(RuntimeException $exception): string
|
||||
{
|
||||
if ($exception instanceof \PDOException) {
|
||||
|
||||
Reference in New Issue
Block a user