load();
$api = new TalpaApi();
$db = new PDO("mysql:host={$_ENV['DB_HOST']};dbname={$_ENV['DB_NAME']}", $_ENV['DB_USER'], $_ENV['DB_PASS']);
// Array om logs voor console te verzamelen
$apiLogs = [];
// 1. Registratie Infomercial (Stap 1, 2, 4)
if (isset($_POST['add_commercial'])) {
$ep = $api->createEpisode($_POST['title'], $_POST['duration'], $_POST['season_id']);
$apiLogs[] = ['call' => 'Create Episode', 'response' => $api->lastResponse];
if (isset($ep['id'])) {
$asset = $api->createMediaAsset($ep['id']);
$apiLogs[] = ['call' => 'Create Media Asset', 'response' => $api->lastResponse];
if (isset($asset['id'])) {
$details = $api->getMediaAssetDetails($asset['id']);
$apiLogs[] = ['call' => 'Get Media Asset Details', 'response' => $api->lastResponse];
$label = $details['mediaAssetLabel'] ?? 'Pending';
$stmt = $db->prepare("INSERT INTO infomercials (title, duration, season_id, content_id, media_asset_id, media_asset_label, upload_status) VALUES (?, ?, ?, ?, ?, ?, 'pending')");
$stmt->execute([$_POST['title'], $_POST['duration'], $_POST['season_id'], $ep['id'], $asset['id'], $label]);
}
}
}
// 2. LOKALE Planning opslaan of bewerken
if (isset($_POST['schedule_transmission'])) {
$stmt = $db->prepare("SELECT duration FROM infomercials WHERE id = ?");
$stmt->execute([$_POST['infomercial_id']]);
$commDuration = $stmt->fetchColumn();
if (!empty($_POST['edit_id'])) {
$stmt = $db->prepare("UPDATE transmissions SET infomercial_id=?, channel=?, template=?, start_date=?, start_time=?, duration=?, api_status='pending' WHERE id=?");
$stmt->execute([$_POST['infomercial_id'], $_POST['channel'], $_POST['template'], $_POST['start_date'], $_POST['start_time'], $commDuration, $_POST['edit_id']]);
} else {
$stmt = $db->prepare("INSERT INTO transmissions (infomercial_id, channel, template, start_date, start_time, duration, api_status) VALUES (?, ?, ?, ?, ?, ?, 'pending')");
$stmt->execute([$_POST['infomercial_id'], $_POST['channel'], $_POST['template'], $_POST['start_date'], $_POST['start_time'], $commDuration]);
}
header("Location: index.php?view_date=" . $_POST['start_date']);
exit;
}
// 3. Handmatige Sync naar Talpa (Stap 3 op verzoek)
if (isset($_POST['sync_item'])) {
$stmt = $db->prepare("SELECT t.*, c.content_id FROM transmissions t JOIN infomercials c ON t.infomercial_id = c.id WHERE t.id = ?");
$stmt->execute([$_POST['sync_id']]);
$tx = $stmt->fetch();
$res = $api->createTransmission([
"channel" => $tx['channel'],
"template" => $tx['template'],
"startDate" => $tx['start_date'],
"startTime" => $tx['start_time'],
"duration" => $tx['duration'],
"contentId" => $tx['content_id']
]);
$apiLogs[] = ['call' => 'Sync Transmission', 'response' => $api->lastResponse];
$status = (isset($res['id']) || (isset($res['statusCode']) && $res['statusCode'] == 201)) ? 'synced' : 'error';
$db->prepare("UPDATE transmissions SET api_status = ?, api_response = ? WHERE id = ?")
->execute([$status, json_encode($res), $_POST['sync_id']]);
}
// 4. Media Asset Label en Status bijwerken
if (isset($_POST['update_media_asset'])) {
$stmt = $db->prepare("UPDATE infomercials SET media_asset_label = ?, upload_status = ? WHERE id = ?");
$stmt->execute([$_POST['media_asset_label'], $_POST['upload_status'], $_POST['infomercial_id']]);
header("Location: index.php?view_date=" . $selectedDate);
exit;
}
// Data ophalen
$infomercials = $db->query("SELECT * FROM infomercials ORDER BY created_at DESC")->fetchAll();
$selectedDate = $_GET['view_date'] ?? date('Y-m-d');
$stmt = $db->prepare("SELECT t.*, c.title, c.duration as comm_duration FROM transmissions t JOIN infomercials c ON t.infomercial_id = c.id WHERE t.start_date = ? ORDER BY t.start_time ASC");
$stmt->execute([$selectedDate]);
$dailySchedule = $stmt->fetchAll();
$editItem = null;
if (isset($_GET['edit'])) {
$stmt = $db->prepare("SELECT * FROM transmissions WHERE id = ?");
$stmt->execute([$_GET['edit']]);
$editItem = $stmt->fetch();
}
?>
Dashboard - Telvero Talpa
query("SELECT COUNT(*) FROM infomercials")->fetchColumn();
$uploadedCommercials = $db->query("SELECT COUNT(*) FROM infomercials WHERE upload_status = 'uploaded'")->fetchColumn();
$totalTransmissions = $db->query("SELECT COUNT(*) FROM transmissions")->fetchColumn();
$pendingSync = $db->query("SELECT COUNT(*) FROM transmissions WHERE api_status = 'pending'")->fetchColumn();
$syncedTransmissions = $db->query("SELECT COUNT(*) FROM transmissions WHERE api_status = 'synced'")->fetchColumn();
?>
= $totalCommercials ?>
Totaal Infomercials
= $uploadedCommercials ?>
Geüpload
= $totalTransmissions ?>
Geplande Uitzendingen
= $pendingSync ?>
Wacht op Sync
Snelle Acties
Recente Activiteit
1. Infomercial Registreren
2. = $editItem ? 'Planning Bewerken' : 'Tijdblok Reserveren' ?>
Uitzendingen vandaag
| Tijd | Zender | Product | Duur | Sync Status | Acties |
| = substr($tx['start_time'], 0, 5) ?> |
= htmlspecialchars($tx['channel']) ?> |
= htmlspecialchars($tx['title']) ?> |
= $tx['duration'] ?> |
= ucfirst($tx['api_status']) ?>
|
|
Media Asset Management
| Titel |
Duur |
Content ID |
Label (Filename) |
Upload Status |
Actie |