Files
deskhub/DeskHub-Webseite/api/staff/v1/business_hours.php
T
2026-10-16 19:12:13 +00:00

78 lines
3.1 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/common.php';
api_require_method('GET', 'POST');
$staff = api_require_staff();
api_require_admin_or_permission($staff, 'manage_business_hours');
$pdo = get_db();
const BUSINESS_WEEKDAYS = [1, 2, 3, 4, 5, 6, 0]; // Mo..So, wie im Web-Adminbereich
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$hoursByWeekday = [];
foreach ($pdo->query('SELECT * FROM business_hours')->fetchAll() as $r) {
$hoursByWeekday[(int) $r['weekday']] = $r;
}
$holidays = $pdo->query('SELECT * FROM business_holidays ORDER BY holiday_date')->fetchAll();
api_json([
'enabled' => get_setting('business_hours_enabled', '0') === '1',
'hours' => array_map(function (int $wd) use ($hoursByWeekday) {
$h = $hoursByWeekday[$wd] ?? ['is_open' => 0, 'open_time' => '09:00:00', 'close_time' => '17:00:00'];
return [
'weekday' => $wd,
'isOpen' => (bool) $h['is_open'],
'openTime' => substr($h['open_time'], 0, 5),
'closeTime' => substr($h['close_time'], 0, 5),
];
}, BUSINESS_WEEKDAYS),
'holidays' => array_map(fn(array $h) => [
'id' => (int) $h['id'],
'date' => $h['holiday_date'],
'name' => $h['name'],
], $holidays),
]);
}
$body = api_request_body();
$action = $body['action'] ?? '';
if ($action === 'save_hours') {
set_setting('business_hours_enabled', !empty($body['enabled']) ? '1' : '0');
$incoming = (array) ($body['hours'] ?? []);
$byWeekday = [];
foreach ($incoming as $entry) {
$byWeekday[(int) ($entry['weekday'] ?? -1)] = $entry;
}
$stmt = $pdo->prepare('UPDATE business_hours SET is_open=?, open_time=?, close_time=? WHERE weekday=?');
foreach (BUSINESS_WEEKDAYS as $wd) {
$entry = $byWeekday[$wd] ?? null;
$isOpen = !empty($entry['isOpen']) ? 1 : 0;
$openTime = trim((string) ($entry['openTime'] ?? '09:00')) . ':00';
$closeTime = trim((string) ($entry['closeTime'] ?? '17:00')) . ':00';
$stmt->execute([$isOpen, $openTime, $closeTime, $wd]);
}
audit_log($staff['id'], 'Geschäftszeiten aktualisiert (Mobile App)');
api_json(['ok' => true]);
}
if ($action === 'add_holiday') {
$date = trim((string) ($body['date'] ?? ''));
$name = trim((string) ($body['name'] ?? ''));
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $date)) api_error(422, 'INVALID_DATE', 'Bitte ein gültiges Datum angeben.');
if ($name === '') api_error(422, 'NAME_REQUIRED', 'Bitte einen Namen für den Feiertag angeben.');
$pdo->prepare('INSERT INTO business_holidays (holiday_date, name) VALUES (?,?)')->execute([$date, $name]);
audit_log($staff['id'], "Feiertag hinzugefügt: {$name} ({$date}) (Mobile App)");
api_json(['id' => (int) $pdo->lastInsertId()]);
}
if ($action === 'delete_holiday') {
$id = (int) ($body['id'] ?? 0);
$pdo->prepare('DELETE FROM business_holidays WHERE id = ?')->execute([$id]);
api_json(['ok' => true]);
}
api_error(422, 'INVALID_ACTION', 'Unbekannte Aktion.');