prepare("SELECT role FROM users WHERE id = ?");
$user_stmt->execute([$user_id]);
$current_user = $user_stmt->fetch();
if (!$current_user || $current_user['role'] !== 'admin') {
die("Accès refusé. Cette console est réservée aux Administrateurs.");
}
$tab = isset($_GET['tab']) ? $_GET['tab'] : 'users';
// --- HANDLERS ---
// Status Profiles
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["action"]) && $_POST["action"] === "upsert_status_profile") {
$id = (int)$_POST["id"]; $name = $_POST["name"]; $slug = $_POST["slug"]; $enabled = isset($_POST["enabled"]) ? 1 : 0; $priority = (int)$_POST["priority"]; $scope_object_type = $_POST["scope_object_type"] === "" ? null : $_POST["scope_object_type"];
$rules = []; if (isset($_POST['rule_status_id']) && is_array($_POST['rule_status_id'])) { foreach ($_POST['rule_status_id'] as $idx => $sid) { $rules[] = ['status_id' => (int)$sid, 'condition_type' => $_POST['rule_condition_type'][$idx], 'min_value' => $_POST['rule_min_value'][$idx] !== "" ? (float)$_POST['rule_min_value'][$idx] : null, 'max_value' => $_POST['rule_max_value'][$idx] !== "" ? (float)$_POST['rule_max_value'][$idx] : null]; } }
$config = json_encode(['rules' => $rules]);
if ($id > 0) { $stmt = $db->prepare("UPDATE celestial_object_status_profiles SET name = ?, slug = ?, enabled = ?, priority = ?, scope_object_type = ?, config = ? WHERE id = ?"); $stmt->execute([$name, $slug, $enabled, $priority, $scope_object_type, $config, $id]); }
else { $stmt = $db->prepare("INSERT INTO celestial_object_status_profiles (name, slug, enabled, priority, scope_object_type, config) VALUES (?, ?, ?, ?, ?, ?)"); $stmt->execute([$name, $slug, $enabled, $priority, $scope_object_type, $config]); }
header("Location: admin.php?tab=status_profiles&success=1"); exit;
}
if (isset($_GET["delete_status_profile"])) { $db->prepare("DELETE FROM celestial_object_status_profiles WHERE id = ?")->execute([(int)$_GET["delete_status_profile"]]); header("Location: admin.php?tab=status_profiles&success=1"); exit; }
// User Roles
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'update_user_role') {
$target_user_id = (int)$_POST['target_user_id']; $new_role = $_POST['new_role'];
if (in_array($new_role, ['user', 'gm', 'admin'])) { $stmt = $db->prepare("UPDATE users SET role = ? WHERE id = ?"); $stmt->execute([$new_role, $target_user_id]); }
header("Location: admin.php?tab=users&success=1"); exit;
}
// Celestial Object Types
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'upsert_object_type') {
$id = (int)$_POST['id']; $name = $_POST['name']; $slug = $_POST['slug']; $icon = $_POST['icon']; $description = $_POST['description']; $status_profile_id = !empty($_POST['status_profile_id']) ? (int)$_POST['status_profile_id'] : null; $modifier_ids = isset($_POST['modifiers']) ? $_POST['modifiers'] : [];
$image_url = null; if ($id > 0) { $stmt_img = $db->prepare("SELECT image_url FROM celestial_object_types WHERE id = ?"); $stmt_img->execute([$id]); $image_url = $stmt_img->fetchColumn(); }
if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) { $ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION); $filename = $slug . "_" . time() . "." . $ext; $target = "assets/images/celestial/" . $filename; if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) $image_url = $target; }
$orbital_enabled = isset($_POST["orbital_control_enabled"]) ? 1 : 0; $terrestrial_enabled = isset($_POST["terrestrial_control_enabled"]) ? 1 : 0;
if ($id > 0) { $stmt = $db->prepare("UPDATE celestial_object_types SET name = ?, slug = ?, icon = ?, description = ?, image_url = ?, orbital_control_enabled = ?, terrestrial_control_enabled = ?, status_profile_id = ? WHERE id = ?"); $stmt->execute([$name, $slug, $icon, $description, $image_url, $orbital_enabled, $terrestrial_enabled, $status_profile_id, $id]); }
else { $stmt = $db->prepare("INSERT INTO celestial_object_types (name, slug, icon, description, image_url, orbital_control_enabled, terrestrial_control_enabled, status_profile_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"); $stmt->execute([$name, $slug, $icon, $description, $image_url, $orbital_enabled, $terrestrial_enabled, $status_profile_id]); $id = $db->lastInsertId(); }
$db->prepare("DELETE FROM celestial_object_type_modifiers WHERE celestial_object_type_id = ?")->execute([$id]);
if (!empty($modifier_ids)) { $ins = $db->prepare("INSERT INTO celestial_object_type_modifiers (celestial_object_type_id, modifier_id) VALUES (?, ?)"); foreach ($modifier_ids as $mid) $ins->execute([$id, (int)$mid]); }
header("Location: admin.php?tab=objects&success=1"); exit;
}
if (isset($_GET['delete_object'])) { $db->prepare("DELETE FROM celestial_object_types WHERE id = ?")->execute([(int)$_GET['delete_object']]); header("Location: admin.php?tab=objects&success=1"); exit; }
// Statuses
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["action"]) && $_POST["action"] === "upsert_status") {
$id = (int)$_POST["id"]; $name = $_POST["name"]; $slug = $_POST["slug"]; $color = $_POST["color"]; $description = $_POST["description"];
if (isset($_POST["is_blinking"]) && $_POST["is_blinking"] === "on") { if (strpos($color, ";blink") === false) $color .= ";blink"; } else { $color = str_replace(";blink", "", $color); }
if ($id > 0) { $stmt = $db->prepare("UPDATE celestial_object_statuses SET name = ?, slug = ?, color = ?, description = ? WHERE id = ?"); $stmt->execute([$name, $slug, $color, $description, $id]); }
else { $stmt = $db->prepare("INSERT INTO celestial_object_statuses (name, slug, color, description) VALUES (?, ?, ?, ?)"); $stmt->execute([$name, $slug, $color, $description]); }
header("Location: admin.php?tab=statuses&success=1"); exit;
}
if (isset($_GET['delete_status'])) { $db->prepare("DELETE FROM celestial_object_statuses WHERE id = ?")->execute([(int)$_GET['delete_status']]); header("Location: admin.php?tab=statuses&success=1"); exit; }
// Factions
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'upsert_faction') {
$id = (int)$_POST['id']; $name = $_POST['name']; $slug = $_POST['slug']; $fa_icon = $_POST['fa_icon']; $color = $_POST['color']; $is_playable = isset($_POST['is_playable']) ? 1 : 0;
$image_url = null; if ($id > 0) { $stmt_img = $db->prepare("SELECT image_url FROM factions WHERE id = ?"); $stmt_img->execute([$id]); $image_url = $stmt_img->fetchColumn(); }
if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) { $ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION); $filename = $slug . "_" . time() . "." . $ext; $target = "assets/images/factions/" . $filename; if (!is_dir("assets/images/factions")) mkdir("assets/images/factions", 0777, true); if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) $image_url = $target; }
if ($id > 0) { $stmt = $db->prepare("UPDATE factions SET name = ?, slug = ?, fa_icon = ?, color = ?, image_url = ?, is_playable = ? WHERE id = ?"); $stmt->execute([$name, $slug, $fa_icon, $color, $image_url, $is_playable, $id]); }
else { $stmt = $db->prepare("INSERT INTO factions (name, slug, fa_icon, color, image_url, is_playable) VALUES (?, ?, ?, ?, ?, ?)"); $stmt->execute([$name, $slug, $fa_icon, $color, $image_url, $is_playable]); }
header("Location: admin.php?tab=factions&success=1"); exit;
}
if (isset($_GET['delete_faction'])) { $db->prepare("DELETE FROM factions WHERE id = ?")->execute([(int)$_GET['delete_faction']]); header("Location: admin.php?tab=factions&success=1"); exit; }
// Resources
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'upsert_resource') {
$id = (int)$_POST['id']; $name = $_POST['name']; $slug = $_POST['slug']; $icon = $_POST['icon']; $description = $_POST['description']; $show_in_header = isset($_POST["show_in_header"]) ? 1 : 0;
$image_url = null; if ($id > 0) { $stmt_img = $db->prepare("SELECT image_url FROM game_resources WHERE id = ?"); $stmt_img->execute([$id]); $image_url = $stmt_img->fetchColumn(); }
if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) { $ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION); $filename = $slug . "_" . time() . "." . $ext; $target = "assets/images/resources/" . $filename; if (!is_dir("assets/images/resources")) mkdir("assets/images/resources", 0777, true); if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) $image_url = $target; }
if ($id > 0) { $stmt = $db->prepare("UPDATE game_resources SET name = ?, slug = ?, icon = ?, description = ?, show_in_header = ?, image_url = ? WHERE id = ?"); $stmt->execute([$name, $slug, $icon, $description, $show_in_header, $image_url, $id]); }
else { $stmt = $db->prepare("INSERT INTO game_resources (name, slug, icon, description, show_in_header, image_url) VALUES (?, ?, ?, ?, ?, ?)"); $stmt->execute([$name, $slug, $icon, $description, $show_in_header, $image_url]); }
header("Location: admin.php?tab=resources&success=1"); exit;
}
if (isset($_GET['delete_resource'])) { $db->prepare("DELETE FROM game_resources WHERE id = ?")->execute([(int)$_GET['delete_resource']]); header("Location: admin.php?tab=resources&success=1"); exit; }
// Modifiers
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'upsert_modifier') {
$id = (int)$_POST['id']; $name = $_POST['name']; $type = $_POST['type']; $slug = $_POST['slug']; $description = $_POST['description']; $icon = $_POST['icon'];
if ($id > 0) { $stmt = $db->prepare("UPDATE modifiers SET name = ?, type = ?, slug = ?, description = ?, icon = ? WHERE id = ?"); $stmt->execute([$name, $type, $slug, $description, $icon, $id]); }
else { $stmt = $db->prepare("INSERT INTO modifiers (name, type, slug, description, icon) VALUES (?, ?, ?, ?, ?)"); $stmt->execute([$name, $type, $slug, $description, $icon]); }
header("Location: admin.php?tab=modifiers&success=1"); exit;
}
if (isset($_GET['delete_modifier'])) { $db->prepare("DELETE FROM modifiers WHERE id = ?")->execute([(int)$_GET['delete_modifier']]); header("Location: admin.php?tab=modifiers&success=1"); exit; }
// Settlement Types
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'upsert_settlement_type') {
$id = (int)$_POST['id']; $name = $_POST['name']; $slug = $_POST['slug']; $description = $_POST['description'];
if ($id > 0) { $stmt = $db->prepare("UPDATE settlement_types SET name = ?, slug = ?, description = ? WHERE id = ?"); $stmt->execute([$name, $slug, $description, $id]); }
else { $stmt = $db->prepare("INSERT INTO settlement_types (name, slug, description) VALUES (?, ?, ?)"); $stmt->execute([$name, $slug, $description]); }
header("Location: admin.php?tab=settlement_types&success=1"); exit;
}
if (isset($_GET['delete_settlement_type'])) { $db->prepare("DELETE FROM settlement_types WHERE id = ?")->execute([(int)$_GET['delete_settlement_type']]); header("Location: admin.php?tab=settlement_types&success=1"); exit; }
// Lootboxes
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'upsert_lootbox') {
$id = (int)$_POST['id']; $name = $_POST['name']; $slug = $_POST['slug']; $description = $_POST['description'];
if ($id > 0) { $db->prepare("UPDATE lootboxes SET name = ?, slug = ?, description = ? WHERE id = ?")->execute([$name, $slug, $description, $id]); }
else { $db->prepare("INSERT INTO lootboxes (name, slug, description) VALUES (?, ?, ?)")->execute([$name, $slug, $description]); }
header("Location: admin.php?tab=lootboxes&success=1"); exit;
}
if (isset($_GET['delete_lootbox'])) { $db->prepare("DELETE FROM lootboxes WHERE id = ?")->execute([(int)$_GET['delete_lootbox']]); header("Location: admin.php?tab=lootboxes&success=1"); exit; }
// --- DATA FETCHING ---
$users_list = []; $objects_list = []; $statuses_list = []; $status_profiles_list = []; $modifiers_list = []; $factions_list = []; $resources_list = []; $settlement_types_list = []; $lootboxes_list = []; $project_logs_list = [];
if ($tab === 'users') { $users_list = $db->query("SELECT id, username, email, role FROM users ORDER BY username ASC")->fetchAll(); }
elseif ($tab === 'objects') {
$objects_list = $db->query("SELECT o.*, p.name as profile_name FROM celestial_object_types o LEFT JOIN celestial_object_status_profiles p ON o.status_profile_id = p.id ORDER BY o.name ASC")->fetchAll();
foreach ($objects_list as &$obj) { $stmt = $db->prepare("SELECT modifier_id FROM celestial_object_type_modifiers WHERE celestial_object_type_id = ?"); $stmt->execute([$obj['id']]); $obj['modifier_ids'] = $stmt->fetchAll(PDO::FETCH_COLUMN); } unset($obj);
$status_profiles_list = $db->query("SELECT id, name FROM celestial_object_status_profiles WHERE enabled = 1 ORDER BY name ASC")->fetchAll();
$modifiers_list = $db->query("SELECT * FROM modifiers ORDER BY type, name ASC")->fetchAll();
}
elseif ($tab === 'statuses') { $statuses_list = $db->query("SELECT * FROM celestial_object_statuses ORDER BY name ASC")->fetchAll(); }
elseif ($tab === 'status_profiles') {
$status_profiles_list = $db->query("SELECT * FROM celestial_object_status_profiles ORDER BY priority DESC, name ASC")->fetchAll();
$statuses_list = $db->query("SELECT id, name FROM celestial_object_statuses ORDER BY name ASC")->fetchAll();
$object_types_list = $db->query("SELECT id, name, slug FROM celestial_object_types ORDER BY name ASC")->fetchAll();
}
elseif ($tab === 'resources') { $resources_list = $db->query("SELECT * FROM game_resources ORDER BY name ASC")->fetchAll(); }
elseif ($tab === 'factions') { $factions_list = $db->query("SELECT * FROM factions ORDER BY name ASC")->fetchAll(); }
elseif ($tab === 'modifiers') { $modifiers_list = $db->query("SELECT * FROM modifiers ORDER BY type, name ASC")->fetchAll(); }
elseif ($tab === 'settlement_types') { $settlement_types_list = $db->query("SELECT * FROM settlement_types ORDER BY name ASC")->fetchAll(); }
elseif ($tab === 'lootboxes') { $lootboxes_list = $db->query("SELECT * FROM lootboxes ORDER BY name ASC")->fetchAll(); }
elseif ($tab === 'project_logs') { $project_logs_list = $db->query("SELECT * FROM project_logs ORDER BY created_at DESC")->fetchAll(); }
?>
Console Admin - Nexus
| Visuel | Nom | Slug | Profil | Actions |
| | | | X |
| Prio | Nom | Slug | Actions |
| | | X |
| Couleur | Nom | Slug | Actions |
| | | X |
| Visuel | Nom | Slug | Actions |
| | | X |
| Icône | Nom | Slug | Header | Actions |
| | | | X |
| Type | Nom | Slug | Actions |
| | | X |