Files
yogibook_aury_new/public/api/push_preference.php
T
2026-08-15 12:00:28 +03:00

38 lines
1.1 KiB
PHP

<?php
require_once __DIR__ . '/_bootstrap.php';
$userId = (int) $user->id;
$metodo = $_SERVER['REQUEST_METHOD'] ?? 'GET';
if ($metodo === 'POST' || $metodo === 'PUT' || $metodo === 'PATCH') {
$input = json_decode((string) file_get_contents('php://input'), true);
if (!is_array($input) || !array_key_exists('enabled', $input)) {
http_response_code(422);
echo json_encode(['success' => false, 'error' => 'Parametro enabled mancante']);
exit;
}
$valore = $input['enabled'];
if (is_bool($valore)) {
$nuovo = $valore ? 'Y' : 'N';
} else {
$nuovo = strtoupper(trim((string) $valore)) === 'Y' ? 'Y' : 'N';
}
$stmt = $db->prepare("UPDATE auth_users SET pushnotification = :valore WHERE id = :uid");
$stmt->execute([':valore' => $nuovo, ':uid' => $userId]);
echo json_encode(['success' => true, 'push_enabled' => $nuovo]);
exit;
}
$stmt = $db->prepare("SELECT pushnotification FROM auth_users WHERE id = :uid LIMIT 1");
$stmt->execute([':uid' => $userId]);
echo json_encode([
'success' => true,
'push_enabled' => (string) ($stmt->fetchColumn() ?: 'Y'),
]);