telvero_whatson_talpa/api/get_block_time.php

64 lines
1.6 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';
use Dotenv\Dotenv;
$dotenv = Dotenv::createImmutable(__DIR__ . '/..');
$dotenv->load();
header('Content-Type: application/json');
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);
}