load(); header('Content-Type: application/json'); try { $db = getDbConnection(); $date = $_GET['date'] ?? date('Y-m-d'); if (!isValidDate($date)) { jsonResponse([ 'error' => 'Invalid date format' ], 400); } // Ensure daily blocks exist for this date ensureDailyBlocks($db, $date, $date); // Get all blocks for this date $stmt = $db->prepare(" SELECT db.*, bt.name as template_name, bt.channel FROM daily_blocks db LEFT JOIN block_templates bt ON db.template_id = bt.id WHERE db.block_date = ? ORDER BY db.channel, db.actual_start_time "); $stmt->execute([$date]); $blocks = $stmt->fetchAll(); // Format for calendar background events - assign to correct resource $backgroundEvents = []; foreach ($blocks as $block) { $startDateTime = $date . 'T' . $block['actual_start_time']; $endDateTime = $date . 'T' . ($block['actual_end_time'] ?? '23:59:59'); // Handle overnight blocks if ($block['actual_end_time'] && $block['actual_end_time'] < $block['actual_start_time']) { $endDateTime = date('Y-m-d', strtotime($date . ' +1 day')) . 'T' . $block['actual_end_time']; } $backgroundEvents[] = [ 'id' => 'block_' . $block['id'], 'title' => $block['template_name'] ?? 'Blok', 'start' => $startDateTime, 'end' => $endDateTime, 'resourceId' => $block['channel'], // Assign to specific resource/channel 'display' => 'background', 'backgroundColor' => getBlockColor($block['channel']), 'extendedProps' => [ 'type' => 'block', 'block_id' => $block['id'], 'channel' => $block['channel'], 'template_name' => $block['template_name'] ] ]; } jsonResponse([ 'blocks' => $blocks, 'backgroundEvents' => $backgroundEvents ]); } catch (Exception $e) { jsonResponse([ 'error' => $e->getMessage() ], 500); } /** * Get color for block background based on channel */ function getBlockColor($channel) { $colors = [ 'SBS9' => 'rgba(52, 152, 219, 0.1)', // Light blue 'NET5' => 'rgba(231, 76, 60, 0.1)', // Light red 'VERONICA' => 'rgba(155, 89, 182, 0.1)' // Light purple ]; return $colors[$channel] ?? 'rgba(200, 200, 200, 0.1)'; }