![]() 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/-4ab5b5f0/ |
<?php
/**
* Toggle Track Visibility within a Crate
* Allows artist to decide which tracks are visible when crate is public
* POST: { crate_id: int, track_id: int }
*/
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;
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
echo json_encode(['success' => false, 'error' => 'Invalid request method']);
exit;
}
$input = json_decode(file_get_contents('php://input'), true);
$crate_id = isset($input['crate_id']) ? (int)$input['crate_id'] : 0;
$track_id = isset($input['track_id']) ? (int)$input['track_id'] : 0;
if (!$crate_id || !$track_id) {
echo json_encode(['success' => false, 'error' => 'Crate ID and Track ID are required']);
exit;
}
try {
$pdo = getDBConnection();
// Verify crate belongs to user
$stmt = $pdo->prepare("SELECT id FROM artist_playlists WHERE id = ? AND user_id = ?");
$stmt->execute([$crate_id, $_SESSION['user_id']]);
if (!$stmt->fetch()) {
echo json_encode(['success' => false, 'error' => 'Crate not found or access denied']);
exit;
}
// Check if is_public column exists in playlist_tracks
$checkColumn = $pdo->query("SHOW COLUMNS FROM playlist_tracks LIKE 'is_public'");
if ($checkColumn->rowCount() === 0) {
// Add the column if it doesn't exist
$pdo->exec("ALTER TABLE playlist_tracks ADD COLUMN is_public TINYINT(1) DEFAULT 1");
error_log("Added is_public column to playlist_tracks table");
}
// Get current visibility
$stmt = $pdo->prepare("SELECT is_public FROM playlist_tracks WHERE playlist_id = ? AND track_id = ?");
$stmt->execute([$crate_id, $track_id]);
$track = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$track) {
echo json_encode(['success' => false, 'error' => 'Track not found in crate']);
exit;
}
// Toggle visibility
$current = (bool)($track['is_public'] ?? true);
$new_visibility = !$current;
$stmt = $pdo->prepare("UPDATE playlist_tracks SET is_public = ? WHERE playlist_id = ? AND track_id = ?");
$stmt->execute([$new_visibility ? 1 : 0, $crate_id, $track_id]);
echo json_encode([
'success' => true,
'is_public' => $new_visibility,
'message' => $new_visibility ? 'Track is now visible in public crate' : 'Track is now hidden from public crate'
]);
} catch (Exception $e) {
error_log("Error toggling crate track visibility: " . $e->getMessage());
echo json_encode(['success' => false, 'error' => 'Failed to update track visibility']);
}