admin.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. session_start();
  3. require_once 'config.php';
  4. require_once 'db_config.php';
  5. // --- 1. LOGIN / LOGOUT ---
  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. if (!(isset($_SESSION['admin_auth']) && $_SESSION['admin_auth'] === true)):
  11. ?>
  12. <!DOCTYPE html>
  13. <html lang="de">
  14. <head><meta charset="UTF-8"><title>Login</title><style>body{font-family:sans-serif;display:flex;justify-content:center;align-items:center;height:100vh;background:#f0f2f5;} .box{background:white;padding:30px;border-radius:8px;box-shadow:0 2px 10px rgba(0,0,0,0.1); text-align:center;}</style></head>
  15. <body><div class="box"><h2>🔐 Admin Login</h2><form method="POST"><input type="password" name="pw" placeholder="Passwort" autofocus style="width:100%;padding:10px;margin:10px 0;"><button type="submit" name="login_auth" style="width:100%;padding:10px;background:#e67e22;color:white;border:none;cursor:pointer;">Login</button></form></div></body>
  16. </html>
  17. <?php exit; endif; ?>
  18. <?php
  19. // --- HELPER ---
  20. function renderStars($val) {
  21. if (!is_numeric($val)) return htmlspecialchars($val);
  22. $n = (int)$val; $out = "";
  23. for($i=1; $i<=5; $i++) { $out .= ($i <= $n) ? "★" : "☆"; }
  24. return "<span style='color:#f1c40f; font-size:1.1em;'>$out</span>";
  25. }
  26. // --- 2. SQL AKTIONEN ---
  27. $msg = ""; $error = "";
  28. if (isset($_POST['save_game'])) {
  29. $id = (int)($_POST['id'] ?? 0);
  30. $r_id = (int)$_POST['game_reihe_id'];
  31. $t_id = ($_POST['game_typ_id'] == '0') ? null : (int)$_POST['game_typ_id'];
  32. $l_id = ($_POST['game_level_id'] == '0') ? null : (int)$_POST['game_level_id'];
  33. $p_id = (isset($_POST['parent_id']) && $_POST['parent_id'] != '0') ? (int)$_POST['parent_id'] : null;
  34. $eanClean = str_replace(' ', '', (string)($_POST['ean'] ?? ''));
  35. $ean = ($eanClean === '') ? null : $eanClean;
  36. $urlInput = trim($_POST['url']);
  37. try {
  38. $pdo->beginTransaction();
  39. if ($id > 0) {
  40. $stmt = $pdo->prepare("UPDATE spiele SET game_reihe_id=?, titel=?, game_typ_id=?, game_level_id=?, ean=?, parent_id=? WHERE id=?");
  41. $stmt->execute([$r_id, $_POST['titel'], $t_id, $l_id, $ean, $p_id, $id]);
  42. $spiel_id = $id;
  43. } else {
  44. $stmt = $pdo->prepare("INSERT INTO spiele (game_reihe_id, titel, game_typ_id, game_level_id, ean, parent_id) VALUES (?,?,?,?,?,?)");
  45. $stmt->execute([$r_id, $_POST['titel'], $t_id, $l_id, $ean, $p_id]);
  46. $spiel_id = $pdo->lastInsertId();
  47. }
  48. if (!empty($urlInput) && strpos($urlInput, 'http') === 0) {
  49. $ext = pathinfo(parse_url($urlInput, PHP_URL_PATH), PATHINFO_EXTENSION) ?: 'jpg';
  50. $fileName = (!empty($eanClean) ? $eanClean : "game_" . $spiel_id) . "." . $ext;
  51. if ($imgData = @file_get_contents($urlInput)) {
  52. if (file_put_contents(IMG_PATH . $fileName, $imgData)) {
  53. $pdo->prepare("UPDATE spiele SET bild_url=? WHERE id=?")->execute([$fileName, $spiel_id]);
  54. }
  55. }
  56. } elseif (!empty($urlInput)) {
  57. $pdo->prepare("UPDATE spiele SET bild_url=? WHERE id=?")->execute([$urlInput, $spiel_id]);
  58. }
  59. $pdo->commit();
  60. $msg = "Gespeichert!";
  61. } catch (Exception $e) { $pdo->rollBack(); $error = $e->getMessage(); }
  62. }
  63. // STAMMDATEN ADD LOGIK
  64. if (isset($_POST['add_reihe'])) {
  65. $pdo->prepare("INSERT INTO game_reihe (name) VALUES (?)")->execute([$_POST['name']]);
  66. $msg="Reihe hinzugefügt!";
  67. }
  68. if (isset($_POST['add_typ'])) {
  69. $pdo->prepare("INSERT INTO game_typ (game_reihe_id, bezeichnung) VALUES (?,?)")->execute([$_POST['r_id'], $_POST['bez']]);
  70. $msg="Typ hinzugefügt!";
  71. }
  72. if (isset($_POST['add_level'])) {
  73. $pdo->prepare("INSERT INTO game_level (game_reihe_id, bezeichnung) VALUES (?,?)")->execute([$_POST['r_id'], $_POST['bez']]);
  74. $msg="Level hinzugefügt!";
  75. }
  76. if (isset($_POST['add_spieler'])) {
  77. $pdo->prepare("INSERT INTO spieler (name) VALUES (?)")->execute([$_POST['name']]);
  78. $msg="Spieler hinzugefügt!";
  79. }
  80. // LÖSCHEN
  81. if (isset($_GET['del_t'], $_GET['del_id'])) {
  82. if (in_array($_GET['del_t'], ['spiele', 'spieler', 'game_reihe', 'game_typ', 'game_level'])) {
  83. $pdo->prepare("DELETE FROM `".$_GET['del_t']."` WHERE id=?")->execute([(int)$_GET['del_id']]);
  84. header("Location: admin.php"); exit;
  85. }
  86. }
  87. // DATEN LADEN
  88. $reihen = $pdo->query("SELECT * FROM game_reihe ORDER BY name")->fetchAll();
  89. $typen = $pdo->query("SELECT t.*, r.name as r_name FROM game_typ t JOIN game_reihe r ON t.game_reihe_id = r.id ORDER BY r.name, t.bezeichnung")->fetchAll();
  90. $levels = $pdo->query("SELECT l.*, r.name as r_name FROM game_level l JOIN game_reihe r ON l.game_reihe_id = r.id ORDER BY r.name, l.bezeichnung")->fetchAll();
  91. $spieler = $pdo->query("SELECT * FROM spieler ORDER BY name")->fetchAll();
  92. $spiele = $pdo->query("SELECT s.*, r.name as r_name, p.titel as parent_titel
  93. FROM spiele s
  94. LEFT JOIN game_reihe r ON s.game_reihe_id = r.id
  95. LEFT JOIN spiele p ON s.parent_id = p.id
  96. ORDER BY s.id DESC")->fetchAll();
  97. ?>
  98. <!DOCTYPE html>
  99. <html lang="de">
  100. <head>
  101. <meta charset="UTF-8">
  102. <title>Admin Dashboard</title>
  103. <style>
  104. :root { --accent: #e67e22; --bg: #f4f7f6; --card: #fff; --text: #333; --border: #ddd; }
  105. body { font-family: 'Segoe UI', sans-serif; background: var(--bg); color: var(--text); margin: 0; padding: 20px; }
  106. .header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
  107. .nav-tabs { display: flex; gap: 5px; margin-bottom: 20px; border-bottom: 2px solid var(--border); }
  108. .t-btn { padding: 12px 20px; border: none; background: none; color: var(--text); cursor: pointer; font-weight: bold; border-radius: 8px 8px 0 0; }
  109. .t-btn:hover { background: #eee; }
  110. .t-btn.active { background: var(--card); border: 1px solid var(--border); border-bottom: 3px solid var(--accent); color: var(--accent); }
  111. .tab { display: none; background: var(--card); padding: 20px; border-radius: 0 0 12px 12px; border: 1px solid var(--border); border-top: none; }
  112. .tab.active { display: block; }
  113. table { width: 100%; border-collapse: collapse; font-size: 0.82em; }
  114. th, td { padding: 10px; border-bottom: 1px solid var(--border); text-align: left; }
  115. input, select { padding: 6px; border: 1px solid var(--border); border-radius: 4px; background: var(--card); color: var(--text); width: 100%; box-sizing: border-box; }
  116. .btn { padding: 6px 10px; border: none; border-radius: 4px; cursor: pointer; color: white; font-weight: bold; text-decoration: none; display: inline-block; }
  117. .btn-s { background: #2980b9; } .btn-d { background: #c0392b; } .btn-a { background: #27ae60; }
  118. .img-preview { width: 40px; height: 40px; object-fit: cover; border-radius: 4px; border: 1px solid #eee; }
  119. .alert { padding: 10px; border-radius: 8px; margin-bottom: 20px; text-align: center; background: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
  120. .scroll-area { max-height: 350px; overflow-y: auto; border: 1px solid var(--border); border-radius: 6px; padding: 5px; margin-top: 10px; }
  121. .badge { background: #eee; padding: 2px 5px; border-radius: 4px; font-size: 0.75em; color: #666; }
  122. </style>
  123. </head>
  124. <body>
  125. <div class="header">
  126. <h1>🛠 Admin Panel</h1>
  127. <div><a href="index.php" class="btn btn-s">Zum Katalog</a> <a href="?logout=1" class="btn btn-d">Logout</a></div>
  128. </div>
  129. <?php if($msg): ?><div class="alert"><?=$msg?></div><?php endif; ?>
  130. <?php if($error): ?><div class="alert error" style="background:#f8d7da; color:#721c24;"><?=$error?></div><?php endif; ?>
  131. <div class="nav-tabs">
  132. <button class="t-btn active" onclick="openTab(event, 't-spiele')">🎮 Spiele & Abenteuer</button>
  133. <button class="t-btn" onclick="openTab(event, 't-spieler')">👥 Spieler</button>
  134. <button class="t-btn" onclick="openTab(event, 't-config')">⚙️ Stammdaten</button>
  135. </div>
  136. <div id="t-spiele" class="tab active">
  137. <form method="POST" style="display:grid; grid-template-columns: 1fr 1.5fr 1fr 1fr 1fr 0.8fr 1fr auto; gap:8px; margin-bottom:20px; background:#f9f9f9; padding:15px; border-radius:8px; border:1px solid #eee;">
  138. <select name="game_reihe_id" id="r_new" onchange="filter('new')" required>
  139. <option value="">Reihe wählen...</option>
  140. <?php foreach($reihen as $r): ?><option value="<?=$r['id']?>"><?=$r['name']?></option><?php endforeach; ?>
  141. </select>
  142. <input type="text" name="titel" placeholder="Titel des Abenteuers" required>
  143. <select name="parent_id" id="p_new">
  144. <option value="0" data-p="0">Hauptbox (optional)...</option>
  145. <?php foreach($spiele as $ps): if(!$ps['parent_id']): ?>
  146. <option data-p="<?=$ps['game_reihe_id']?>" value="<?=$ps['id']?>"><?=$ps['titel']?></option>
  147. <?php endif; endforeach; ?>
  148. </select>
  149. <select name="game_typ_id" id="t_new"><option value="0" data-p="0">Typ...</option><?php foreach($typen as $t): ?><option data-p="<?=$t['game_reihe_id']?>" value="<?=$t['id']?>"><?=$t['bezeichnung']?></option><?php endforeach; ?></select>
  150. <select name="game_level_id" id="l_new"><option value="0" data-p="0">Level...</option><?php foreach($levels as $l): ?><option data-p="<?=$l['game_reihe_id']?>" value="<?=$l['id']?>"><?=$l['bezeichnung']?></option><?php endforeach; ?></select>
  151. <input type="text" name="ean" placeholder="EAN">
  152. <input type="text" name="url" placeholder="Bild-URL">
  153. <button type="submit" name="save_game" class="btn btn-a">Hinzufügen</button>
  154. </form>
  155. <table>
  156. <thead><tr><th>Bild</th><th>Reihe</th><th>Titel / Parent-Box</th><th>Typ / Level</th><th>EAN / Bildpfad</th><th>Aktion</th></tr></thead>
  157. <?php foreach($spiele as $s): ?>
  158. <tr>
  159. <form method="POST">
  160. <input type="hidden" name="id" value="<?=$s['id']?>">
  161. <td><img src="<?= (strpos($s['bild_url'], 'http') === 0) ? $s['bild_url'] : IMG_URL . $s['bild_url'] ?>" class="img-preview" onerror="this.src='https://via.placeholder.com/45';"></td>
  162. <td><select name="game_reihe_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>
  163. <td>
  164. <input type="text" name="titel" value="<?=htmlspecialchars($s['titel'])?>" style="margin-bottom:3px; font-weight:bold;">
  165. <select name="parent_id" id="p_<?=$s['id']?>" style="font-size:0.85em; color:#e67e22;">
  166. <option value="0" data-p="0">- Eigenständiges Spiel -</option>
  167. <?php foreach($spiele as $ps): if($ps['id'] != $s['id']): ?>
  168. <option data-p="<?=$ps['game_reihe_id']?>" value="<?=$ps['id']?>" <?=$s['parent_id']==$ps['id']?'selected':''?>>📦 Teil von: <?=$ps['titel']?></option>
  169. <?php endif; endforeach; ?>
  170. </select>
  171. </td>
  172. <td>
  173. <select name="game_typ_id" id="t_<?=$s['id']?>" style="margin-bottom:3px;"><option value="0" data-p="0">Typ...</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>
  174. <select name="game_level_id" id="l_<?=$s['id']?>"><option value="0" data-p="0">Lvl...</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>
  175. </td>
  176. <td>
  177. <input type="text" name="ean" value="<?=htmlspecialchars($s['ean'])?>" placeholder="EAN" style="margin-bottom:3px;">
  178. <input type="text" name="url" value="<?=htmlspecialchars($s['bild_url'])?>" placeholder="Bildpfad">
  179. </td>
  180. <td><button type="submit" name="save_game" class="btn btn-s">💾</button> <a href="?del_t=spiele&del_id=<?=$s['id']?>" class="btn btn-d" onclick="return confirm('Löschen?')">🗑</a></td>
  181. </form>
  182. </tr>
  183. <?php endforeach; ?>
  184. </table>
  185. </div>
  186. <div id="t-spieler" class="tab">
  187. <h3>Spieler Profile</h3>
  188. <form method="POST" style="display:flex; gap:10px; margin-bottom:20px;">
  189. <input type="text" name="name" placeholder="Name" required style="width:300px;">
  190. <button type="submit" name="add_spieler" class="btn btn-a">Anlegen</button>
  191. </form>
  192. <table><?php foreach($spieler as $sl): ?><tr><td><?=$sl['name']?></td><td style="text-align:right;"><a href="?del_t=spieler&del_id=<?=$sl['id']?>" class="btn btn-d">Löschen</a></td></tr><?php endforeach; ?></table>
  193. </div>
  194. <div id="t-config" class="tab">
  195. <div style="display:grid; grid-template-columns: 1fr 1fr 1fr; gap:20px;">
  196. <div>
  197. <h4>1. Reihen</h4>
  198. <form method="POST" style="display:flex; gap:5px;"><input type="text" name="name" placeholder="z.B. EXIT" required><button type="submit" name="add_reihe" class="btn btn-a">+</button></form>
  199. <div class="scroll-area"><?php foreach($reihen as $r): ?><div style="padding:8px; border-bottom:1px solid #eee;"><span><?=$r['name']?></span> <a href="?del_t=game_reihe&del_id=<?=$r['id']?>" style="float:right; color:red; text-decoration:none;">✕</a></div><?php endforeach; ?></div>
  200. </div>
  201. <div>
  202. <h4>2. Typen</h4>
  203. <form method="POST">
  204. <select name="r_id" style="margin-bottom:5px;"><?php foreach($reihen as $r): ?><option value="<?=$r['id']?>"><?=$r['name']?></option><?php endforeach; ?></select>
  205. <div style="display:flex; gap:5px;"><input type="text" name="bez" placeholder="z.B. Spiel" required><button type="submit" name="add_typ" class="btn btn-a">+</button></div>
  206. </form>
  207. <div class="scroll-area"><?php foreach($typen as $t): ?><div style="padding:8px; border-bottom:1px solid #eee;"><span><?=$t['bezeichnung']?> <span class="badge"><?=$t['r_name']?></span></span> <a href="?del_t=game_typ&del_id=<?=$t['id']?>" style="float:right; color:red; text-decoration:none;">✕</a></div><?php endforeach; ?></div>
  208. </div>
  209. <div>
  210. <h4>3. Level / Sterne</h4>
  211. <form method="POST">
  212. <select name="r_id" style="margin-bottom:5px;"><?php foreach($reihen as $r): ?><option value="<?=$r['id']?>"><?=$r['name']?></option><?php endforeach; ?></select>
  213. <div style="display:flex; gap:5px;"><input type="text" name="bez" placeholder="z.B. 3" required><button type="submit" name="add_level" class="btn btn-a">+</button></div>
  214. </form>
  215. <div class="scroll-area"><?php foreach($levels as $l): ?><div style="padding:8px; border-bottom:1px solid #eee;"><span><?=renderStars($l['bezeichnung'])?> <span class="badge"><?=$l['r_name']?></span></span> <a href="?del_t=game_level&del_id=<?=$l['id']?>" style="float:right; color:red; text-decoration:none;">✕</a></div><?php endforeach; ?></div>
  216. </div>
  217. </div>
  218. </div>
  219. <script>
  220. function openTab(evt, name) {
  221. document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
  222. document.querySelectorAll('.t-btn').forEach(b => b.classList.remove('active'));
  223. document.getElementById(name).classList.add('active');
  224. evt.currentTarget.classList.add('active');
  225. }
  226. function filter(id) {
  227. let r_el = document.getElementById('r_'+id); if(!r_el) return;
  228. let r_val = r_el.value;
  229. // t = Typ, l = Level, p = Parent/Hauptbox
  230. ['t_'+id, 'l_'+id, 'p_'+id].forEach(sid => {
  231. let sel = document.getElementById(sid); if(!sel) return;
  232. sel.querySelectorAll('option').forEach(o => {
  233. let p = o.getAttribute('data-p');
  234. o.style.display = (p == "0" || p == r_val) ? "block" : "none";
  235. });
  236. if(sel.options[sel.selectedIndex] && sel.options[sel.selectedIndex].style.display == "none") sel.value = "0";
  237. });
  238. }
  239. document.addEventListener('DOMContentLoaded', () => {
  240. filter('new');
  241. <?php foreach($spiele as $s): ?>filter(<?=$s['id']?>);<?php endforeach; ?>
  242. });
  243. </script>
  244. </body>
  245. </html>