Current directory: /home/klas4s23/domains/585455.klas4s23.mid-ica.nl/public_html/Gastenboek/uploads
<?php
session_start();
header('Content-Type: application/json');
require_once 'config.php';
$user_id = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : null;
try {
$pdo = getDbConnection();
// Get all lists (user's own + public lists)
if ($user_id) {
$stmt = $pdo->prepare("
SELECT
wl.list_id,
wl.list_name,
wl.description,
wl.is_public,
wl.created_at,
wl.updated_at,
u.username as creator_username,
COUNT(lw.list_word_id) as word_count,
CASE WHEN wl.user_id = :user_id THEN 1 ELSE 0 END as is_owner
FROM word_lists wl
LEFT JOIN users u ON wl.user_id = u.user_id
LEFT JOIN list_words lw ON wl.list_id = lw.list_id
WHERE wl.user_id = :user_id_check OR wl.is_public = 1
GROUP BY wl.list_id
ORDER BY is_owner DESC, wl.created_at DESC
");
$stmt->execute([
':user_id' => $user_id,
':user_id_check' => $user_id
]);
} else {
// Not logged in - only show public lists
$stmt = $pdo->prepare("
SELECT
wl.list_id,
wl.list_name,
wl.description,
wl.is_public,
wl.created_at,
wl.updated_at,
u.username as creator_username,
COUNT(lw.list_word_id) as word_count,
0 as is_owner
FROM word_lists wl
LEFT JOIN users u ON wl.user_id = u.user_id
LEFT JOIN list_words lw ON wl.list_id = lw.list_id
WHERE wl.is_public = 1
GROUP BY wl.list_id
ORDER BY wl.created_at DESC
");
$stmt->execute();
}
$lists = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode([
'success' => true,
'lists' => $lists
]);
} catch(PDOException $e) {
http_response_code(500);
echo json_encode(['success' => false, 'error' => 'Database error: ' . $e->getMessage()]);
}
?>