telvero_whatson_talpa/api/delete_transmission.php
2026-02-19 15:58:15 +01:00

92 lines
2.6 KiB
PHP

<?php
/**
* API Endpoint: Delete Transmission
* Deletes a transmission from the schedule
*/
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../helpers.php';
require_once __DIR__ . '/../TalpaAPI.php';
require_once __DIR__ . '/../auth/auth_functions.php';
use Dotenv\Dotenv;
$dotenv = Dotenv::createImmutable(__DIR__ . '/..');
$dotenv->load();
header('Content-Type: application/json');
// Authentication check
if (!isLoggedIn()) {
http_response_code(401);
echo json_encode(['success' => false, 'error' => 'Niet geautoriseerd. Log eerst in.']);
exit;
}
// Authorization check - admin only
if (!canDelete()) {
http_response_code(403);
echo json_encode(['success' => false, 'error' => 'Geen toegang. Alleen admins kunnen uitzendingen verwijderen.']);
exit;
}
try {
$db = getDbConnection();
// Get POST data
$input = json_decode(file_get_contents('php://input'), true);
if (!$input) {
$input = $_POST;
}
// Validate required fields
if (empty($input['id'])) {
jsonResponse([
'success' => false,
'error' => 'Missing transmission ID'
], 400);
}
// Check if transmission exists and get talpa_transmission_id
$stmt = $db->prepare("SELECT id, talpa_transmission_id FROM transmissions WHERE id = ?");
$stmt->execute([$input['id']]);
$transmission = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$transmission) {
jsonResponse([
'success' => false,
'error' => 'Transmission not found'
], 404);
}
// Delete from Talpa API if talpa_transmission_id exists
if (!empty($transmission['talpa_transmission_id'])) {
try {
$talpaApi = new TalpaApi();
$talpaResponse = $talpaApi->deleteTransmission($transmission['talpa_transmission_id']);
// Log the Talpa API response
error_log("Talpa API delete response: " . json_encode($talpaResponse));
} catch (Exception $e) {
// Log error but continue with local deletion
error_log("Failed to delete transmission from Talpa API: " . $e->getMessage());
}
}
// Delete transmission from local database
$stmt = $db->prepare("DELETE FROM transmissions WHERE id = ?");
$stmt->execute([$input['id']]);
jsonResponse([
'success' => true,
'message' => 'Transmission deleted successfully'
]);
} catch (Exception $e) {
jsonResponse([
'success' => false,
'error' => $e->getMessage()
], 500);
}