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 if (!canEdit()) { http_response_code(403); echo json_encode(['success' => false, 'error' => 'Geen toegang. Alleen admins kunnen bloktijden bijwerken.']); exit; } try { $db = getDbConnection(); // Get POST data $input = json_decode(file_get_contents('php://input'), true); if (!$input) { $input = $_POST; } // Validate required fields $required = ['date', 'channel', 'start_time']; foreach ($required as $field) { if (empty($input[$field])) { jsonResponse([ 'success' => false, 'error' => "Missing required field: $field" ], 400); } } // Validate formats if (!isValidDate($input['date'])) { jsonResponse([ 'success' => false, 'error' => 'Invalid date format' ], 400); } if (!isValidTime($input['start_time'])) { jsonResponse([ 'success' => false, 'error' => 'Invalid time format' ], 400); } // Get template_id if provided, or find matching template $templateId = $input['template_id'] ?? null; if (!$templateId) { // Try to find matching template $dayOfWeek = ['zo', 'ma', 'di', 'wo', 'do', 'vr', 'za'][date('w', strtotime($input['date']))]; $stmt = $db->prepare(" SELECT id FROM block_templates WHERE channel = ? AND (day_of_week = ? OR day_of_week = 'all') AND is_active = 1 ORDER BY day_of_week DESC LIMIT 1 "); $stmt->execute([$input['channel'], $dayOfWeek]); $templateId = $stmt->fetchColumn(); } // Check if daily block exists for this template $stmt = $db->prepare(" SELECT id FROM daily_blocks WHERE block_date = ? AND channel = ? AND template_id = ? "); $stmt->execute([$input['date'], $input['channel'], $templateId]); $block = $stmt->fetch(); if ($block) { // Update existing block $stmt = $db->prepare(" UPDATE daily_blocks SET actual_start_time = ?, updated_at = NOW() WHERE id = ? "); $stmt->execute([$input['start_time'], $block['id']]); } else { // Update or insert - use ON DUPLICATE KEY UPDATE $stmt = $db->prepare(" INSERT INTO daily_blocks (template_id, channel, block_date, actual_start_time) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE actual_start_time = VALUES(actual_start_time), updated_at = NOW() "); $stmt->execute([ $templateId, $input['channel'], $input['date'], $input['start_time'] ]); } // Optionally recalculate all transmission times in this block if (!empty($input['recalculate'])) { $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 = ? ORDER BY t.start_time ASC "); $stmt->execute([$input['date'], $input['channel']]); $transmissions = $stmt->fetchAll(); $currentTime = $input['start_time']; foreach ($transmissions as $tx) { // Update transmission start time $stmt = $db->prepare(" UPDATE transmissions SET start_time = ?, api_status = 'pending' WHERE id = ? "); $stmt->execute([$currentTime, $tx['id']]); // Calculate next start time $currentTime = addTimeToTime($currentTime, $tx['duration']); } } jsonResponse([ 'success' => true, 'message' => 'Block time updated successfully' ]); } catch (Exception $e) { jsonResponse([ 'success' => false, 'error' => $e->getMessage() ], 500); }