forked from laysense/laysense.cn
Add canvas element tools and categorized article editing
This commit is contained in:
@@ -6,10 +6,13 @@ namespace App\Content;
|
||||
|
||||
final class BlockTemplates
|
||||
{
|
||||
public const ARTICLE_FEED = '{"categories":["announcement","news","product"],"mode":"merged","limit":6}';
|
||||
|
||||
public const GROUPS = [
|
||||
'content' => ['标题与正文', [
|
||||
'headline' => ['标题与简介', '<div class="studio-intro"><h3>填写区块标题</h3><p>用一句话补充背景或说明。</p></div>'],
|
||||
'text' => ['正文', '<p>在这里输入区块内容。</p>'],
|
||||
'articles' => ['分类文章', ''],
|
||||
'list' => ['步骤列表', '<ol><li>填写第一步</li><li>填写第二步</li><li>填写第三步</li></ol>'],
|
||||
]],
|
||||
'cover' => ['封面与横幅', [
|
||||
|
||||
+29
-36
@@ -39,27 +39,24 @@ final class Database
|
||||
$pdo->exec('PRAGMA journal_mode = WAL');
|
||||
|
||||
self::write(function (PDO $pdo) use ($root, $seed): void {
|
||||
$articleColumns = array_column($pdo->query('PRAGMA table_info(articles)')->fetchAll(), 'name');
|
||||
if (in_array('category', $articleColumns, true)) {
|
||||
throw new RuntimeException('请将 LAYSENSE_DB 指向一个新的 SQLite 文件,再运行安装命令。');
|
||||
}
|
||||
$pdo->exec(file_get_contents($root . '/resources/schema.sql'));
|
||||
$pageColumns = array_column($pdo->query('PRAGMA table_info(pages)')->fetchAll(), 'name');
|
||||
if (!in_array('status', $pageColumns, true)) {
|
||||
$pdo->exec("ALTER TABLE pages ADD COLUMN status TEXT NOT NULL DEFAULT 'published' CHECK (status IN ('draft', 'published'))");
|
||||
}
|
||||
if (!in_array('image_scale', $pageColumns, true)) {
|
||||
$pdo->exec('ALTER TABLE pages ADD COLUMN image_scale INTEGER NOT NULL DEFAULT 0');
|
||||
}
|
||||
$site = $pdo->prepare('INSERT OR IGNORE INTO site_settings (id, payload) VALUES (1, ?)');
|
||||
$site->execute([self::json($seed['site'])]);
|
||||
|
||||
$pageQuery = $pdo->prepare('INSERT OR IGNORE INTO pages
|
||||
(slug, name, title, description, heading, accent, lead, image, alt, actions, quicklinks, partners)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
|
||||
(slug, name, title, description, heading, accent, lead, image, alt, actions, quicklinks)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
|
||||
$sectionQuery = $pdo->prepare('INSERT OR IGNORE INTO sections
|
||||
(page_slug, key, title, lead, style, body, position) VALUES (?, ?, ?, ?, ?, ?, ?)');
|
||||
(page_slug, key, title, lead, style, body, feed, position) VALUES (?, ?, ?, ?, ?, ?, ?, ?)');
|
||||
foreach ($seed['pages'] as $page) {
|
||||
$pageQuery->execute([
|
||||
$page['slug'], $page['name'], $page['title'], $page['description'], $page['heading'],
|
||||
$page['accent'], $page['lead'], $page['image'], $page['alt'],
|
||||
self::json($page['actions']), self::json($page['quicklinks']), self::json($page['partners'] ?? []),
|
||||
self::json($page['actions']), self::json($page['quicklinks']),
|
||||
]);
|
||||
if ($pageQuery->rowCount() !== 1) {
|
||||
continue;
|
||||
@@ -67,38 +64,34 @@ final class Database
|
||||
foreach ($page['sections'] as $index => $section) {
|
||||
$sectionQuery->execute([
|
||||
$page['slug'], $section['key'], $section['title'], $section['lead'],
|
||||
$section['style'], Html::sanitize($section['body']), $index,
|
||||
$section['style'], Html::sanitize($section['body']), isset($section['feed']) ? self::json($section['feed']) : null, $index,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$article = $pdo->prepare('INSERT OR IGNORE INTO articles
|
||||
(slug, title, category, summary, body, tags, status, published_at, updated_at, legacy_path)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
|
||||
$category = $pdo->prepare('INSERT OR IGNORE INTO article_categories (slug, name) VALUES (?, ?)');
|
||||
foreach (Repository::CATEGORIES as $slug => $name) {
|
||||
$category->execute([$slug, $name]);
|
||||
}
|
||||
$article = $pdo->prepare("INSERT OR IGNORE INTO articles
|
||||
(slug, title, summary, body, body_format, card, tags, status, published_at, updated_at, legacy_path)
|
||||
SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? WHERE NOT EXISTS (SELECT 1 FROM article_redirects WHERE slug = ?)");
|
||||
$link = $pdo->prepare('INSERT INTO article_category_links (article_id, category_slug) VALUES (?, ?)');
|
||||
foreach ($seed['articles'] as $item) {
|
||||
$exists = $pdo->prepare('SELECT 1 FROM article_redirects WHERE slug = ?');
|
||||
$exists->execute([$item['slug']]);
|
||||
if ($exists->fetchColumn()) {
|
||||
continue;
|
||||
}
|
||||
$format = $item['body_format'] ?? 'markdown';
|
||||
$date = $item['date'] ?? null;
|
||||
$article->execute([
|
||||
$item['slug'], $item['title'], $item['category'], $item['summary'], $item['body'],
|
||||
self::json($item['tags']), $item['status'], $item['date'], $item['date'], $item['legacy'],
|
||||
$item['slug'], $item['title'], $item['summary'] ?? '',
|
||||
$format === 'html' ? Html::sanitize($item['body']) : $item['body'], $format,
|
||||
self::json($item['card'] ?? []), self::json($item['tags'] ?? []),
|
||||
$item['status'] ?? 'published', $date, $date ?? gmdate(DATE_ATOM), $item['legacy'] ?? null, $item['slug'],
|
||||
]);
|
||||
}
|
||||
|
||||
$migration = $pdo->prepare('INSERT OR IGNORE INTO schema_migrations (name) VALUES (?)');
|
||||
$migration->execute(['restore_pagepan_feature_icons']);
|
||||
if ($migration->rowCount() === 1) {
|
||||
PagepanFeatureIcons::migrate($pdo);
|
||||
}
|
||||
$migration->execute(['product_directory_and_service_icons']);
|
||||
if ($migration->rowCount() === 1) {
|
||||
DesignMigration::apply($pdo);
|
||||
}
|
||||
$migration->execute(['restore_home_featured_products']);
|
||||
if ($migration->rowCount() === 1) {
|
||||
DesignMigration::restoreFeaturedProducts($pdo, $seed);
|
||||
if ($article->rowCount() === 1) {
|
||||
$id = $pdo->lastInsertId();
|
||||
foreach ($item['categories'] as $slug) {
|
||||
$link->execute([$id, $slug]);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,83 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Content;
|
||||
|
||||
use DOMDocument;
|
||||
use DOMElement;
|
||||
use DOMXPath;
|
||||
use PDO;
|
||||
|
||||
final class DesignMigration
|
||||
{
|
||||
private const DIRECTORY_SUMMARY = '<p>浏览来笙的建站工具、智能 DNS 和软件著作权服务。</p><a class="button button-primary" href="/services">查看产品与服务</a>';
|
||||
|
||||
public static function apply(PDO $pdo): void
|
||||
{
|
||||
$stock = json_decode(file_get_contents(dirname(__DIR__, 2) . '/resources/seed.json'), true, flags: JSON_THROW_ON_ERROR);
|
||||
$home = array_column($stock['pages'], null, 'slug')['home'];
|
||||
$stockBody = Html::sanitize(array_column($home['sections'], 'body', 'key')['product']);
|
||||
$doc = new DOMDocument('1.0', 'UTF-8');
|
||||
$doc->loadHTML('<meta charset="UTF-8">' . $stockBody, LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_NONET);
|
||||
$xpath = new DOMXPath($doc);
|
||||
$insert = $pdo->prepare('INSERT INTO catalog (name, category, body, image, alt, url, label, extra_url, extra_label, position)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
|
||||
foreach ($xpath->query('//article[contains(concat(" ", @class, " "), " project ")]') as $position => $card) {
|
||||
$image = $xpath->query('.//img', $card)->item(0);
|
||||
$links = $xpath->query('.//a', $card);
|
||||
$body = '';
|
||||
foreach ($xpath->query('.//p', $card) as $paragraph) {
|
||||
$body .= $doc->saveHTML($paragraph);
|
||||
}
|
||||
$insert->execute([
|
||||
trim($xpath->evaluate('string(.//h3)', $card)), trim($xpath->evaluate('string(.//span)', $card)),
|
||||
Html::sanitize($body), $image->getAttribute('src'), $image->getAttribute('alt'),
|
||||
$links->item(0)->getAttribute('href'), trim($links->item(0)->textContent),
|
||||
$links->item(1)?->getAttribute('href') ?? '', trim($links->item(1)?->textContent ?? ''), $position * 10,
|
||||
]);
|
||||
}
|
||||
$pdo->prepare('INSERT INTO catalog (name, category, body, image, alt, url, label, position) VALUES (?, ?, ?, ?, ?, ?, ?, ?)')
|
||||
->execute(['SHNU 学生软著服务', '校园服务', '<p>面向 SHNU 学生的软件著作权申请服务。</p>',
|
||||
'/assets/original/shnu-hero.webp', '', '/shnu-sc', '查看学生方案', 30]);
|
||||
$pdo->prepare("UPDATE sections SET body = ?, version = version + 1
|
||||
WHERE page_slug = 'home' AND key = 'product' AND version = 0 AND body = ?")
|
||||
->execute([self::DIRECTORY_SUMMARY, $stockBody]);
|
||||
|
||||
$choices = $pdo->query("SELECT page_slug, body FROM sections WHERE key = 'why' AND page_slug IN ('sc', 'shnu-sc')");
|
||||
$updateChoice = $pdo->prepare("UPDATE sections SET body = ?, version = version + 1 WHERE page_slug = ? AND key = 'why'");
|
||||
foreach ($choices->fetchAll() as $section) {
|
||||
$body = preg_replace('~(<article\b[^>]*class="[^"]*\bfeature-card)("(?:[^>]*>)\s*<h[2-4]>选择来笙</h[2-4]>)~u',
|
||||
'$1 choice-card$2', $section['body']);
|
||||
if ($body !== null && $body !== $section['body']) {
|
||||
$updateChoice->execute([$body, $section['page_slug']]);
|
||||
}
|
||||
}
|
||||
|
||||
$icons = ['/assets/original/10-1.png' => '/assets/service/project.svg',
|
||||
'/assets/original/10-2.png' => '/assets/service/code.svg', '/assets/original/10-3.png' => '/assets/service/support.svg'];
|
||||
$sections = $pdo->query("SELECT page_slug, body FROM sections WHERE key = 'options' AND page_slug IN ('sc', 'shnu-sc')");
|
||||
$update = $pdo->prepare("UPDATE sections SET body = ?, version = version + 1 WHERE page_slug = ? AND key = 'options'");
|
||||
foreach ($sections->fetchAll() as $section) {
|
||||
$body = strtr($section['body'], $icons);
|
||||
$body = preg_replace('/(<article\b[^>]*>)\s*<li\b[^>]*>(.*?)<\/li>\s*(<\/article>)/s', '$1$2$3', $body);
|
||||
if ($body !== $section['body']) {
|
||||
$update->execute([$body, $section['page_slug']]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function restoreFeaturedProducts(PDO $pdo, array $seed): void
|
||||
{
|
||||
$home = array_column($seed['pages'], null, 'slug')['home'];
|
||||
$stockBody = Html::sanitize(array_column($home['sections'], 'body', 'key')['product']);
|
||||
$doc = new DOMDocument('1.0', 'UTF-8');
|
||||
$doc->loadHTML('<meta charset="UTF-8">' . $stockBody, LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_NONET);
|
||||
$projects = $doc->getElementsByTagName('article');
|
||||
$body = '<div class="project-stack">' . $doc->saveHTML($projects->item(0))
|
||||
. $doc->saveHTML($projects->item(1)) . '</div>';
|
||||
$pdo->prepare("UPDATE sections SET body = ?, version = version + 1
|
||||
WHERE page_slug = 'home' AND key = 'product' AND body = ?")
|
||||
->execute([Html::sanitize($body), self::DIRECTORY_SUMMARY]);
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ namespace App\Content;
|
||||
use DOMDocument;
|
||||
use DOMElement;
|
||||
use DOMNode;
|
||||
use League\CommonMark\CommonMarkConverter;
|
||||
use League\CommonMark\GithubFlavoredMarkdownConverter;
|
||||
use RuntimeException;
|
||||
|
||||
final class Html
|
||||
@@ -43,13 +43,18 @@ final class Html
|
||||
public static function markdown(string $text): string
|
||||
{
|
||||
static $converter;
|
||||
$converter ??= new CommonMarkConverter(['html_input' => 'strip', 'allow_unsafe_links' => false, 'max_nesting_level' => 30]);
|
||||
$converter ??= new GithubFlavoredMarkdownConverter(['html_input' => 'strip', 'allow_unsafe_links' => false, 'max_nesting_level' => 30]);
|
||||
|
||||
$html = $converter->convert($text)->getContent();
|
||||
|
||||
return str_replace(['<h1>', '</h1>'], ['<h2>', '</h2>'], $html);
|
||||
}
|
||||
|
||||
public static function articleBody(array $article): string
|
||||
{
|
||||
return $article['body_format'] === 'html' ? self::sanitize($article['body']) : self::markdown($article['body']);
|
||||
}
|
||||
|
||||
public static function applyCanvasEdits(string $body, array $edits): string
|
||||
{
|
||||
if (!array_is_list($edits) || count($edits) > 512) {
|
||||
|
||||
@@ -1,55 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Content;
|
||||
|
||||
use DOMDocument;
|
||||
use DOMElement;
|
||||
use DOMXPath;
|
||||
use PDO;
|
||||
|
||||
final class PagepanFeatureIcons
|
||||
{
|
||||
public static function migrate(PDO $pdo): void
|
||||
{
|
||||
$query = $pdo->query("SELECT body FROM sections WHERE page_slug = 'pagepan' AND key = 'features'");
|
||||
$body = $query->fetchColumn();
|
||||
if (!is_string($body) || $body === '') {
|
||||
return;
|
||||
}
|
||||
$paths = json_decode(file_get_contents(dirname(__DIR__, 2) . '/resources/pagepan-feature-icons.json'), true, flags: JSON_THROW_ON_ERROR);
|
||||
$document = new DOMDocument('1.0', 'UTF-8');
|
||||
$document->loadHTML('<!DOCTYPE html><html><head><meta charset="UTF-8"></head><body>' . $body . '</body></html>', LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_NONET);
|
||||
$xpath = new DOMXPath($document);
|
||||
|
||||
$changed = false;
|
||||
foreach ($xpath->query('//article[contains(concat(" ", normalize-space(@class), " "), " feature-card ")]') as $card) {
|
||||
$heading = $xpath->query('(./h2 | ./h3 | ./h4)[1]', $card)->item(0);
|
||||
if (!$heading instanceof DOMElement || $card->getElementsByTagName('img')->length > 0) {
|
||||
continue;
|
||||
}
|
||||
$title = trim($heading->textContent);
|
||||
if (!isset($paths[$title])) {
|
||||
continue;
|
||||
}
|
||||
$icon = $document->createElement('img');
|
||||
foreach (['src' => $paths[$title], 'alt' => '', 'width' => '50', 'height' => '50', 'loading' => 'lazy'] as $key => $value) {
|
||||
$icon->setAttribute($key, $value);
|
||||
}
|
||||
$card->insertBefore($icon, $card->firstChild);
|
||||
$changed = true;
|
||||
}
|
||||
|
||||
if ($changed) {
|
||||
$updated = '';
|
||||
foreach ($document->getElementsByTagName('body')->item(0)->childNodes as $child) {
|
||||
$updated .= $document->saveHTML($child);
|
||||
}
|
||||
$pdo->prepare("UPDATE sections SET body = ?, version = version + 1 WHERE page_slug = 'pagepan' AND key = 'features'")
|
||||
->execute([trim($updated)]);
|
||||
$pdo->prepare("UPDATE pages SET updated_at = ? WHERE slug = 'pagepan'")
|
||||
->execute([(new \DateTimeImmutable('now', new \DateTimeZone('Asia/Shanghai')))->format('Y-m-d H:i:s')]);
|
||||
}
|
||||
}
|
||||
}
|
||||
+73
-34
@@ -9,7 +9,13 @@ use RuntimeException;
|
||||
|
||||
final class Repository
|
||||
{
|
||||
public const CATEGORIES = ['announcement' => '公告', 'news' => '新闻', 'product' => '产品'];
|
||||
public const DIRECTORIES = [
|
||||
'services' => '产品与服务', 'supported-projects' => '来笙支持项目', 'partners' => '来笙合作伙伴',
|
||||
];
|
||||
public const CATEGORIES = ['announcement' => '公告', 'news' => '新闻', 'product' => '产品'] + self::DIRECTORIES;
|
||||
private const ARTICLE_FIELDS = 'articles.*, (SELECT json_group_object(c.slug, c.name)
|
||||
FROM article_category_links l JOIN article_categories c ON c.slug = l.category_slug
|
||||
WHERE l.article_id = articles.id) AS categories';
|
||||
private PDO $pdo;
|
||||
|
||||
public function __construct()
|
||||
@@ -32,8 +38,8 @@ final class Repository
|
||||
public function createPage(string $slug, string $name): void
|
||||
{
|
||||
$query = $this->pdo->prepare("INSERT OR IGNORE INTO pages (slug, name, title, description, heading, accent,
|
||||
lead, image, alt, actions, quicklinks, partners, status, updated_at)
|
||||
VALUES (?, ?, ?, '', ?, '', '', '', '', '[]', '[]', '[]', 'draft', ?)");
|
||||
lead, image, alt, actions, quicklinks, status, updated_at)
|
||||
VALUES (?, ?, ?, '', ?, '', '', '', '', '[]', '[]', 'draft', ?)");
|
||||
$query->execute([$slug, $name, $name . ' · 来笙', $name, self::now()]);
|
||||
if ($query->rowCount() !== 1) {
|
||||
throw new RuntimeException('此页面地址已存在。');
|
||||
@@ -46,7 +52,7 @@ final class Repository
|
||||
if (!$page) {
|
||||
return null;
|
||||
}
|
||||
foreach (['actions', 'quicklinks', 'partners'] as $key) {
|
||||
foreach (['actions', 'quicklinks'] as $key) {
|
||||
$page[$key] = json_decode($page[$key], true, flags: JSON_THROW_ON_ERROR);
|
||||
}
|
||||
$query = $this->pdo->prepare('SELECT * FROM sections WHERE page_slug = ? ORDER BY position, key');
|
||||
@@ -92,25 +98,25 @@ final class Repository
|
||||
|
||||
private static function updateSection(PDO $pdo, string $page, string $key, array $data, int $version): void
|
||||
{
|
||||
$query = $pdo->prepare('UPDATE sections SET title = ?, lead = ?, body = ?, style = ?, version = version + 1
|
||||
$query = $pdo->prepare('UPDATE sections SET title = ?, lead = ?, body = ?, style = ?, feed = ?, version = version + 1
|
||||
WHERE page_slug = ? AND key = ? AND version = ?');
|
||||
$query->execute([$data['title'], $data['lead'], $data['body'], $data['style'], $page, $key, $version]);
|
||||
$query->execute([$data['title'], $data['lead'], $data['body'], $data['style'], $data['feed'] ?? null, $page, $key, $version]);
|
||||
self::checkVersion($query->rowCount());
|
||||
}
|
||||
|
||||
public function createSection(string $page, string $key, string $title, string $style, string $body): void
|
||||
public function createSection(string $page, string $key, string $title, string $style, string $body, ?string $feed = null): void
|
||||
{
|
||||
Database::write(function (PDO $pdo) use ($page, $key, $title, $style, $body): void {
|
||||
self::insertSection($pdo, $page, $key, compact('title', 'style', 'body') + ['lead' => '']);
|
||||
Database::write(function (PDO $pdo) use ($page, $key, $title, $style, $body, $feed): void {
|
||||
self::insertSection($pdo, $page, $key, compact('title', 'style', 'body', 'feed') + ['lead' => '']);
|
||||
self::touchPage($pdo, $page, structureChanged: true);
|
||||
});
|
||||
}
|
||||
|
||||
private static function insertSection(PDO $pdo, string $page, string $key, array $data): void
|
||||
{
|
||||
$query = $pdo->prepare('INSERT OR IGNORE INTO sections (page_slug, key, title, lead, style, body, position)
|
||||
SELECT ?, ?, ?, ?, ?, ?, COALESCE(MAX(position), -1) + 1 FROM sections WHERE page_slug = ?');
|
||||
$query->execute([$page, $key, $data['title'], $data['lead'], $data['style'], $data['body'], $page]);
|
||||
$query = $pdo->prepare('INSERT OR IGNORE INTO sections (page_slug, key, title, lead, style, body, feed, position)
|
||||
SELECT ?, ?, ?, ?, ?, ?, ?, COALESCE(MAX(position), -1) + 1 FROM sections WHERE page_slug = ?');
|
||||
$query->execute([$page, $key, $data['title'], $data['lead'], $data['style'], $data['body'], $data['feed'] ?? null, $page]);
|
||||
if ($query->rowCount() !== 1) {
|
||||
throw new RuntimeException('此区块锚点已被使用。');
|
||||
}
|
||||
@@ -159,23 +165,44 @@ final class Repository
|
||||
$pdo->prepare('UPDATE pages SET updated_at = ?' . $revision . ' WHERE slug = ?')->execute([self::now(), $slug]);
|
||||
}
|
||||
|
||||
public function articles(?string $category = null, ?string $tag = null, bool $public = true, int $limit = 30, int $offset = 0): array
|
||||
public function categories(): array
|
||||
{
|
||||
$sql = 'SELECT * FROM articles WHERE 1 = 1';
|
||||
return $this->pdo->query('SELECT slug, name FROM article_categories ORDER BY rowid')->fetchAll(PDO::FETCH_KEY_PAIR);
|
||||
}
|
||||
|
||||
public function createCategory(string $slug, string $name): void
|
||||
{
|
||||
$query = $this->pdo->prepare('INSERT OR IGNORE INTO article_categories (slug, name) VALUES (?, ?)');
|
||||
$query->execute([$slug, $name]);
|
||||
if ($query->rowCount() !== 1) {
|
||||
throw new RuntimeException('此分类网址标识已被使用。');
|
||||
}
|
||||
}
|
||||
|
||||
public function articles(string|array|null $category = null, ?string $tag = null, bool $public = true,
|
||||
int $limit = 30, int $offset = 0, bool $directoryOrder = false): array
|
||||
{
|
||||
if ($category === []) {
|
||||
return [];
|
||||
}
|
||||
$sql = 'SELECT ' . self::ARTICLE_FIELDS . ' FROM articles WHERE 1 = 1';
|
||||
$args = [];
|
||||
if ($public) {
|
||||
$sql .= " AND status = 'published' AND published_at IS NOT NULL";
|
||||
$sql .= " AND status = 'published'";
|
||||
}
|
||||
if ($category !== null) {
|
||||
$sql .= ' AND category = ?';
|
||||
$args[] = $category;
|
||||
$args = (array) $category;
|
||||
$marks = implode(', ', array_fill(0, count($args), '?'));
|
||||
$sql .= " AND EXISTS (SELECT 1 FROM article_category_links
|
||||
WHERE article_id = articles.id AND category_slug IN ($marks))";
|
||||
}
|
||||
if ($tag !== null) {
|
||||
$sql .= ' AND EXISTS (SELECT 1 FROM json_each(articles.tags) WHERE value = ?)';
|
||||
$args[] = $tag;
|
||||
}
|
||||
$order = $public ? 'published_at' : 'updated_at';
|
||||
$query = $this->pdo->prepare($sql . ' ORDER BY ' . $order . ' DESC, id DESC LIMIT ? OFFSET ?');
|
||||
$order = $directoryOrder ? "COALESCE(json_extract(card, '$.position'), 0), id" : "$order DESC, id DESC";
|
||||
$query = $this->pdo->prepare($sql . ' ORDER BY ' . $order . ' LIMIT ? OFFSET ?');
|
||||
$query->execute([...$args, $limit, $offset]);
|
||||
|
||||
return array_map(self::decodeArticle(...), $query->fetchAll());
|
||||
@@ -183,12 +210,12 @@ final class Repository
|
||||
|
||||
public function articleById(int $id): ?array
|
||||
{
|
||||
return self::decodeArticle($this->one('SELECT * FROM articles WHERE id = ?', [$id]));
|
||||
return self::decodeArticle($this->one('SELECT ' . self::ARTICLE_FIELDS . ' FROM articles WHERE id = ?', [$id]));
|
||||
}
|
||||
|
||||
public function published(string $slug): ?array
|
||||
{
|
||||
return self::decodeArticle($this->one("SELECT * FROM articles WHERE slug = ? AND status = 'published'", [$slug]));
|
||||
return self::decodeArticle($this->one('SELECT ' . self::ARTICLE_FIELDS . " FROM articles WHERE slug = ? AND status = 'published'", [$slug]));
|
||||
}
|
||||
|
||||
public function articleRedirect(string $slug): ?array
|
||||
@@ -215,26 +242,34 @@ final class Repository
|
||||
if (($existing && $existing['id'] !== $id) || ($redirect && $redirect['article_id'] !== $id)) {
|
||||
throw new RuntimeException('此网址标识已被使用。');
|
||||
}
|
||||
$publishedAt = $old['published_at'] ?? ($data['status'] === 'published' ? self::now() : null);
|
||||
$publishedAt = $old['published_at'] ?? ($data['status'] === 'published' && ($old['status'] ?? '') !== 'published' ? self::now() : null);
|
||||
$values = [
|
||||
$data['slug'], $data['title'], $data['category'], $data['summary'], $data['body'],
|
||||
$data['slug'], $data['title'], $data['summary'], $data['body'],
|
||||
$data['body_format'], Database::json($data['card']),
|
||||
Database::json($data['tags']), $data['status'], $publishedAt, self::now(),
|
||||
];
|
||||
if ($id === null) {
|
||||
$query = $pdo->prepare('INSERT INTO articles
|
||||
(slug, title, category, summary, body, tags, status, published_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)');
|
||||
(slug, title, summary, body, body_format, card, tags, status, published_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
|
||||
$query->execute($values);
|
||||
|
||||
return (int) $pdo->lastInsertId();
|
||||
$id = (int) $pdo->lastInsertId();
|
||||
} else {
|
||||
if ($old['slug'] !== $data['slug']) {
|
||||
$pdo->prepare('DELETE FROM article_redirects WHERE slug = ? AND article_id = ?')->execute([$data['slug'], $id]);
|
||||
$pdo->prepare('INSERT INTO article_redirects (slug, article_id) VALUES (?, ?)')->execute([$old['slug'], $id]);
|
||||
}
|
||||
$query = $pdo->prepare('UPDATE articles SET slug = ?, title = ?, summary = ?, body = ?,
|
||||
body_format = ?, card = ?, tags = ?, status = ?, published_at = ?, updated_at = ?, version = version + 1
|
||||
WHERE id = ? AND version = ?');
|
||||
$query->execute([...$values, $id, $version]);
|
||||
self::checkVersion($query->rowCount());
|
||||
$pdo->prepare('DELETE FROM article_category_links WHERE article_id = ?')->execute([$id]);
|
||||
}
|
||||
if ($old['slug'] !== $data['slug']) {
|
||||
$pdo->prepare('DELETE FROM article_redirects WHERE slug = ? AND article_id = ?')->execute([$data['slug'], $id]);
|
||||
$pdo->prepare('INSERT INTO article_redirects (slug, article_id) VALUES (?, ?)')->execute([$old['slug'], $id]);
|
||||
$link = $pdo->prepare('INSERT INTO article_category_links (article_id, category_slug) VALUES (?, ?)');
|
||||
foreach ($data['categories'] as $category) {
|
||||
$link->execute([$id, $category]);
|
||||
}
|
||||
$query = $pdo->prepare('UPDATE articles SET slug = ?, title = ?, category = ?, summary = ?, body = ?, tags = ?,
|
||||
status = ?, published_at = ?, updated_at = ?, version = version + 1 WHERE id = ? AND version = ?');
|
||||
$query->execute([...$values, $id, $version]);
|
||||
self::checkVersion($query->rowCount());
|
||||
|
||||
return $id;
|
||||
});
|
||||
@@ -242,7 +277,9 @@ final class Repository
|
||||
|
||||
public function sitemapArticles(): iterable
|
||||
{
|
||||
$query = $this->pdo->query("SELECT slug, updated_at FROM articles WHERE status = 'published' AND trim(body) <> '' ORDER BY slug");
|
||||
$query = $this->pdo->query("SELECT slug, updated_at FROM articles WHERE status = 'published'
|
||||
AND (trim(body) <> '' OR trim(summary) <> '' OR json_extract(card, '$.image') <> '' OR json_extract(card, '$.url') <> '')
|
||||
ORDER BY slug");
|
||||
yield from $query;
|
||||
}
|
||||
|
||||
@@ -297,7 +334,9 @@ final class Repository
|
||||
private static function decodeArticle(?array $article): ?array
|
||||
{
|
||||
if ($article !== null) {
|
||||
$article['tags'] = json_decode($article['tags'], true, flags: JSON_THROW_ON_ERROR);
|
||||
foreach (['tags', 'categories', 'card'] as $field) {
|
||||
$article[$field] = json_decode($article[$field], true, flags: JSON_THROW_ON_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
return $article;
|
||||
|
||||
@@ -58,50 +58,4 @@ final class SiteDesign
|
||||
$pdo->prepare('UPDATE site_settings SET payload = ? WHERE id = 1')->execute([Database::json($site)]);
|
||||
});
|
||||
}
|
||||
|
||||
public static function catalog(): array
|
||||
{
|
||||
return Database::connect()->query('SELECT * FROM catalog ORDER BY position, id')->fetchAll();
|
||||
}
|
||||
|
||||
public static function product(int $id): ?array
|
||||
{
|
||||
$query = Database::connect()->prepare('SELECT * FROM catalog WHERE id = ?');
|
||||
$query->execute([$id]);
|
||||
|
||||
return $query->fetch() ?: null;
|
||||
}
|
||||
|
||||
public static function saveProduct(?int $id, array $product, int $version): int
|
||||
{
|
||||
$pdo = Database::connect();
|
||||
$fields = ['name', 'category', 'body', 'image', 'alt', 'url', 'label', 'extra_url', 'extra_label', 'position'];
|
||||
$values = array_map(static fn (string $field) => $product[$field], $fields);
|
||||
if ($id === null) {
|
||||
$pdo->prepare('INSERT INTO catalog (' . implode(', ', $fields) . ') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)')
|
||||
->execute($values);
|
||||
|
||||
return (int) $pdo->lastInsertId();
|
||||
}
|
||||
|
||||
$query = $pdo->prepare('UPDATE catalog SET ' . implode(' = ?, ', $fields) . ' = ?, version = version + 1 WHERE id = ? AND version = ?');
|
||||
$query->execute([...$values, $id, $version]);
|
||||
self::checkProduct($query->rowCount());
|
||||
|
||||
return $id;
|
||||
}
|
||||
|
||||
public static function deleteProduct(int $id, int $version): void
|
||||
{
|
||||
$query = Database::connect()->prepare('DELETE FROM catalog WHERE id = ? AND version = ?');
|
||||
$query->execute([$id, $version]);
|
||||
self::checkProduct($query->rowCount());
|
||||
}
|
||||
|
||||
private static function checkProduct(int $count): void
|
||||
{
|
||||
if ($count !== 1) {
|
||||
throw new RuntimeException('此项目已在另一页面更新,请重新打开。');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,13 +15,16 @@ final class View
|
||||
$html = self::fragment($template, $data);
|
||||
$page = Assets::rewrite(self::fragment($admin ? 'admin-layout' : 'layout', $data + ['content' => $html]), $cdn);
|
||||
$assetSources = $cdn === '' ? "'self'" : "'self' " . $cdn;
|
||||
$articleEditor = $admin && !empty($data['articleEditor']);
|
||||
$editorImages = $articleEditor ? ' data:' : '';
|
||||
$editorStyles = $articleEditor ? "style-src-attr 'unsafe-inline'; " : '';
|
||||
$headers = [
|
||||
'Content-Type' => 'text/html; charset=utf-8',
|
||||
'X-Content-Type-Options' => 'nosniff',
|
||||
'Referrer-Policy' => 'strict-origin-when-cross-origin',
|
||||
'X-Frame-Options' => !empty($data['previewMode']) ? 'SAMEORIGIN' : 'DENY',
|
||||
'Content-Security-Policy' => "default-src 'self'; img-src 'self' https:; media-src 'self' https:; "
|
||||
. "style-src $assetSources; script-src $assetSources; font-src $assetSources; frame-src 'self'; frame-ancestors "
|
||||
'Content-Security-Policy' => "default-src 'self'; img-src 'self' https:$editorImages; media-src 'self' https:; "
|
||||
. "style-src $assetSources; {$editorStyles}script-src $assetSources; font-src $assetSources; frame-src 'self'; frame-ancestors "
|
||||
. (!empty($data['previewMode']) ? "'self'" : "'none'") . "; form-action 'self'; base-uri 'none'; object-src 'none'",
|
||||
];
|
||||
if ($admin || !empty($data['previewMode'])) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -6,7 +6,6 @@ namespace App\Controller;
|
||||
|
||||
use App\Content\Html;
|
||||
use App\Content\Repository;
|
||||
use App\Content\SiteDesign;
|
||||
use App\Content\View;
|
||||
use support\Request;
|
||||
use support\Response;
|
||||
@@ -32,6 +31,22 @@ final class SiteController
|
||||
{
|
||||
$slug = $page['slug'];
|
||||
$isHome = $slug === 'home';
|
||||
$categories = $repo->categories();
|
||||
foreach ($page['sections'] as &$section) {
|
||||
if (!$section['feed']) {
|
||||
continue;
|
||||
}
|
||||
$feed = json_decode($section['feed'], true, flags: JSON_THROW_ON_ERROR);
|
||||
$tabs = $feed['mode'] === 'tabs' && count($feed['categories']) > 1;
|
||||
$section['articleFeed'] = [];
|
||||
foreach ($tabs ? $feed['categories'] : [''] as $category) {
|
||||
$section['articleFeed'][] = [
|
||||
'label' => $categories[$category] ?? '',
|
||||
'articles' => $repo->articles($category === '' ? $feed['categories'] : $category, limit: $feed['limit']),
|
||||
];
|
||||
}
|
||||
}
|
||||
unset($section);
|
||||
|
||||
return View::render('page', [
|
||||
'site' => $repo->site(),
|
||||
@@ -41,25 +56,51 @@ final class SiteController
|
||||
'description' => $page['description'],
|
||||
'socialImage' => $page['image'],
|
||||
'socialAlt' => $page['alt'],
|
||||
'articles' => $isHome ? $repo->articles(limit: 3) : [],
|
||||
'network' => $isHome ? $this->networkGroups($repo) : [],
|
||||
'previewMode' => $preview, 'canvas' => $preview && $canvas, 'noindex' => $preview,
|
||||
]);
|
||||
}
|
||||
|
||||
public function services(Request $request): Response
|
||||
{
|
||||
$repo = new Repository();
|
||||
|
||||
return View::render('services', [
|
||||
'site' => (new Repository())->site(), 'path' => '/services',
|
||||
'site' => $repo->site(), 'path' => '/services',
|
||||
'title' => '产品与服务 · 来笙 Laysense',
|
||||
'description' => '浏览来笙的建站工具、智能 DNS 和软件著作权服务。',
|
||||
'products' => SiteDesign::catalog(),
|
||||
'products' => $repo->articles('services', limit: -1, directoryOrder: true),
|
||||
]);
|
||||
}
|
||||
|
||||
public function network(Request $request): Response
|
||||
{
|
||||
$repo = new Repository();
|
||||
|
||||
return View::render('network', [
|
||||
'site' => $repo->site(), 'path' => '/network', 'title' => '支持项目与合作伙伴 · 来笙',
|
||||
'description' => '浏览来笙支持的开源项目和合作伙伴。', 'network' => $this->networkGroups($repo),
|
||||
]);
|
||||
}
|
||||
|
||||
private function networkGroups(Repository $repo): array
|
||||
{
|
||||
$groups = [];
|
||||
foreach (array_diff_key(Repository::DIRECTORIES, ['services' => true]) as $slug => $name) {
|
||||
$groups[$slug] = ['name' => $name, 'articles' => $repo->articles($slug, limit: -1, directoryOrder: true)];
|
||||
}
|
||||
|
||||
return $groups;
|
||||
}
|
||||
|
||||
public function archive(Request $request, ?string $category = null, ?string $tag = null): Response
|
||||
{
|
||||
$repo = new Repository();
|
||||
$title = $category ? Repository::CATEGORIES[$category] : ($tag ?: '来笙动态');
|
||||
$categories = $repo->categories();
|
||||
if ($category !== null && !isset($categories[$category])) {
|
||||
return $this->missing($request);
|
||||
}
|
||||
$title = $category ? $categories[$category] : ($tag ?: '来笙动态');
|
||||
$path = '/articles' . ($category ? '/category/' . $category : ($tag ? '/tag/' . rawurlencode($tag) : ''));
|
||||
$number = $request->get('page', '1');
|
||||
if (!is_scalar($number) || !preg_match('/^[1-9][0-9]{0,4}$/D', (string) $number)) {
|
||||
@@ -75,7 +116,7 @@ final class SiteController
|
||||
'site' => $repo->site(), 'title' => $title . ' · 来笙', 'headline' => $title,
|
||||
'description' => '查看来笙的公告、新闻和产品动态。',
|
||||
'path' => $path . ($page > 1 ? '?page=' . $page : ''), 'archivePath' => $path,
|
||||
'category' => $category, 'tag' => $tag, 'pageNumber' => $page,
|
||||
'categories' => $categories, 'category' => $category, 'tag' => $tag, 'pageNumber' => $page,
|
||||
'hasNext' => count($articles) > 20, 'articles' => array_slice($articles, 0, 20),
|
||||
'noindex' => $articles === [],
|
||||
]);
|
||||
@@ -100,13 +141,15 @@ final class SiteController
|
||||
|
||||
return $redirect ? View::redirect('/articles/' . $redirect['slug'], 301) : $this->missing($request);
|
||||
}
|
||||
$articleHtml = Html::markdown($article['body']);
|
||||
$articleHtml = Html::articleBody($article);
|
||||
|
||||
return View::render('article', [
|
||||
'site' => $repo->site(), 'article' => $article, 'articleHtml' => $articleHtml,
|
||||
'title' => $article['title'] . ' · 来笙',
|
||||
'description' => $article['summary'] ?: mb_substr(trim(strip_tags($articleHtml)), 0, 150) ?: $article['title'],
|
||||
'path' => '/articles/' . $article['slug'], 'noindex' => trim($articleHtml) === '',
|
||||
'path' => '/articles/' . $article['slug'],
|
||||
'socialImage' => $article['card']['image'] ?? '', 'socialAlt' => $article['card']['alt'] ?? '',
|
||||
'noindex' => trim($articleHtml) === '' && !$article['summary'] && empty($article['card']['url']) && empty($article['card']['image']),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -138,6 +181,7 @@ final class SiteController
|
||||
$xml = ['<?xml version="1.0" encoding="UTF-8"?>', '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'];
|
||||
$pages = $repo->pages(publishedOnly: true);
|
||||
$pages[] = ['slug' => 'services', 'updated_at' => null];
|
||||
$pages[] = ['slug' => 'network', 'updated_at' => null];
|
||||
if ($repo->articles(limit: 1)) {
|
||||
$pages[] = ['slug' => 'articles', 'updated_at' => null];
|
||||
}
|
||||
@@ -165,7 +209,7 @@ final class SiteController
|
||||
$normal = rtrim($path, '/');
|
||||
$single = preg_match('~^/[a-z0-9-]+$~D', $normal);
|
||||
$known = $single && ((new Repository())->page(substr($normal, 1))['status'] ?? null) === 'published' && $normal !== '/home';
|
||||
if ($known || $normal === '/services' || preg_match('~^/articles(?:/[a-z0-9-]+){0,2}$~D', $normal)) {
|
||||
if ($known || in_array($normal, ['/services', '/network'], true) || preg_match('~^/articles(?:/[a-z0-9-]+){0,2}$~D', $normal)) {
|
||||
$query = $request->queryString();
|
||||
|
||||
return View::redirect($normal . ($query !== '' ? '?' . $query : ''), 301);
|
||||
|
||||
@@ -27,7 +27,7 @@ final class StudioController extends AdminController
|
||||
$data['name'] = $this->input($request, 'name');
|
||||
$data['slug'] = $this->input($request, 'slug');
|
||||
try {
|
||||
if (!$this->slug($data['slug']) || in_array($data['slug'], ['admin', 'assets', 'articles', 'home', 'services', 'news', 'main', 'favicon'], true)
|
||||
if (!$this->slug($data['slug']) || in_array($data['slug'], ['admin', 'assets', 'articles', 'home', 'services', 'network', 'news', 'main', 'favicon'], true)
|
||||
|| $data['name'] === '' || mb_strlen($data['name']) > 100) {
|
||||
throw new RuntimeException('请填写页面名称及可用的网址标识。');
|
||||
}
|
||||
@@ -65,15 +65,16 @@ final class StudioController extends AdminController
|
||||
$action = $this->input($request, 'operation');
|
||||
$key = $this->input($request, 'key');
|
||||
try {
|
||||
if (!$this->slug($key) || in_array($key, ['preview', 'home', 'contact', 'main-content', 'blog', 'partners', 'supported-projects'], true)) {
|
||||
if (!$this->slug($key) || in_array($key, ['preview', 'home', 'contact', 'main-content', 'partners', 'supported-projects'], true)) {
|
||||
throw new RuntimeException('区块锚点不可用。');
|
||||
}
|
||||
if ($action === 'create') {
|
||||
$preset = BlockTemplates::find($this->input($request, 'preset'));
|
||||
$type = $this->input($request, 'preset');
|
||||
$preset = BlockTemplates::find($type);
|
||||
if ($preset === null) {
|
||||
throw new RuntimeException('请选择区块类型。');
|
||||
}
|
||||
$repo->createSection($slug, $key, $preset[0], 'plain', $preset[1]);
|
||||
$repo->createSection($slug, $key, $preset[0], 'plain', $preset[1], $type === 'articles' ? BlockTemplates::ARTICLE_FEED : null);
|
||||
|
||||
return $this->success($request, "/admin/pages/$slug/$key");
|
||||
}
|
||||
@@ -121,53 +122,6 @@ final class StudioController extends AdminController
|
||||
], $error === '' ? 200 : 422);
|
||||
}
|
||||
|
||||
public function catalog(Request $request): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
return $failure;
|
||||
}
|
||||
|
||||
return $this->render($request, 'studio-catalog', ['title' => '产品与服务', 'products' => SiteDesign::catalog()]);
|
||||
}
|
||||
|
||||
public function product(Request $request, ?string $id = null): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
return $failure;
|
||||
}
|
||||
$fields = ['name', 'category', 'body', 'image', 'alt', 'url', 'label', 'extra_url', 'extra_label', 'position'];
|
||||
$product = $id !== null ? SiteDesign::product((int) $id) : array_fill_keys($fields, '') + ['version' => 0];
|
||||
if (!$product) {
|
||||
return $this->error($request, '找不到此产品或服务。', 404);
|
||||
}
|
||||
$error = '';
|
||||
if ($request->method() === 'POST') {
|
||||
try {
|
||||
if ($id !== null && $this->input($request, 'operation') === 'delete') {
|
||||
SiteDesign::deleteProduct((int) $id, $this->version($request));
|
||||
|
||||
return $this->success($request, '/admin/catalog');
|
||||
}
|
||||
foreach ($fields as $field) {
|
||||
$product[$field] = $this->input($request, $field);
|
||||
}
|
||||
$product['version'] = $this->version($request);
|
||||
$this->checkProduct($product);
|
||||
$product['body'] = Html::sanitize($product['body']);
|
||||
$savedId = SiteDesign::saveProduct($id === null ? null : (int) $id, $product, $product['version']);
|
||||
|
||||
return $this->success($request, '/admin/catalog/' . $savedId . '?saved=1');
|
||||
} catch (RuntimeException $exception) {
|
||||
$error = $this->validationMessage($exception);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render($request, 'studio-product', [
|
||||
'title' => $id === null ? '添加产品或服务' : '编辑产品或服务', 'product' => $product,
|
||||
'id' => $id, 'error' => $error, 'saved' => $request->get('saved') === '1',
|
||||
], $error === '' ? 200 : 422);
|
||||
}
|
||||
|
||||
public function media(Request $request): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
@@ -256,23 +210,6 @@ final class StudioController extends AdminController
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function checkProduct(array $item): void
|
||||
{
|
||||
if ($item['name'] === '' || mb_strlen($item['name']) > 100 || $item['category'] === '' || mb_strlen($item['category']) > 70
|
||||
|| strlen($item['body']) > 300000 || strlen($item['image']) > 1000 || mb_strlen($item['alt']) > 200
|
||||
|| ($item['image'] !== '' && !Html::url($item['image'], true)) || !preg_match('/^-?\d{1,6}$/D', $item['position'])) {
|
||||
throw new RuntimeException('请检查名称、分类、简介、封面及排序字段。');
|
||||
}
|
||||
foreach ([['url', 'label'], ['extra_url', 'extra_label']] as [$href, $label]) {
|
||||
if ($href === 'extra_url' && $item[$href] === '' && $item[$label] === '') {
|
||||
continue;
|
||||
}
|
||||
if (strlen($item[$href]) > 1000 || !Html::url($item[$href]) || $item[$label] === '' || mb_strlen($item[$label]) > 70) {
|
||||
throw new RuntimeException('请填写按钮文案和有效链接。');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function slug(string $value): bool
|
||||
{
|
||||
return strlen($value) <= 70 && preg_match('/^[a-z0-9]+(?:-[a-z0-9]+)*$/D', $value) === 1;
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
use App\Content\View;
|
||||
|
||||
$e = View::escape(...);
|
||||
?>
|
||||
<a class="back-link" href="/admin/articles">返回文章列表</a>
|
||||
<header class="admin-heading"><h1>文章分类</h1></header>
|
||||
<?= View::fragment('admin-feedback', compact('error', 'saved')) ?>
|
||||
<form class="admin-form admin-panel" method="post" action="/admin/article-categories">
|
||||
<input type="hidden" name="csrf" value="<?= $e($token) ?>">
|
||||
<div class="form-grid">
|
||||
<label>分类名称<input name="name" value="<?= $e($name) ?>" required maxlength="60"></label>
|
||||
<label>网址标识<input name="slug" value="<?= $e($slug) ?>" required maxlength="60" pattern="[a-z0-9]+(-[a-z0-9]+)*"></label>
|
||||
</div>
|
||||
<div class="form-submit"><button class="button button-primary" type="submit">创建分类</button></div>
|
||||
</form>
|
||||
<nav class="filter-nav" aria-label="查看分类">
|
||||
<?php foreach ($categories as $key => $label): ?>
|
||||
<a href="/admin/articles?category=<?= $e($key) ?>"><?= $e($label) ?></a>
|
||||
<?php endforeach ?>
|
||||
</nav>
|
||||
@@ -1,5 +1,4 @@
|
||||
<?php
|
||||
use App\Content\Repository;
|
||||
use App\Content\View;
|
||||
|
||||
$e = View::escape(...);
|
||||
@@ -9,7 +8,7 @@ $e = View::escape(...);
|
||||
<?php foreach ($articles as $item): ?>
|
||||
<li>
|
||||
<div><strong><?= $e($item['title']) ?></strong>
|
||||
<small><?= $e(Repository::CATEGORIES[$item['category']]) ?> · <?= $item['status'] === 'published' ? '已发布' : '草稿' ?> · <?= $e(View::date($item['updated_at'])) ?></small>
|
||||
<small><?= $e(implode('、', $item['categories'])) ?> · <?= $item['status'] === 'published' ? '已发布' : '草稿' ?> · <?= $e(View::date($item['updated_at'])) ?></small>
|
||||
</div>
|
||||
<a class="button button-outline" href="/admin/articles/<?= $e($item['id']) ?>">编辑文章</a>
|
||||
</li>
|
||||
|
||||
@@ -16,17 +16,33 @@ $e = View::escape(...);
|
||||
<input type="hidden" name="csrf" value="<?= $e($token) ?>">
|
||||
<input type="hidden" name="version" value="<?= $e($article['version']) ?>">
|
||||
<label>文章标题<input name="title" value="<?= $e($article['title']) ?>" required maxlength="120"></label>
|
||||
<div class="form-grid">
|
||||
<label>网址标识<input name="slug" value="<?= $e($article['slug']) ?>" required maxlength="100" pattern="[a-z0-9]+(-[a-z0-9]+)*" placeholder="product-update"><small>使用小写英文字母、数字和连字符。</small></label>
|
||||
<label>分类<select name="category">
|
||||
<?php foreach (Repository::CATEGORIES as $key => $label): ?>
|
||||
<option value="<?= $e($key) ?>" <?= $article['category'] === $key ? 'selected' : '' ?>><?= $e($label) ?></option>
|
||||
<?php endforeach ?>
|
||||
</select></label>
|
||||
</div>
|
||||
<label>网址标识<input name="slug" value="<?= $e($article['slug']) ?>" required maxlength="100" pattern="[a-z0-9]+(-[a-z0-9]+)*" placeholder="product-update"><small>使用小写英文字母、数字和连字符。</small></label>
|
||||
<?= View::fragment('admin-category-checkboxes', ['name' => 'categories', 'selected' => array_keys($article['categories']), 'categories' => $categories, 'legend' => '文章分类(可多选)']) ?>
|
||||
<label>标签<input name="tags" value="<?= $e(implode(',', $article['tags'])) ?>" placeholder="用逗号分隔"></label>
|
||||
<label>摘要<textarea name="summary" rows="2" maxlength="250"><?= $e($article['summary']) ?></textarea><small>用于动态列表和搜索摘要。</small></label>
|
||||
<?= View::fragment('admin-editor', ['format' => 'markdown', 'body' => $article['body']]) ?>
|
||||
<div class="article-editor" data-article-editor>
|
||||
<div class="article-editor-heading">
|
||||
<label for="article-source" data-article-editor-label>正文</label>
|
||||
<button type="button" class="button button-outline" data-insert-article-image hidden>从媒体库插入图片</button>
|
||||
</div>
|
||||
<input type="hidden" name="body_format" value="<?= $e($article['body_format']) ?>">
|
||||
<textarea id="article-source" name="body" rows="16" aria-label="文章正文"><?= $e($article['body']) ?></textarea>
|
||||
<div data-editor-host hidden></div>
|
||||
<p class="article-editor-help" data-article-editor-status role="status"></p>
|
||||
</div>
|
||||
<details class="admin-options" <?= array_intersect(array_keys($article['categories']), array_keys(Repository::DIRECTORIES)) ? 'open' : '' ?>>
|
||||
<summary>目录卡片</summary>
|
||||
<div class="form-grid">
|
||||
<?= View::fragment('studio-asset-field', ['name' => 'card_image', 'label' => '封面或标志', 'value' => $article['card']['image'] ?? '']) ?>
|
||||
<label>图片替代文字<input name="card_alt" value="<?= $e($article['card']['alt'] ?? '') ?>" maxlength="120"></label>
|
||||
<label>主链接<input name="card_url" value="<?= $e($article['card']['url'] ?? '') ?>"></label>
|
||||
<label>主链接文字<input name="card_label" value="<?= $e($article['card']['label'] ?? '') ?>" maxlength="120"></label>
|
||||
<label>附加链接<input name="card_extra_url" value="<?= $e($article['card']['extra_url'] ?? '') ?>"></label>
|
||||
<label>附加链接文字<input name="card_extra_label" value="<?= $e($article['card']['extra_label'] ?? '') ?>" maxlength="120"></label>
|
||||
<label>类型说明<input name="card_category_label" value="<?= $e($article['card']['category_label'] ?? '') ?>" maxlength="120"></label>
|
||||
<label>目录排序(从小到大)<input name="card_position" type="number" min="0" max="9999" value="<?= $e($article['card']['position'] ?? 0) ?>"></label>
|
||||
</div>
|
||||
</details>
|
||||
<div class="form-submit">
|
||||
<label>发布状态<select name="status">
|
||||
<option value="draft" <?= $article['status'] === 'draft' ? 'selected' : '' ?>>草稿</option>
|
||||
|
||||
@@ -1,17 +1,30 @@
|
||||
<?php
|
||||
use App\Content\View;
|
||||
|
||||
$e = View::escape(...);
|
||||
$filters = $category ? 'category=' . rawurlencode($category) . '&' : '';
|
||||
|
||||
?>
|
||||
<header class="admin-heading">
|
||||
<h1>文章与动态</h1>
|
||||
<a class="button button-primary" href="/admin/articles/new">新建文章</a>
|
||||
<div class="actions">
|
||||
<a class="button button-outline" href="/admin/article-categories">管理分类</a>
|
||||
<a class="button button-primary" href="/admin/articles/new<?= $category ? '?category=' . $e($category) : '' ?>">新建文章</a>
|
||||
</div>
|
||||
</header>
|
||||
<nav class="filter-nav" aria-label="筛选文章">
|
||||
<a href="/admin/articles" <?= $category === null ? 'aria-current="page"' : '' ?>>全部</a>
|
||||
<?php foreach ($categories as $key => $name): ?>
|
||||
<a href="/admin/articles?category=<?= $e($key) ?>" <?= $category === $key ? 'aria-current="page"' : '' ?>><?= $e($name) ?></a>
|
||||
<?php endforeach ?>
|
||||
</nav>
|
||||
<div class="admin-panel">
|
||||
<?= View::fragment('admin-article-list', ['articles' => $articles]) ?>
|
||||
</div>
|
||||
<?php if ($pageNumber > 1 || $hasNext): ?>
|
||||
<nav class="pagination" aria-label="文章分页">
|
||||
<?php if ($pageNumber > 1): ?><a class="button button-outline" href="/admin/articles?page=<?= $pageNumber - 1 ?>">上一页</a><?php endif ?>
|
||||
<?php if ($pageNumber > 1): ?><a class="button button-outline" href="/admin/articles?<?= $e($filters) ?>page=<?= $pageNumber - 1 ?>">上一页</a><?php endif ?>
|
||||
<span>第 <?= $pageNumber ?> 页</span>
|
||||
<?php if ($hasNext): ?><a class="button button-outline" href="/admin/articles?page=<?= $pageNumber + 1 ?>">下一页</a><?php endif ?>
|
||||
<?php if ($hasNext): ?><a class="button button-outline" href="/admin/articles?<?= $e($filters) ?>page=<?= $pageNumber + 1 ?>">下一页</a><?php endif ?>
|
||||
</nav>
|
||||
<?php endif ?>
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
use App\Content\View;
|
||||
|
||||
$e = View::escape(...);
|
||||
?>
|
||||
<fieldset class="category-checks">
|
||||
<legend><?= $e($legend) ?></legend>
|
||||
<div>
|
||||
<?php foreach ($categories as $key => $label): ?>
|
||||
<label><input type="checkbox" name="<?= $e($name) ?>[]" value="<?= $e($key) ?>" <?= in_array($key, $selected, true) ? 'checked' : '' ?>> <?= $e($label) ?></label>
|
||||
<?php endforeach ?>
|
||||
</div>
|
||||
</fieldset>
|
||||
@@ -16,6 +16,12 @@ $e = View::escape(...);
|
||||
<script src="/assets/jquery-4.0.0.min.js" defer></script>
|
||||
<script src="/assets/admin.js" defer></script>
|
||||
<script src="/assets/studio.js" defer></script>
|
||||
<?php if (!empty($articleEditor)): ?>
|
||||
<link rel="stylesheet" href="/assets/vendor/toastui/toastui-editor.min.css">
|
||||
<script src="/assets/vendor/toastui/toastui-editor.min.js" defer></script>
|
||||
<script src="/assets/vendor/toastui/zh-cn.js" defer></script>
|
||||
<script src="/assets/article-editor.js" defer></script>
|
||||
<?php endif ?>
|
||||
</head>
|
||||
<body class="admin-body">
|
||||
<a class="skip-link" href="#admin-content">跳到内容</a>
|
||||
@@ -27,7 +33,6 @@ $e = View::escape(...);
|
||||
<a href="/admin">页面</a>
|
||||
<a href="/admin/articles">文章</a>
|
||||
<a href="/admin/media">媒体库</a>
|
||||
<a href="/admin/catalog">产品与服务</a>
|
||||
<a href="/admin/settings/header">网站设置</a>
|
||||
<a href="/admin/password">修改密码</a>
|
||||
<a href="/">访问网站</a>
|
||||
|
||||
@@ -91,19 +91,19 @@ $previewUrl = '/admin/pages/' . $slug . '/preview';
|
||||
</div>
|
||||
<div class="studio-frame-window">
|
||||
<iframe title="可编辑页面画布" src="<?= $e($previewUrl) ?>?canvas=1" sandbox="allow-same-origin"></iframe>
|
||||
<div class="studio-element-tools" data-element-tools role="group" aria-label="所选元素" hidden>
|
||||
<button type="button" data-element-properties>属性</button>
|
||||
<button type="button" data-element-copy>复制</button>
|
||||
<button type="button" data-element-delete>删除</button>
|
||||
</div>
|
||||
<button class="studio-media-handle" type="button" role="slider" aria-label="媒体宽度" aria-valuemin="10" aria-valuemax="100" aria-valuenow="100" data-media-handle hidden></button>
|
||||
<div class="studio-media-sizing" data-media-sizing hidden>
|
||||
<label>媒体宽度
|
||||
<select data-media-width>
|
||||
<option value="0">原始尺寸</option>
|
||||
<?php foreach ([25, 40, 50, 60, 75, 85, 100] as $scale): ?>
|
||||
<option value="<?= $scale ?>"><?= $scale ?>%</option>
|
||||
<?php endforeach ?>
|
||||
</select>
|
||||
</label>
|
||||
<output data-media-size aria-hidden="true"></output>
|
||||
<button class="button button-outline" type="button" data-reset-media>原始尺寸</button>
|
||||
<button class="button button-outline" type="button" data-replace-media>替换媒体</button>
|
||||
</div>
|
||||
</div>
|
||||
<p class="field-note">直接编辑画布文字。单击媒体调整宽度,双击替换。</p>
|
||||
<p class="field-note">单击画布中的元素可打开浮动工具条。选中媒体后可拖动边缘手柄调整宽度。</p>
|
||||
<dialog class="media-picker canvas-properties" data-canvas-properties aria-labelledby="canvas-properties-heading">
|
||||
<div class="admin-heading compact"><h2 id="canvas-properties-heading" data-property-heading>编辑属性</h2></div>
|
||||
<form class="admin-form" method="dialog">
|
||||
|
||||
@@ -4,6 +4,7 @@ use App\Content\View;
|
||||
$e = View::escape(...);
|
||||
$slug = $section['page_slug'];
|
||||
$key = $section['key'];
|
||||
$feed = json_decode($section['feed'] ?? '{}', true, flags: JSON_THROW_ON_ERROR);
|
||||
?>
|
||||
<a class="back-link" href="/admin/pages/<?= $e($slug) ?>">返回页面编辑</a>
|
||||
<header class="admin-heading">
|
||||
@@ -21,6 +22,18 @@ $key = $section['key'];
|
||||
<option value="<?= $e($style) ?>" <?= $style === $section['style'] ? 'selected' : '' ?>><?= $e(['plain' => '默认', 'tinted' => '浅色背景'][$style] ?? '保留原布局') ?></option>
|
||||
<?php endforeach ?>
|
||||
</select></label>
|
||||
<details class="admin-options" <?= $feed ? 'open' : '' ?>>
|
||||
<summary>文章组件</summary>
|
||||
<div class="form-grid">
|
||||
<label>展示方式<select name="feed_mode">
|
||||
<?php foreach (['off' => '不展示', 'merged' => '合并为一个列表', 'tabs' => '按分类分标签页'] as $mode => $label): ?>
|
||||
<option value="<?= $mode ?>" <?= ($feed['mode'] ?? 'off') === $mode ? 'selected' : '' ?>><?= $e($label) ?></option>
|
||||
<?php endforeach ?>
|
||||
</select></label>
|
||||
<label>展示数量<input name="feed_limit" type="number" min="1" max="30" value="<?= $e($feed['limit'] ?? 6) ?>"><small>分标签页时,每个分类独立计数。</small></label>
|
||||
</div>
|
||||
<?= View::fragment('admin-category-checkboxes', ['name' => 'feed_categories', 'selected' => $feed['categories'] ?? [], 'categories' => $categories, 'legend' => '展示哪些分类(可多选)']) ?>
|
||||
</details>
|
||||
<?= View::fragment('admin-editor', ['format' => 'html', 'body' => $section['body']]) ?>
|
||||
<div class="form-submit"><button class="button button-primary" type="submit">保存区块</button></div>
|
||||
</form>
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
<?php
|
||||
use App\Content\Repository;
|
||||
use App\Content\View;
|
||||
|
||||
$e = View::escape(...);
|
||||
@@ -13,7 +12,7 @@ $e = View::escape(...);
|
||||
</div>
|
||||
<nav class="filter-nav" aria-label="动态分类">
|
||||
<a href="/articles" <?= !$category && !$tag ? 'aria-current="page"' : '' ?>>全部</a>
|
||||
<?php foreach (Repository::CATEGORIES as $key => $label): ?>
|
||||
<?php foreach ($categories as $key => $label): ?>
|
||||
<a href="/articles/category/<?= $e($key) ?>" <?= $category === $key ? 'aria-current="page"' : '' ?>><?= $e($label) ?></a>
|
||||
<?php endforeach ?>
|
||||
</nav>
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
use App\Content\View;
|
||||
|
||||
$e = View::escape(...);
|
||||
?>
|
||||
<div class="article-feed" <?= count($groups) > 1 ? 'data-article-tabs' : '' ?>>
|
||||
<?php if (count($groups) > 1): ?>
|
||||
<div class="filter-nav article-tabs" data-tab-list role="tablist" aria-labelledby="heading-<?= $e($sectionKey) ?>" hidden>
|
||||
<?php foreach ($groups as $index => $group): ?>
|
||||
<button type="button" role="tab" id="<?= $e($sectionKey) ?>-tab-<?= $index ?>" aria-controls="<?= $e($sectionKey) ?>-articles-<?= $index ?>"><?= $e($group['label']) ?></button>
|
||||
<?php endforeach ?>
|
||||
</div>
|
||||
<?php endif ?>
|
||||
<?php foreach ($groups as $index => $group): ?>
|
||||
<div id="<?= $e($sectionKey) ?>-articles-<?= $index ?>" data-tab-panel>
|
||||
<?php if ($group['label'] !== ''): ?><h3 data-tab-heading><?= $e($group['label']) ?></h3><?php endif ?>
|
||||
<?= View::fragment('article-list', ['articles' => $group['articles']]) ?>
|
||||
</div>
|
||||
<?php endforeach ?>
|
||||
</div>
|
||||
@@ -1,5 +1,4 @@
|
||||
<?php
|
||||
use App\Content\Repository;
|
||||
use App\Content\View;
|
||||
|
||||
$e = View::escape(...);
|
||||
@@ -9,11 +8,11 @@ $e = View::escape(...);
|
||||
<?php foreach ($articles as $item): ?>
|
||||
<li>
|
||||
<a class="article-row" href="/articles/<?= $e($item['slug']) ?>">
|
||||
<span class="article-category"><?= $e(Repository::CATEGORIES[$item['category']]) ?></span>
|
||||
<span class="article-category"><?= $e(implode('、', $item['categories'])) ?></span>
|
||||
<span class="article-row-title"><?= $e($item['title']) ?>
|
||||
<?php if ($item['summary']): ?><small><?= $e($item['summary']) ?></small><?php endif ?>
|
||||
</span>
|
||||
<time datetime="<?= $e(View::date($item['published_at'], 'Y-m-d')) ?>"><?= $e(View::date($item['published_at'], 'Y.m.d')) ?></time>
|
||||
<?php if ($item['published_at']): ?><time datetime="<?= $e(View::date($item['published_at'], 'Y-m-d')) ?>"><?= $e(View::date($item['published_at'], 'Y.m.d')) ?></time><?php endif ?>
|
||||
</a>
|
||||
</li>
|
||||
<?php endforeach ?>
|
||||
|
||||
+16
-5
@@ -1,29 +1,40 @@
|
||||
<?php
|
||||
use App\Content\Repository;
|
||||
use App\Content\View;
|
||||
|
||||
$e = View::escape(...);
|
||||
?>
|
||||
<article class="section article-page" itemscope itemtype="https://schema.org/Article" data-enter>
|
||||
<?php $card = $article['card']; ?>
|
||||
<div class="wrap reading-wrap">
|
||||
<a class="back-link" href="/articles">全部动态</a>
|
||||
<header class="section-heading">
|
||||
<div class="article-meta">
|
||||
<a href="/articles/category/<?= $e($article['category']) ?>" itemprop="articleSection"><?= $e(Repository::CATEGORIES[$article['category']]) ?></a>
|
||||
<time itemprop="datePublished" datetime="<?= $e(View::date($article['published_at'], DATE_ATOM)) ?>"><?= $e(View::date($article['published_at'])) ?></time>
|
||||
<?php foreach ($article['categories'] as $key => $name): ?>
|
||||
<a href="/articles/category/<?= $e($key) ?>" itemprop="articleSection"><?= $e($name) ?></a>
|
||||
<?php endforeach ?>
|
||||
<?php if ($article['published_at']): ?><time itemprop="datePublished" datetime="<?= $e(View::date($article['published_at'], DATE_ATOM)) ?>"><?= $e(View::date($article['published_at'])) ?></time><?php endif ?>
|
||||
<meta itemprop="dateModified" content="<?= $e(View::date($article['updated_at'], DATE_ATOM)) ?>">
|
||||
</div>
|
||||
<h1 itemprop="headline"><?= $e($article['title']) ?></h1>
|
||||
<?php if ($article['summary']): ?><p itemprop="description"><?= $e($article['summary']) ?></p><?php endif ?>
|
||||
<span itemprop="author" itemscope itemtype="https://schema.org/Organization"><meta itemprop="name" content="<?= $e($site['name']) ?>"></span>
|
||||
</header>
|
||||
<?php if (!empty($card['image'])): ?>
|
||||
<img class="article-cover" src="<?= $e($card['image']) ?>" alt="<?= $e($card['alt'] ?? '') ?>">
|
||||
<?php endif ?>
|
||||
<div class="rich prose" itemprop="articleBody">
|
||||
<?php if (trim($articleHtml) !== ''): ?>
|
||||
<?= $articleHtml ?>
|
||||
<?php else: ?>
|
||||
<p class="empty-state">此公告的原文暂不可用。</p>
|
||||
<?php elseif (!$article['summary'] && empty($card['image']) && empty($card['url'])): ?>
|
||||
<p class="empty-state">此文章的原文暂不可用。</p>
|
||||
<?php endif ?>
|
||||
</div>
|
||||
<?php if (!empty($card['url']) || !empty($card['extra_url'])): ?>
|
||||
<div class="actions">
|
||||
<?php if (!empty($card['url'])): ?><a class="button button-primary" href="<?= $e($card['url']) ?>"><?= $e(($card['label'] ?? '') ?: '访问网站') ?></a><?php endif ?>
|
||||
<?php if (!empty($card['extra_url'])): ?><a href="<?= $e($card['extra_url']) ?>"><?= $e(($card['extra_label'] ?? '') ?: '相关链接') ?></a><?php endif ?>
|
||||
</div>
|
||||
<?php endif ?>
|
||||
<?php if ($article['tags']): ?>
|
||||
<nav class="filter-nav" aria-label="文章标签">
|
||||
<?php foreach ($article['tags'] as $tag): ?>
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
use App\Content\View;
|
||||
|
||||
$e = View::escape(...);
|
||||
?>
|
||||
<section class="section network" aria-label="支持项目与合作伙伴">
|
||||
<div class="wrap">
|
||||
<?php if (!$standalone): ?>
|
||||
<div class="section-heading inline-heading">
|
||||
<h2>支持项目与合作伙伴</h2>
|
||||
<a class="button button-outline" href="/network">查看全部</a>
|
||||
</div>
|
||||
<?php endif ?>
|
||||
<?php foreach ($network as $slug => $group): ?>
|
||||
<div class="network-group" id="<?= $e($slug) ?>">
|
||||
<?php if ($standalone): ?><h2><?= $e($group['name']) ?></h2><?php else: ?><h3><?= $e($group['name']) ?></h3><?php endif ?>
|
||||
<?php if (!$group['articles']): ?><p class="empty-state">暂无已发布条目。</p><?php endif ?>
|
||||
<ul class="network-list">
|
||||
<?php foreach ($group['articles'] as $item): ?>
|
||||
<?php $card = $item['card']; $articlePath = '/articles/' . $item['slug']; ?>
|
||||
<li>
|
||||
<a href="<?= $e(($card['url'] ?? '') ?: $articlePath) ?>">
|
||||
<?php if (!empty($card['image'])): ?>
|
||||
<img src="<?= $e($card['image']) ?>" alt="" loading="lazy" width="76" height="28">
|
||||
<?php endif ?>
|
||||
<span><?= $e($item['title']) ?></span>
|
||||
</a>
|
||||
<?php if ($standalone && !empty($card['url']) && ($item['body'] !== '' || $item['summary'] !== '')): ?>
|
||||
<a class="network-detail" href="<?= $e($articlePath) ?>">查看详情</a>
|
||||
<?php endif ?>
|
||||
</li>
|
||||
<?php endforeach ?>
|
||||
</ul>
|
||||
</div>
|
||||
<?php endforeach ?>
|
||||
</div>
|
||||
</section>
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
use App\Content\View;
|
||||
?>
|
||||
<section class="section catalog-hero">
|
||||
<div class="wrap">
|
||||
<nav class="breadcrumbs" aria-label="面包屑"><a href="/">首页</a><span aria-hidden="true">/</span><span aria-current="page">支持项目与合作伙伴</span></nav>
|
||||
<h1>支持项目与合作伙伴</h1>
|
||||
</div>
|
||||
</section>
|
||||
<?= View::fragment('network-groups', ['network' => $network, 'standalone' => true]) ?>
|
||||
+14
-36
@@ -36,8 +36,19 @@ $field = static fn (string $name): string => $canvas ? ' data-studio-field="' .
|
||||
<?php if ($canvas || $page['image'] !== ''): ?>
|
||||
<div class="hero-visual <?= $home ? 'hero-illustration' : ($service ? 'hero-service-art' : 'app-frame') ?><?= $page['image_scale'] ? ' studio-media-' . $e($page['image_scale']) : '' ?>" data-visual<?= $page['image'] === '' ? ' hidden' : '' ?>>
|
||||
<img<?= $page['image'] === '' ? '' : ' src="' . $e($page['image']) . '"' ?> alt="<?= $e($page['alt']) ?>" width="1448" height="1086" fetchpriority="high"<?= $canvas ? ' data-studio-hero-image' : '' ?>>
|
||||
<?php if ($home && !$canvas && $page['image'] === '/assets/original/home-hero.webp' && !$page['image_scale']): ?>
|
||||
<div class="home-pipe-flow" aria-hidden="true"><span></span><span></span><span></span><span></span></div>
|
||||
<?php if ($home && !$canvas && $page['image'] === '/assets/original/home-hero.webp'): ?>
|
||||
<svg class="home-pipe-flow" viewBox="0 0 640 480" aria-hidden="true" focusable="false">
|
||||
<defs>
|
||||
<path id="hero-pipe-tower" pathLength="100" d="M174 347 241 312 Q246 310 246 305 L246 299 Q246 295 252 293 L273 282"/>
|
||||
<path id="hero-pipe-disk" pathLength="100" d="M409 282 428 293 Q433 296 433 300 L433 304 Q433 307 438 309 L500 337"/>
|
||||
<path id="hero-pipe-node" pathLength="100" d="M119 201 125 205 Q129 207 129 212 L129 214 Q130 217 134 219 L190 243"/>
|
||||
<path id="hero-pipe-chart" pathLength="100" d="M443 226 471 213 Q478 208 483 209 L488 207 Q492 205 492 200 L492 182"/>
|
||||
</defs>
|
||||
<g class="home-pipe-route"><use href="#hero-pipe-tower"/><use href="#hero-pipe-tower"/></g>
|
||||
<g class="home-pipe-route"><use href="#hero-pipe-disk"/><use href="#hero-pipe-disk"/></g>
|
||||
<g class="home-pipe-route"><use href="#hero-pipe-node"/><use href="#hero-pipe-node"/></g>
|
||||
<g class="home-pipe-route"><use href="#hero-pipe-chart"/><use href="#hero-pipe-chart"/></g>
|
||||
</svg>
|
||||
<?php endif ?>
|
||||
</div>
|
||||
<?php endif ?>
|
||||
@@ -71,38 +82,5 @@ $field = static fn (string $name): string => $canvas ? ' data-studio-field="' .
|
||||
]) ?></template>
|
||||
<?php endif ?>
|
||||
<?php if ($home): ?>
|
||||
<section class="section home-news" id="blog" aria-labelledby="updates-heading">
|
||||
<div class="wrap">
|
||||
<div class="section-heading inline-heading">
|
||||
<h2 id="updates-heading">来笙动态</h2>
|
||||
<a class="button button-outline" href="/articles">查看全部动态</a>
|
||||
</div>
|
||||
<?= View::fragment('article-list', ['articles' => $articles]) ?>
|
||||
</div>
|
||||
</section>
|
||||
<?php if ($page['partners']): ?>
|
||||
<section class="section network" aria-labelledby="network-heading">
|
||||
<div class="wrap">
|
||||
<div class="section-heading"><h2 id="network-heading">支持项目与合作伙伴</h2></div>
|
||||
<?php foreach (['来笙支持项目' => 'supported-projects', '来笙合作伙伴' => 'partners'] as $group => $id): ?>
|
||||
<div class="network-group" id="<?= $e($id) ?>">
|
||||
<h3><?= $e($group) ?></h3>
|
||||
<ul class="network-list">
|
||||
<?php foreach ($page['partners'] as $partner): ?>
|
||||
<?php if ($partner['group'] !== $group) { continue; } ?>
|
||||
<li>
|
||||
<?php if ($partner['url']): ?><a href="<?= $e($partner['url']) ?>"><?php else: ?><div><?php endif ?>
|
||||
<?php if ($partner['image']): ?>
|
||||
<img src="<?= $e($partner['image']) ?>" alt="" loading="lazy" width="76" height="28">
|
||||
<?php endif ?>
|
||||
<span><?= $e($partner['name']) ?></span>
|
||||
<?php if ($partner['url']): ?></a><?php else: ?></div><?php endif ?>
|
||||
</li>
|
||||
<?php endforeach ?>
|
||||
</ul>
|
||||
</div>
|
||||
<?php endforeach ?>
|
||||
</div>
|
||||
</section>
|
||||
<?php endif ?>
|
||||
<?= View::fragment('network-groups', ['network' => $network, 'standalone' => false]) ?>
|
||||
<?php endif ?>
|
||||
|
||||
@@ -10,6 +10,9 @@ $canvas ??= false;
|
||||
<h2 id="heading-<?= $e($section['key']) ?>"<?= $canvas ? ' data-studio-part="title"' : '' ?>><?= $e($section['title']) ?></h2>
|
||||
<?php if ($canvas || $section['lead']): ?><p<?= $canvas ? ' data-studio-part="lead"' : '' ?>><?= $e($section['lead']) ?></p><?php endif ?>
|
||||
</div>
|
||||
<div class="rich"<?= $canvas ? ' data-studio-part="body"' : '' ?>><?= $section['body'] ?></div>
|
||||
<?php if (!empty($section['articleFeed'])): ?>
|
||||
<?= View::fragment('article-feed', ['groups' => $section['articleFeed'], 'sectionKey' => $section['key']]) ?>
|
||||
<?php endif ?>
|
||||
<?php if ($canvas || $section['body'] !== ''): ?><div class="rich<?= !empty($section['articleFeed']) ? ' article-feed-note' : '' ?>"<?= $canvas ? ' data-studio-part="body"' : '' ?>><?= $section['body'] ?></div><?php endif ?>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
+13
-7
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
use App\Content\Html;
|
||||
use App\Content\View;
|
||||
|
||||
$e = View::escape(...);
|
||||
@@ -15,17 +16,22 @@ $e = View::escape(...);
|
||||
<?php if (!$products): ?><p class="empty-state">暂无可查看的产品和服务。</p><?php endif ?>
|
||||
<div class="catalog-grid">
|
||||
<?php foreach ($products as $product): ?>
|
||||
<?php $card = $product['card']; $articlePath = '/articles/' . $product['slug']; ?>
|
||||
<article class="catalog-card">
|
||||
<?php if ($product['image']): ?>
|
||||
<div class="catalog-image"><img src="<?= $e($product['image']) ?>" alt="<?= $e($product['alt']) ?>" loading="lazy"></div>
|
||||
<?php if (!empty($card['image'])): ?>
|
||||
<div class="catalog-image"><img src="<?= $e($card['image']) ?>" alt="<?= $e($card['alt'] ?? '') ?>" loading="lazy"></div>
|
||||
<?php endif ?>
|
||||
<div class="catalog-content">
|
||||
<p class="overline"><?= $e($product['category']) ?></p>
|
||||
<h2><?= $e($product['name']) ?></h2>
|
||||
<div class="rich"><?= $product['body'] ?></div>
|
||||
<?php if (!empty($card['category_label'])): ?><p class="overline"><?= $e($card['category_label']) ?></p><?php endif ?>
|
||||
<h2><a href="<?= $e($articlePath) ?>"><?= $e($product['title']) ?></a></h2>
|
||||
<?php if ($product['summary']): ?>
|
||||
<p><?= $e($product['summary']) ?></p>
|
||||
<?php else: ?>
|
||||
<div class="rich"><?= Html::articleBody($product) ?></div>
|
||||
<?php endif ?>
|
||||
<div class="actions">
|
||||
<a class="button button-primary" href="<?= $e($product['url']) ?>"><?= $e($product['label']) ?></a>
|
||||
<?php if ($product['extra_url']): ?><a href="<?= $e($product['extra_url']) ?>"><?= $e($product['extra_label']) ?></a><?php endif ?>
|
||||
<a class="button button-primary" href="<?= $e(($card['url'] ?? '') ?: $articlePath) ?>"><?= $e(($card['label'] ?? '') ?: '了解详情') ?></a>
|
||||
<?php if (!empty($card['extra_url'])): ?><a href="<?= $e($card['extra_url']) ?>"><?= $e(($card['extra_label'] ?? '') ?: '相关链接') ?></a><?php endif ?>
|
||||
</div>
|
||||
</div>
|
||||
</article>
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
<?php
|
||||
use App\Content\View;
|
||||
|
||||
$e = View::escape(...);
|
||||
?>
|
||||
<header class="admin-heading">
|
||||
<div><h1>产品与服务</h1><p>管理公开目录中的条目、分类和按钮链接。</p></div>
|
||||
<div class="actions"><a class="button button-outline" href="/services">查看目录</a><a class="button button-primary" href="/admin/catalog/new">添加条目</a></div>
|
||||
</header>
|
||||
<section class="admin-panel">
|
||||
<?php if (!$products): ?><p class="empty-state">暂无产品或服务。</p><?php endif ?>
|
||||
<ul class="admin-list">
|
||||
<?php foreach ($products as $item): ?>
|
||||
<li><div><strong><?= $e($item['name']) ?></strong><small><?= $e($item['category']) ?> · <?= $e($item['url']) ?></small></div>
|
||||
<a class="button button-outline" href="/admin/catalog/<?= $e($item['id']) ?>">编辑</a>
|
||||
</li>
|
||||
<?php endforeach ?>
|
||||
</ul>
|
||||
</section>
|
||||
@@ -1,36 +0,0 @@
|
||||
<?php
|
||||
use App\Content\View;
|
||||
|
||||
$e = View::escape(...);
|
||||
?>
|
||||
<a class="back-link" href="/admin/catalog">返回目录管理</a>
|
||||
<header class="admin-heading"><h1><?= $e($title) ?></h1></header>
|
||||
<?= View::fragment('admin-feedback', compact('error', 'saved')) ?>
|
||||
<form class="admin-form admin-panel" method="post" action="/admin/catalog/<?= $e($id ?? 'new') ?>">
|
||||
<input type="hidden" name="csrf" value="<?= $e($token) ?>">
|
||||
<input type="hidden" name="version" value="<?= $e($product['version']) ?>">
|
||||
<div class="form-grid">
|
||||
<label>名称<input name="name" value="<?= $e($product['name']) ?>" required maxlength="100"></label>
|
||||
<label>分类<input name="category" value="<?= $e($product['category']) ?>" required maxlength="70"></label>
|
||||
</div>
|
||||
<div class="form-grid">
|
||||
<?= View::fragment('studio-asset-field', ['label' => '封面', 'name' => 'image', 'value' => $product['image']]) ?>
|
||||
<label>封面替代文本<input name="alt" value="<?= $e($product['alt']) ?>" maxlength="200"></label>
|
||||
</div>
|
||||
<div class="form-grid">
|
||||
<label>按钮文案<input name="label" value="<?= $e($product['label']) ?>" required maxlength="70"></label>
|
||||
<label>按钮链接<input name="url" value="<?= $e($product['url']) ?>" required maxlength="1000"></label>
|
||||
<label>次要链接文案<input name="extra_label" value="<?= $e($product['extra_label']) ?>" maxlength="70"></label>
|
||||
<label>次要链接<input name="extra_url" value="<?= $e($product['extra_url']) ?>" maxlength="1000"></label>
|
||||
</div>
|
||||
<label>排序<input type="number" name="position" min="-999999" max="999999" value="<?= $e($product['position'] === '' ? 100 : $product['position']) ?>" required><small>数值越小越靠前。</small></label>
|
||||
<?= View::fragment('admin-editor', ['format' => 'html', 'body' => $product['body']]) ?>
|
||||
<div class="form-submit"><button class="button button-primary" type="submit">保存条目</button></div>
|
||||
</form>
|
||||
<?php if ($id !== null): ?>
|
||||
<form class="admin-form" method="post" action="/admin/catalog/<?= $e($id) ?>">
|
||||
<input type="hidden" name="csrf" value="<?= $e($token) ?>">
|
||||
<input type="hidden" name="version" value="<?= $e($product['version']) ?>">
|
||||
<div class="form-submit"><button class="button button-outline" type="submit" name="operation" value="delete" data-confirm="从公开目录删除此条目?">删除条目</button></div>
|
||||
</form>
|
||||
<?php endif ?>
|
||||
Reference in New Issue
Block a user