Files
bookmarkly2/api/fix_orphaned_bookmarks.php
2025-09-28 16:05:53 +02:00

40 lines
1.3 KiB
PHP

<?php
require_once '../include/init.php';
require_once '../include/auth.php';
header('Content-Type: application/json');
// Security Check: Only Admin can access
if (!isAdmin()) {
http_response_code(403);
echo json_encode(['success' => false, 'message' => 'You do not have permission to perform this action.']);
exit;
}
try {
// Find the default tab (first one by sort order or ID) to assign orphans to
$stmt = $pdo->query("SELECT id FROM tabs ORDER BY sort_order ASC, id ASC LIMIT 1");
$default_tab_id = $stmt->fetchColumn();
if (!$default_tab_id) {
throw new Exception("No default tab found to assign bookmarks to.");
}
$update_stmt = $pdo->prepare("UPDATE bookmarks SET tab_id = ? WHERE tab_id IS NULL OR tab_id = '' OR tab_id = 0");
$update_stmt->execute([$default_tab_id]);
$count = $update_stmt->rowCount();
if ($count > 0) {
$message = "Successfully assigned {$count} orphaned bookmarks to the default tab (ID: {$default_tab_id}).";
} else {
$message = "No orphaned bookmarks were found.";
}
echo json_encode(['success' => true, 'message' => $message, 'updated_count' => $count]);
} catch (Exception $e) {
http_response_code(500);
echo json_encode(['success' => false, 'message' => 'Database error: ' . $e->getMessage()]);
}