load(); $api = new TalpaApi(); $db = getDbConnection(); $apiLogs = []; // Retrieve and clear refresh logs from session session_start(); $refreshLogs = $_SESSION['refresh_logs'] ?? null; if ($refreshLogs) { unset($_SESSION['refresh_logs']); } // 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 infomercial update (sync to Talpa) if (isset($_POST['update_infomercial'])) { // Get current infomercial data $stmt = $db->prepare("SELECT content_id, title, duration FROM infomercials WHERE id = ?"); $stmt->execute([$_POST['infomercial_id']]); $current = $stmt->fetch(); if ($current && $current['content_id']) { // Update in Talpa if title or duration changed $titleChanged = $current['title'] !== $_POST['title']; $durationChanged = $current['duration'] !== $_POST['duration']; if ($titleChanged || $durationChanged) { $result = $api->updateEpisode( $current['content_id'], $_POST['title'], $_POST['duration'] ); $apiLogs[] = ['call' => 'Update Episode', 'request' => [ 'content_id' => $current['content_id'], 'title' => $_POST['title'], 'duration' => $_POST['duration'] ], 'response' => $api->lastResponse]; } // Update in local database $stmt = $db->prepare(" UPDATE infomercials SET title = ?, duration = ?, upload_status = ?, series_code = ?, color_code = ? WHERE id = ? "); $stmt->execute([ $_POST['title'], $_POST['duration'], $_POST['upload_status'], $_POST['series_code'] ?? null, $_POST['color_code'], $_POST['infomercial_id'] ]); header('Location: infomercials.php?success=updated'); exit; } } // Handle refresh single infomercial from Talpa if (isset($_POST['refresh_infomercial'])) { $stmt = $db->prepare("SELECT title, media_asset_id FROM infomercials WHERE id = ?"); $stmt->execute([$_POST['infomercial_id']]); $infomercial = $stmt->fetch(); if ($infomercial && $infomercial['media_asset_id']) { $details = $api->getMediaAssetDetails($infomercial['media_asset_id']); $apiLogs[] = ['call' => 'Refresh Media Asset Details', 'request' => [ 'asset_id' => $infomercial['media_asset_id'] ], 'response' => $api->lastResponse]; if (isset($details['mediaAssetLabel'])) { $stmt = $db->prepare(" UPDATE infomercials SET media_asset_label = ? WHERE id = ? "); $stmt->execute([ $details['mediaAssetLabel'], $_POST['infomercial_id'] ]); header('Location: infomercials.php?success=refreshed'); exit; } else { // Store failed refresh log session_start(); $_SESSION['refresh_logs'] = [[ 'status' => 'failed', 'title' => $infomercial['title'], 'asset_id' => $infomercial['media_asset_id'], 'error' => $api->lastResponse['message'] ?? 'Geen mediaAssetLabel gevonden in response' ]]; } } header('Location: infomercials.php?error=refresh_failed'); exit; } // Handle refresh all infomercials from Talpa if (isset($_POST['refresh_all'])) { $stmt = $db->query("SELECT id, title, media_asset_id FROM infomercials WHERE media_asset_id IS NOT NULL"); $infomercials_to_refresh = $stmt->fetchAll(); $refreshed = 0; $failed = 0; $refreshLogs = []; foreach ($infomercials_to_refresh as $inf) { $details = $api->getMediaAssetDetails($inf['media_asset_id']); $apiLogs[] = ['call' => 'Refresh All - Media Asset Details', 'request' => [ 'asset_id' => $inf['media_asset_id'] ], 'response' => $api->lastResponse]; if (isset($details['mediaAssetLabel'])) { $stmt = $db->prepare(" UPDATE infomercials SET media_asset_label = ? WHERE id = ? "); $stmt->execute([ $details['mediaAssetLabel'], $inf['id'] ]); $refreshed++; } else { $failed++; // Only log failures $refreshLogs[] = [ 'status' => 'failed', 'title' => $inf['title'], 'asset_id' => $inf['media_asset_id'], 'error' => $api->lastResponse['message'] ?? 'Geen mediaAssetLabel gevonden in response' ]; } } // Store logs in session for display session_start(); $_SESSION['refresh_logs'] = $refreshLogs; header("Location: infomercials.php?success=refreshed_all&count=$refreshed&failed=$failed"); 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
0): ?>
Refresh Fouten
De volgende infomercials konden niet worden ververst vanuit Talpa:
Infomercial Asset ID Foutmelding
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