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

48 lines
1.3 KiB
PHP

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