134 lines
4.0 KiB
PHP
134 lines
4.0 KiB
PHP
<?php
|
|
/**
|
|
* API Endpoint: Insert Transmission at Position
|
|
* Inserts a transmission at a specific position in the block
|
|
*/
|
|
|
|
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();
|
|
|
|
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
|
|
$required = ['infomercial_id', 'channel', 'date', 'block_id', 'position'];
|
|
foreach ($required as $field) {
|
|
if (!isset($input[$field])) {
|
|
jsonResponse([
|
|
'success' => false,
|
|
'error' => "Missing required field: $field"
|
|
], 400);
|
|
}
|
|
}
|
|
|
|
// Get infomercial duration
|
|
$stmt = $db->prepare("SELECT duration FROM infomercials WHERE id = ?");
|
|
$stmt->execute([$input['infomercial_id']]);
|
|
$duration = $stmt->fetchColumn();
|
|
|
|
if (!$duration) {
|
|
jsonResponse([
|
|
'success' => false,
|
|
'error' => 'Infomercial not found'
|
|
], 404);
|
|
}
|
|
|
|
// Get block start and end time
|
|
$stmt = $db->prepare("SELECT actual_start_time, actual_end_time FROM daily_blocks WHERE id = ?");
|
|
$stmt->execute([$input['block_id']]);
|
|
$blockInfo = $stmt->fetch();
|
|
$blockStartTime = $blockInfo['actual_start_time'];
|
|
$blockEndTime = $blockInfo['actual_end_time'] ?? '23:59:59';
|
|
|
|
// Get all transmissions in THIS SPECIFIC block only
|
|
$stmt = $db->prepare("
|
|
SELECT t.id, t.start_time, c.duration
|
|
FROM transmissions t
|
|
JOIN infomercials c ON t.infomercial_id = c.id
|
|
WHERE t.start_date = ? AND t.channel = ?
|
|
AND t.start_time >= ? AND t.start_time < ?
|
|
ORDER BY t.start_time ASC
|
|
");
|
|
$stmt->execute([$input['date'], $input['channel'], $blockStartTime, $blockEndTime]);
|
|
$transmissions = $stmt->fetchAll();
|
|
|
|
// Insert new transmission at position
|
|
$position = (int)$input['position'];
|
|
|
|
// Create temporary transmission entry
|
|
$newTransmission = [
|
|
'infomercial_id' => $input['infomercial_id'],
|
|
'duration' => $duration
|
|
];
|
|
|
|
// Insert at position
|
|
array_splice($transmissions, $position, 0, [$newTransmission]);
|
|
|
|
// Recalculate all start times
|
|
$currentTime = $blockStartTime;
|
|
$newTransmissionId = null;
|
|
|
|
foreach ($transmissions as $index => $tx) {
|
|
if ($index === $position) {
|
|
// Insert new transmission
|
|
$stmt = $db->prepare("
|
|
INSERT INTO transmissions
|
|
(infomercial_id, channel, template, start_date, start_time, duration, api_status)
|
|
VALUES (?, ?, 'HOME030', ?, ?, ?, 'pending')
|
|
");
|
|
$stmt->execute([
|
|
$input['infomercial_id'],
|
|
$input['channel'],
|
|
$input['date'],
|
|
$currentTime,
|
|
$duration
|
|
]);
|
|
$newTransmissionId = $db->lastInsertId();
|
|
} else {
|
|
// Update existing transmission
|
|
$stmt = $db->prepare("UPDATE transmissions SET start_time = ?, api_status = 'pending' WHERE id = ?");
|
|
$stmt->execute([$currentTime, $tx['id']]);
|
|
}
|
|
|
|
$currentTime = addTimeToTime($currentTime, $tx['duration']);
|
|
}
|
|
|
|
// Sync all transmissions in the block to Talpa
|
|
$api = new TalpaApi();
|
|
$blockUpdateResult = updateBlockTransmissionsToTalpa(
|
|
$db,
|
|
$api,
|
|
$input['date'],
|
|
$input['channel'],
|
|
$input['block_id']
|
|
);
|
|
|
|
jsonResponse([
|
|
'success' => true,
|
|
'message' => 'Transmission inserted successfully',
|
|
'new_transmission_id' => $newTransmissionId,
|
|
'block_update' => $blockUpdateResult
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
jsonResponse([
|
|
'success' => false,
|
|
'error' => $e->getMessage()
|
|
], 500);
|
|
}
|