load(); $api = new TalpaApi(); $db = getDbConnection(); $apiLogs = []; // Handle commercial registration if (isset($_POST['add_commercial'])) { $apiLogs[] = ['step' => 'Start registration', 'input' => $_POST]; $ep = $api->createEpisode($_POST['title'], $_POST['duration'], $_POST['season_id']); $apiLogs[] = ['call' => 'Create Episode', 'request' => [ 'title' => $_POST['title'], 'duration' => $_POST['duration'], 'season_id' => $_POST['season_id'] ], 'response' => $api->lastResponse]; if (isset($ep['id'])) { $apiLogs[] = ['step' => 'Episode created', 'episode_id' => $ep['id']]; $asset = $api->createMediaAsset($ep['id']); $apiLogs[] = ['call' => 'Create Media Asset', 'request' => [ 'content_id' => $ep['id'] ], 'response' => $api->lastResponse]; if (isset($asset['id'])) { $apiLogs[] = ['step' => 'Media asset created', 'asset_id' => $asset['id']]; $details = $api->getMediaAssetDetails($asset['id']); $apiLogs[] = ['call' => 'Get Media Asset Details', 'request' => [ 'asset_id' => $asset['id'] ], 'response' => $api->lastResponse]; $label = $details['mediaAssetLabel'] ?? 'Pending'; $apiLogs[] = ['step' => 'Media asset label', 'label' => $label]; // Auto-generate color $stmt = $db->query("SELECT color_code FROM commercials WHERE color_code IS NOT NULL"); $existingColors = $stmt->fetchAll(PDO::FETCH_COLUMN); $colorCode = generateDistinctColor($existingColors); $apiLogs[] = ['step' => 'Color generated', 'color' => $colorCode]; $stmt = $db->prepare(" INSERT INTO commercials (title, duration, season_id, content_id, media_asset_id, media_asset_label, upload_status, color_code, series_code) VALUES (?, ?, ?, ?, ?, ?, 'pending', ?, ?) "); $stmt->execute([ $_POST['title'], $_POST['duration'], $_POST['season_id'], $ep['id'], $asset['id'], $label, $colorCode, $_POST['series_code'] ?? null ]); $apiLogs[] = ['step' => 'Database insert', 'success' => true]; header('Location: commercials.php?success=created'); exit; } else { $apiLogs[] = ['step' => 'Media asset creation failed', 'response' => $asset]; } } else { $apiLogs[] = ['step' => 'Episode creation failed', 'response' => $ep]; } } // Handle media asset update if (isset($_POST['update_media_asset'])) { $stmt = $db->prepare(" UPDATE commercials SET media_asset_label = ?, upload_status = ?, series_code = ?, color_code = ? WHERE id = ? "); $stmt->execute([ $_POST['media_asset_label'], $_POST['upload_status'], $_POST['series_code'] ?? null, $_POST['color_code'], $_POST['commercial_id'] ]); header('Location: commercials.php?success=updated'); exit; } // Handle delete if (isset($_POST['delete_commercial'])) { // Check if commercial is used in transmissions $stmt = $db->prepare("SELECT COUNT(*) FROM transmissions WHERE commercial_id = ?"); $stmt->execute([$_POST['commercial_id']]); $count = $stmt->fetchColumn(); if ($count > 0) { header('Location: commercials.php?error=in_use'); exit; } // Get commercial details before deletion $stmt = $db->prepare("SELECT content_id, media_asset_id FROM commercials WHERE id = ?"); $stmt->execute([$_POST['commercial_id']]); $commercial = $stmt->fetch(); // Delete from Talpa API if content_id exists if ($commercial && $commercial['content_id']) { try { $api->deleteEpisode($commercial['content_id']); $apiLogs[] = ['call' => 'Delete Episode', 'response' => $api->lastResponse]; } catch (Exception $e) { // Log error but continue with local deletion error_log("Failed to delete episode from Talpa: " . $e->getMessage()); } } // Delete from local database $stmt = $db->prepare("DELETE FROM commercials WHERE id = ?"); $stmt->execute([$_POST['commercial_id']]); header('Location: commercials.php?success=deleted'); exit; } // Get all commercials $commercials = $db->query(" SELECT c.*, (SELECT COUNT(*) FROM transmissions WHERE commercial_id = c.id) as usage_count FROM commercials c ORDER BY c.created_at DESC ")->fetchAll(); ?> Commercials - Telvero Talpa

Commercial Management

Naar Kalender
Nieuwe Commercial Registreren
Formaat: UU:MM:SS
Voor groepering in kalender
Dit registreert de commercial bij Talpa en maakt automatisch een media asset aan.
Geregistreerde Commercials

Nog geen commercials geregistreerd

Kleur Titel Duur Series Label Status Gebruik Acties

ID:
' . htmlspecialchars($c['series_code']) . '' : '-' ?> Uploaded Pending x