telvero_whatson_talpa/api/get_block_time.php
2026-02-19 15:58:15 +01:00

71 lines
1.8 KiB
PHP

<?php
/**
* API Endpoint: Get Block Time
* Returns the start time for a specific daily block
*/
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../helpers.php';
require_once __DIR__ . '/../auth/auth_functions.php';
use Dotenv\Dotenv;
$dotenv = Dotenv::createImmutable(__DIR__ . '/..');
$dotenv->load();
header('Content-Type: application/json');
if (!isLoggedIn()) {
http_response_code(401);
echo json_encode(['success' => false, 'error' => 'Niet geautoriseerd.']);
exit;
}
try {
$db = getDbConnection();
$date = $_GET['date'] ?? null;
$channel = $_GET['channel'] ?? null;
if (!$date || !$channel) {
jsonResponse([
'error' => 'Missing date or channel parameter'
], 400);
}
// Get daily block
$stmt = $db->prepare("
SELECT actual_start_time as start_time, actual_end_time as end_time
FROM daily_blocks
WHERE block_date = ? AND channel = ?
LIMIT 1
");
$stmt->execute([$date, $channel]);
$block = $stmt->fetch();
if ($block) {
jsonResponse($block);
} else {
// Return default from template
$dayOfWeek = ['zo', 'ma', 'di', 'wo', 'do', 'vr', 'za'][date('w', strtotime($date))];
$stmt = $db->prepare("
SELECT default_start_time as start_time, default_end_time as end_time
FROM block_templates
WHERE channel = ?
AND (day_of_week = ? OR day_of_week = 'all')
AND is_active = 1
LIMIT 1
");
$stmt->execute([$channel, $dayOfWeek]);
$template = $stmt->fetch();
jsonResponse($template ?: ['start_time' => '07:00:00', 'end_time' => null]);
}
} catch (Exception $e) {
jsonResponse([
'error' => $e->getMessage()
], 500);
}