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

186 lines
5.8 KiB
PHP

<?php
/**
* API Endpoint: Copy Block
* Copies a block (block times + transmissions) from source block to target block
*/
require_once __DIR__ . '/../vendor/autoload.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 (!canEdit()) {
http_response_code(403);
echo json_encode(['success' => false, 'error' => 'Geen toegang. Alleen admins kunnen blokken kopiƫren.']);
exit;
}
try {
$db = getDbConnection();
// Get POST data
$input = json_decode(file_get_contents('php://input'), true);
$sourceBlockId = $input['source_block_id'] ?? null;
$targetBlockId = $input['target_block_id'] ?? null;
$targetDate = $input['target_date'] ?? null;
// Validate input
if (!$sourceBlockId || !$targetBlockId || !$targetDate) {
jsonResponse(['success' => false, 'error' => 'Missing required parameters'], 400);
}
if (!isValidDate($targetDate)) {
jsonResponse(['success' => false, 'error' => 'Invalid target date format'], 400);
}
// Start transaction
$db->beginTransaction();
try {
// Get source block info
$stmt = $db->prepare("
SELECT * FROM daily_blocks WHERE id = ?
");
$stmt->execute([$sourceBlockId]);
$sourceBlock = $stmt->fetch();
if (!$sourceBlock) {
throw new Exception('Source block not found');
}
// Get target block info
$stmt = $db->prepare("
SELECT * FROM daily_blocks WHERE id = ?
");
$stmt->execute([$targetBlockId]);
$targetBlock = $stmt->fetch();
if (!$targetBlock) {
throw new Exception('Target block not found');
}
// Validate that blocks are compatible (same channel)
if ($sourceBlock['channel'] !== $targetBlock['channel']) {
throw new Exception('Source and target blocks must be on the same channel');
}
// Get template names to compare (allows copying between different template IDs with same name)
$stmt = $db->prepare("SELECT name FROM block_templates WHERE id = ?");
$stmt->execute([$sourceBlock['template_id']]);
$sourceTemplateName = $stmt->fetchColumn();
$stmt = $db->prepare("SELECT name FROM block_templates WHERE id = ?");
$stmt->execute([$targetBlock['template_id']]);
$targetTemplateName = $stmt->fetchColumn();
if ($sourceTemplateName !== $targetTemplateName) {
throw new Exception('Source and target blocks must have the same template name');
}
// Step 1: Update target block times
$stmt = $db->prepare("
UPDATE daily_blocks
SET actual_start_time = ?,
actual_end_time = ?
WHERE id = ?
");
$stmt->execute([
$sourceBlock['actual_start_time'],
$sourceBlock['actual_end_time'],
$targetBlockId
]);
// Step 2: Delete existing transmissions in target block
// Use the OLD target block times to find transmissions to delete
$stmt = $db->prepare("
DELETE FROM transmissions
WHERE start_date = ?
AND channel = ?
AND start_time >= ?
AND start_time < ?
");
$stmt->execute([
$targetDate,
$targetBlock['channel'],
$targetBlock['actual_start_time'],
$targetBlock['actual_end_time'] ?? '23:59:59'
]);
$deletedCount = $stmt->rowCount();
// Step 3: Get source transmissions
$stmt = $db->prepare("
SELECT t.*, 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([
$sourceBlock['block_date'],
$sourceBlock['channel'],
$sourceBlock['actual_start_time'],
$sourceBlock['actual_end_time'] ?? '23:59:59'
]);
$sourceTransmissions = $stmt->fetchAll();
// Step 4: Copy transmissions to target block
$copiedCount = 0;
$insertStmt = $db->prepare("
INSERT INTO transmissions
(infomercial_id, channel, template, start_date, start_time, duration, api_status)
VALUES (?, ?, ?, ?, ?, ?, 'pending')
");
foreach ($sourceTransmissions as $tx) {
$insertStmt->execute([
$tx['infomercial_id'],
$tx['channel'],
$tx['template'],
$targetDate, // Use target date
$tx['start_time'],
$tx['duration'],
]);
$copiedCount++;
}
// Commit transaction
$db->commit();
jsonResponse([
'success' => true,
'message' => 'Block copied successfully',
'copied_count' => $copiedCount,
'deleted_count' => $deletedCount,
'block_times_updated' => true
]);
} catch (Exception $e) {
$db->rollBack();
throw $e;
}
} catch (Exception $e) {
jsonResponse([
'success' => false,
'error' => $e->getMessage()
], 500);
}