admin.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. session_start();
  3. require_once 'config.php';
  4. require_once 'db_config.php';
  5. // --- 1. LOGIN / LOGOUT SCHUTZ ---
  6. if (isset($_GET['logout'])) { session_destroy(); header("Location: admin.php"); exit; }
  7. $show_login = true;
  8. if (isset($_SESSION['admin_auth']) && $_SESSION['admin_auth'] === true) { $show_login = false; }
  9. if (isset($_POST['login_auth'])) {
  10. if ($_POST['pw'] === ADMIN_PASSWORD) {
  11. $_SESSION['admin_auth'] = true;
  12. $show_login = false;
  13. } else { $login_error = "Falsches Passwort!"; }
  14. }
  15. if ($show_login):
  16. ?>
  17. <!DOCTYPE html>
  18. <html lang="de">
  19. <head>
  20. <meta charset="UTF-8">
  21. <title>Admin Login</title>
  22. <style>
  23. :root { --bg: #f4f7f6; --card: #ffffff; --text: #333; --border: #ddd; }
  24. .dark-theme { --bg: #121212cf; --card: #1e1e1e; --text: #e0e0e0; --border: #333; }
  25. body { font-family: 'Segoe UI', sans-serif; background: var(--bg); color: var(--text); display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; }
  26. .login-box { background: var(--card); padding: 40px; border-radius: 12px; border: 1px solid var(--border); box-shadow: 0 4px 15px rgba(0,0,0,0.1); text-align: center; width: 300px; }
  27. input { width: 100%; padding: 12px; margin: 15px 0; border-radius: 5px; border: 1px solid var(--border); background: var(--card); color: var(--text); box-sizing: border-box; }
  28. button { background: #e67e22; color: white; border: none; padding: 12px; border-radius: 5px; cursor: pointer; width: 100%; font-weight: bold; }
  29. </style>
  30. <script>if (localStorage.getItem('theme') === 'dark') document.documentElement.classList.add('dark-theme');</script>
  31. </head>
  32. <body>
  33. <div class="login-box">
  34. <h2>🔐 Admin Login</h2>
  35. <?php if(isset($login_error)) echo "<div style='color:red; margin-bottom:10px;'>$login_error</div>"; ?>
  36. <form method="POST">
  37. <input type="password" name="pw" placeholder="Passwort" autofocus required>
  38. <button type="submit" name="login_auth">Einloggen</button>
  39. </form>
  40. </div>
  41. </body>
  42. </html>
  43. <?php exit; endif; ?>
  44. <?php
  45. // --- 2. DATENBANK LOGIK ---
  46. $msg = "";
  47. $msg_type = "success";
  48. // Spiel hinzufügen
  49. if (isset($_POST['add_new_game'])) {
  50. $ean = trim($_POST['new_ean']);
  51. $titel = trim($_POST['new_titel']);
  52. $level = (empty($_POST['new_level']) || $_POST['new_level'] == 'Unknown') ? 'Unknown' : $_POST['new_level'];
  53. $typ_id = ($_POST['new_typ_id'] == '0') ? null : (int)$_POST['new_typ_id'];
  54. $bild_url = trim($_POST['new_bild_url']);
  55. try {
  56. $stmt = $pdo->prepare("INSERT INTO spiele (titel, typ_id, ean, level, bild_url) VALUES (?, ?, ?, ?, ?)");
  57. $stmt->execute([$titel, $typ_id, $ean, $level, $bild_url]);
  58. $msg = "✅ Spiel erfolgreich angelegt!";
  59. } catch (PDOException $e) {
  60. if ($e->getCode() == 23000) {
  61. $msg = "❌ Fehler: EAN existiert bereits!";
  62. $msg_type = "error";
  63. } else { $msg = "Fehler: " . $e->getMessage(); }
  64. }
  65. }
  66. // Spiel updaten
  67. if (isset($_POST['update_game'])) {
  68. $stmt = $pdo->prepare("UPDATE spiele SET titel = ?, typ_id = ?, ean = ?, level = ?, bild_url = ? WHERE id = ?");
  69. $stmt->execute([$_POST['titel'], ($_POST['typ_id'] == '0' ? null : (int)$_POST['typ_id']), $_POST['ean'], $_POST['level'], $_POST['bild_url'], (int)$_POST['spiel_id']]);
  70. $msg = "💾 Änderungen gespeichert!";
  71. }
  72. // Spiele-Typ hinzufügen
  73. if (isset($_POST['add_new_typ'])) {
  74. $bez = trim($_POST['new_typ_bez']);
  75. if(!empty($bez)) {
  76. $pdo->prepare("INSERT INTO game_typen (bezeichnung) VALUES (?)")->execute([$bez]);
  77. $msg = "✅ Neuer Typ '$bez' hinzugefügt!";
  78. }
  79. }
  80. // Spiele-Typ löschen
  81. if (isset($_POST['delete_typ'])) {
  82. try {
  83. $pdo->prepare("DELETE FROM game_typen WHERE id = ?")->execute([(int)$_POST['typ_id']]);
  84. $msg = "🗑 Typ gelöscht!";
  85. } catch (Exception $e) {
  86. $msg = "❌ Fehler: Typ wird noch von Spielen verwendet!";
  87. $msg_type = "error";
  88. }
  89. }
  90. // Spiel löschen
  91. if (isset($_POST['delete_game'])) {
  92. $pdo->prepare("DELETE FROM spiele WHERE id = ?")->execute([(int)$_POST['spiel_id']]);
  93. $msg = "🗑 Spiel gelöscht!";
  94. }
  95. // NEU: Spieler umbenennen
  96. if (isset($_POST['rename_player'])) {
  97. $p_id = (int)$_POST['player_id'];
  98. $p_name = trim($_POST['new_name']);
  99. if (!empty($p_name)) {
  100. $pdo->prepare("UPDATE spieler SET name = ? WHERE id = ?")->execute([$p_name, $p_id]);
  101. $msg = "💾 Teamname aktualisiert!";
  102. }
  103. }
  104. // Spieler löschen
  105. if (isset($_POST['delete_player'])) {
  106. $pdo->prepare("DELETE FROM spieler WHERE id = ?")->execute([(int)$_POST['player_id']]);
  107. $msg = "👤 Spieler/Team gelöscht!";
  108. }
  109. $spiele = $pdo->query("SELECT s.*, t.bezeichnung as typ_name FROM spiele s LEFT JOIN game_typen t ON s.typ_id = t.id ORDER BY s.id DESC")->fetchAll();
  110. $typen = $pdo->query("SELECT * FROM game_typen ORDER BY bezeichnung ASC")->fetchAll();
  111. $spieler = $pdo->query("SELECT * FROM spieler ORDER BY name ASC")->fetchAll();
  112. ?>
  113. <!DOCTYPE html>
  114. <html lang="de">
  115. <head>
  116. <meta charset="UTF-8">
  117. <title>EXIT Admin - Stammdaten</title>
  118. <style>
  119. :root {
  120. --bg: #f4f7f6; --card: #ffffff; --text: #333; --border: #ddd; --accent: #e67e22;
  121. --header-bg: #f8f9fa; --input-bg: #fff; --unknown-bg: #fff3e0;
  122. }
  123. .dark-theme {
  124. --bg: #121212cf; --card: #1e1e1e; --text: #e0e0e0; --border: #333;
  125. --header-bg: #252525; --header-text: #e67e22; --input-bg: #2a2a2a; --unknown-bg: #4d2b00;
  126. }
  127. body { font-family: 'Segoe UI', sans-serif; background: var(--bg); color: var(--text); padding: 20px; transition: 0.3s; }
  128. .container { max-width: 1500px; margin: 0 auto; }
  129. .tab-nav { display: flex; gap: 10px; margin-bottom: 20px; border-bottom: 2px solid var(--border); padding-bottom: 10px; flex-wrap: wrap; }
  130. .tab-btn { padding: 10px 20px; border: none; background: none; color: var(--text); cursor: pointer; font-weight: bold; border-radius: 8px; transition: 0.2s; }
  131. .tab-btn.active { background: var(--accent); color: white; }
  132. .tab-content { display: none; }
  133. .tab-content.active { display: block; }
  134. .admin-card { background: var(--card); padding: 20px; border-radius: 12px; box-shadow: 0 4px 15px rgba(0,0,0,0.1); margin-bottom: 30px; border: 1px solid var(--border); }
  135. .alert { padding: 15px; border-radius: 8px; margin-bottom: 20px; font-weight: bold; text-align: center; border: 1px solid transparent; }
  136. .alert-success { background: #d4edda; color: #155724; border-color: #c3e6cb; }
  137. .alert-error { background: #f8d7da; color: #721c24; border-color: #f5c6cb; }
  138. table { width: 100%; border-collapse: collapse; margin-top: 10px; background: var(--card); }
  139. th { background: var(--header-bg); padding: 12px; text-align: left; font-size: 0.85em; border-bottom: 2px solid var(--border); color: var(--text); }
  140. td { padding: 10px; border-bottom: 1px solid var(--border); }
  141. input, select { padding: 8px; border: 1px solid var(--border); border-radius: 6px; background: var(--input-bg); color: var(--text); width: 100%; box-sizing: border-box; }
  142. .btn { padding: 10px 15px; border: none; border-radius: 6px; cursor: pointer; font-weight: bold; color: white; text-decoration: none; display: inline-block; }
  143. .btn-add { background: #27ae60; margin-top: 10px; width: 100%; }
  144. .btn-save { background: #2980b9; }
  145. .btn-del { background: #c0392b; }
  146. .btn-nav { background: var(--accent); }
  147. .img-preview { width: 40px; height: 40px; object-fit: cover; border-radius: 4px; }
  148. .lvl-unknown { background: var(--unknown-bg) !important; color: #e67e22 !important; }
  149. #scrollToTop { position: fixed; bottom: 30px; right: 30px; background: var(--accent); color: white; width: 45px; height: 45px; border-radius: 50%; border: none; cursor: pointer; display: none; }
  150. </style>
  151. </head>
  152. <body>
  153. <div class="container">
  154. <div style="display:flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
  155. <h1>🛠 Admin-Bereich</h1>
  156. <div style="display: flex; align-items: center; gap: 10px;">
  157. <button onclick="toggleTheme()" class="theme-toggle" style="background:none; border:none; font-size:1.5rem; cursor:pointer;" id="theme-icon">🌙</button>
  158. <a href="index.php" class="btn btn-nav">Dashboard</a>
  159. <a href="admin.php?logout=1" class="btn btn-del">Logout</a>
  160. </div>
  161. </div>
  162. <?php if ($msg): ?>
  163. <div class="alert alert-<?= $msg_type ?>" id="msg-box"><?= $msg ?></div>
  164. <?php endif; ?>
  165. <div class="tab-nav">
  166. <button class="tab-btn active" onclick="openTab(event, 'games')">🎮 Spiele</button>
  167. <button class="tab-btn" onclick="openTab(event, 'typen')">🏷 Spieletypen</button>
  168. <button class="tab-btn" onclick="openTab(event, 'players')">👥 Spieler / Teams</button>
  169. </div>
  170. <div id="games" class="tab-content active">
  171. <div class="admin-card">
  172. <h3>🆕 Neues Spiel hinzufügen</h3>
  173. <form method="POST">
  174. <div style="display: grid; grid-template-columns: 2fr 1fr 1fr 1fr 2fr; gap: 10px;">
  175. <input type="text" name="new_titel" placeholder="Spieltitel" required>
  176. <input type="text" name="new_ean" placeholder="EAN" required>
  177. <select name="new_typ_id">
  178. <option value="0">-- Typ: Unbekannt --</option>
  179. <?php foreach($typen as $t): ?><option value="<?= $t['id'] ?>"><?= htmlspecialchars($t['bezeichnung']) ?></option><?php endforeach; ?>
  180. </select>
  181. <select name="new_level">
  182. <option value="Unknown">-- Level: Unbekannt --</option>
  183. <option value="Einsteiger">Einsteiger</option>
  184. <option value="Fortgeschrittene">Fortgeschrittene</option>
  185. <option value="Profi">Profi</option>
  186. </select>
  187. <input type="text" name="new_bild_url" placeholder="Bild-URL">
  188. </div>
  189. <button type="submit" name="add_new_game" class="btn btn-add">In Datenbank speichern</button>
  190. </form>
  191. </div>
  192. <div class="admin-card" style="padding:0; overflow-x:auto;">
  193. <table>
  194. <thead>
  195. <tr>
  196. <th style="width:50px;">Bild</th>
  197. <th>Titel</th>
  198. <th style="width:120px;">EAN</th>
  199. <th style="width:140px;">Typ</th>
  200. <th style="width:140px;">Level</th>
  201. <th style="width:200px;">Bild-URL</th>
  202. <th style="width:90px;">Aktion</th>
  203. </tr>
  204. </thead>
  205. <tbody>
  206. <?php foreach ($spiele as $sp): ?>
  207. <tr>
  208. <form method="POST">
  209. <input type="hidden" name="spiel_id" value="<?= $sp['id'] ?>">
  210. <td><img src="<?= htmlspecialchars($sp['bild_url']) ?>" class="img-preview" onerror="this.src='https://via.placeholder.com/40?text=?';"></td>
  211. <td><input type="text" name="titel" value="<?= htmlspecialchars($sp['titel']) ?>"></td>
  212. <td><input type="text" name="ean" value="<?= htmlspecialchars($sp['ean']) ?>"></td>
  213. <td>
  214. <select name="typ_id">
  215. <option value="0">Unbekannt</option>
  216. <?php foreach($typen as $t): ?>
  217. <option value="<?= $t['id'] ?>" <?= ($sp['typ_id']==$t['id'])?'selected':'' ?>><?= htmlspecialchars($t['bezeichnung']) ?></option>
  218. <?php endforeach; ?>
  219. </select>
  220. </td>
  221. <td>
  222. <select name="level">
  223. <option value="Unknown" <?= ($sp['level']=='Unknown' || empty($sp['level']))?'selected':'' ?>>Unbekannt</option>
  224. <option value="Einsteiger" <?= $sp['level']=='Einsteiger'?'selected':'' ?>>Einsteiger</option>
  225. <option value="Fortgeschrittene" <?= $sp['level']=='Fortgeschrittene'?'selected':'' ?>>Fortgeschrittene</option>
  226. <option value="Profi" <?= $sp['level']=='Profi'?'selected':'' ?>>Profi</option>
  227. </select>
  228. </td>
  229. <td><input type="text" name="bild_url" value="<?= htmlspecialchars($sp['bild_url']) ?>"></td>
  230. <td>
  231. <button type="submit" name="update_game" class="btn btn-save">💾</button>
  232. <button type="submit" name="delete_game" class="btn btn-del" onclick="return confirm('Löschen?')">🗑</button>
  233. </td>
  234. </form>
  235. </tr>
  236. <?php endforeach; ?>
  237. </tbody>
  238. </table>
  239. </div>
  240. </div>
  241. <div id="typen" class="tab-content">
  242. <div class="admin-card">
  243. <h3>🏷 Spieletypen verwalten</h3>
  244. <form method="POST" style="display:flex; gap:10px; margin-bottom:20px;">
  245. <input type="text" name="new_typ_bez" placeholder="z.B. Adventure Games" required>
  246. <button type="submit" name="add_new_typ" class="btn btn-add" style="margin:0; width:auto;">Hinzufügen</button>
  247. </form>
  248. <table>
  249. <thead>
  250. <tr><th>ID</th><th>Bezeichnung</th><th style="width:100px;">Aktion</th></tr>
  251. </thead>
  252. <tbody>
  253. <?php foreach ($typen as $t): ?>
  254. <tr>
  255. <td>#<?= $t['id'] ?></td>
  256. <td><strong><?= htmlspecialchars($t['bezeichnung']) ?></strong></td>
  257. <td>
  258. <form method="POST" onsubmit="return confirm('Typ löschen?')">
  259. <input type="hidden" name="typ_id" value="<?= $t['id'] ?>">
  260. <button type="submit" name="delete_typ" class="btn btn-del">Löschen</button>
  261. </form>
  262. </td>
  263. </tr>
  264. <?php endforeach; ?>
  265. </tbody>
  266. </table>
  267. </div>
  268. </div>
  269. <div id="players" class="tab-content">
  270. <div class="admin-card">
  271. <h3>👥 Spieler / Teams bearbeiten</h3>
  272. <table>
  273. <thead>
  274. <tr><th>ID</th><th>Name</th><th style="width:160px;">Aktion</th></tr>
  275. </thead>
  276. <tbody>
  277. <?php foreach ($spieler as $p): ?>
  278. <tr>
  279. <td>#<?= $p['id'] ?></td>
  280. <form method="POST">
  281. <td>
  282. <input type="hidden" name="player_id" value="<?= $p['id'] ?>">
  283. <input type="text" name="new_name" value="<?= htmlspecialchars($p['name']) ?>" required>
  284. </td>
  285. <td>
  286. <button type="submit" name="rename_player" class="btn btn-save">💾</button>
  287. <button type="submit" name="delete_player" class="btn btn-del" onclick="return confirm('Diesen Spieler wirklich löschen?')">🗑</button>
  288. </td>
  289. </form>
  290. </tr>
  291. <?php endforeach; ?>
  292. </tbody>
  293. </table>
  294. </div>
  295. </div>
  296. </div>
  297. <script>
  298. function openTab(evt, tabName) {
  299. let i, content, btns;
  300. content = document.getElementsByClassName("tab-content");
  301. for (i = 0; i < content.length; i++) { content[i].classList.remove("active"); }
  302. btns = document.getElementsByClassName("tab-btn");
  303. for (i = 0; i < btns.length; i++) { btns[i].classList.remove("active"); }
  304. document.getElementById(tabName).classList.add("active");
  305. evt.currentTarget.classList.add("active");
  306. }
  307. const themeIcon = document.getElementById('theme-icon');
  308. function toggleTheme() {
  309. const isDark = document.documentElement.classList.toggle('dark-theme');
  310. localStorage.setItem('theme', isDark ? 'dark' : 'light');
  311. themeIcon.innerText = isDark ? '☀️' : '🌙';
  312. }
  313. if (localStorage.getItem('theme') === 'dark') {
  314. document.documentElement.classList.add('dark-theme');
  315. themeIcon.innerText = '☀️';
  316. }
  317. setTimeout(() => {
  318. const msg = document.getElementById('msg-box');
  319. if(msg) { msg.style.transition = 'opacity 0.5s'; msg.style.opacity = '0'; }
  320. }, 3000);
  321. </script>
  322. </body>
  323. </html>