diff --git a/public/userarea/add_user.php b/public/userarea/add_user.php new file mode 100644 index 0000000..d43b7e0 --- /dev/null +++ b/public/userarea/add_user.php @@ -0,0 +1,369 @@ +getConnection(); + +if (!isset($iduserlogin)) { + die("Errore: utente non loggato."); +} + +// 1. Recupera SOLO la scuola corrente del proprietario loggato +$stmt = $pdo->prepare(" + SELECT id, name, email AS school_email + FROM schools + WHERE owner_id = ? AND status = 'active' +"); +$stmt->execute([$iduserlogin]); +$school = $stmt->fetch(PDO::FETCH_ASSOC); + +if (!$school) { + die("Nessuna scuola trovata per questo proprietario."); +} + +$school_id = $school['id']; +$school_name = $school['name']; +$school_email = $school['school_email']; + +// 2. Messaggi di feedback +$success_message = $_GET['success'] ?? null; +$error_message = $_GET['error'] ?? null; + +// 3. GESTIONE POST - Aggiungi/Collega Utente +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'add_or_link_user') { + + $email = trim($_POST['email'] ?? ''); + $first_name = trim($_POST['first_name'] ?? ''); + $last_name = trim($_POST['last_name'] ?? ''); + $phone = trim($_POST['phone'] ?? ''); + + // Validazioni + if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) { + $error_message = "Email non valida."; + } elseif (empty($first_name) || empty($last_name)) { + $error_message = "Nome e cognome obbligatori."; + } else { + + // CASE 1: Verifica se utente ESISTE già (case-insensitive) + $stmt = $pdo->prepare(" + SELECT id, first_name, last_name, email_verified_at, status + FROM auth_users + WHERE LOWER(email) = LOWER(?) + "); + $stmt->execute([$email]); + $existingUser = $stmt->fetch(PDO::FETCH_ASSOC); + + if ($existingUser) { + // ✅ UTENTE ESISTE → SOLO COLLEGA alla scuola + $user_id = $existingUser['id']; + + // Verifica se è già collegato a questa scuola + $stmt = $pdo->prepare(" + SELECT id FROM user_schools + WHERE user_id = ? AND school_id = ? + "); + $stmt->execute([$user_id, $school_id]); + if ($stmt->fetch()) { + $error_message = "Questo utente è già associato alla tua scuola."; + } else { + // COLLEGAMENTO + $stmt = $pdo->prepare(" + INSERT INTO user_schools (user_id, school_id, status, created_at, updated_at) + VALUES (?, ?, 'active', NOW(), NOW()) + "); + $stmt->execute([$user_id, $school_id]); + + // 📧 EMAIL: "Sei stato agganciato alla scuola XXXX" + $subject = "Associato a {$school_name} - Yogibook"; + $body = " +

Ciao {$first_name} {$last_name},

+

Sei stato associato alla scuola {$school_name} sulla piattaforma Yogibook.

+

Ora puoi accedere con le tue credenziali e vedere le lezioni di questa scuola.

+

Login: app.yogiboook.com/login

+
+

Se non riconosci questa scuola, contatta: {$school_email}

+

Messaggio automatico – non rispondere.

+ "; + + $emailResult = sendEmail($email, $subject, $body); + + if ($emailResult['success']) { + $success_message = "Utente {$first_name} {$last_name} collegato con successo a {$school_name}! 📧 Email inviata."; + } else { + $error_message = "Utente collegato, ma errore email: " . $emailResult['message']; + } + } + } else { + // ❌ UTENTE NON ESISTE → CREA + link reset password + $tempPassword = bin2hex(random_bytes(16)); // Password random (inutile) + $hashedPassword = password_hash($tempPassword, PASSWORD_DEFAULT); + + $stmt = $pdo->prepare(" + INSERT INTO auth_users ( + email, first_name, last_name, phone, + password, role_id, status, + created_at, updated_at, email_verified_at + ) VALUES (?, ?, ?, ?, ?, 2, 'active', NOW(), NOW(), NULL) + "); + $success = $stmt->execute([ + $email, + $first_name, + $last_name, + empty($phone) ? null : $phone, + $hashedPassword + ]); + + if ($success) { + $user_id = $pdo->lastInsertId(); + + // COLLEGA alla scuola + $stmt = $pdo->prepare(" + INSERT INTO user_schools (user_id, school_id, status, created_at, updated_at) + VALUES (?, ?, 'active', NOW(), NOW()) + "); + $stmt->execute([$user_id, $school_id]); + + // 📧 EMAIL: Link diretto a reset password + $resetLink = "https://app.yogiboook.com/public/password/reset?email=" . urlencode($email); + + $subject = "Benvenuto in {$school_name} - Imposta Password | Yogibook"; + $body = " +

