load(); $api = new TalpaApi(); $db = getDbConnection(); $apiLogs = []; // Handle infomercial 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 infomercials 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 infomercials (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: infomercials.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 infomercials 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['infomercial_id'] ]); header('Location: infomercials.php?success=updated'); exit; } // Handle delete if (isset($_POST['delete_commercial'])) { // Check if infomercial is used in transmissions $stmt = $db->prepare("SELECT COUNT(*) FROM transmissions WHERE infomercial_id = ?"); $stmt->execute([$_POST['infomercial_id']]); $count = $stmt->fetchColumn(); if ($count > 0) { header('Location: infomercials.php?error=in_use'); exit; } // Get infomercial details before deletion $stmt = $db->prepare("SELECT content_id, media_asset_id FROM infomercials WHERE id = ?"); $stmt->execute([$_POST['infomercial_id']]); $infomercial = $stmt->fetch(); // Delete from Talpa API if content_id exists if ($infomercial && $infomercial['content_id']) { try { $api->deleteEpisode($infomercial['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 infomercials WHERE id = ?"); $stmt->execute([$_POST['infomercial_id']]); header('Location: infomercials.php?success=deleted'); exit; } // Get all infomercials $infomercials = $db->query(" SELECT c.*, (SELECT COUNT(*) FROM transmissions WHERE infomercial_id = c.id) as usage_count FROM infomercials c ORDER BY c.created_at DESC ")->fetchAll(); ?> Infomercials - Telvero Talpa

Infomercial Management

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

Nog geen infomercials geregistreerd

Kleur Titel Duur Series Label Status Gebruik Acties

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