telvero_whatson_talpa/api/get_transmissions.php

99 lines
2.7 KiB
PHP

<?php
/**
* API Endpoint: Get Transmissions
* Returns transmissions for calendar view in FullCalendar format
*/
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 date range from query parameters
$start = $_GET['start'] ?? date('Y-m-d', strtotime('-7 days'));
$end = $_GET['end'] ?? date('Y-m-d', strtotime('+30 days'));
$channel = $_GET['channel'] ?? null;
// Ensure daily blocks exist for this date range
ensureDailyBlocks($db, $start, $end);
// Build query
$sql = "
SELECT
t.id,
t.infomercial_id,
t.channel,
t.template,
t.start_date,
t.start_time,
t.duration,
t.api_status,
c.title,
c.color_code,
c.series_code
FROM transmissions t
JOIN infomercials c ON t.infomercial_id = c.id
WHERE t.start_date BETWEEN ? AND ?
";
$params = [$start, $end];
if ($channel) {
$sql .= " AND t.channel = ?";
$params[] = $channel;
}
$sql .= " ORDER BY t.start_date, t.start_time";
$stmt = $db->prepare($sql);
$stmt->execute($params);
$transmissions = $stmt->fetchAll();
// Format for FullCalendar
$events = [];
foreach ($transmissions as $tx) {
// Calculate end time
$startDateTime = new DateTime($tx['start_date'] . ' ' . $tx['start_time']);
$endDateTime = clone $startDateTime;
if ($tx['duration']) {
list($h, $m, $s) = explode(':', $tx['duration']);
$endDateTime->add(new DateInterval("PT{$h}H{$m}M{$s}S"));
}
$events[] = [
'id' => $tx['id'],
'title' => $tx['title'],
'start' => $startDateTime->format('Y-m-d\TH:i:s'),
'end' => $endDateTime->format('Y-m-d\TH:i:s'),
'resourceId' => $tx['channel'],
'backgroundColor' => $tx['color_code'] ?? '#cccccc',
'borderColor' => $tx['color_code'] ?? '#cccccc',
'textColor' => '#ffffff',
'extendedProps' => [
'infomercial_id' => $tx['infomercial_id'],
'template' => $tx['template'],
'duration' => $tx['duration'],
'api_status' => $tx['api_status'],
'series_code' => $tx['series_code']
]
];
}
jsonResponse($events);
} catch (Exception $e) {
jsonResponse([
'error' => true,
'message' => $e->getMessage()
], 500);
}