admin.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. if (isset($_POST['login_auth'])) {
  8. if ($_POST['pw'] === ADMIN_PASSWORD) { $_SESSION['admin_auth'] = true; }
  9. }
  10. $is_admin = (isset($_SESSION['admin_auth']) && $_SESSION['admin_auth'] === true);
  11. if (!$is_admin):
  12. ?>
  13. <!DOCTYPE html>
  14. <html lang="de">
  15. <head>
  16. <meta charset="UTF-8">
  17. <title>Admin Login</title>
  18. <style>
  19. body { font-family: sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; background: #f4f7f6; margin: 0; }
  20. .login-box { background: white; padding: 30px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); text-align: center; }
  21. input { width: 100%; padding: 10px; margin: 10px 0; box-sizing: border-box; border: 1px solid #ddd; border-radius: 4px; }
  22. button { width: 100%; padding: 10px; background: #e67e22; color: white; border: none; cursor: pointer; border-radius: 4px; font-weight: bold; }
  23. </style>
  24. </head>
  25. <body>
  26. <div class="login-box">
  27. <h2>🔒 Admin Login</h2>
  28. <form method="POST"><input type="password" name="pw" placeholder="Passwort" autofocus required><button type="submit" name="login_auth">Einloggen</button></form>
  29. </div>
  30. </body>
  31. </html>
  32. <?php exit; endif; ?>
  33. <?php
  34. // --- 2. VERARBEITUNG DER FORMULARE ---
  35. $msg = "";
  36. // SPIELE (Add/Update/Delete)
  37. if (isset($_POST['add_game'])) {
  38. $stmt = $pdo->prepare("INSERT INTO spiele (game_reihe_id, titel, game_typ_id, game_level_id, ean, bild_url) VALUES (?,?,?,?,?,?)");
  39. $stmt->execute([(int)$_POST['r_id'], $_POST['titel'], (int)$_POST['t_id'], (int)$_POST['l_id'], $_POST['ean'], $_POST['url']]);
  40. $msg = "✅ Spiel hinzugefügt!";
  41. }
  42. if (isset($_POST['upd_game'])) {
  43. $stmt = $pdo->prepare("UPDATE spiele SET game_reihe_id=?, titel=?, game_typ_id=?, game_level_id=?, ean=?, bild_url=? WHERE id=?");
  44. $stmt->execute([(int)$_POST['r_id'], $_POST['titel'], (int)$_POST['t_id'], (int)$_POST['l_id'], $_POST['ean'], $_POST['url'], (int)$_POST['id']]);
  45. $msg = "💾 Änderungen gespeichert!";
  46. }
  47. // SPIELER
  48. if (isset($_POST['add_spieler'])) {
  49. $pdo->prepare("INSERT INTO spieler (name) VALUES (?)")->execute([$_POST['name']]);
  50. }
  51. // REIHEN / TYPEN / LEVEL
  52. if (isset($_POST['add_reihe'])) { $pdo->prepare("INSERT INTO game_reihe (name) VALUES (?)")->execute([$_POST['name']]); }
  53. if (isset($_POST['add_typ'])) { $pdo->prepare("INSERT INTO game_typ (game_reihe_id, bezeichnung) VALUES (?,?)")->execute([$_POST['r_id'], $_POST['bez']]); }
  54. if (isset($_POST['add_level'])) { $pdo->prepare("INSERT INTO game_level (game_reihe_id, bezeichnung) VALUES (?,?)")->execute([$_POST['r_id'], $_POST['bez']]); }
  55. // LÖSCHEN (Universal)
  56. if (isset($_GET['del_table']) && isset($_GET['del_id'])) {
  57. $allowed = ['spiele', 'spieler', 'game_reihe', 'game_typ', 'game_level'];
  58. if (in_array($_GET['del_table'], $allowed)) {
  59. $stmt = $pdo->prepare("DELETE FROM `".$_GET['del_table']."` WHERE id = ?");
  60. $stmt->execute([(int)$_GET['del_id']]);
  61. header("Location: admin.php"); exit;
  62. }
  63. }
  64. // --- 3. DATEN LADEN ---
  65. $reihen = $pdo->query("SELECT * FROM game_reihe ORDER BY name")->fetchAll();
  66. $typen = $pdo->query("SELECT * FROM game_typ ORDER BY bezeichnung")->fetchAll();
  67. $levels = $pdo->query("SELECT * FROM game_level ORDER BY bezeichnung")->fetchAll();
  68. $spieler = $pdo->query("SELECT * FROM spieler ORDER BY name")->fetchAll();
  69. $spiele = $pdo->query("SELECT s.*, r.name as r_name FROM spiele s LEFT JOIN game_reihe r ON s.game_reihe_id = r.id ORDER BY s.id DESC")->fetchAll();
  70. ?>
  71. <!DOCTYPE html>
  72. <html lang="de">
  73. <head>
  74. <meta charset="UTF-8">
  75. <title>Admin Dashboard</title>
  76. <style>
  77. :root { --accent: #e67e22; --bg: #f4f7f6; --card: #fff; --text: #333; --border: #ddd; }
  78. .dark-theme { --bg: #1a1a1a; --card: #2d2d2d; --text: #eee; --border: #444; }
  79. body { font-family: 'Segoe UI', sans-serif; background: var(--bg); color: var(--text); margin: 0; padding: 20px; transition: 0.3s; }
  80. .container { max-width: 1400px; margin: 0 auto; }
  81. /* Header & Buttons */
  82. .header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
  83. .header-btns { display: flex; gap: 10px; align-items: center; }
  84. .btn { padding: 8px 15px; border: none; border-radius: 6px; cursor: pointer; color: white; text-decoration: none; font-weight: bold; font-size: 0.9em; transition: 0.2s; }
  85. .btn-s { background: #2980b9; } .btn-s:hover { background: #3498db; }
  86. .btn-d { background: #c0392b; } .btn-d:hover { background: #e74c3c; }
  87. .btn-a { background: #27ae60; } .btn-a:hover { background: #2ecc71; }
  88. .btn-nav { background: var(--accent); }
  89. #theme-icon { background: none; border: none; font-size: 1.4rem; cursor: pointer; padding: 5px; }
  90. /* Tabs */
  91. .nav-tabs { display: flex; gap: 5px; margin-bottom: 20px; border-bottom: 2px solid var(--border); padding-bottom: 10px; }
  92. .t-btn { padding: 10px 20px; border: none; background: transparent; color: var(--text); cursor: pointer; font-weight: bold; border-radius: 6px; }
  93. .t-btn.active { background: var(--accent); color: white; }
  94. .tab { display: none; background: var(--card); padding: 25px; border-radius: 12px; box-shadow: 0 4px 15px rgba(0,0,0,0.05); border: 1px solid var(--border); }
  95. .tab.active { display: block; }
  96. /* Table & Forms */
  97. table { width: 100%; border-collapse: collapse; margin-top: 15px; }
  98. th { text-align: left; padding: 12px; border-bottom: 2px solid var(--border); background: rgba(0,0,0,0.02); }
  99. td { padding: 10px; border-bottom: 1px solid var(--border); }
  100. input, select { padding: 9px; border: 1px solid var(--border); border-radius: 6px; background: var(--card); color: var(--text); width: 100%; box-sizing: border-box; }
  101. .alert { padding: 15px; background: #d4edda; color: #155724; border-radius: 8px; margin-bottom: 20px; text-align: center; font-weight: bold; }
  102. </style>
  103. </head>
  104. <body>
  105. <div class="container">
  106. <div class="header">
  107. <h1>🛠 Admin Dashboard</h1>
  108. <div class="header-btns">
  109. <button onclick="toggleTheme()" id="theme-icon">🌙</button>
  110. <a href="index.php" class="btn btn-nav">Katalog</a>
  111. <a href="admin.php?logout=1" class="btn btn-d">Logout</a>
  112. </div>
  113. </div>
  114. <?php if($msg): ?><div class="alert" id="msgbox"><?=$msg?></div><?php endif; ?>
  115. <div class="nav-tabs">
  116. <button class="t-btn active" onclick="openTab(event, 't-spiele')">🎮 Spiele</button>
  117. <button class="t-btn" onclick="openTab(event, 't-spieler')">👥 Spieler</button>
  118. <button class="t-btn" onclick="openTab(event, 't-config')">⚙️ Stammdaten</button>
  119. </div>
  120. <div id="t-spiele" class="tab active">
  121. <h3>Neues Spiel hinzufügen</h3>
  122. <form method="POST" style="display:grid; grid-template-columns: 1fr 2fr 1fr 1fr 1fr 2fr auto; gap:10px; align-items: end;">
  123. <div><label>Reihe</label><select name="r_id" id="r_new" onchange="filter('new')" required><option value="">Wählen...</option><?php foreach($reihen as $r): ?><option value="<?=$r['id']?>"><?=$r['name']?></option><?php endforeach; ?></select></div>
  124. <div><label>Titel</label><input type="text" name="titel" required></div>
  125. <div><label>Typ</label><select name="t_id" id="t_new"><option value="0" data-p="0">--</option><?php foreach($typen as $t): ?><option data-p="<?=$t['game_reihe_id']?>" value="<?=$t['id']?>"><?=$t['bezeichnung']?></option><?php endforeach; ?></select></div>
  126. <div><label>Level</label><select name="l_id" id="l_new"><option value="0" data-p="0">--</option><?php foreach($levels as $l): ?><option data-p="<?=$l['game_reihe_id']?>" value="<?=$l['id']?>"><?=$l['bezeichnung']?></option><?php endforeach; ?></select></div>
  127. <div><label>EAN</label><input type="text" name="ean"></div>
  128. <div><label>Bild URL</label><input type="text" name="url"></div>
  129. <button type="submit" name="add_game" class="btn btn-a">Hinzufügen</button>
  130. </form>
  131. <hr style="margin: 30px 0; border: 0; border-top: 1px solid var(--border);">
  132. <table>
  133. <thead><tr><th>Reihe</th><th>Titel</th><th>Typ</th><th>Level</th><th style="width:100px;">Aktion</th></tr></thead>
  134. <?php foreach($spiele as $s): ?>
  135. <form method="POST">
  136. <input type="hidden" name="id" value="<?=$s['id']?>">
  137. <tr>
  138. <td><select name="r_id" id="r_<?=$s['id']?>" onchange="filter(<?=$s['id']?>)"><?php foreach($reihen as $r): ?><option value="<?=$r['id']?>" <?=$s['game_reihe_id']==$r['id']?'selected':''?>><?=$r['name']?></option><?php endforeach; ?></select></td>
  139. <td><input type="text" name="titel" value="<?=htmlspecialchars($s['titel'])?>"></td>
  140. <td><select name="t_id" id="t_<?=$s['id']?>"><option value="0" data-p="0">--</option><?php foreach($typen as $t): ?><option data-p="<?=$t['game_reihe_id']?>" value="<?=$t['id']?>" <?=$s['game_typ_id']==$t['id']?'selected':''?>><?=$t['bezeichnung']?></option><?php endforeach; ?></select></td>
  141. <td><select name="l_id" id="l_<?=$s['id']?>"><option value="0" data-p="0">--</option><?php foreach($levels as $l): ?><option data-p="<?=$l['game_reihe_id']?>" value="<?=$l['id']?>" <?=$s['game_level_id']==$l['id']?'selected':''?>><?=$l['bezeichnung']?></option><?php endforeach; ?></select></td>
  142. <td>
  143. <div style="display:flex; gap:5px;">
  144. <button type="submit" name="upd_game" class="btn btn-s">💾</button>
  145. <a href="?del_table=spiele&del_id=<?=$s['id']?>" class="btn btn-d" onclick="return confirm('Löschen?')">🗑</a>
  146. </div>
  147. </td>
  148. </tr>
  149. </form>
  150. <?php endforeach; ?>
  151. </table>
  152. </div>
  153. <div id="t-spieler" class="tab">
  154. <h3>Spieler Profile</h3>
  155. <form method="POST" style="display:flex; gap:10px; margin-bottom:20px;"><input type="text" name="name" placeholder="Name" required style="max-width:300px;"> <button type="submit" name="add_spieler" class="btn btn-a">Spieler anlegen</button></form>
  156. <table>
  157. <?php foreach($spieler as $sl): ?>
  158. <tr><td><?=$sl['name']?></td><td style="text-align:right;"><a href="?del_table=spieler&del_id=<?=$sl['id']?>" class="btn btn-d">Löschen</a></td></tr>
  159. <?php endforeach; ?>
  160. </table>
  161. </div>
  162. <div id="t-config" class="tab">
  163. <div style="display:grid; grid-template-columns: 1fr 1fr 1fr; gap:30px;">
  164. <div>
  165. <h4>Reihen</h4>
  166. <form method="POST" style="display:flex; gap:5px; margin-bottom:15px;"><input type="text" name="name" placeholder="Neu..."><button type="submit" name="add_reihe" class="btn btn-a">+</button></form>
  167. <div style="max-height:400px; overflow-y:auto;">
  168. <?php foreach($reihen as $r): ?> <div style="display:flex; justify-content:space-between; padding:5px; border-bottom:1px solid var(--border);"><span><?=$r['name']?></span> <a href="?del_table=game_reihe&del_id=<?=$r['id']?>" style="color:red; text-decoration:none;">✕</a></div> <?php endforeach; ?>
  169. </div>
  170. </div>
  171. <div>
  172. <h4>Typen (pro Reihe)</h4>
  173. <form method="POST">
  174. <select name="r_id" style="margin-bottom:5px;"><?php foreach($reihen as $r): ?><option value="<?=$r['id']?>"><?=$r['name']?></option><?php endforeach; ?></select>
  175. <div style="display:flex; gap:5px;"><input type="text" name="bez" placeholder="Neu..."><button type="submit" name="add_typ" class="btn btn-a">+</button></div>
  176. </form>
  177. <div style="margin-top:15px; font-size:0.85em;">
  178. <?php foreach($typen as $t): ?> <div style="padding:3px; border-bottom:1px solid #eee;"><?=$t['bezeichnung']?> <small>(ID: <?=$t['game_reihe_id']?>)</small> <a href="?del_table=game_typ&del_id=<?=$t['id']?>" style="color:red; float:right;">✕</a></div> <?php endforeach; ?>
  179. </div>
  180. </div>
  181. <div>
  182. <h4>Level (pro Reihe)</h4>
  183. <form method="POST">
  184. <select name="r_id" style="margin-bottom:5px;"><?php foreach($reihen as $r): ?><option value="<?=$r['id']?>"><?=$r['name']?></option><?php endforeach; ?></select>
  185. <div style="display:flex; gap:5px;"><input type="text" name="bez" placeholder="Neu..."><button type="submit" name="add_level" class="btn btn-a">+</button></div>
  186. </form>
  187. <div style="margin-top:15px; font-size:0.85em;">
  188. <?php foreach($levels as $l): ?> <div style="padding:3px; border-bottom:1px solid #eee;"><?=$l['bezeichnung']?> <small>(ID: <?=$l['game_reihe_id']?>)</small> <a href="?del_table=game_level&del_id=<?=$l['id']?>" style="color:red; float:right;">✕</a></div> <?php endforeach; ?>
  189. </div>
  190. </div>
  191. </div>
  192. </div>
  193. </div>
  194. <script>
  195. function openTab(evt, tabName) {
  196. document.querySelectorAll(".tab").forEach(t => t.classList.remove("active"));
  197. document.querySelectorAll(".t-btn").forEach(b => b.classList.remove("active"));
  198. document.getElementById(tabName).classList.add("active");
  199. evt.currentTarget.classList.add("active");
  200. }
  201. function toggleTheme() {
  202. const isDark = document.documentElement.classList.toggle('dark-theme');
  203. localStorage.setItem('theme', isDark ? 'dark' : 'light');
  204. document.getElementById('theme-icon').innerText = isDark ? '☀️' : '🌙';
  205. }
  206. function filter(id) {
  207. let reiheSelect = document.getElementById('r_'+id);
  208. if(!reiheSelect) return;
  209. let val = reiheSelect.value;
  210. ['t_'+id, 'l_'+id].forEach(sid => {
  211. let s = document.getElementById(sid);
  212. if(!s) return;
  213. s.querySelectorAll('option').forEach(o => {
  214. let p = o.getAttribute('data-p');
  215. o.style.display = (p == "0" || p == val) ? "block" : "none";
  216. });
  217. if(s.options[s.selectedIndex].style.display == "none") s.value = "0";
  218. });
  219. }
  220. document.addEventListener('DOMContentLoaded', () => {
  221. if (localStorage.getItem('theme') === 'dark') {
  222. document.documentElement.classList.add('dark-theme');
  223. document.getElementById('theme-icon').innerText = '☀️';
  224. }
  225. filter('new');
  226. <?php foreach($spiele as $s): ?> filter(<?=$s['id']?>); <?php endforeach; ?>
  227. setTimeout(() => { if(document.getElementById('msgbox')) document.getElementById('msgbox').style.display='none'; }, 3000);
  228. });
  229. </script>
  230. </body>
  231. </html>