![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/.cursor-server/data/User/History/-5fdaacea/ |
<?php
require_once '../config/database.php';
session_start();
header('Content-Type: application/json');
// Only allow POST requests
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['success' => false, 'error' => 'Method not allowed']);
exit;
}
// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
http_response_code(401);
echo json_encode(['success' => false, 'error' => 'User must be logged in']);
exit;
}
// Get JSON input
$input = json_decode(file_get_contents('php://input'), true);
$crate_id = isset($input['crate_id']) ? (int)$input['crate_id'] : null;
if (!$crate_id) {
http_response_code(400);
echo json_encode(['success' => false, 'error' => 'Crate ID is required']);
exit;
}
try {
$pdo = getDBConnection();
// Verify crate belongs to user
$stmt = $pdo->prepare("SELECT id, name FROM artist_playlists WHERE id = ? AND user_id = ?");
$stmt->execute([$crate_id, $_SESSION['user_id']]);
$crate = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$crate) {
http_response_code(403);
echo json_encode(['success' => false, 'error' => 'Crate not found or access denied']);
exit;
}
// Get all tracks in the crate
$stmt = $pdo->prepare("
SELECT
mt.id as track_id,
mt.title,
mt.price,
mt.user_id as track_owner_id
FROM playlist_tracks pt
JOIN music_tracks mt ON pt.track_id = mt.id
WHERE pt.playlist_id = ?
AND mt.status = 'complete'
AND mt.price > 0
AND mt.user_id != ?
ORDER BY pt.position ASC
");
$stmt->execute([$crate_id, $_SESSION['user_id']]);
$tracks = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (empty($tracks)) {
echo json_encode([
'success' => true,
'added_count' => 0,
'skipped_count' => 0,
'message' => 'No tracks available to add to cart'
]);
exit;
}
$added_count = 0;
$skipped_count = 0;
$errors = [];
// Add each track to cart (skip if already in cart)
foreach ($tracks as $track) {
// Check if track is already in cart
$check_stmt = $pdo->prepare("SELECT id FROM cart_items WHERE user_id = ? AND track_id = ?");
$check_stmt->execute([$_SESSION['user_id'], $track['track_id']]);
if ($check_stmt->fetch()) {
$skipped_count++;
continue;
}
// Check if user already owns or purchased this track
$own_check = $pdo->prepare("
SELECT COUNT(*) as count FROM track_purchases
WHERE user_id = ? AND track_id = ?
");
$own_check->execute([$_SESSION['user_id'], $track['track_id']]);
$purchase = $own_check->fetch();
if ($purchase['count'] > 0) {
$skipped_count++;
continue;
}
// Add track to cart
try {
$insert_stmt = $pdo->prepare("
INSERT INTO cart_items (user_id, track_id, title, price, added_at)
VALUES (?, ?, ?, ?, NOW())
");
$insert_stmt->execute([
$_SESSION['user_id'],
$track['track_id'],
$track['title'],
$track['price']
]);
$added_count++;
} catch (Exception $e) {
$errors[] = "Failed to add track {$track['track_id']}: " . $e->getMessage();
$skipped_count++;
}
}
echo json_encode([
'success' => true,
'added_count' => $added_count,
'skipped_count' => $skipped_count,
'total_tracks' => count($tracks),
'errors' => $errors,
'message' => "Added $added_count track(s) to cart" . ($skipped_count > 0 ? " ($skipped_count already in cart or owned)" : "")
]);
} catch (Exception $e) {
error_log("Error adding crate tracks to cart: " . $e->getMessage());
http_response_code(500);
echo json_encode(['success' => false, 'error' => 'Internal server error: ' . $e->getMessage()]);
}
?>