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

77 lines
2.1 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';
use Dotenv\Dotenv;
$dotenv = Dotenv::createImmutable(__DIR__ . '/..');
$dotenv->load();
header('Content-Type: application/json');
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);
}