Files
pilates-platform/admin/users.php
2025-12-09 17:50:01 +01:00

214 lines
9.7 KiB
PHP

<?php
/**
* Gestione Utenti
*
* Visualizza tutti gli utenti registrati con opzioni di gestione
*/
require_once '../includes/config.php';
require_once '../includes/functions.php';
session_start();
check_session_timeout();
require_admin();
// Gestione blocco/sblocco utente
if (isset($_GET['toggle_active']) && is_numeric($_GET['toggle_active'])) {
$user_id = (int)$_GET['toggle_active'];
$pdo = get_db_connection();
$stmt = $pdo->prepare("UPDATE users SET is_active = NOT is_active WHERE id = ?");
if ($stmt->execute([$user_id])) {
set_flash_message('success', 'Stato utente aggiornato');
} else {
set_flash_message('error', 'Errore durante l\'aggiornamento');
}
header('Location: users.php');
exit;
}
// Gestione re-invio email verifica
if (isset($_GET['resend_verification']) && is_numeric($_GET['resend_verification'])) {
$user_id = (int)$_GET['resend_verification'];
try {
$pdo = get_db_connection();
$stmt = $pdo->prepare("SELECT id, email, first_name, email_verified FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch();
if ($user && !$user['email_verified']) {
// Genera nuovo token
$email_token = bin2hex(random_bytes(32));
$token_expires = date('Y-m-d H:i:s', strtotime('+24 hours'));
$stmt = $pdo->prepare("UPDATE users SET email_token = ?, email_token_expires = ? WHERE id = ?");
$stmt->execute([$email_token, $token_expires, $user_id]);
// Invia email
$verify_url = SITE_URL . "/verify_email.php?token=" . $email_token;
$subject = "Conferma il tuo account su " . SITE_NAME;
$body = "<p>Ciao <strong>" . htmlspecialchars($user['first_name']) . "</strong>,</p>
<p>Un amministratore ha inviato nuovamente il link di verifica per il tuo account.</p>
<p><a href='" . $verify_url . "' style='display: inline-block; padding: 12px 30px; background: #667eea; color: white; text-decoration: none; border-radius: 5px;'>Conferma Email</a></p>
<p>Questo link è valido per 24 ore.</p>";
send_email($user['email'], $subject, $body);
set_flash_message('success', 'Email di verifica inviata a ' . htmlspecialchars($user['email']));
} else {
set_flash_message('error', 'Utente già verificato o non trovato');
}
} catch (Exception $e) {
set_flash_message('error', 'Errore durante l\'invio dell\'email');
}
header('Location: users.php');
exit;
}
// Ottieni tutti gli utenti non admin
$pdo = get_db_connection();
$stmt = $pdo->query("
SELECT u.*,
COUNT(DISTINCT p.id) as purchase_count,
SUM(CASE WHEN p.status = 'completed' THEN p.amount ELSE 0 END) as total_spent
FROM users u
LEFT JOIN purchases p ON u.id = p.user_id
WHERE u.is_admin = 0 AND u.deleted_at IS NULL
GROUP BY u.id
ORDER BY u.created_at DESC
");
$users = $stmt->fetchAll();
?>
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gestione Utenti - Admin</title>
<link rel="stylesheet" href="../assets/css/style.css">
</head>
<body>
<header class="header">
<div class="container">
<div class="header-content">
<?php if (file_exists('../uploads/images/logo.png')): ?>
<div class="logo">
<img src="../uploads/images/logo.png" alt="Pilates Studio" class="logo-image">
<span style="margin-left: 10px; color: var(--primary-color); font-weight: 600;">Admin</span>
</div>
<?php else: ?>
<h1 class="logo">Pilates Studio - Admin</h1>
<?php endif; ?>
<nav class="nav">
<a href="../index.php" class="btn btn-outline">Vedi Sito</a>
<a href="../includes/logout.php" class="btn btn-secondary">Logout</a>
</nav>
</div>
</div>
</header>
<div class="container">
<div class="dashboard">
<!-- Sidebar -->
<aside class="sidebar">
<ul class="sidebar-menu">
<li><a href="dashboard.php">📊 Dashboard</a></li>
<li><a href="lessons.php">🎥 Gestione Lezioni</a></li>
<li><a href="users.php" class="active">👥 Gestione Utenti</a></li>
<li><a href="purchases.php">💰 Acquisti</a></li>
<li><a href="../convert_videos.php">🔄 Converti Video</a></li>
<li><a href="profile.php">👤 Profilo</a></li>
</ul>
</aside>
<!-- Main Content -->
<main class="main-content">
<h2 class="section-title" style="text-align: left;">Gestione Utenti</h2>
<?php echo display_flash_message(); ?>
<div class="card">
<p class="text-muted mb-2">Totale utenti registrati: <strong><?php echo count($users); ?></strong></p>
<?php if (!empty($users)): ?>
<div class="table-container">
<table class="table">
<thead>
<tr>
<th>Nome</th>
<th>Email</th>
<th>Stato Email</th>
<th>Registrato il</th>
<th>Ultimo Accesso</th>
<th>Acquisti</th>
<th>Speso</th>
<th>Status</th>
<th>Azioni</th>
</tr>
</thead>
<tbody>
<?php foreach ($users as $user): ?>
<tr>
<td>
<strong><?php echo htmlspecialchars($user['first_name'] . ' ' . $user['last_name']); ?></strong>
</td>
<td>
<?php echo htmlspecialchars($user['email']); ?>
</td>
<td>
<?php if ($user['email_verified']): ?>
<span class="text-success">✓ Verificata</span>
<?php else: ?>
<span class="text-warning">⚠️ Non verificata</span>
<?php endif; ?>
</td>
<td><?php echo format_date($user['created_at']); ?></td>
<td>
<?php
echo $user['last_login'] ? format_datetime($user['last_login']) : 'Mai';
?>
</td>
<td><?php echo $user['purchase_count']; ?></td>
<td><strong><?php echo format_price($user['total_spent'] ?? 0); ?></strong></td>
<td>
<?php if ($user['is_active']): ?>
<span class="text-success">✓ Attivo</span>
<?php else: ?>
<span class="text-danger">✗ Bloccato</span>
<?php endif; ?>
</td>
<td>
<a href="users.php?toggle_active=<?php echo $user['id']; ?>"
class="btn btn-small <?php echo $user['is_active'] ? 'btn-danger' : 'btn-success'; ?>">
<?php echo $user['is_active'] ? 'Blocca' : 'Sblocca'; ?>
</a>
<?php if (!$user['email_verified']): ?>
<a href="users.php?resend_verification=<?php echo $user['id']; ?>"
class="btn btn-small btn-secondary"
style="margin-left: 5px;"
onclick="return confirm('Inviare nuovamente l\'email di verifica?');">
📧 Re-Invia
</a>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php else: ?>
<p class="text-muted text-center">Nessun utente registrato ancora.</p>
<?php endif; ?>
</div>
</main>
</div>
</div>
<script src="../assets/js/main.js"></script>
</body>
</html>