98 lines
3.5 KiB
PHP
98 lines
3.5 KiB
PHP
<?php
|
|
require_once('include/headscript.php');
|
|
|
|
/**
|
|
* Inserisce un partecipante in una classe (bookingclass).
|
|
* Se l'utente non esiste ancora (userid mancante), lo crea in auth_users
|
|
* riusando un eventuale account con la stessa email. Tutto in transazione.
|
|
*/
|
|
$pdo = DBHandlerSelect::getInstance()->getConnection();
|
|
|
|
// --- Input ---
|
|
$name = trim($_POST['name'] ?? '');
|
|
$surname = trim($_POST['surname'] ?? '');
|
|
$email = trim($_POST['email'] ?? '');
|
|
$userid = isset($_POST['userid']) ? (int) $_POST['userid'] : 0;
|
|
$idserviceschedule = isset($_POST['idserviceschedule']) ? (int) $_POST['idserviceschedule'] : 0;
|
|
$status = trim($_POST['status'] ?? 'booked');
|
|
$idorder = isset($_POST['idorder']) ? (int) $_POST['idorder'] : 0;
|
|
$idservice = isset($_POST['idservice']) ? (int) $_POST['idservice'] : 0;
|
|
$bookingstart = trim($_POST['bookingstart'] ?? '');
|
|
|
|
// --- Validazione minima ---
|
|
if ($idserviceschedule <= 0 || $idservice <= 0) {
|
|
header('Location: adminpanel.php?error=missingdata');
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$pdo->beginTransaction();
|
|
|
|
// Se non abbiamo un userid valido, dobbiamo risolvere/creare l'utente
|
|
if ($userid <= 0) {
|
|
|
|
// 1) Se è stata fornita un'email, controlliamo se l'utente esiste già
|
|
if ($email !== '') {
|
|
$check = $pdo->prepare("SELECT id FROM auth_users WHERE email = :email LIMIT 1");
|
|
$check->execute([':email' => $email]);
|
|
$existing = $check->fetchColumn();
|
|
if ($existing) {
|
|
$userid = (int) $existing;
|
|
}
|
|
}
|
|
|
|
// 2) Se ancora non c'è, creiamo un nuovo utente
|
|
if ($userid <= 0) {
|
|
// Password casuale hashata (questi account non usano una password condivisa)
|
|
$randomPassword = bin2hex(random_bytes(8));
|
|
$hashed = password_hash($randomPassword, PASSWORD_BCRYPT);
|
|
|
|
$insUser = $pdo->prepare(
|
|
"INSERT INTO auth_users
|
|
(first_name, last_name, email, password, role_id, status, created_at, avatar)
|
|
VALUES
|
|
(:first, :last, :email, :password, :role, :status, :created, :avatar)"
|
|
);
|
|
$insUser->execute([
|
|
':first' => $name,
|
|
':last' => $surname,
|
|
':email' => $email !== '' ? $email : null,
|
|
':password' => $hashed,
|
|
':role' => 2,
|
|
':status' => 'Active',
|
|
':created' => date('Y-m-d H:i:s'),
|
|
':avatar' => 'mediationb.png',
|
|
]);
|
|
$userid = (int) $pdo->lastInsertId();
|
|
}
|
|
}
|
|
|
|
// 3) Inserimento della prenotazione
|
|
$insBooking = $pdo->prepare(
|
|
"INSERT INTO bookingclass
|
|
(iduser, idserviceschedule, status, idorder, idservice, bookingstart)
|
|
VALUES
|
|
(:iduser, :idschedule, :status, :idorder, :idservice, :bookingstart)"
|
|
);
|
|
$insBooking->execute([
|
|
':iduser' => $userid,
|
|
':idschedule' => $idserviceschedule,
|
|
':status' => $status,
|
|
':idorder' => $idorder,
|
|
':idservice' => $idservice,
|
|
':bookingstart' => $bookingstart !== '' ? $bookingstart : null,
|
|
]);
|
|
|
|
$pdo->commit();
|
|
|
|
header('Location: adminpanel.php?success=added');
|
|
exit;
|
|
} catch (Throwable $ex) {
|
|
if ($pdo->inTransaction()) {
|
|
$pdo->rollBack();
|
|
}
|
|
error_log('inserisci_record error: ' . $ex->getMessage());
|
|
header('Location: adminpanel.php?error=insert');
|
|
exit;
|
|
}
|