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

211 lines
6.1 KiB
PHP

<?php
/**
* API Endpoint: Create Transmission
* Creates a new transmission (planning entry)
*/
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');
// Authentication check
if (!isLoggedIn()) {
http_response_code(401);
echo json_encode(['success' => false, 'error' => 'Niet geautoriseerd. Log eerst in.']);
exit;
}
// Authorization check - admin only for write operations
if (!canCreate()) {
http_response_code(403);
echo json_encode(['success' => false, 'error' => 'Geen toegang. Alleen admins kunnen uitzendingen aanmaken.']);
exit;
}
try {
$db = getDbConnection();
// Get POST data
$input = json_decode(file_get_contents('php://input'), true);
if (!$input) {
$input = $_POST;
}
// Validate required fields
$required = ['infomercial_id', 'channel', 'start_date', 'start_time'];
foreach ($required as $field) {
if (empty($input[$field])) {
jsonResponse([
'success' => false,
'error' => "Missing required field: $field"
], 400);
}
}
// Validate date and time formats
if (!isValidDate($input['start_date'])) {
jsonResponse([
'success' => false,
'error' => 'Invalid date format'
], 400);
}
if (!isValidTime($input['start_time'])) {
jsonResponse([
'success' => false,
'error' => 'Invalid time format'
], 400);
}
// Get infomercial details (duration)
$stmt = $db->prepare("SELECT duration, title FROM infomercials WHERE id = ?");
$stmt->execute([$input['infomercial_id']]);
$infomercial = $stmt->fetch();
if (!$infomercial) {
jsonResponse([
'success' => false,
'error' => 'Infomercial not found'
], 404);
}
// Get template from input or use default
$template = $input['template'] ?? 'HOME030';
// Calculate end time
$endTime = addTimeToTime($input['start_time'], $infomercial['duration']);
// Validate that transmission falls within a block (inline to avoid curl issues)
ensureDailyBlocks($db, $input['start_date'], $input['start_date']);
$stmt = $db->prepare("
SELECT * FROM daily_blocks
WHERE block_date = ? AND channel = ?
ORDER BY actual_start_time
");
$stmt->execute([$input['start_date'], $input['channel']]);
$blocks = $stmt->fetchAll();
$withinBlock = false;
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) {
if ($input['start_time'] >= $blockStart || $input['start_time'] < $blockEnd) {
if ($endTime <= $blockEnd || $endTime >= $blockStart) {
$withinBlock = true;
break;
}
}
} else {
if ($input['start_time'] >= $blockStart && $endTime <= $blockEnd) {
$withinBlock = true;
break;
}
}
}
if (!$withinBlock) {
$errorMessage = "Uitzending valt buiten blok tijden. ";
if (!empty($blocks)) {
$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([
'success' => false,
'error' => $errorMessage,
'attempted_time' => $input['start_time'] . ' - ' . $endTime
], 400);
}
// Check for overlapping transmissions
$stmt = $db->prepare("
SELECT t.id, t.start_time, t.duration, c.title
FROM transmissions t
JOIN infomercials c ON t.infomercial_id = c.id
WHERE t.start_date = ?
AND t.channel = ?
AND t.id != ?
");
$stmt->execute([
$input['start_date'],
$input['channel'],
$input['id'] ?? 0
]);
$existing = $stmt->fetchAll();
foreach ($existing as $tx) {
$txEnd = addTimeToTime($tx['start_time'], $tx['duration']);
if (timeRangesOverlap($input['start_time'], $endTime, $tx['start_time'], $txEnd)) {
jsonResponse([
'success' => false,
'error' => "Overlap detected with: {$tx['title']} ({$tx['start_time']} - {$txEnd})",
'overlap' => true
], 409);
}
}
// Insert transmission
$stmt = $db->prepare("
INSERT INTO transmissions
(infomercial_id, channel, template, start_date, start_time, duration, api_status)
VALUES (?, ?, ?, ?, ?, ?, 'pending')
");
$stmt->execute([
$input['infomercial_id'],
$input['channel'],
$template,
$input['start_date'],
$input['start_time'],
$infomercial['duration']
]);
$transmissionId = $db->lastInsertId();
// Get the created transmission with all details
$stmt = $db->prepare("
SELECT
t.*,
c.title,
c.color_code,
c.series_code
FROM transmissions t
JOIN infomercials c ON t.infomercial_id = c.id
WHERE t.id = ?
");
$stmt->execute([$transmissionId]);
$transmission = $stmt->fetch();
jsonResponse([
'success' => true,
'message' => 'Transmission created successfully',
'transmission' => $transmission
]);
} catch (Exception $e) {
jsonResponse([
'success' => false,
'error' => $e->getMessage()
], 500);
}