Files

88 lines
3.6 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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/original/footer-wordmark.webp', 'alt' => '来笙 Laysense',
'name' => '上海来笙信息科技有限公司',
'address' => '中国(上海)自由贸易试验区世博村路231号2单元3层328室',
'email' => 'public@laysense.cn',
'products_title' => '产品与服务',
'products' => [['/highspeaker', 'HighSpeaker DNS'], ['/pagepan', 'PAGEPAN'], ['/sc', '软著代理服务']],
'company_title' => '公司',
'company' => [['/#about', '关于来笙'], ['/contact', '联系来笙']],
'resources_title' => '资源',
'resources' => [['https://git.laysense.com', 'Laysense Git'], ['https://www.layso.de', 'LaySo']],
'phones' => [['tel:+16699004532', '+1 (669) 900 4532'], ['tel:+85257012856', '+852 5701 2856']],
'copyright' => '© Shanghai Laysense Information Technology Co. Ltd. 2024',
'terms' => '本网站中产品价格仅作展示,不作为订立合同、邀约或要约邀请的有效因素。',
'registration' => '沪ICP备2024051698号-1',
];
public static function footer(array $site): array
{
$saved = $site['footer'] ?? null;
if ($saved === null) {
return self::DEFAULT_FOOTER;
}
// Older configurations store the entire footer navigation in two columns.
if (!array_key_exists('resources', $saved)) {
$saved += ['resources' => [], 'resources_title' => ''];
if (!empty($saved['copyright'])) {
$saved['copyright'] .= ' 2024–' . date('Y');
}
}
return $saved + self::DEFAULT_FOOTER;
}
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)]);
});
}
}