telvero_whatson_talpa/infomercials.php

438 lines
23 KiB
PHP

<?php
/**
* Infomercial Management
* Enhanced version of infomercial registration with color management
*/
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/TalpaAPI.php';
require_once __DIR__ . '/helpers.php';
use Dotenv\Dotenv;
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->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();
?>
<!DOCTYPE html>
<html lang="nl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Infomercials - Telvero Talpa</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css">
<link rel="stylesheet" href="assets/css/custom.css">
</head>
<body class="bg-light">
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container-fluid">
<a class="navbar-brand" href="index.php">
<i class="bi bi-tv"></i> Telvero Talpa Planner
</a>
<div class="navbar-nav">
<a class="nav-link" href="index.php">Dashboard</a>
<a class="nav-link" href="planner.php">Excel Planner</a>
<a class="nav-link" href="calendar.php">Kalender</a>
<a class="nav-link" href="blocks.php">Blokken</a>
<a class="nav-link active" href="infomercials.php">Infomercials</a>
</div>
</div>
</nav>
<div class="container mt-4">
<div class="d-flex justify-content-between align-items-center mb-4">
<h1><i class="bi bi-collection-play"></i> Infomercial Management</h1>
<a href="calendar.php" class="btn btn-primary">
<i class="bi bi-calendar-week"></i> Naar Kalender
</a>
</div>
<?php if (isset($_GET['success'])): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
<?php
$messages = [
'created' => 'Infomercial succesvol aangemaakt en geregistreerd bij Talpa!',
'updated' => 'Infomercial succesvol bijgewerkt!',
'deleted' => 'Infomercial succesvol verwijderd!'
];
echo $messages[$_GET['success']] ?? 'Actie succesvol uitgevoerd!';
?>
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>
<?php if (isset($_GET['error'])): ?>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<?php
$messages = [
'in_use' => 'Deze infomercial kan niet verwijderd worden omdat deze nog in gebruik is in de planning!'
];
echo $messages[$_GET['error']] ?? 'Er is een fout opgetreden!';
?>
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>
<div class="row">
<!-- Registration Form -->
<div class="col-md-4">
<div class="card shadow-sm">
<div class="card-header bg-primary text-white">
<h5 class="mb-0">
<i class="bi bi-plus-circle"></i> Nieuwe Infomercial Registreren
</h5>
</div>
<div class="card-body">
<form method="POST">
<div class="mb-3">
<label class="form-label">Product Naam</label>
<input type="text" name="title" class="form-control"
placeholder="bijv. Clever Cane" required>
</div>
<div class="mb-3">
<label class="form-label">Duur (HH:MM:SS)</label>
<input type="text" name="duration" class="form-control"
placeholder="00:30:00" pattern="[0-9]{2}:[0-9]{2}:[0-9]{2}" required>
<small class="text-muted">Formaat: UU:MM:SS</small>
</div>
<div class="mb-3">
<label class="form-label">Series Code (optioneel)</label>
<input type="text" name="series_code" class="form-control"
placeholder="bijv. 006a">
<small class="text-muted">Voor groepering in kalender</small>
</div>
<input type="hidden" name="season_id" value="<?= $_ENV['TV_SEASON_ID'] ?>">
<div class="d-grid">
<button type="submit" name="add_commercial" class="btn btn-primary">
<i class="bi bi-cloud-upload"></i> Registreren bij Talpa
</button>
</div>
</form>
<div class="mt-3 p-2 bg-light rounded">
<small class="text-muted">
<i class="bi bi-info-circle"></i>
Dit registreert de infomercial bij Talpa en maakt automatisch een media asset aan.
</small>
</div>
</div>
</div>
</div>
<!-- Infomercial List -->
<div class="col-md-8">
<div class="card shadow-sm">
<div class="card-header bg-secondary text-white">
<h5 class="mb-0"><i class="bi bi-list-ul"></i> Geregistreerde Infomercials</h5>
</div>
<div class="card-body p-0">
<?php if (empty($infomercials)): ?>
<div class="p-4 text-center text-muted">
<i class="bi bi-inbox" style="font-size: 3rem;"></i>
<p class="mt-2">Nog geen infomercials geregistreerd</p>
</div>
<?php else: ?>
<div class="table-responsive">
<table class="table table-hover mb-0">
<thead class="table-light">
<tr>
<th>Kleur</th>
<th>Titel</th>
<th>Duur</th>
<th>Series</th>
<th>Label</th>
<th>Status</th>
<th>Gebruik</th>
<th>Acties</th>
</tr>
</thead>
<tbody>
<?php foreach ($infomercials as $c): ?>
<tr>
<td>
<div class="color-preview"
style="background-color: <?= htmlspecialchars($c['color_code'] ?? '#cccccc') ?>;"
title="<?= htmlspecialchars($c['color_code'] ?? '#cccccc') ?>">
</div>
</td>
<td>
<strong><?= htmlspecialchars($c['title']) ?></strong>
<br>
<small class="text-muted">ID: <?= htmlspecialchars($c['content_id']) ?></small>
</td>
<td>
<span class="badge bg-info"><?= $c['duration'] ?></span>
</td>
<td>
<?= $c['series_code'] ? '<span class="badge bg-secondary">' . htmlspecialchars($c['series_code']) . '</span>' : '-' ?>
</td>
<td>
<code class="small"><?= htmlspecialchars($c['media_asset_label']) ?></code>
</td>
<td>
<?php if ($c['upload_status'] == 'uploaded'): ?>
<span class="badge bg-success">Uploaded</span>
<?php else: ?>
<span class="badge bg-warning text-dark">Pending</span>
<?php endif; ?>
</td>
<td>
<span class="badge bg-<?= $c['usage_count'] > 0 ? 'primary' : 'secondary' ?>">
<?= $c['usage_count'] ?>x
</span>
</td>
<td>
<div class="btn-group btn-group-sm">
<button type="button"
class="btn btn-outline-primary"
data-bs-toggle="modal"
data-bs-target="#editModal<?= $c['id'] ?>"
title="Bewerken">
<i class="bi bi-pencil"></i>
</button>
<?php if ($c['usage_count'] == 0): ?>
<form method="POST" style="display:inline;"
onsubmit="return confirm('Weet je zeker dat je deze infomercial wilt verwijderen?');">
<input type="hidden" name="infomercial_id" value="<?= $c['id'] ?>">
<button type="submit" name="delete_commercial"
class="btn btn-outline-danger" title="Verwijderen">
<i class="bi bi-trash"></i>
</button>
</form>
<?php endif; ?>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
<!-- Edit Modals (outside the table loop) -->
<?php foreach ($infomercials as $c): ?>
<div class="modal fade" id="editModal<?= $c['id'] ?>" tabindex="-1" aria-labelledby="editModalLabel<?= $c['id'] ?>" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form method="POST">
<input type="hidden" name="infomercial_id" value="<?= $c['id'] ?>">
<div class="modal-header">
<h5 class="modal-title">Bewerk: <?= htmlspecialchars($c['title']) ?></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label class="form-label">Media Asset Label</label>
<input type="text" name="media_asset_label" class="form-control"
value="<?= htmlspecialchars($c['media_asset_label']) ?>">
</div>
<div class="mb-3">
<label class="form-label">Upload Status</label>
<select name="upload_status" class="form-select">
<option value="pending" <?= $c['upload_status'] == 'pending' ? 'selected' : '' ?>>Pending</option>
<option value="uploaded" <?= $c['upload_status'] == 'uploaded' ? 'selected' : '' ?>>Uploaded</option>
</select>
</div>
<div class="mb-3">
<label class="form-label">Series Code</label>
<input type="text" name="series_code" class="form-control"
value="<?= htmlspecialchars($c['series_code'] ?? '') ?>"
placeholder="bijv. 006a">
</div>
<div class="mb-3">
<label class="form-label">Kleurcode</label>
<input type="color" name="color_code" class="form-control form-control-color"
value="<?= htmlspecialchars($c['color_code'] ?? '#cccccc') ?>">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Annuleren</button>
<button type="submit" name="update_media_asset" class="btn btn-primary">
<i class="bi bi-check-circle"></i> Opslaan
</button>
</div>
</form>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
// API Logs to console with enhanced debugging
const apiLogs = <?= json_encode($apiLogs) ?>;
if (apiLogs.length > 0) {
console.group("🔍 Talpa API Debug Logs - Infomercial Registration");
apiLogs.forEach((log, index) => {
if (log.call) {
console.group(`${index + 1}. ${log.call}`);
if (log.request) {
console.log("%cRequest:", "color: #3498db; font-weight: bold;", log.request);
}
console.log("%cResponse:", "color: #2ecc71; font-weight: bold;", log.response);
console.groupEnd();
} else if (log.step) {
console.log(`%c${log.step}`, "color: #9b59b6; font-weight: bold;", log);
}
});
console.groupEnd();
// Show summary
const hasErrors = apiLogs.some(log =>
(log.step && log.step.includes('failed')) ||
(log.response && log.response.error)
);
if (hasErrors) {
console.warn("⚠️ Er zijn fouten opgetreden tijdens de registratie. Zie bovenstaande logs voor details.");
} else {
console.log("✅ Infomercial registratie succesvol voltooid");
}
}
</script>
</body>
</html>