![]() 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/public_html/api/ |
<?php
/**
* Toggle Crate Visibility (Public/Private)
* POST: { crate_id: int }
* Returns: { success: bool, is_public: bool, message: string }
*/
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;
if (!$crate_id) {
echo json_encode(['success' => false, 'error' => 'Crate ID is required']);
exit;
}
try {
$pdo = getDBConnection();
// First check if the is_public column exists
$checkColumn = $pdo->query("SHOW COLUMNS FROM artist_playlists LIKE 'is_public'");
if ($checkColumn->rowCount() === 0) {
// Add the column if it doesn't exist
$pdo->exec("ALTER TABLE artist_playlists ADD COLUMN is_public BOOLEAN DEFAULT TRUE");
error_log("Added is_public column to artist_playlists table");
}
// Verify crate belongs to user
$stmt = $pdo->prepare("SELECT id, is_public FROM artist_playlists WHERE id = ? AND user_id = ?");
$stmt->execute([$crate_id, $_SESSION['user_id']]);
$crate = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$crate) {
echo json_encode(['success' => false, 'error' => 'Crate not found or access denied']);
exit;
}
// Toggle visibility - handle MySQL boolean as int/string
$current_visibility = (bool)$crate['is_public'];
$new_visibility = !$current_visibility;
$stmt = $pdo->prepare("UPDATE artist_playlists SET is_public = ?, updated_at = NOW() WHERE id = ?");
$stmt->execute([$new_visibility ? 1 : 0, $crate_id]);
echo json_encode([
'success' => true,
'is_public' => $new_visibility,
'message' => $new_visibility ? 'Crate is now public and visible on your profile' : 'Crate is now private'
]);
} catch (Exception $e) {
error_log("Error toggling crate visibility: " . $e->getMessage());
echo json_encode(['success' => false, 'error' => 'Failed to update crate visibility: ' . $e->getMessage()]);
}