push notifications
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
<?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'),
|
||||
]);
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/_bootstrap.php';
|
||||
|
||||
$userId = (int) $user->id;
|
||||
$metodo = $_SERVER['REQUEST_METHOD'] ?? 'POST';
|
||||
|
||||
$input = json_decode((string) file_get_contents('php://input'), true);
|
||||
if (!is_array($input)) {
|
||||
$input = [];
|
||||
}
|
||||
|
||||
$deviceToken = trim((string) ($input['device_token'] ?? ''));
|
||||
|
||||
if ($deviceToken === '') {
|
||||
http_response_code(422);
|
||||
echo json_encode(['success' => false, 'error' => 'device_token mancante']);
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($metodo === 'DELETE') {
|
||||
$stmt = $db->prepare("
|
||||
UPDATE userdevicetoken
|
||||
SET active = 'N'
|
||||
WHERE devicetoken = :token AND iduser = :uid
|
||||
");
|
||||
$stmt->execute([':token' => $deviceToken, ':uid' => $userId]);
|
||||
|
||||
echo json_encode(['success' => true, 'deactivated' => $stmt->rowCount()]);
|
||||
exit;
|
||||
}
|
||||
|
||||
$platform = strtolower(trim((string) ($input['platform'] ?? 'android')));
|
||||
if (!in_array($platform, ['android', 'ios'], true)) {
|
||||
$platform = 'android';
|
||||
}
|
||||
|
||||
$appVersion = trim((string) ($input['app_version'] ?? ''));
|
||||
if ($appVersion === '') {
|
||||
$appVersion = null;
|
||||
}
|
||||
|
||||
$stmt = $db->prepare("
|
||||
INSERT INTO userdevicetoken (iduser, devicetoken, platform, appversion, active, lastseen_at)
|
||||
VALUES (:uid, :token, :platform, :versione, 'Y', NOW())
|
||||
ON DUPLICATE KEY UPDATE
|
||||
iduser = VALUES(iduser),
|
||||
platform = VALUES(platform),
|
||||
appversion = COALESCE(VALUES(appversion), appversion),
|
||||
active = 'Y',
|
||||
lastseen_at = NOW()
|
||||
");
|
||||
|
||||
$stmt->execute([
|
||||
':uid' => $userId,
|
||||
':token' => $deviceToken,
|
||||
':platform' => $platform,
|
||||
':versione' => $appVersion,
|
||||
]);
|
||||
|
||||
$stmt = $db->prepare("SELECT pushnotification FROM auth_users WHERE id = :uid LIMIT 1");
|
||||
$stmt->execute([':uid' => $userId]);
|
||||
$pushEnabled = (string) ($stmt->fetchColumn() ?: 'Y');
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'push_enabled' => $pushEnabled,
|
||||
]);
|
||||
Reference in New Issue
Block a user