Add canvas element tools and categorized article editing

This commit is contained in:
2026-11-21 22:24:58 +08:00
parent 6efcdefadf
commit a5b3408358
45 changed files with 1607 additions and 660 deletions
-46
View File
@@ -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('此项目已在另一页面更新,请重新打开。');
}
}
}