262 lines
13 KiB
PHP

<?php
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';
use Dotenv\Dotenv;
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->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 Commercial (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 commercials (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 commercials WHERE id = ?");
$stmt->execute([$_POST['commercial_id']]);
$commDuration = $stmt->fetchColumn();
if (!empty($_POST['edit_id'])) {
$stmt = $db->prepare("UPDATE transmissions SET commercial_id=?, channel=?, template=?, start_date=?, start_time=?, duration=?, api_status='pending' WHERE id=?");
$stmt->execute([$_POST['commercial_id'], $_POST['channel'], $_POST['template'], $_POST['start_date'], $_POST['start_time'], $commDuration, $_POST['edit_id']]);
} else {
$stmt = $db->prepare("INSERT INTO transmissions (commercial_id, channel, template, start_date, start_time, duration, api_status) VALUES (?, ?, ?, ?, ?, ?, 'pending')");
$stmt->execute([$_POST['commercial_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 commercials c ON t.commercial_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 commercials SET media_asset_label = ?, upload_status = ? WHERE id = ?");
$stmt->execute([$_POST['media_asset_label'], $_POST['upload_status'], $_POST['commercial_id']]);
header("Location: index.php?view_date=" . $selectedDate);
exit;
}
// Data ophalen
$commercials = $db->query("SELECT * FROM commercials 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 commercials c ON t.commercial_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();
}
?>
<!DOCTYPE html>
<html lang="nl">
<head>
<meta charset="UTF-8">
<title>Telvero Talpa Control Panel</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
</head>
<body class="bg-light">
<div class="container mt-5">
<h1>Telvero Homeshopping Planner</h1>
<div class="row mt-4">
<div class="col-md-4">
<div class="card p-3 shadow-sm">
<h5>1. Commercial Registreren</h5>
<form method="POST">
<input type="text" name="title" class="form-control mb-2" placeholder="Product Naam" required>
<input type="text" name="duration" class="form-control mb-2" placeholder="Duur (HH:MM:SS)" required>
<input type="hidden" name="season_id" value="<?= $_ENV['TV_SEASON_ID'] ?>">
<button type="submit" name="add_commercial" class="btn btn-primary w-100">Registreren bij Talpa</button>
</form>
</div>
</div>
<div class="col-md-8">
<div class="card p-3 shadow-sm">
<h5>2. <?= $editItem ? 'Planning Bewerken' : 'Tijdblok Reserveren' ?></h5>
<form method="POST" class="row g-2" id="planningForm">
<input type="hidden" name="edit_id" value="<?= $editItem['id'] ?? '' ?>">
<div class="col-md-6">
<select name="commercial_id" id="commercial_select" class="form-select" required>
<option value="">Selecteer Commercial...</option>
<?php foreach($commercials as $c): ?>
<option value="<?= $c['id'] ?>" data-duration="<?= $c['duration'] ?>" <?= (isset($editItem) && $editItem['commercial_id'] == $c['id']) ? 'selected' : '' ?>>
<?= htmlspecialchars($c['title']) ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-3"><input type="date" name="start_date" class="form-control" value="<?= $editItem['start_date'] ?? $selectedDate ?>" required></div>
<div class="col-md-3"><input type="time" name="start_time" class="form-control" value="<?= $editItem['start_time'] ?? '' ?>" required></div>
<div class="col-md-4">
<label class="small text-muted">Duur (automatisch)</label>
<input type="text" id="display_duration" class="form-control" value="<?= $editItem['duration'] ?? '' ?>" readonly>
</div>
<div class="col-md-4">
<label class="small text-muted">Channel</label>
<input type="text" name="channel" class="form-control" value="<?= $editItem['channel'] ?? 'NET5' ?>" required>
</div>
<div class="col-md-4">
<label class="small text-muted">Template</label>
<input type="text" name="template" class="form-control" value="<?= $editItem['template'] ?? 'HOME030' ?>" required>
</div>
<div class="col-md-12 mt-3">
<button type="submit" name="schedule_transmission" class="btn btn-success w-100"><?= $editItem ? 'Bijwerken' : 'Opslaan' ?></button>
<?php if($editItem): ?> <a href="index.php?view_date=<?= $selectedDate ?>" class="btn btn-link w-100 text-muted">Annuleren</a> <?php endif; ?>
</div>
</form>
</div>
</div>
</div>
<hr class="my-5">
<h3>Dagplanning: <?= htmlspecialchars($selectedDate) ?></h3>
<form method="GET" class="row g-2 mb-3">
<div class="col-md-3"><input type="date" name="view_date" class="form-control" value="<?= $selectedDate ?>"></div>
<div class="col-md-2"><button type="submit" class="btn btn-secondary">Bekijk Datum</button></div>
</form>
<table class="table table-bordered bg-white mb-5 text-center align-middle">
<thead class="table-dark">
<tr><th>Tijd</th><th>Product</th><th>Duur</th><th>Sync Status</th><th>Acties</th></tr>
</thead>
<tbody>
<?php foreach($dailySchedule as $tx): ?>
<tr>
<td><?= substr($tx['start_time'], 0, 5) ?></td>
<td><?= htmlspecialchars($tx['title']) ?></td>
<td><?= $tx['duration'] ?></td>
<td>
<span class="badge <?= $tx['api_status'] == 'synced' ? 'bg-success' : ($tx['api_status'] == 'error' ? 'bg-danger' : 'bg-warning text-dark') ?>">
<?= ucfirst($tx['api_status']) ?>
</span>
</td>
<td class="d-flex justify-content-center gap-2">
<a href="?edit=<?= $tx['id'] ?>&view_date=<?= $selectedDate ?>" class="btn btn-sm btn-outline-primary">Bewerk</a>
<?php if($tx['api_status'] !== 'synced'): ?>
<form method="POST" style="display:inline;">
<input type="hidden" name="sync_id" value="<?= $tx['id'] ?>">
<button type="submit" name="sync_item" class="btn btn-sm btn-success">Sync naar Talpa</button>
</form>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<h3>Media Asset Management</h3>
<div class="card p-3 shadow-sm">
<table class="table table-striped bg-white align-middle">
<thead class="table-secondary">
<tr>
<th>Titel</th>
<th>Duur</th>
<th>Content ID</th>
<th>Label (Filename)</th>
<th>Upload Status</th>
<th style="width: 120px;">Actie</th>
</tr>
</thead>
<tbody>
<?php foreach($commercials as $c): ?>
<tr>
<form method="POST">
<input type="hidden" name="commercial_id" value="<?= $c['id'] ?>">
<td><?= htmlspecialchars($c['title']) ?></td>
<td><span class="badge bg-info text-dark"><?= $c['duration'] ?></span></td>
<td><code><?= htmlspecialchars($c['content_id']) ?></code></td>
<td>
<input type="text" name="media_asset_label" class="form-control form-control-sm" value="<?= htmlspecialchars($c['media_asset_label']) ?>">
</td>
<td>
<select name="upload_status" class="form-select form-select-sm">
<option value="pending" <?= ($c['upload_status'] ?? 'pending') == 'pending' ? 'selected' : '' ?>>Pending</option>
<option value="uploaded" <?= ($c['upload_status'] ?? '') == 'uploaded' ? 'selected' : '' ?>>Uploaded</option>
</select>
</td>
<td>
<button type="submit" name="update_media_asset" class="btn btn-sm btn-primary">Opslaan</button>
</td>
</form>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
<script>
// Bestaande JS voor duur selectie
const select = document.getElementById('commercial_select');
const durationInput = document.getElementById('display_duration');
select.addEventListener('change', function() {
const selectedOption = this.options[this.selectedIndex];
const duration = selectedOption.getAttribute('data-duration');
durationInput.value = duration || '';
});
// LOGGING NAAR CONSOLE
const apiLogs = <?= json_encode($apiLogs) ?>;
if (apiLogs.length > 0) {
console.group("Talpa API Logs");
apiLogs.forEach(log => {
console.log("%c" + log.call, "color: #007bff; font-weight: bold;", log.response);
});
console.groupEnd();
}
</script>
</body>
</html>