![]() 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/domains/soundstudiopro.com/private_html/api/ |
<?php
session_start();
header('Content-Type: application/json');
require_once '../config/database.php';
if (!isset($_SESSION['user_id'])) {
echo json_encode(['success' => false, 'error' => 'Not authenticated']);
exit;
}
try {
$pdo = getDBConnection();
// Get track_id from request if provided (to check if track is already in crates)
$track_id = isset($_GET['track_id']) ? (int)$_GET['track_id'] : null;
// Get all crates (playlists) for the user with track counts and total duration
$stmt = $pdo->prepare("
SELECT
ap.id,
ap.name,
ap.description,
ap.is_primary,
ap.is_public,
ap.created_at,
ap.updated_at,
COUNT(DISTINCT pt.track_id) as track_count,
COALESCE(SUM(mt.duration), 0) as total_duration,
COALESCE(SUM(CASE
WHEN mt.duration >= 300 THEN 2.5
ELSE mt.duration * 0.5 / 60
END), 0) as set_duration_minutes
FROM artist_playlists ap
LEFT JOIN playlist_tracks pt ON ap.id = pt.playlist_id
LEFT JOIN music_tracks mt ON pt.track_id = mt.id AND mt.status = 'complete'
WHERE ap.user_id = ?
GROUP BY ap.id, ap.name, ap.description, ap.is_primary, ap.is_public, ap.created_at, ap.updated_at
ORDER BY ap.created_at DESC
");
$stmt->execute([$_SESSION['user_id']]);
$crates = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Check which crates already have this track (if track_id provided)
if ($track_id) {
$check_stmt = $pdo->prepare("
SELECT playlist_id
FROM playlist_tracks
WHERE track_id = ? AND playlist_id IN (
SELECT id FROM artist_playlists WHERE user_id = ?
)
");
$check_stmt->execute([$track_id, $_SESSION['user_id']]);
$crates_with_track = $check_stmt->fetchAll(PDO::FETCH_COLUMN);
} else {
$crates_with_track = [];
}
// Calculate set info for each crate (2-hour set = 120 minutes)
foreach ($crates as &$crate) {
$set_duration = floatval($crate['set_duration_minutes']);
$crate['set_duration_minutes'] = round($set_duration, 1);
$crate['is_2_hour_set'] = $set_duration >= 120;
$crate['set_progress_percent'] = min(100, round(($set_duration / 120) * 100, 1));
$crate['tracks_needed_for_2h'] = max(0, ceil((120 - $set_duration) / 2.5));
$crate['has_track'] = $track_id ? in_array($crate['id'], $crates_with_track) : false;
}
unset($crate);
echo json_encode([
'success' => true,
'crates' => $crates
]);
} catch (Exception $e) {
error_log("Error getting crates: " . $e->getMessage());
echo json_encode(['success' => false, 'error' => 'Failed to get crates: ' . $e->getMessage()]);
}