143 lines
4.5 KiB
PHP
143 lines
4.5 KiB
PHP
<?php
|
|
|
|
/**
|
|
* updatepushsetting.php
|
|
* --------------------------------------------------------------------------
|
|
* Salva una riga della tabella notificationsetting (chiamato in AJAX da
|
|
* push-settings.php).
|
|
*
|
|
* Richiede la sessione di un amministratore: da qui si decidono i testi e gli
|
|
* orari delle notifiche inviate a tutti gli iscritti.
|
|
* --------------------------------------------------------------------------
|
|
*/
|
|
|
|
// Non usiamo include/headscript.php: quello stampa HTML e qui dobbiamo poter
|
|
// mandare gli header JSON. extra/auth.php avvia la sessione senza output.
|
|
require_once __DIR__ . '/../extra/auth.php';
|
|
require_once __DIR__ . '/class/db-functions.php';
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
if (!Auth::check()) {
|
|
http_response_code(401);
|
|
echo json_encode(['success' => false, 'error' => 'Sessione scaduta']);
|
|
exit;
|
|
}
|
|
|
|
// Solo amministratori (role_id = 1).
|
|
if ((int) Auth::user()->role_id !== 1) {
|
|
http_response_code(403);
|
|
echo json_encode(['success' => false, 'error' => 'Non autorizzato']);
|
|
exit;
|
|
}
|
|
|
|
$pdo = DBHandlerSelect::getInstance()->getConnection();
|
|
|
|
$id = (int) ($_POST['idnotificationsetting'] ?? 0);
|
|
$active = ($_POST['active'] ?? 'N') === 'Y' ? 'Y' : 'N';
|
|
$daysbefore = (int) ($_POST['daysbefore'] ?? 0);
|
|
$sendhour = trim((string) ($_POST['sendhour'] ?? ''));
|
|
$title = trim((string) ($_POST['title'] ?? ''));
|
|
$body = trim((string) ($_POST['body'] ?? ''));
|
|
// Testi usati quando la notifica raggruppa piu' elementi (piu' lezioni in un
|
|
// giorno). Possono restare vuoti: in quel caso si usa il testo singolo.
|
|
//
|
|
// Attenzione alla differenza: campo inviato vuoto = l'admin lo ha svuotato
|
|
// apposta; campo NON inviato = chi chiama non se ne occupa, e quello che c'e'
|
|
// gia' nel database non va perso.
|
|
$aggiornaMulti = array_key_exists('title_multi', $_POST) || array_key_exists('body_multi', $_POST);
|
|
$titleMulti = trim((string) ($_POST['title_multi'] ?? ''));
|
|
$bodyMulti = trim((string) ($_POST['body_multi'] ?? ''));
|
|
|
|
if ($id <= 0) {
|
|
echo json_encode(['success' => false, 'error' => 'Impostazione non valida']);
|
|
exit;
|
|
}
|
|
|
|
if (!preg_match('/^([01][0-9]|2[0-3]):[0-5][0-9]$/', $sendhour)) {
|
|
echo json_encode(['success' => false, 'error' => "L'ora deve essere nel formato HH:MM (es. 08:00)"]);
|
|
exit;
|
|
}
|
|
|
|
if ($daysbefore < 0 || $daysbefore > 60) {
|
|
echo json_encode(['success' => false, 'error' => 'I giorni di anticipo devono essere tra 0 e 60']);
|
|
exit;
|
|
}
|
|
|
|
if ($title === '' || $body === '') {
|
|
echo json_encode(['success' => false, 'error' => 'Titolo e testo non possono essere vuoti']);
|
|
exit;
|
|
}
|
|
|
|
if (mb_strlen($title) > 150 || mb_strlen($titleMulti) > 150) {
|
|
echo json_encode(['success' => false, 'error' => 'Il titolo non puo superare i 150 caratteri']);
|
|
exit;
|
|
}
|
|
|
|
// Il tipo non si cambia da qui: le righe sono quelle previste dai cron.
|
|
$stmt = $pdo->prepare("
|
|
SELECT notificationtype FROM notificationsetting WHERE idnotificationsetting = :id LIMIT 1
|
|
");
|
|
$stmt->execute([':id' => $id]);
|
|
$tipo = $stmt->fetchColumn();
|
|
|
|
if (!$tipo) {
|
|
echo json_encode(['success' => false, 'error' => 'Impostazione inesistente']);
|
|
exit;
|
|
}
|
|
|
|
// Per le lezioni l'anticipo non ha senso: e' sempre l'invio del mattino stesso.
|
|
if ($tipo === 'lesson') {
|
|
$daysbefore = 0;
|
|
}
|
|
|
|
// La coppia tipo + giorni è unica: due righe uguali romperebbero i cron.
|
|
$stmt = $pdo->prepare("
|
|
SELECT 1 FROM notificationsetting
|
|
WHERE notificationtype = :tipo AND daysbefore = :giorni AND idnotificationsetting != :id
|
|
LIMIT 1
|
|
");
|
|
$stmt->execute([':tipo' => $tipo, ':giorni' => $daysbefore, ':id' => $id]);
|
|
|
|
if ($stmt->fetchColumn()) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => "Esiste gia' una notifica di questo tipo con $daysbefore giorni di anticipo",
|
|
]);
|
|
exit;
|
|
}
|
|
|
|
$stmt = $pdo->prepare("
|
|
UPDATE notificationsetting
|
|
SET active = :active,
|
|
daysbefore = :giorni,
|
|
sendhour = :ora,
|
|
title = :titolo,
|
|
body = :testo"
|
|
. ($aggiornaMulti ? ",
|
|
title_multi = :titoloMulti,
|
|
body_multi = :testoMulti" : '') . "
|
|
WHERE idnotificationsetting = :id
|
|
");
|
|
|
|
$parametri = [
|
|
':active' => $active,
|
|
':giorni' => $daysbefore,
|
|
':ora' => $sendhour,
|
|
':titolo' => $title,
|
|
':testo' => $body,
|
|
':id' => $id,
|
|
];
|
|
|
|
if ($aggiornaMulti) {
|
|
$parametri[':titoloMulti'] = $titleMulti !== '' ? $titleMulti : null;
|
|
$parametri[':testoMulti'] = $bodyMulti !== '' ? $bodyMulti : null;
|
|
}
|
|
|
|
$stmt->execute($parametri);
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'daysbefore' => $daysbefore,
|
|
]);
|