218 lines
7.3 KiB
PHP
218 lines
7.3 KiB
PHP
<?php
|
|
/**
|
|
* Sync Block to Talpa
|
|
* Synchronizes all transmissions in a specific block to Talpa API
|
|
*/
|
|
|
|
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';
|
|
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 (!canSync()) {
|
|
http_response_code(403);
|
|
echo json_encode(['success' => false, 'error' => 'Geen toegang. Alleen admins kunnen synchroniseren.']);
|
|
exit;
|
|
}
|
|
|
|
$api = new TalpaApi();
|
|
$db = getDbConnection();
|
|
|
|
// Debug logging
|
|
$debugLog = [];
|
|
$debugLog[] = ['step' => 'Start', 'time' => date('Y-m-d H:i:s')];
|
|
|
|
// Get POST data
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
$debugLog[] = ['step' => 'Input received', 'data' => $input];
|
|
|
|
if (!isset($input['date']) || !isset($input['channel'])) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => 'Datum en channel zijn verplicht',
|
|
'debug' => $debugLog
|
|
]);
|
|
exit;
|
|
}
|
|
|
|
$date = $input['date'];
|
|
$channel = $input['channel'];
|
|
$debugLog[] = ['step' => 'Parameters', 'date' => $date, 'channel' => $channel];
|
|
|
|
try {
|
|
// Get all transmissions for this block
|
|
// Include both new (pending) and existing (synced) transmissions
|
|
$stmt = $db->prepare("
|
|
SELECT t.*, c.content_id, c.title as commercial_title
|
|
FROM transmissions t
|
|
JOIN infomercials c ON t.infomercial_id = c.id
|
|
WHERE t.start_date = ?
|
|
AND t.channel = ?
|
|
ORDER BY t.start_time ASC
|
|
");
|
|
$stmt->execute([$date, $channel]);
|
|
$transmissions = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
$debugLog[] = ['step' => 'Query executed', 'count' => count($transmissions)];
|
|
|
|
if (empty($transmissions)) {
|
|
echo json_encode([
|
|
'success' => true,
|
|
'message' => 'Geen uitzendingen om te synchroniseren',
|
|
'synced' => 0,
|
|
'failed' => 0,
|
|
'debug' => $debugLog
|
|
]);
|
|
exit;
|
|
}
|
|
|
|
$syncedCount = 0;
|
|
$failedCount = 0;
|
|
$errors = [];
|
|
$apiCalls = [];
|
|
|
|
// Sync each transmission
|
|
foreach ($transmissions as $tx) {
|
|
$txDebug = [
|
|
'transmission_id' => $tx['id'],
|
|
'title' => $tx['commercial_title'],
|
|
'time' => $tx['start_time'],
|
|
'talpa_transmission_id' => $tx['talpa_transmission_id']
|
|
];
|
|
|
|
try {
|
|
$requestData = [
|
|
"channel" => $tx['channel'],
|
|
"template" => $tx['template'],
|
|
"startDate" => $tx['start_date'],
|
|
"startTime" => $tx['start_time'],
|
|
"duration" => $tx['duration'],
|
|
"contentId" => $tx['content_id']
|
|
];
|
|
|
|
$txDebug['request'] = $requestData;
|
|
|
|
// Determine if this is a new transmission or an update
|
|
if (!empty($tx['talpa_transmission_id'])) {
|
|
// Update existing transmission in Talpa
|
|
$txDebug['action'] = 'update';
|
|
$res = $api->updateTransmission($tx['talpa_transmission_id'], $requestData);
|
|
} else {
|
|
// Create new transmission in Talpa
|
|
$txDebug['action'] = 'create';
|
|
$res = $api->createTransmission($requestData);
|
|
}
|
|
|
|
$txDebug['response'] = $res;
|
|
$txDebug['lastResponse'] = $api->lastResponse;
|
|
|
|
// Check if sync was successful
|
|
$status = 'error';
|
|
$talpaTransmissionId = $tx['talpa_transmission_id'];
|
|
|
|
if ($txDebug['action'] === 'create') {
|
|
// For create: check for id in response
|
|
if (isset($res['id'])) {
|
|
$status = 'synced';
|
|
$talpaTransmissionId = $res['id'];
|
|
} elseif (isset($res['statusCode']) && $res['statusCode'] == 201) {
|
|
$status = 'synced';
|
|
$talpaTransmissionId = $res['id'] ?? $talpaTransmissionId;
|
|
}
|
|
} else {
|
|
// For update: check for success status
|
|
if (isset($res['statusCode']) && ($res['statusCode'] == 200 || $res['statusCode'] == '200')) {
|
|
$status = 'synced';
|
|
} elseif (isset($res['message']) && strpos($res['message'], 'updated') !== false) {
|
|
$status = 'synced';
|
|
}
|
|
}
|
|
|
|
$txDebug['determined_status'] = $status;
|
|
$txDebug['talpa_transmission_id_saved'] = $talpaTransmissionId;
|
|
|
|
// Update transmission status and talpa_transmission_id
|
|
$updateStmt = $db->prepare("
|
|
UPDATE transmissions
|
|
SET api_status = ?, api_response = ?, talpa_transmission_id = ?
|
|
WHERE id = ?
|
|
");
|
|
$updateStmt->execute([$status, json_encode($res), $talpaTransmissionId, $tx['id']]);
|
|
|
|
if ($status === 'synced') {
|
|
$syncedCount++;
|
|
$txDebug['result'] = 'success';
|
|
} else {
|
|
$failedCount++;
|
|
$txDebug['result'] = 'failed';
|
|
$errors[] = [
|
|
'transmission_id' => $tx['id'],
|
|
'title' => $tx['commercial_title'],
|
|
'time' => $tx['start_time'],
|
|
'error' => $res['message'] ?? json_encode($res)
|
|
];
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
$failedCount++;
|
|
$txDebug['result'] = 'exception';
|
|
$txDebug['exception'] = $e->getMessage();
|
|
|
|
$errors[] = [
|
|
'transmission_id' => $tx['id'],
|
|
'title' => $tx['commercial_title'],
|
|
'time' => $tx['start_time'],
|
|
'error' => $e->getMessage()
|
|
];
|
|
|
|
// Update status to error
|
|
$updateStmt = $db->prepare("
|
|
UPDATE transmissions
|
|
SET api_status = 'error', api_response = ?
|
|
WHERE id = ?
|
|
");
|
|
$updateStmt->execute([json_encode(['error' => $e->getMessage()]), $tx['id']]);
|
|
}
|
|
|
|
$apiCalls[] = $txDebug;
|
|
}
|
|
|
|
$debugLog[] = ['step' => 'Sync completed', 'synced' => $syncedCount, 'failed' => $failedCount];
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'message' => "Synchronisatie voltooid: {$syncedCount} geslaagd, {$failedCount} mislukt",
|
|
'synced' => $syncedCount,
|
|
'failed' => $failedCount,
|
|
'errors' => $errors,
|
|
'debug' => $debugLog,
|
|
'api_calls' => $apiCalls
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
$debugLog[] = ['step' => 'Exception caught', 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString()];
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => 'Database fout: ' . $e->getMessage(),
|
|
'debug' => $debugLog
|
|
]);
|
|
}
|