38676-vm/guilde.php
2026-03-09 10:50:52 +00:00

322 lines
14 KiB
PHP

<?php
require_once 'db/config.php';
require_once 'includes/status_helper.php';
session_start();
$db = db();
if (!isset($_SESSION['user_id'])) {
header('Location: auth.php');
exit;
}
$user_id = $_SESSION['user_id'];
// Récupérer les informations de l'utilisateur
$stmt = $db->prepare("SELECT u.*, g.name as guild_name, g.owner_id as guild_owner_id
FROM users u
LEFT JOIN guilds g ON u.guild_id = g.id
WHERE u.id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch();
$guild_id = $user['guild_id'];
$is_owner = ($guild_id && $user['id'] == $user['guild_owner_id']);
// Gérer la création de guilde
$error = '';
$success = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['create_guild']) && !$guild_id) {
$name = trim($_POST['guild_name']);
$tag = trim($_POST['guild_tag']);
if (empty($name) || empty($tag)) {
$error = "Le nom et le tag sont obligatoires.";
} else {
try {
$db->beginTransaction();
$stmt = $db->prepare("INSERT INTO guilds (name, tag, owner_id) VALUES (?, ?, ?)");
$stmt->execute([$name, $tag, $user_id]);
$new_guild_id = $db->lastInsertId();
$stmt = $db->prepare("UPDATE users SET guild_id = ? WHERE id = ?");
$stmt->execute([$new_guild_id, $user_id]);
$stmt = $db->prepare("INSERT INTO guild_members (guild_id, user_id, role) VALUES (?, ?, 'owner')");
$stmt->execute([$new_guild_id, $user_id]);
$db->commit();
header('Location: guilde.php');
exit;
} catch (Exception $e) {
$db->rollBack();
$error = "Erreur lors de la création : " . $e->getMessage();
}
}
}
// Récupérer les membres si en guilde
$members = [];
if ($guild_id) {
$stmt = $db->prepare("SELECT u.id, u.username, u.display_name, m.role, m.joined_at, l.name as level_name
FROM guild_members m
JOIN users u ON m.user_id = u.id
LEFT JOIN levels l ON u.level_id = l.id
WHERE m.guild_id = ?
ORDER BY CASE WHEN m.role = 'owner' THEN 1 WHEN m.role = 'officer' THEN 2 ELSE 3 END ASC, joined_at ASC");
$stmt->execute([$guild_id]);
$members = $stmt->fetchAll();
}
// Ressources pour le header (copié de index.php)
$resources = [];
$stmt = $db->prepare("
SELECT gr.*, COALESCE(ur.amount, 0) as amount
FROM game_resources gr
LEFT JOIN user_resources ur ON gr.id = ur.resource_id AND ur.user_id = ?
WHERE gr.show_in_header = 1
ORDER BY CASE
WHEN gr.name LIKE 'Crédit%' THEN 1
WHEN gr.name LIKE 'Matériau%' THEN 2
WHEN gr.name LIKE 'Energie%' THEN 3
WHEN gr.name LIKE 'Donnée%' THEN 4
ELSE 5
END ASC, gr.name ASC
");
$stmt->execute([$user_id]);
$header_resources = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($header_resources as $hr) {
$resources[$hr["name"]] = [
"val" => (string)$hr["amount"],
"prod" => "",
"icon" => $hr["icon"] ?: "fa-gem",
"image" => $hr["image_url"]
];
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Nexus - Guilde</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
<link href="assets/css/custom.css?v=<?php echo time(); ?>" rel="stylesheet">
<style>
body { background: #000; color: #fff; font-family: 'Segoe UI', sans-serif; margin: 0; }
#top-bar { background: #0f172a; border-bottom: 1px solid #1e293b; padding: 10px 40px; display: flex; flex-direction: column; gap: 10px; }
.user-auth-bar { display: flex; justify-content: flex-end; align-items: center; gap: 20px; font-size: 11px; color: #8c92a3; }
.user-auth-bar a { color: #88c0d0; text-decoration: none; font-weight: bold; }
.resource-container { display: flex; justify-content: center; align-items: center; gap: 30px; }
.resource-box { display: flex; align-items: center; gap: 12px; background: rgba(30, 41, 59, 0.4); padding: 6px 15px; border-radius: 8px; border: 1px solid rgba(136, 192, 208, 0.1); min-width: 140px; }
.resource-icon img { width: 24px; height: 24px; object-fit: contain; }
.resource-value { font-size: 14px; font-weight: bold; color: #f8fafc; }
#main-content { padding: 40px; max-width: 1200px; margin: 0 auto; }
.card { background: rgba(15, 23, 42, 0.8); border: 1px solid #1e293b; border-radius: 12px; padding: 30px; margin-bottom: 30px; backdrop-filter: blur(10px); }
.card-title { font-size: 24px; color: #88c0d0; margin-bottom: 20px; display: flex; align-items: center; gap: 15px; }
.member-table { width: 100%; border-collapse: collapse; margin-top: 20px; }
.member-table th { text-align: left; padding: 15px; border-bottom: 2px solid #1e293b; color: #8c92a3; font-size: 12px; text-transform: uppercase; }
.member-table td { padding: 15px; border-bottom: 1px solid #1e293b; font-size: 14px; }
.role-badge { padding: 4px 10px; border-radius: 20px; font-size: 11px; font-weight: bold; text-transform: uppercase; }
.role-owner { background: rgba(235, 203, 139, 0.2); color: #ebcb8b; border: 1px solid #ebcb8b; }
.role-officer { background: rgba(136, 192, 208, 0.2); color: #88c0d0; border: 1px solid #88c0d0; }
.role-member { background: rgba(148, 163, 184, 0.2); color: #94a3b8; border: 1px solid #94a3b8; }
.form-group { margin-bottom: 20px; }
.form-group label { display: block; margin-bottom: 8px; font-size: 14px; color: #94a3b8; }
.form-control { width: 100%; background: #0f172a; border: 1px solid #334155; border-radius: 6px; padding: 12px; color: #fff; box-sizing: border-box; }
.btn-primary { background: #88c0d0; color: #0f172a; border: none; padding: 12px 24px; border-radius: 6px; font-weight: bold; cursor: pointer; transition: all 0.2s; }
.btn-primary:hover { background: #a3be8c; transform: translateY(-2px); }
.member-link { color: #88c0d0; text-decoration: none; cursor: pointer; font-weight: bold; }
.member-link:hover { color: #fff; text-decoration: underline; }
/* MODAL STYLES (Copiés de index.php) */
.modal-overlay {
display: none;
position: fixed;
top: 0; left: 0;
width: 100%; height: 100%;
background: rgba(0, 0, 0, 0.85);
backdrop-filter: blur(5px);
z-index: 2000;
align-items: center;
justify-content: center;
}
.modal-container {
background: #0f172a;
border: 1px solid #1e293b;
border-radius: 12px;
width: 600px;
max-height: 90vh;
overflow-y: auto;
position: relative;
box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
}
.modal-header {
padding: 20px;
border-bottom: 1px solid #1e293b;
display: flex;
justify-content: space-between;
align-items: center;
background: rgba(30, 41, 59, 0.5);
}
.modal-header h2 { margin: 0; font-size: 20px; color: #88c0d0; }
.modal-close {
background: none; border: none; color: #8c92a3; font-size: 24px; cursor: pointer;
transition: color 0.2s;
}
.modal-close:hover { color: #fff; }
.modal-body { padding: 25px; }
.loader {
border: 4px solid rgba(136, 192, 208, 0.1);
border-left-color: #88c0d0;
border-radius: 50%;
width: 40px;
height: 40px;
animation: spin 1s linear infinite;
margin: 40px auto;
}
@keyframes spin { to { transform: rotate(360deg); } }
</style>
</head>
<body>
<header id="top-bar">
<div class="user-auth-bar">
<span>Bienvenue, <span style="color: #ebcb8b;">@<?php echo htmlspecialchars($user['display_name'] ?: $user['username']); ?></span></span>
<a href="index.php"><i class="fa-solid fa-earth-europe"></i> Retour au Nexus</a>
<a href="auth.php?logout=1" style="color: #bf616a;"><i class="fa-solid fa-right-from-bracket"></i> Déconnexion</a>
</div>
<div class="resource-container">
<?php foreach($resources as $name => $res): ?>
<div class="resource-box">
<div class="resource-icon">
<?php if (!empty($res["image"])): ?>
<img src="<?php echo htmlspecialchars($res["image"]); ?>?v=<?php echo time(); ?>">
<?php else: ?>
<i class="fa-solid <?php echo htmlspecialchars($res["icon"]); ?>" style="color: #88c0d0;"></i>
<?php endif; ?>
</div>
<div class="resource-info">
<div style="font-size: 9px; text-transform: uppercase; color: #64748b;"><?php echo htmlspecialchars($name); ?></div>
<span class="resource-value"><?php echo htmlspecialchars($res["val"]); ?></span>
</div>
</div>
<?php endforeach; ?>
</div>
</header>
<main id="main-content">
<?php if ($guild_id): ?>
<div class="card">
<div class="card-title">
<i class="fa-solid fa-building-shield"></i>
<span><?php echo htmlspecialchars($user['guild_name']); ?> [<?php echo htmlspecialchars($members[0]['role'] == 'owner' ? $members[0]['role'] : 'Membre'); ?>]</span>
</div>
<table class="member-table">
<thead>
<tr>
<th>Niveau</th>
<th>Membre</th>
<th>Rang</th>
<th>Date d'arrivée</th>
</tr>
</thead>
<tbody>
<?php foreach ($members as $m): ?>
<tr>
<td style="color: #88c0d0; font-weight: bold;">
<?php echo htmlspecialchars($m['level_name'] ?: 'Niveau 1'); ?>
</td>
<td>
<span class="member-link" onclick="openProfileModal(<?php echo $m['id']; ?>)">
<?php echo htmlspecialchars($m['display_name'] ?: $m['username']); ?>
</span>
</td>
<td>
<span class="role-badge role-<?php echo $m['role']; ?>">
<?php
switch($m['role']) {
case 'owner': echo 'Superviseur'; break;
case 'officer': echo 'Officier'; break;
default: echo 'Explorateur';
}
?>
</span>
</td>
<td style="color: #64748b; font-size: 12px;">
<?php echo date('d/m/Y', strtotime($m['joined_at'])); ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php else: ?>
<div class="card">
<div class="card-title">
<i class="fa-solid fa-plus-circle"></i>
<span>Fonder une nouvelle guilde</span>
</div>
<?php if ($error): ?><div style="color: #bf616a; margin-bottom: 20px; padding: 10px; background: rgba(191,97,106,0.1); border-radius: 6px;"><?php echo $error; ?></div><?php endif; ?>
<form method="POST">
<div class="form-group">
<label>Nom de la guilde</label>
<input type="text" name="guild_name" class="form-control" placeholder="ex: Les Pionniers du Vide" required>
</div>
<div class="form-group">
<label>Tag (3-4 caractères)</label>
<input type="text" name="guild_tag" class="form-control" placeholder="ex: LPV" maxlength="4" required>
</div>
<button type="submit" name="create_guild" class="btn-primary">Fonder la guilde</button>
</form>
</div>
<?php endif; ?>
</main>
<!-- MODAL DE PROFIL (Même structure que index.php) -->
<div id="profileModal" class="modal-overlay" onclick="if(event.target === this) closeProfileModal()">
<div class="modal-container">
<div class="modal-header">
<h2>Profil Public</h2>
<button class="modal-close" onclick="closeProfileModal()">&times;</button>
</div>
<div id="profileModalContent" class="modal-body">
<!-- Le contenu sera chargé ici via AJAX -->
</div>
</div>
</div>
<script>
function openProfileModal(userId) {
const modal = document.getElementById('profileModal');
const content = document.getElementById('profileModalContent');
content.innerHTML = '<div class="loader"></div>';
modal.style.display = 'flex';
fetch(`profile.php?id=${userId}`, {
headers: { 'X-Requested-With': 'XMLHttpRequest' }
})
.then(response => response.text())
.then(html => {
content.innerHTML = html;
})
.catch(error => {
content.innerHTML = '<div style="color: #bf616a; text-align: center;">Erreur lors du chargement du profil.</div>';
console.error('Error:', error);
});
}
function closeProfileModal() {
document.getElementById('profileModal').style.display = 'none';
}
</script>
</body>
</html>