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.commercial_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 commercials c ON t.commercial_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' => [ 'commercial_id' => $tx['commercial_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); }