Files
laysense.cn/app/Content/SiteDesign.php

108 lines
4.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Content;
use PDO;
use RuntimeException;
final class SiteDesign
{
public const DEFAULT_HEADER = [
'logo' => '/assets/wordmark.webp', 'alt' => 'Laysense 来笙',
'links' => [['/', '首页'], ['/services', '产品与服务'], ['/articles', '动态'], ['/#about', '关于来笙']],
'button' => ['#contact', '联系来笙'],
];
public const DEFAULT_FOOTER = [
'logo' => '/assets/wordmark.webp', 'alt' => '来笙 Laysense',
'products_title' => '产品与服务',
'products' => [['/pagepan', 'PAGEPAN 小飞页'], ['/highspeaker', 'HighSpeaker DNS'], ['/sc', '软著代理服务'], ['/shnu-sc', 'SHNU 学生软著服务']],
'company_title' => '公司',
'company' => [['/#about', '关于来笙'], ['/articles', '来笙动态'], ['https://git.laysense.com', 'Laysense Git'], ['https://www.layso.de', 'LaySo']],
'phones' => [['tel:+16699004532', '+1 (669) 900 4532'], ['tel:+85257012856', '+852 5701 2856']],
];
public static function parseLinks(string $value): array
{
$links = [];
foreach (preg_split('/\R/u', $value, -1, PREG_SPLIT_NO_EMPTY) as $line) {
if (trim($line) === '') {
continue;
}
[$label, $url] = array_map('trim', array_pad(explode('|', $line, 2), 2, ''));
if ($label === '' || mb_strlen($label) > 70 || strlen($url) > 1000 || !Html::url($url) || count($links) >= 20) {
throw new RuntimeException('每行填写「文案 | 链接」,最多 20 条。');
}
$links[] = [$url, $label];
}
return $links;
}
public static function linkText(array $links): string
{
return implode("\n", array_map(static fn (array $link): string => $link[1] . ' | ' . $link[0], $links));
}
public static function saveSettings(string $part, array $data, int $version): void
{
Database::write(function (PDO $pdo) use ($part, $data, $version): void {
$site = json_decode((string) $pdo->query('SELECT payload FROM site_settings WHERE id = 1')->fetchColumn(), true, flags: JSON_THROW_ON_ERROR);
if (($site['revisions'][$part] ?? 0) !== $version) {
throw new RuntimeException('设置已在另一页面更新。请复制本次修改后重新打开。');
}
$site[$part] = $data;
$site['revisions'][$part] = $version + 1;
$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('此项目已在另一页面更新,请重新打开。');
}
}
}