admin.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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>
  31. if (localStorage.getItem('theme') === 'dark') document.documentElement.classList.add('dark-theme');
  32. </script>
  33. </head>
  34. <body>
  35. <div class="login-box">
  36. <h2>🔐 Admin Login</h2>
  37. <?php if(isset($login_error)) echo "<div style='color:red;'>$login_error</div>"; ?>
  38. <form method="POST">
  39. <input type="password" name="pw" placeholder="Passwort" autofocus required>
  40. <button type="submit" name="login_auth">Einloggen</button>
  41. </form>
  42. </div>
  43. </body>
  44. </html>
  45. <?php exit; endif; ?>
  46. <?php
  47. // --- 2. DATENBANK LOGIK ---
  48. $msg = "";
  49. $msg_type = "success";
  50. if (isset($_POST['add_new_game'])) {
  51. $ean = trim($_POST['new_ean']);
  52. $titel = trim($_POST['new_titel']);
  53. $level = (empty($_POST['new_level']) || $_POST['new_level'] == 'Unknown') ? 'Unknown' : $_POST['new_level'];
  54. $typ_id = ($_POST['new_typ_id'] == '0') ? null : (int)$_POST['new_typ_id'];
  55. $bild_url = trim($_POST['new_bild_url']);
  56. try {
  57. $stmt = $pdo->prepare("INSERT INTO spiele (titel, typ_id, ean, level, bild_url) VALUES (?, ?, ?, ?, ?)");
  58. $stmt->execute([$titel, $typ_id, $ean, $level, $bild_url]);
  59. $msg = "✅ Spiel erfolgreich angelegt!";
  60. } catch (PDOException $e) {
  61. if ($e->getCode() == 23000) {
  62. $msg = "❌ Fehler: Die EAN $ean existiert bereits!";
  63. $msg_type = "error";
  64. } else { $msg = "Fehler: " . $e->getMessage(); }
  65. }
  66. }
  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. if (isset($_POST['delete_game'])) {
  73. $pdo->prepare("DELETE FROM spiele WHERE id = ?")->execute([(int)$_POST['spiel_id']]);
  74. $msg = "🗑 Spiel gelöscht!";
  75. }
  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. /* THEME VARIABLEN */
  86. :root {
  87. --bg: #f4f7f6; --card: #ffffff; --text: #333; --border: #ddd;
  88. --header-bg: #f8f9fa; --input-bg: #fff; --unknown-bg: #fff3e0;
  89. }
  90. .dark-theme {
  91. --bg: #121212cf; --card: #1e1e1e; --text: #e0e0e0; --border: #333;
  92. --header-bg: #252525; --input-bg: #2a2a2a; --unknown-bg: #4d2b00;
  93. }
  94. body { font-family: 'Segoe UI', sans-serif; background: var(--bg); color: var(--text); padding: 20px; scroll-behavior: smooth; transition: background 0.3s, color 0.3s; }
  95. .container { max-width: 1400px; margin: 0 auto; position: relative; }
  96. /* Theme Switcher Button */
  97. .theme-toggle { background: var(--card); border: 1px solid var(--border); color: var(--text); padding: 8px 12px; border-radius: 20px; cursor: pointer; font-size: 1.2rem; margin-right: 10px; transition: 0.3s; }
  98. .theme-toggle:hover { transform: scale(1.1); }
  99. /* Admin Layout */
  100. .alert { padding: 15px; border-radius: 8px; margin-bottom: 20px; font-weight: bold; text-align: center; position: sticky; top: 10px; z-index: 1000; border: 1px solid transparent; }
  101. .alert-success { background: #d4edda; color: #155724; border-color: #c3e6cb; }
  102. .dark-theme .alert-success { background: #1b4332; color: #74c69d; }
  103. .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); }
  104. table { width: 100%; border-collapse: collapse; margin-top: 20px; table-layout: fixed; background: var(--card); }
  105. th { background: var(--header-bg); color: var(--text); padding: 12px 10px; text-align: left; font-size: 0.85em; text-transform: uppercase; border-bottom: 2px solid var(--border); }
  106. td { padding: 10px; border-bottom: 1px solid var(--border); }
  107. input, select { padding: 8px; border: 1px solid var(--border); border-radius: 6px; width: 100%; box-sizing: border-box; background: var(--input-bg); color: var(--text); font-size: 0.95em; }
  108. /* Spaltenbreiten */
  109. .col-img { width: 60px; } .col-titel { width: auto; } .col-ean { width: 130px; } .col-typ { width: 160px; } .col-lvl { width: 150px; } .col-url { width: 180px; } .col-akt { width: 100px; text-align: center; }
  110. .lvl-unknown { background: var(--unknown-bg) !important; color: #e67e22 !important; }
  111. .btn { padding: 10px 15px; border: none; border-radius: 6px; cursor: pointer; font-weight: bold; color: white; text-decoration: none; display: inline-block; font-size: 0.9em; }
  112. .btn-add { background: #27ae60; width: 100%; margin-top: 10px; }
  113. .btn-save { background: #2980b9; }
  114. .btn-del { background: #c0392b; }
  115. .btn-nav { background: #e67e22; }
  116. #scrollToTop { position: fixed; bottom: 30px; right: 30px; background: #e67e22; color: white; width: 45px; height: 45px; border-radius: 50%; border: none; cursor: pointer; display: none; z-index: 1000; box-shadow: 0 4px 10px rgba(0,0,0,0.2); }
  117. .img-preview { width: 40px; height: 40px; object-fit: cover; border-radius: 4px; border: 1px solid var(--border); background: #eee; }
  118. </style>
  119. <script>
  120. // Theme sofort beim Laden anwenden (verhindert weißes Flackern)
  121. if (localStorage.getItem('theme') === 'dark') document.documentElement.classList.add('dark-theme');
  122. </script>
  123. </head>
  124. <body id="top">
  125. <div class="container">
  126. <div style="display:flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
  127. <h1>🛠 Stammdaten</h1>
  128. <div style="display: flex; align-items: center;">
  129. <button onclick="toggleTheme()" class="theme-toggle" id="theme-icon">🌙</button>
  130. <a href="index.php" class="btn btn-nav">Dashboard</a>
  131. <a href="admin.php?logout=1" class="btn btn-del" style="margin-left:15px;">Logout</a>
  132. </div>
  133. </div>
  134. <?php if ($msg): ?>
  135. <div class="alert alert-<?= $msg_type ?>" id="msg-box"><?= $msg ?></div>
  136. <?php endif; ?>
  137. <div class="admin-card">
  138. <h3>🆕 Neues Spiel hinzufügen</h3>
  139. <form method="POST">
  140. <div style="display: grid; grid-template-columns: 3fr 1fr 1.2fr 1.2fr 2fr; gap: 10px;">
  141. <input type="text" name="new_titel" placeholder="Spieltitel" required>
  142. <input type="text" name="new_ean" placeholder="EAN" required>
  143. <select name="new_typ_id">
  144. <option value="0">-- Typ: Unbekannt --</option>
  145. <?php foreach($typen as $t): ?>
  146. <option value="<?= $t['id'] ?>"><?= htmlspecialchars($t['bezeichnung']) ?></option>
  147. <?php endforeach; ?>
  148. </select>
  149. <select name="new_level">
  150. <option value="Unknown" selected>-- Level: Unbekannt --</option>
  151. <option value="Einsteiger">Einsteiger</option>
  152. <option value="Fortgeschrittene">Fortgeschrittene</option>
  153. <option value="Profi">Profi</option>
  154. </select>
  155. <input type="text" name="new_bild_url" placeholder="Bild-URL">
  156. </div>
  157. <button type="submit" name="add_new_game" class="btn btn-add">In Datenbank speichern</button>
  158. </form>
  159. </div>
  160. <div class="admin-card" style="padding: 0; overflow: hidden;">
  161. <table id="bestand-table">
  162. <thead>
  163. <tr>
  164. <th class="col-img">Bild</th>
  165. <th class="col-titel">Titel</th>
  166. <th class="col-ean">EAN</th>
  167. <th class="col-typ">Typ</th>
  168. <th class="col-lvl">Level</th>
  169. <th class="col-url">Bild-URL</th>
  170. <th class="col-akt">Aktion</th>
  171. </tr>
  172. </thead>
  173. <tbody>
  174. <?php foreach ($spiele as $sp):
  175. $is_unknown = ($sp['level'] == 'Unknown' || empty($sp['level']));
  176. ?>
  177. <tr>
  178. <form method="POST">
  179. <input type="hidden" name="spiel_id" value="<?= $sp['id'] ?>">
  180. <td class="col-img"><img src="<?= htmlspecialchars($sp['bild_url']) ?>" class="img-preview" alt="Cover" onerror="this.src='https://via.placeholder.com/40?text=?';"></td>
  181. <td class="col-titel"><input type="text" name="titel" value="<?= htmlspecialchars($sp['titel']) ?>"></td>
  182. <td class="col-ean"><input type="text" name="ean" value="<?= htmlspecialchars($sp['ean']) ?>"></td>
  183. <td class="col-typ">
  184. <select name="typ_id">
  185. <option value="0">-- Unbekannt --</option>
  186. <?php foreach($typen as $t): ?>
  187. <option value="<?= $t['id'] ?>" <?= ($sp['typ_id']==$t['id'])?'selected':'' ?>><?= htmlspecialchars($t['bezeichnung']) ?></option>
  188. <?php endforeach; ?>
  189. </select>
  190. </td>
  191. <td class="col-lvl">
  192. <select name="level" class="<?= $is_unknown ? 'lvl-unknown' : '' ?>" onchange="this.className=this.value=='Unknown'?'lvl-unknown':''">
  193. <option value="Unknown" <?= $is_unknown ? 'selected' : '' ?>>Unbekannt</option>
  194. <option value="Einsteiger" <?= $sp['level']=='Einsteiger'?'selected':'' ?>>Einsteiger</option>
  195. <option value="Fortgeschrittene" <?= $sp['level']=='Fortgeschrittene'?'selected':'' ?>>Fortgeschrittene</option>
  196. <option value="Profi" <?= $sp['level']=='Profi'?'selected':'' ?>>Profi</option>
  197. </select>
  198. </td>
  199. <td class="col-url"><input type="text" name="bild_url" value="<?= htmlspecialchars($sp['bild_url']) ?>"></td>
  200. <td class="col-akt">
  201. <button type="submit" name="update_game" class="btn btn-save">💾</button>
  202. <button type="submit" name="delete_game" class="btn btn-del" onclick="return confirm('Löschen?')">🗑</button>
  203. </td>
  204. </form>
  205. </tr>
  206. <?php endforeach; ?>
  207. </tbody>
  208. </table>
  209. </div>
  210. </div>
  211. <button id="scrollToTop" onclick="window.scrollTo(0,0);">▲</button>
  212. <script>
  213. // THEME LOGIK
  214. function toggleTheme() {
  215. const isDark = document.documentElement.classList.toggle('dark-theme');
  216. localStorage.setItem('theme', isDark ? 'dark' : 'light');
  217. document.getElementById('theme-icon').innerText = isDark ? '☀️' : '🌙';
  218. }
  219. // Icon beim Laden anpassen
  220. if (localStorage.getItem('theme') === 'dark') {
  221. document.getElementById('theme-icon').innerText = '☀️';
  222. }
  223. // Alerts ausblenden
  224. setTimeout(() => {
  225. const msg = document.getElementById('msg-box');
  226. if(msg) { msg.style.transition = 'opacity 0.5s'; msg.style.opacity = '0'; }
  227. }, 3000);
  228. // Scroll Button
  229. window.onscroll = function() {
  230. document.getElementById("scrollToTop").style.display = (window.scrollY > 300) ? "block" : "none";
  231. };
  232. </script>
  233. </body>
  234. </html>