Ciao {$first_name}, benvenuto in {$school_name}!

+

La scuola ti ha invitato sulla piattaforma Yogibook.

+

PRIMO PASSO OBBLIGATORIO: imposta la tua password:

+

+ + IMPOSTA LA TUA PASSWORD + +

+

Non funziona il link? Copia-incolla:
{$resetLink}

+
+

Login: app.yogiboook.com/login

+

Contatta la scuola: {$school_email}

+

Messaggio automatico – non rispondere.

+ "; + + $emailResult = sendEmail($email, $subject, $body); + + if ($emailResult['success']) { + $success_message = "✅ Nuovo utente {$first_name} {$last_name} creato e collegato a {$school_name}! 📧 Link reset password inviato."; + } else { + $error_message = "Utente creato/collegato, ma errore email: " . $emailResult['message']; + } + } else { + $error_message = "Errore creazione utente. Riprova."; + } + } + } +} + +// 4. Lista UTENTI ASSOCIATI SOLO A QUESTA SCUOLA (punto 1 ✅) +$stmt = $pdo->prepare(" + SELECT + au.id, au.first_name, au.last_name, au.email, au.phone, + au.email_verified_at, au.status AS user_status, + us.status AS school_status, us.created_at + FROM user_schools us + JOIN auth_users au ON us.user_id = au.id + WHERE us.school_id = ? + ORDER BY au.last_name, au.first_name +"); +$stmt->execute([$school_id]); +$schoolUsers = $stmt->fetchAll(PDO::FETCH_ASSOC); + +?> + + + + + + + + Gestione Utenti - <?= htmlspecialchars($school_name) ?> + + + + + +
+ + + +
+
+
+
+
+

Gestione Utenti

+ +
+
+
+ + + + + + + + + + +
+ +
+
+
+
👤 Aggiungi / Collega Utente
+
+
+
+ + +
+ + +
+ +
+ + +
+ +
+ + +
Se esiste già → lo collega. Altrimenti → lo crea.
+
+ +
+ + +
+ + +
+
+
+
+ + +
+
+
+

+

Utenti associati a

+ 0): ?> + + + $u['school_status'] === 'active')) ?> attivi + + +
+
+
+
+ + +
+
+
👥 Utenti di ()
+
+
+ +
+ +

Nessun utente associato ancora.

+

Usa il form qui sopra per aggiungerne uno!

+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
NomeEmailTelefonoAssociato ilStato ScuolaEmail Verificata
+ + + + —') ?> + + + + + + + + + Sì' + : 'Non ancora' ?> +
+
+ +
+
+
+
+ + +
+ + + + + + \ No newline at end of file diff --git a/public/userarea/include/navbar.php b/public/userarea/include/navbar.php index 6f08a40..50a6dfb 100644 --- a/public/userarea/include/navbar.php +++ b/public/userarea/include/navbar.php @@ -72,86 +72,97 @@ if (!empty($_SESSION['school_id'])) { - - -
  • - -
    - -
    -
  • -
  • - -
    - -
    -
  • -
  • - -
    - -
    -
  • - + hasRole('User')) || (Auth::user()->hasRole('Admin'))) : ?> +
  • - + +
    + +
    +
  • +
  • +
    - + +
    +
  • +
  • + +
    + +
    +
  • + +
  • + +
    + +
    +
  • + +
  • + +
    + +
    +
  • +
  • + +
    +
  • -
  • - -
    - -
    -
  • -
  • - -
    - -
    -
  • - -
  • - -
    - -
    -
  • -
  • - -
    - -
    -
  • -
  • - -
    - -
    -
  • + hasRole('school_owner')) || (Auth::user()->hasRole('Admin'))) : ?> + +
  • + +
    + +
    +
  • +
  • + +
    + +
    +
  • +
  • + +
    + +
    +
  • + - -
  • - -
    - -
    -
  • -
  • - -
    - -
    -
  • -
  • - -
    - -
    -
  • + hasRole('Admin'))) : ?> + +
  • + +
    + +
    +
  • +
  • + +
    + +
    +
  • +
  • + +
    + +
    +
  • + diff --git a/public/userarea/school_dashboard.php b/public/userarea/school_dashboard.php index c0d0c31..4051e73 100644 --- a/public/userarea/school_dashboard.php +++ b/public/userarea/school_dashboard.php @@ -859,9 +859,9 @@ $daily_sessions = $stmt->fetchAll(); Prodotti - + - Abbonamenti + Aggiungi Utente