forked from laysense/laysense.cn
Add canvas element tools and categorized article editing
This commit is contained in:
@@ -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('此项目已在另一页面更新,请重新打开。');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user