load(); // Require login requireLogin(); $db = getDbConnection(); // Handle adding transmission to block - Admin only if (isset($_POST['add_to_block']) && canCreate()) { $commercialId = $_POST['infomercial_id']; $channel = $_POST['channel']; $date = $_POST['date']; $blockId = $_POST['block_id'] ?? null; // Get infomercial duration $stmt = $db->prepare("SELECT duration FROM infomercials WHERE id = ?"); $stmt->execute([$commercialId]); $duration = $stmt->fetchColumn(); // Calculate next start time for this specific block $nextStartTime = calculateNextStartTimeForBlock($db, $date, $channel, $blockId); // Insert transmission $stmt = $db->prepare(" INSERT INTO transmissions (infomercial_id, channel, template, start_date, start_time, duration, api_status) VALUES (?, ?, 'HOME030', ?, ?, ?, 'pending') "); $stmt->execute([$commercialId, $channel, $date, $nextStartTime, $duration]); header("Location: planner.php?date=$date&success=added"); exit; } // Handle remove transmission - REMOVED: Now handled via AJAX to delete_transmission.php API // This ensures proper Talpa API deletion // Handle reorder (move up/down) - Admin only if (isset($_POST['reorder']) && canEdit()) { $transmissionId = $_POST['transmission_id']; $direction = $_POST['direction']; // 'up' or 'down' $date = $_POST['date']; $channel = $_POST['channel']; $blockId = $_POST['block_id']; // Get block info including end time $stmt = $db->prepare("SELECT actual_start_time, actual_end_time FROM daily_blocks WHERE id = ?"); $stmt->execute([$blockId]); $blockInfo = $stmt->fetch(); $blockStartTime = $blockInfo['actual_start_time']; $blockEndTime = $blockInfo['actual_end_time'] ?? '23:59:59'; // Get all transmissions for THIS SPECIFIC block only $stmt = $db->prepare(" SELECT t.id, t.start_time, c.duration FROM transmissions t JOIN infomercials c ON t.infomercial_id = c.id WHERE t.start_date = ? AND t.channel = ? AND t.start_time >= ? AND t.start_time < ? ORDER BY t.start_time ASC "); $stmt->execute([$date, $channel, $blockStartTime, $blockEndTime]); $transmissions = $stmt->fetchAll(); // Find current position $currentIndex = array_search($transmissionId, array_column($transmissions, 'id')); if ($currentIndex !== false) { if ($direction === 'up' && $currentIndex > 0) { // Swap with previous $temp = $transmissions[$currentIndex]; $transmissions[$currentIndex] = $transmissions[$currentIndex - 1]; $transmissions[$currentIndex - 1] = $temp; } elseif ($direction === 'down' && $currentIndex < count($transmissions) - 1) { // Swap with next $temp = $transmissions[$currentIndex]; $transmissions[$currentIndex] = $transmissions[$currentIndex + 1]; $transmissions[$currentIndex + 1] = $temp; } // Recalculate all start times $currentTime = $blockStartTime; foreach ($transmissions as $tx) { $stmt = $db->prepare("UPDATE transmissions SET start_time = ?, api_status = 'pending' WHERE id = ?"); $stmt->execute([$currentTime, $tx['id']]); $currentTime = addTimeToTime($currentTime, $tx['duration']); } } header("Location: planner.php?date=$date"); exit; } // Handle block time update - Admin only if (isset($_POST['update_block_time']) && canEdit()) { $blockId = $_POST['block_id']; $newStartTime = $_POST['new_start_time']; $recalculate = isset($_POST['recalculate']); // Update block time $stmt = $db->prepare("UPDATE daily_blocks SET actual_start_time = ? WHERE id = ?"); $stmt->execute([$newStartTime, $blockId]); // Optionally recalculate transmissions if ($recalculate) { $stmt = $db->prepare("SELECT block_date, channel FROM daily_blocks WHERE id = ?"); $stmt->execute([$blockId]); $blockInfo = $stmt->fetch(); $stmt = $db->prepare(" SELECT t.id, c.duration FROM transmissions t JOIN infomercials c ON t.infomercial_id = c.id WHERE t.start_date = ? AND t.channel = ? AND t.start_time >= ? ORDER BY t.start_time ASC "); $stmt->execute([$blockInfo['block_date'], $blockInfo['channel'], $newStartTime]); $transmissions = $stmt->fetchAll(); $currentTime = $newStartTime; foreach ($transmissions as $tx) { $stmt = $db->prepare("UPDATE transmissions SET start_time = ?, api_status = 'pending' WHERE id = ?"); $stmt->execute([$currentTime, $tx['id']]); $currentTime = addTimeToTime($currentTime, $tx['duration']); } } header("Location: planner.php?date={$blockInfo['block_date']}&success=block_updated"); exit; } // Get selected date $selectedDate = $_GET['date'] ?? date('Y-m-d'); // Ensure daily blocks exist ensureDailyBlocks($db, $selectedDate, $selectedDate); // Get all blocks for this date (both channels, all blocks) $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([$selectedDate]); $allBlocks = $stmt->fetchAll(); // Group blocks by channel $blocksByChannel = []; foreach ($allBlocks as $block) { $blocksByChannel[$block['channel']][] = $block; } // Get all infomercials (including pending status - they can be used in planner) $infomercials = $db->query(" SELECT * FROM infomercials ORDER BY title ASC ")->fetchAll(); // Helper function to calculate next start time for a specific block function calculateNextStartTimeForBlock($db, $date, $channel, $blockId) { if ($blockId) { $stmt = $db->prepare("SELECT actual_start_time, actual_end_time FROM daily_blocks WHERE id = ?"); $stmt->execute([$blockId]); $blockInfo = $stmt->fetch(); $blockStartTime = $blockInfo['actual_start_time']; $blockEndTime = $blockInfo['actual_end_time'] ?? '23:59:59'; } else { $blockStartTime = '07:00:00'; // Default $blockEndTime = '23:59:59'; } // Get all transmissions in THIS SPECIFIC block only $stmt = $db->prepare(" SELECT start_time, duration FROM transmissions WHERE start_date = ? AND channel = ? AND start_time >= ? AND start_time < ? ORDER BY start_time DESC LIMIT 1 "); $stmt->execute([$date, $channel, $blockStartTime, $blockEndTime]); $lastTx = $stmt->fetch(); if ($lastTx) { return addTimeToTime($lastTx['start_time'], $lastTx['duration']); } return $blockStartTime; } ?>
Geen blokken voor deze dag
| Product | Duur | Start | Eind | Restant | Talpa | Acties | |
|---|---|---|---|---|---|---|---|
| Nog geen uitzendingen in dit blok | |||||||
| = htmlspecialchars($tx['title']) ?> | = $durationMinutes ?> min | = substr($tx['start_time'], 0, 5) ?> | = substr($endTime, 0, 5) ?> | = $txRemainingMinutes ?> min | |||