This commit is contained in:
107
src/App/SystemSettings.php
Normal file
107
src/App/SystemSettings.php
Normal file
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App;
|
||||
|
||||
final class SystemSettings
|
||||
{
|
||||
private array $tableCache = [];
|
||||
|
||||
private const DEFAULTS = [
|
||||
'google_places_enabled' => '0',
|
||||
'forum_maintenance_mode' => '0',
|
||||
'site_maintenance_mode' => '0',
|
||||
'site_maintenance_message' => 'Papa-Kind-Treff ist gerade kurz in Wartung. Bitte versuche es in Kürze erneut.',
|
||||
'place_data_provider' => 'osm',
|
||||
];
|
||||
|
||||
public function __construct(private \PDO $pdo)
|
||||
{
|
||||
}
|
||||
|
||||
public function ensureSchema(): void
|
||||
{
|
||||
$this->pdo->exec(
|
||||
'CREATE TABLE IF NOT EXISTS system_settings (
|
||||
`key` VARCHAR(120) NOT NULL PRIMARY KEY,
|
||||
`value` TEXT NULL,
|
||||
updated_by BIGINT UNSIGNED NULL,
|
||||
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
CONSTRAINT fk_system_settings_updated_by FOREIGN KEY (updated_by) REFERENCES users(id) ON DELETE SET NULL
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci'
|
||||
);
|
||||
|
||||
$stmt = $this->pdo->prepare(
|
||||
'INSERT INTO system_settings (`key`, `value`, updated_by)
|
||||
VALUES (:key, :value, NULL)
|
||||
ON DUPLICATE KEY UPDATE `value` = `value`'
|
||||
);
|
||||
foreach (self::DEFAULTS as $key => $value) {
|
||||
$stmt->execute([
|
||||
'key' => $key,
|
||||
'value' => $value,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function getAll(): array
|
||||
{
|
||||
$this->ensureSchema();
|
||||
$settings = self::DEFAULTS;
|
||||
$stmt = $this->pdo->query('SELECT `key`, `value` FROM system_settings');
|
||||
foreach ($stmt->fetchAll(\PDO::FETCH_ASSOC) ?: [] as $row) {
|
||||
$settings[(string)$row['key']] = (string)($row['value'] ?? '');
|
||||
}
|
||||
return $settings;
|
||||
}
|
||||
|
||||
public function get(string $key, ?string $default = null): ?string
|
||||
{
|
||||
$this->ensureSchema();
|
||||
$stmt = $this->pdo->prepare('SELECT `value` FROM system_settings WHERE `key` = :key LIMIT 1');
|
||||
$stmt->execute(['key' => $key]);
|
||||
$value = $stmt->fetchColumn();
|
||||
if ($value === false) {
|
||||
return $default ?? (self::DEFAULTS[$key] ?? null);
|
||||
}
|
||||
return (string)$value;
|
||||
}
|
||||
|
||||
public function getBool(string $key, bool $default = false): bool
|
||||
{
|
||||
$value = $this->get($key, $default ? '1' : '0');
|
||||
return in_array((string)$value, ['1', 'true', 'yes', 'on'], true);
|
||||
}
|
||||
|
||||
public function set(string $key, string $value, ?int $updatedBy = null): void
|
||||
{
|
||||
$this->ensureSchema();
|
||||
$stmt = $this->pdo->prepare(
|
||||
'INSERT INTO system_settings (`key`, `value`, updated_by)
|
||||
VALUES (:key, :value, :updatedBy)
|
||||
ON DUPLICATE KEY UPDATE `value` = VALUES(`value`), updated_by = VALUES(updated_by), updated_at = CURRENT_TIMESTAMP'
|
||||
);
|
||||
$stmt->execute([
|
||||
'key' => $key,
|
||||
'value' => $value,
|
||||
'updatedBy' => $updatedBy,
|
||||
]);
|
||||
}
|
||||
|
||||
public function updateMany(array $values, ?int $updatedBy = null): void
|
||||
{
|
||||
$this->ensureSchema();
|
||||
$stmt = $this->pdo->prepare(
|
||||
'INSERT INTO system_settings (`key`, `value`, updated_by)
|
||||
VALUES (:key, :value, :updatedBy)
|
||||
ON DUPLICATE KEY UPDATE `value` = VALUES(`value`), updated_by = VALUES(updated_by), updated_at = CURRENT_TIMESTAMP'
|
||||
);
|
||||
foreach ($values as $key => $value) {
|
||||
$stmt->execute([
|
||||
'key' => (string)$key,
|
||||
'value' => (string)$value,
|
||||
'updatedBy' => $updatedBy,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user