131 lines
3.8 KiB
PHP
131 lines
3.8 KiB
PHP
<?php
|
|
/**
|
|
* API Endpoint: Validate Transmission Time
|
|
* Checks if a transmission falls within an active 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();
|
|
|
|
// Get POST data
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (!$input) {
|
|
$input = $_POST;
|
|
}
|
|
|
|
// Validate required fields
|
|
$required = ['channel', 'start_date', 'start_time', 'duration'];
|
|
foreach ($required as $field) {
|
|
if (empty($input[$field])) {
|
|
jsonResponse([
|
|
'valid' => false,
|
|
'error' => "Missing required field: $field"
|
|
], 400);
|
|
}
|
|
}
|
|
|
|
$channel = $input['channel'];
|
|
$startDate = $input['start_date'];
|
|
$startTime = $input['start_time'];
|
|
$duration = $input['duration'];
|
|
|
|
// Calculate end time
|
|
$endTime = addTimeToTime($startTime, $duration);
|
|
|
|
// Ensure daily blocks exist
|
|
ensureDailyBlocks($db, $startDate, $startDate);
|
|
|
|
// Get all blocks for this channel and date
|
|
$stmt = $db->prepare("
|
|
SELECT * FROM daily_blocks
|
|
WHERE block_date = ? AND channel = ?
|
|
ORDER BY actual_start_time
|
|
");
|
|
$stmt->execute([$startDate, $channel]);
|
|
$blocks = $stmt->fetchAll();
|
|
|
|
if (empty($blocks)) {
|
|
jsonResponse([
|
|
'valid' => false,
|
|
'error' => "Geen actief blok gevonden voor {$channel} op {$startDate}",
|
|
'blocks' => []
|
|
]);
|
|
}
|
|
|
|
// Check if transmission falls within any block
|
|
$withinBlock = false;
|
|
$matchingBlock = null;
|
|
|
|
foreach ($blocks as $block) {
|
|
$blockStart = $block['actual_start_time'];
|
|
$blockEnd = $block['actual_end_time'] ?? '23:59:59';
|
|
|
|
// Handle overnight blocks
|
|
$isOvernight = $blockEnd < $blockStart;
|
|
|
|
if ($isOvernight) {
|
|
// For overnight blocks, check if time is after start OR before end
|
|
if ($startTime >= $blockStart || $startTime < $blockEnd) {
|
|
// Also check end time
|
|
if ($endTime <= $blockEnd || $endTime >= $blockStart) {
|
|
$withinBlock = true;
|
|
$matchingBlock = $block;
|
|
break;
|
|
}
|
|
}
|
|
} else {
|
|
// Normal block: check if both start and end are within block
|
|
if ($startTime >= $blockStart && $endTime <= $blockEnd) {
|
|
$withinBlock = true;
|
|
$matchingBlock = $block;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($withinBlock) {
|
|
jsonResponse([
|
|
'valid' => true,
|
|
'message' => 'Uitzending valt binnen blok',
|
|
'block' => $matchingBlock
|
|
]);
|
|
} else {
|
|
// Find closest block for helpful error message
|
|
$closestBlock = $blocks[0];
|
|
$errorMessage = "Uitzending valt buiten blok tijden. ";
|
|
$errorMessage .= "Beschikbare blokken: ";
|
|
|
|
foreach ($blocks as $block) {
|
|
$blockStart = substr($block['actual_start_time'], 0, 5);
|
|
$blockEnd = $block['actual_end_time'] ? substr($block['actual_end_time'], 0, 5) : '∞';
|
|
$errorMessage .= "{$blockStart}-{$blockEnd}, ";
|
|
}
|
|
|
|
$errorMessage = rtrim($errorMessage, ', ');
|
|
|
|
jsonResponse([
|
|
'valid' => false,
|
|
'error' => $errorMessage,
|
|
'blocks' => $blocks,
|
|
'attempted_time' => $startTime . ' - ' . $endTime
|
|
]);
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
jsonResponse([
|
|
'valid' => false,
|
|
'error' => $e->getMessage()
|
|
], 500);
|
|
}
|