admin.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. body { font-family: 'Segoe UI', sans-serif; background: #121212; color: white; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; }
  24. .login-box { background: #1e1e1e; padding: 40px; border-radius: 12px; border: 1px solid #e67e22; text-align: center; width: 300px; }
  25. input { width: 100%; padding: 12px; margin: 15px 0; border-radius: 5px; border: 1px solid #444; background: #2a2a2a; color: white; box-sizing: border-box; }
  26. button { background: #e67e22; color: white; border: none; padding: 12px; border-radius: 5px; cursor: pointer; width: 100%; font-weight: bold; }
  27. .error { color: #e74c3c; font-size: 0.9em; }
  28. </style>
  29. </head>
  30. <body>
  31. <div class="login-box">
  32. <h2>🔐 Admin Login</h2>
  33. <?php if(isset($login_error)) echo "<div class='error'>$login_error</div>"; ?>
  34. <form method="POST">
  35. <input type="password" name="pw" placeholder="Passwort" autofocus required>
  36. <button type="submit" name="login_auth">Einloggen</button>
  37. </form>
  38. </div>
  39. </body>
  40. </html>
  41. <?php exit; endif; ?>
  42. <?php
  43. // --- 2. DATENBANK LOGIK ---
  44. $msg = "";
  45. $msg_type = "success";
  46. // Spiel hinzufügen
  47. if (isset($_POST['add_new_game'])) {
  48. $ean = trim($_POST['new_ean']);
  49. $titel = trim($_POST['new_titel']);
  50. $level = (empty($_POST['new_level']) || $_POST['new_level'] == 'Unknown') ? 'Unknown' : $_POST['new_level'];
  51. $typ_id = ($_POST['new_typ_id'] == '0') ? null : (int)$_POST['new_typ_id'];
  52. $bild_url = trim($_POST['new_bild_url']);
  53. try {
  54. $stmt = $pdo->prepare("INSERT INTO spiele (titel, typ_id, ean, level, bild_url) VALUES (?, ?, ?, ?, ?)");
  55. $stmt->execute([$titel, $typ_id, $ean, $level, $bild_url]);
  56. $msg = "✅ Spiel erfolgreich angelegt!";
  57. } catch (PDOException $e) {
  58. if ($e->getCode() == 23000) {
  59. $msg = "❌ Fehler: Die EAN $ean existiert bereits!";
  60. $msg_type = "error";
  61. } else { $msg = "Fehler: " . $e->getMessage(); }
  62. }
  63. }
  64. // Spiel aktualisieren
  65. if (isset($_POST['update_game'])) {
  66. $stmt = $pdo->prepare("UPDATE spiele SET titel = ?, typ_id = ?, ean = ?, level = ?, bild_url = ? WHERE id = ?");
  67. $stmt->execute([$_POST['titel'], ($_POST['typ_id'] == '0' ? null : (int)$_POST['typ_id']), $_POST['ean'], $_POST['level'], $_POST['bild_url'], (int)$_POST['spiel_id']]);
  68. $msg = "💾 Änderungen gespeichert!";
  69. }
  70. // Löschen
  71. if (isset($_POST['delete_game'])) {
  72. $pdo->prepare("DELETE FROM spiele WHERE id = ?")->execute([(int)$_POST['spiel_id']]);
  73. $msg = "🗑 Spiel gelöscht!";
  74. }
  75. // Daten abrufen
  76. $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();
  77. $typen = $pdo->query("SELECT * FROM game_typen ORDER BY bezeichnung ASC")->fetchAll();
  78. ?>
  79. <!DOCTYPE html>
  80. <html lang="de">
  81. <head>
  82. <meta charset="UTF-8">
  83. <title>EXIT Admin - Stammdaten</title>
  84. <style>
  85. body { font-family: 'Segoe UI', sans-serif; background: #121212; color: #e0e0e0; padding: 20px; }
  86. .container { max-width: 1300px; margin: 0 auto; }
  87. .alert { padding: 15px; border-radius: 8px; margin-bottom: 20px; font-weight: bold; text-align: center; }
  88. .alert-success { background: #27ae60; color: white; }
  89. .alert-error { background: #e74c3c; color: white; }
  90. .admin-card { background: #1e1e1e; padding: 20px; border-radius: 12px; box-shadow: 0 4px 15px rgba(0,0,0,0.3); margin-bottom: 30px; border: 1px solid #333; }
  91. table { width: 100%; border-collapse: collapse; margin-top: 20px; }
  92. th, td { padding: 10px; border-bottom: 1px solid #333; text-align: left; }
  93. th { color: #e67e22; }
  94. input, select { padding: 8px; border: 1px solid #444; border-radius: 6px; width: 100%; box-sizing: border-box; background: #2a2a2a; color: white; }
  95. .lvl-unknown { background: #d35400 !important; color: white; }
  96. .btn { padding: 10px 15px; border: none; border-radius: 6px; cursor: pointer; font-weight: bold; color: white; text-decoration: none; display: inline-block; }
  97. .btn-add { background: #27ae60; width: 100%; margin-top: 10px; }
  98. .btn-save { background: #2980b9; }
  99. .btn-del { background: #c0392b; }
  100. .btn-nav { background: #e67e22; }
  101. .img-preview { width: 40px; height: 40px; object-fit: cover; border-radius: 4px; background: #333; }
  102. </style>
  103. </head>
  104. <body>
  105. <div class="container">
  106. <div style="display:flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
  107. <h1>🛠 Stammdaten Verwaltung</h1>
  108. <div>
  109. <a href="index.php" class="btn btn-nav">Dashboard</a>
  110. <a href="admin.php?logout=1" class="btn btn-del">Logout</a>
  111. </div>
  112. </div>
  113. <?php if ($msg): ?>
  114. <div class="alert alert-<?= $msg_type ?>" id="msg-box"><?= $msg ?></div>
  115. <?php endif; ?>
  116. <div class="admin-card">
  117. <h3>🆕 Neues Spiel hinzufügen</h3>
  118. <form method="POST">
  119. <div style="display: grid; grid-template-columns: 2fr 1fr 1fr 1fr 2fr; gap: 10px;">
  120. <input type="text" name="new_titel" placeholder="Spieltitel" required>
  121. <input type="text" name="new_ean" placeholder="EAN Scannen" required>
  122. <select name="new_typ_id">
  123. <option value="0">-- Typ: Unbekannt --</option>
  124. <?php foreach($typen as $t): ?>
  125. <option value="<?= $t['id'] ?>"><?= htmlspecialchars($t['bezeichnung']) ?></option>
  126. <?php endforeach; ?>
  127. </select>
  128. <select name="new_level">
  129. <option value="Unknown" selected>-- Level: Unbekannt --</option>
  130. <option value="Einsteiger">Einsteiger</option>
  131. <option value="Fortgeschrittene">Fortgeschrittene</option>
  132. <option value="Profi">Profi</option>
  133. </select>
  134. <input type="text" name="new_bild_url" placeholder="Bild-URL (z.B. https://...)">
  135. </div>
  136. <button type="submit" name="add_new_game" class="btn btn-add">Spiel in Datenbank speichern</button>
  137. </form>
  138. </div>
  139. <div class="admin-card">
  140. <h3>📋 Aktueller Bestand</h3>
  141. <table>
  142. <thead>
  143. <tr>
  144. <th style="width: 50px;">Bild</th>
  145. <th>Titel</th>
  146. <th>EAN</th>
  147. <th>Typ</th>
  148. <th>Level</th>
  149. <th>Bild-URL</th>
  150. <th>Aktion</th>
  151. </tr>
  152. </thead>
  153. <tbody>
  154. <?php foreach ($spiele as $sp):
  155. $is_unknown = ($sp['level'] == 'Unknown' || empty($sp['level']));
  156. ?>
  157. <tr>
  158. <form method="POST">
  159. <input type="hidden" name="spiel_id" value="<?= $sp['id'] ?>">
  160. <td><img src="<?= htmlspecialchars($sp['bild_url']) ?>" class="img-preview" alt="Cover"></td>
  161. <td><input type="text" name="titel" value="<?= htmlspecialchars($sp['titel']) ?>"></td>
  162. <td><input type="text" name="ean" value="<?= htmlspecialchars($sp['ean']) ?>"></td>
  163. <td>
  164. <select name="typ_id">
  165. <option value="0">-- Unbekannt --</option>
  166. <?php foreach($typen as $t): ?>
  167. <option value="<?= $t['id'] ?>" <?= ($sp['typ_id']==$t['id'])?'selected':'' ?>><?= htmlspecialchars($t['bezeichnung']) ?></option>
  168. <?php endforeach; ?>
  169. </select>
  170. </td>
  171. <td>
  172. <select name="level" class="<?= $is_unknown ? 'lvl-unknown' : '' ?>" onchange="this.className=this.value=='Unknown'?'lvl-unknown':''">
  173. <option value="Unknown" <?= $is_unknown ? 'selected' : '' ?>>Unbekannt</option>
  174. <option value="Einsteiger" <?= $sp['level']=='Einsteiger'?'selected':'' ?>>Einsteiger</option>
  175. <option value="Fortgeschrittene" <?= $sp['level']=='Fortgeschrittene'?'selected':'' ?>>Fortgeschrittene</option>
  176. <option value="Profi" <?= $sp['level']=='Profi'?'selected':'' ?>>Profi</option>
  177. </select>
  178. </td>
  179. <td><input type="text" name="bild_url" value="<?= htmlspecialchars($sp['bild_url']) ?>"></td>
  180. <td style="white-space:nowrap;">
  181. <button type="submit" name="update_game" class="btn btn-save" title="Speichern">💾</button>
  182. <button type="submit" name="delete_game" class="btn btn-del" onclick="return confirm('Wirklich löschen?')" title="Löschen">🗑</button>
  183. </td>
  184. </form>
  185. </tr>
  186. <?php endforeach; ?>
  187. </tbody>
  188. </table>
  189. </div>
  190. </div>
  191. <script>
  192. setTimeout(() => {
  193. const msg = document.getElementById('msg-box');
  194. if(msg) msg.style.display = 'none';
  195. }, 3000);
  196. </script>
  197. </body>
  198. </html>