132 lines
5.4 KiB
PHP
132 lines
5.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Content;
|
|
|
|
use PDO;
|
|
use RuntimeException;
|
|
|
|
final class LayoutPage
|
|
{
|
|
public const TYPES = ['page' => '普通页面', 'header' => '页头', 'footer' => '页脚'];
|
|
|
|
public static function slug(string $kind, int $id): string
|
|
{
|
|
return "layout--$kind-$id";
|
|
}
|
|
|
|
public static function create(string $kind, string $name): string
|
|
{
|
|
return Database::write(static function (PDO $pdo) use ($kind, $name): string {
|
|
$id = self::insertPreset($pdo, $kind, $name);
|
|
$slug = self::slug($kind, $id);
|
|
Repository::insertPage($pdo, $slug, $name, ['visibility' => ['hero' => false]], $id);
|
|
|
|
return $slug;
|
|
});
|
|
}
|
|
|
|
private static function insertPreset(PDO $pdo, string $kind, string $name): int
|
|
{
|
|
$pdo->prepare('INSERT INTO design_presets (kind, name, preset, payload) VALUES (?, ?, ?, ?)')
|
|
->execute([$kind, $name, array_key_first(DesignLibrary::PRESETS[$kind]), Database::json(DesignLibrary::defaults($kind))]);
|
|
|
|
return (int) $pdo->lastInsertId();
|
|
}
|
|
|
|
public static function migrate(PDO $pdo): void
|
|
{
|
|
$site = json_decode($pdo->query('SELECT payload FROM site_settings WHERE id = 1')->fetchColumn(), true, flags: JSON_THROW_ON_ERROR);
|
|
foreach (['header', 'footer'] as $kind) {
|
|
$site['layout_defaults'][$kind] ??= self::insertPreset($pdo, $kind, '基础' . self::TYPES[$kind]);
|
|
$site[$kind . '_id'] ??= $site['layout_defaults'][$kind];
|
|
}
|
|
$pdo->prepare('UPDATE site_settings SET payload = ? WHERE id = 1')->execute([Database::json($site)]);
|
|
|
|
$rows = $pdo->query("SELECT d.* FROM design_presets d LEFT JOIN pages p ON p.layout_id = d.id
|
|
WHERE d.kind IN ('header', 'footer') AND p.slug IS NULL")->fetchAll();
|
|
foreach ($rows as $row) {
|
|
$kind = $row['kind'];
|
|
$layout = $row;
|
|
$layout['payload'] = DesignLibrary::normalize($kind, $row['preset'], json_decode($row['payload'], true, flags: JSON_THROW_ON_ERROR));
|
|
$body = self::component($kind, $layout, $site['name']);
|
|
Repository::insertPage($pdo, self::slug($kind, $row['id']), $row['name'], [
|
|
'visibility' => ['hero' => false],
|
|
'sections' => [[
|
|
'key' => 'layout-content', 'title' => DesignLibrary::PRESETS[$kind][$row['preset']], 'lead' => '',
|
|
'body' => Html::sanitize($body), 'style' => 'layout',
|
|
]],
|
|
], $row['id']);
|
|
}
|
|
}
|
|
|
|
public static function groups(): array
|
|
{
|
|
$groups = [];
|
|
foreach (['header', 'footer'] as $kind) {
|
|
$items = [];
|
|
foreach (DesignLibrary::PRESETS[$kind] as $preset => $label) {
|
|
$body = self::sample($kind, $preset);
|
|
$items[$kind . '-' . $preset] = [$label, Html::sanitize($body), '', null, 'layout'];
|
|
}
|
|
$groups[$kind . 's'] = [self::TYPES[$kind], $items];
|
|
}
|
|
|
|
return $groups;
|
|
}
|
|
|
|
public static function sample(string $kind, string $preset): string
|
|
{
|
|
$layout = ['id' => 0, 'preset' => $preset, 'payload' => array_replace(DesignLibrary::defaults($kind), [
|
|
'brand' => '品牌名称', 'links' => [['/', '首页'], ['/articles', '文章']],
|
|
'action' => [['/articles', '了解更多', 'solid', false]], 'text' => '关于网站的一句话介绍。', 'legal' => '© 品牌名称',
|
|
])];
|
|
|
|
return self::component($kind, $layout, '品牌名称');
|
|
}
|
|
|
|
private static function component(string $kind, array $layout, string $name): string
|
|
{
|
|
return View::fragment($kind, [
|
|
$kind => $layout, 'site' => ['name' => $name], 'showHeader' => true, 'showFooter' => true,
|
|
'path' => null, 'visitor' => null, 'member' => false, 'layoutTemplate' => true,
|
|
]);
|
|
}
|
|
|
|
public static function hasMember(array $sections): bool
|
|
{
|
|
foreach ($sections as $section) {
|
|
if (str_contains($section['body'], 'studio-member-slot')
|
|
&& preg_match_all('~<a\b[^>]*\sclass="([^"]*)"~i', $section['body'], $matches)) {
|
|
foreach ($matches[1] as $classes) {
|
|
if (in_array('studio-member-slot', preg_split('/\s+/', $classes), true)) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public static function saveSettings(PDO $pdo, array $page, array $config): void
|
|
{
|
|
$kind = $page['layout_kind'];
|
|
$id = $page['layout_id'];
|
|
if ($kind === 'header' && $config['provider'] !== null) {
|
|
if (!self::hasMember($page['sections'])) {
|
|
throw new RuntimeException('请先在画布中添加会员导航。');
|
|
}
|
|
$layout = DesignLibrary::find($id, $kind);
|
|
$payload = $layout['payload'];
|
|
$payload['provider'] = $config['provider'];
|
|
$payload['components'] = array_values(array_unique([...$payload['components'], 'account']));
|
|
DesignLibrary::saveIn($pdo, $kind, $id, $page['name'], 'member', $payload, $layout['version'], $config['default'], $config['secret']);
|
|
} else {
|
|
$pdo->prepare('UPDATE design_presets SET name = ?, version = version + 1 WHERE id = ?')->execute([$page['name'], $id]);
|
|
DesignLibrary::setDefault($pdo, $kind, $id, $config['default']);
|
|
}
|
|
}
|
|
}
|