Files
2025-09-28 16:05:53 +02:00

48 lines
1.3 KiB
PHP

<?php
// API endpoint for updating sort order
require_once __DIR__ . '/../include/auth.php';
header('Content-Type: application/json');
$data = json_decode(file_get_contents('php://input'), true);
if (!$data || !isset($data['type']) || !isset($data['order'])) {
http_response_code(400);
echo json_encode(['error' => 'Invalid input.']);
exit;
}
$type = $data['type'];
$order = $data['order'];
if (!is_array($order)) {
http_response_code(400);
echo json_encode(['error' => 'Order data must be an array.']);
exit;
}
$table = '';
if ($type === 'category') {
$table = 'categories';
} elseif ($type === 'bookmark') {
$table = 'bookmarks';
} else {
http_response_code(400);
echo json_encode(['error' => 'Invalid type specified.']);
exit;
}
try {
$pdo->beginTransaction();
$stmt = $pdo->prepare("UPDATE {$table} SET sort_order = ? WHERE id = ?");
foreach ($order as $index => $id) {
if (!is_numeric($id)) continue; // Basic sanitization
$stmt->execute([$index + 1, $id]);
}
$pdo->commit();
echo json_encode(['success' => true]);
} catch (Exception $e) {
$pdo->rollBack();
http_response_code(500);
echo json_encode(['error' => 'Database update failed.', 'details' => $e->getMessage()]);
}