attributes.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <?php
  2. // Αρχείο: /var/www/html/hockey-cms/admin/attributes.php
  3. require_once 'admin_auth.php';
  4. require_once '../config/database.php';
  5. // --- Μηχανισμός Αλλαγής Γλώσσας ---
  6. if (isset($_GET['lang']) && in_array($_GET['lang'], ['el', 'en'])) {
  7. $_SESSION['lang'] = $_GET['lang'];
  8. }
  9. $current_lang = $_SESSION['lang'] ?? 'el';
  10. $translations = [
  11. 'el' => [
  12. 'page_title' => 'Χαρακτηριστικά - Hockey CMS',
  13. 'header_title' => 'Διαχείριση Χαρακτηριστικών Εξοπλισμού',
  14. 'edit_group' => '✏️ Επεξεργασία Ομάδας',
  15. 'new_group' => '📁 Νέα Ομάδα (Group)',
  16. 'name_gr' => 'Όνομα 🇬🇷 (GR) *',
  17. 'name_en' => 'Όνομα 🇬🇧 (EN)',
  18. 'btn_save_group' => 'Αποθήκευση Ομάδας',
  19. 'btn_add_group' => 'Προσθήκη Ομάδας',
  20. 'edit_attr' => '✏️ Επεξεργασία Χαρακτηριστικού',
  21. 'new_attr' => '⚡ Νέο Χαρακτηριστικό',
  22. 'belongs_group' => 'Ανήκει στην Ομάδα *',
  23. 'select_group' => '-- Επίλεξε Ομάδα --',
  24. 'unit' => 'Μονάδα Μέτρησης (Προαιρετικό)',
  25. 'btn_save_attr' => 'Αποθήκευση Χαρακ.',
  26. 'btn_add_attr' => 'Προσθήκη Χαρακτηριστικού',
  27. 'existing_groups' => '📁 Υπάρχουσες Ομάδες',
  28. 'group_name' => 'Όνομα Ομάδας',
  29. 'actions' => 'Ενέργειες',
  30. 'no_groups' => 'Δεν υπάρχουν ομάδες.',
  31. 'existing_attrs' => '⚡ Υπάρχοντα Χαρακτηριστικά',
  32. 'attr_name' => 'Χαρακτηριστικό',
  33. 'no_attrs' => 'Δεν υπάρχουν χαρακτηριστικά.',
  34. 'btn_cancel' => 'Ακύρωση',
  35. 'msg_group_ok' => 'Η ομάδα δημιουργήθηκε!',
  36. 'msg_group_upd' => 'Η ομάδα ενημερώθηκε!',
  37. 'msg_attr_ok' => 'Το χαρακτηριστικό προστέθηκε!',
  38. 'msg_attr_upd' => 'Το χαρακτηριστικό ενημερώθηκε!',
  39. 'msg_group_del' => 'Η ομάδα διαγράφηκε επιτυχώς!',
  40. 'msg_attr_del' => 'Το χαρακτηριστικό διαγράφηκε επιτυχώς!',
  41. 'confirm_del_grp' => 'Σίγουρα διαγραφή της ομάδας;',
  42. 'confirm_del_attr' => 'Σίγουρα διαγραφή του χαρακτηριστικού;'
  43. ],
  44. 'en' => [
  45. 'page_title' => 'Attributes - Hockey CMS',
  46. 'header_title' => 'Equipment Attributes Management',
  47. 'edit_group' => '✏️ Edit Group',
  48. 'new_group' => '📁 New Group',
  49. 'name_gr' => 'Name 🇬🇷 (GR) *',
  50. 'name_en' => 'Name 🇬🇧 (EN)',
  51. 'btn_save_group' => 'Save Group',
  52. 'btn_add_group' => 'Add Group',
  53. 'edit_attr' => '✏️ Edit Attribute',
  54. 'new_attr' => '⚡ New Attribute',
  55. 'belongs_group' => 'Belongs to Group *',
  56. 'select_group' => '-- Select Group --',
  57. 'unit' => 'Unit of Measurement (Optional)',
  58. 'btn_save_attr' => 'Save Attr.',
  59. 'btn_add_attr' => 'Add Attribute',
  60. 'existing_groups' => '📁 Existing Groups',
  61. 'group_name' => 'Group Name',
  62. 'actions' => 'Actions',
  63. 'no_groups' => 'No groups found.',
  64. 'existing_attrs' => '⚡ Existing Attributes',
  65. 'attr_name' => 'Attribute',
  66. 'no_attrs' => 'No attributes found.',
  67. 'btn_cancel' => 'Cancel',
  68. 'msg_group_ok' => 'Group created!',
  69. 'msg_group_upd' => 'Group updated!',
  70. 'msg_attr_ok' => 'Attribute added!',
  71. 'msg_attr_upd' => 'Attribute updated!',
  72. 'msg_group_del' => 'Group deleted successfully!',
  73. 'msg_attr_del' => 'Attribute deleted successfully!',
  74. 'confirm_del_grp' => 'Are you sure you want to delete this group?',
  75. 'confirm_del_attr' => 'Are you sure you want to delete this attribute?'
  76. ]
  77. ];
  78. $t = $translations[$current_lang];
  79. header('Content-Type: text/html; charset=utf-8');
  80. $message = '';
  81. // ==========================================
  82. // 1. ΧΕΙΡΙΣΜΟΣ POST (Προσθήκη / Επεξεργασία)
  83. // ==========================================
  84. if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
  85. $action = $_POST['action'];
  86. if ($action === 'add_group' || $action === 'edit_group') {
  87. $name = trim($_POST['name']);
  88. $name_en = trim($_POST['name_en']);
  89. if (!empty($name)) {
  90. try {
  91. if ($action === 'add_group') {
  92. $stmt = $pdo->prepare("INSERT INTO attribute_groups (name, name_en) VALUES (?, ?)");
  93. $stmt->execute([$name, $name_en]);
  94. $message = "<div class='alert success'>{$t['msg_group_ok']}</div>";
  95. } else {
  96. $group_id = (int)$_POST['group_id'];
  97. $stmt = $pdo->prepare("UPDATE attribute_groups SET name = ?, name_en = ? WHERE id = ?");
  98. $stmt->execute([$name, $name_en, $group_id]);
  99. $message = "<div class='alert success'>{$t['msg_group_upd']}</div>";
  100. }
  101. } catch (Exception $e) {
  102. $message = "<div class='alert error'>Σφάλμα: " . $e->getMessage() . "</div>";
  103. }
  104. }
  105. }
  106. if ($action === 'add_attr' || $action === 'edit_attr') {
  107. $group_id = (int)$_POST['group_id'];
  108. $name = trim($_POST['name']);
  109. $name_en = trim($_POST['name_en']);
  110. $unit = trim($_POST['unit']);
  111. if (!empty($group_id) && !empty($name)) {
  112. try {
  113. if ($action === 'add_attr') {
  114. $stmt = $pdo->prepare("INSERT INTO attributes (group_id, name, name_en, unit) VALUES (?, ?, ?, ?)");
  115. $stmt->execute([$group_id, $name, $name_en, $unit]);
  116. $message = "<div class='alert success'>{$t['msg_attr_ok']}</div>";
  117. } else {
  118. $attr_id = (int)$_POST['attr_id'];
  119. $stmt = $pdo->prepare("UPDATE attributes SET group_id = ?, name = ?, name_en = ?, unit = ? WHERE id = ?");
  120. $stmt->execute([$group_id, $name, $name_en, $unit, $attr_id]);
  121. $message = "<div class='alert success'>{$t['msg_attr_upd']}</div>";
  122. }
  123. } catch (Exception $e) {
  124. $message = "<div class='alert error'>Σφάλμα: " . $e->getMessage() . "</div>";
  125. }
  126. }
  127. }
  128. }
  129. // ==========================================
  130. // 2. ΧΕΙΡΙΣΜΟΣ GET (Διαγραφές)
  131. // ==========================================
  132. if (isset($_GET['delete_group'])) {
  133. $del_id = (int)$_GET['delete_group'];
  134. $checkAttrs = $pdo->prepare("SELECT COUNT(*) FROM attributes WHERE group_id = ?");
  135. $checkAttrs->execute([$del_id]);
  136. $checkCats = $pdo->prepare("SELECT COUNT(*) FROM categories WHERE attribute_group_id = ?");
  137. $checkCats->execute([$del_id]);
  138. if ($checkAttrs->fetchColumn() > 0) {
  139. $message = "<div class='alert error'>Αδυναμία διαγραφής! Υπάρχουν χαρακτηριστικά μέσα σε αυτή την ομάδα.</div>";
  140. } elseif ($checkCats->fetchColumn() > 0) {
  141. $message = "<div class='alert error'>Αδυναμία διαγραφής! Η ομάδα συνδέεται με Κατηγορία.</div>";
  142. } else {
  143. $pdo->prepare("DELETE FROM attribute_groups WHERE id = ?")->execute([$del_id]);
  144. $message = "<div class='alert success'>{$t['msg_group_del']}</div>";
  145. }
  146. }
  147. if (isset($_GET['delete_attr'])) {
  148. $del_id = (int)$_GET['delete_attr'];
  149. $checkVals = $pdo->prepare("SELECT COUNT(*) FROM product_attribute_values WHERE attribute_id = ?");
  150. $checkVals->execute([$del_id]);
  151. if ($checkVals->fetchColumn() > 0) {
  152. $message = "<div class='alert error'>Αδυναμία διαγραφής! Το χαρακτηριστικό χρησιμοποιείται σε προϊόντα.</div>";
  153. } else {
  154. $pdo->prepare("DELETE FROM attributes WHERE id = ?")->execute([$del_id]);
  155. $message = "<div class='alert success'>{$t['msg_attr_del']}</div>";
  156. }
  157. }
  158. $edit_group = null;
  159. if (isset($_GET['edit_group'])) {
  160. $stmt = $pdo->prepare("SELECT * FROM attribute_groups WHERE id = ?");
  161. $stmt->execute([(int)$_GET['edit_group']]);
  162. $edit_group = $stmt->fetch(PDO::FETCH_ASSOC);
  163. }
  164. $edit_attr = null;
  165. if (isset($_GET['edit_attr'])) {
  166. $stmt = $pdo->prepare("SELECT * FROM attributes WHERE id = ?");
  167. $stmt->execute([(int)$_GET['edit_attr']]);
  168. $edit_attr = $stmt->fetch(PDO::FETCH_ASSOC);
  169. }
  170. $groups = $pdo->query("SELECT * FROM attribute_groups ORDER BY name")->fetchAll(PDO::FETCH_ASSOC);
  171. $attributes = $pdo->query("
  172. SELECT a.id, a.name, a.name_en, a.unit, ag.name as group_name
  173. FROM attributes a
  174. JOIN attribute_groups ag ON a.group_id = ag.id
  175. ORDER BY ag.name, a.name
  176. ")->fetchAll(PDO::FETCH_ASSOC);
  177. ?>
  178. <!DOCTYPE html>
  179. <html lang="<?php echo $current_lang; ?>">
  180. <head>
  181. <meta charset="UTF-8">
  182. <title><?php echo $t['page_title']; ?></title>
  183. <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
  184. <style>
  185. :root {
  186. --primary: #f97316;
  187. --primary-hover: #ea580c;
  188. --bg-color: #0b1120;
  189. --panel-bg: #1e293b;
  190. --border-color: #334155;
  191. --text-main: #f8fafc;
  192. --text-muted: #94a3b8;
  193. }
  194. body { font-family: 'Inter', sans-serif; background-color: var(--bg-color); color: var(--text-main); margin: 0; }
  195. .content { margin-left: 250px; padding: 30px; }
  196. .content h2 { margin-top: 0; color: var(--primary); font-size: 26px; text-transform: uppercase; letter-spacing: 0.5px; }
  197. .card { background: var(--panel-bg); padding: 25px; border-radius: 8px; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.3); border: 1px solid var(--border-color); margin-bottom: 25px; }
  198. .card h3 { margin-top: 0; color: var(--text-main); border-bottom: 1px solid rgba(255,255,255,0.05); padding-bottom: 15px; font-size: 18px; text-transform: uppercase; letter-spacing: 0.5px; }
  199. .form-group { margin-bottom: 20px; }
  200. .form-group label { display: block; margin-bottom: 8px; font-weight: 700; font-size: 13px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px;}
  201. .form-group input, .form-group select { width: 100%; padding: 12px; background: #0f172a; border: 1px solid var(--border-color); color: var(--text-main); border-radius: 6px; box-sizing: border-box; font-family: 'Inter', sans-serif; outline: none; transition: 0.2s; }
  202. .form-group input:focus, .form-group select:focus { border-color: var(--primary); box-shadow: 0 0 0 2px rgba(249, 115, 22, 0.2); }
  203. .btn { padding: 12px 20px; background: var(--primary); color: white; border: none; border-radius: 6px; cursor: pointer; font-weight: 800; font-size: 14px; text-transform: uppercase; transition: 0.2s; }
  204. .btn:hover { background: var(--primary-hover); transform: translateY(-1px); }
  205. .btn-cancel { padding: 12px 20px; background: rgba(255,255,255,0.05); color: var(--text-main); text-decoration: none; border-radius: 6px; display: inline-block; font-weight: 700; border: 1px solid var(--border-color); text-transform: uppercase; transition: 0.2s;}
  206. .btn-cancel:hover { background: rgba(255,255,255,0.1); }
  207. .grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 25px; }
  208. table { width: 100%; border-collapse: collapse; margin-top: 10px; }
  209. table th, table td { padding: 15px; border-bottom: 1px solid rgba(255,255,255,0.05); text-align: left; vertical-align: middle; }
  210. table th { background: rgba(0,0,0,0.2); color: var(--text-muted); text-transform: uppercase; font-size: 13px; font-weight: 700; }
  211. table td { color: var(--text-main); font-size: 14px; }
  212. .alert { padding: 15px; border-radius: 6px; margin-bottom: 20px; font-weight: 600; font-size: 14px;}
  213. .success { background: rgba(16, 185, 129, 0.1); color: #10b981; border: 1px solid rgba(16, 185, 129, 0.2); }
  214. .error { background: rgba(239, 68, 68, 0.1); color: #ef4444; border: 1px solid rgba(239, 68, 68, 0.2); }
  215. .action-link { padding: 6px 12px; text-decoration: none; border-radius: 4px; color: #fff; font-size: 12px; margin-right: 5px; display: inline-block; font-weight: 700; text-transform: uppercase; transition: 0.2s;}
  216. .action-edit { background-color: rgba(249, 115, 22, 0.1); color: var(--primary); border: 1px solid rgba(249, 115, 22, 0.2); }
  217. .action-edit:hover { background-color: var(--primary); color: white; }
  218. .action-delete { background-color: rgba(239, 68, 68, 0.1); color: #ef4444; border: 1px solid rgba(239, 68, 68, 0.2); }
  219. .action-delete:hover { background-color: #ef4444; color: white; }
  220. .lang-hint { font-size: 11px; color: var(--text-muted); }
  221. </style>
  222. </head>
  223. <body>
  224. <?php include 'sidebar.php'; ?>
  225. <div class="content">
  226. <h2><?php echo $t['header_title']; ?></h2>
  227. <?php echo $message; ?>
  228. <div class="grid-2">
  229. <div class="card">
  230. <h3><?php echo $edit_group ? $t['edit_group'] : $t['new_group']; ?></h3>
  231. <form method="POST" action="attributes.php">
  232. <input type="hidden" name="action" value="<?php echo $edit_group ? 'edit_group' : 'add_group'; ?>">
  233. <?php if ($edit_group): ?>
  234. <input type="hidden" name="group_id" value="<?php echo $edit_group['id']; ?>">
  235. <?php endif; ?>
  236. <div class="form-group">
  237. <label><?php echo $t['name_gr']; ?></label>
  238. <input type="text" name="name" value="<?php echo $edit_group ? htmlspecialchars($edit_group['name']) : ''; ?>" required>
  239. </div>
  240. <div class="form-group">
  241. <label><?php echo $t['name_en']; ?></label>
  242. <input type="text" name="name_en" value="<?php echo $edit_group ? htmlspecialchars($edit_group['name_en'] ?? '') : ''; ?>">
  243. </div>
  244. <button type="submit" class="btn"><?php echo $edit_group ? $t['btn_save_group'] : $t['btn_add_group']; ?></button>
  245. <?php if ($edit_group): ?>
  246. <a href="attributes.php" class="btn-cancel"><?php echo $t['btn_cancel']; ?></a>
  247. <?php endif; ?>
  248. </form>
  249. </div>
  250. <div class="card">
  251. <h3><?php echo $edit_attr ? $t['edit_attr'] : $t['new_attr']; ?></h3>
  252. <form method="POST" action="attributes.php">
  253. <input type="hidden" name="action" value="<?php echo $edit_attr ? 'edit_attr' : 'add_attr'; ?>">
  254. <?php if ($edit_attr): ?>
  255. <input type="hidden" name="attr_id" value="<?php echo $edit_attr['id']; ?>">
  256. <?php endif; ?>
  257. <div class="form-group">
  258. <label><?php echo $t['belongs_group']; ?></label>
  259. <select name="group_id" required>
  260. <option value=""><?php echo $t['select_group']; ?></option>
  261. <?php foreach($groups as $group): ?>
  262. <option value="<?php echo $group['id']; ?>" <?php echo ($edit_attr && $edit_attr['group_id'] == $group['id']) ? 'selected' : ''; ?>>
  263. <?php echo htmlspecialchars($group['name']); ?>
  264. </option>
  265. <?php endforeach; ?>
  266. </select>
  267. </div>
  268. <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 15px;">
  269. <div class="form-group">
  270. <label><?php echo $t['name_gr']; ?></label>
  271. <input type="text" name="name" value="<?php echo $edit_attr ? htmlspecialchars($edit_attr['name']) : ''; ?>" required>
  272. </div>
  273. <div class="form-group">
  274. <label><?php echo $t['name_en']; ?></label>
  275. <input type="text" name="name_en" value="<?php echo $edit_attr ? htmlspecialchars($edit_attr['name_en'] ?? '') : ''; ?>">
  276. </div>
  277. </div>
  278. <div class="form-group">
  279. <label><?php echo $t['unit']; ?></label>
  280. <input type="text" name="unit" value="<?php echo $edit_attr ? htmlspecialchars($edit_attr['unit'] ?? '') : ''; ?>" placeholder="cm, kg, mm">
  281. </div>
  282. <button type="submit" class="btn" style="background: #10b981;"><?php echo $edit_attr ? $t['btn_save_attr'] : $t['btn_add_attr']; ?></button>
  283. <?php if ($edit_attr): ?>
  284. <a href="attributes.php" class="btn-cancel"><?php echo $t['btn_cancel']; ?></a>
  285. <?php endif; ?>
  286. </form>
  287. </div>
  288. </div>
  289. <div class="grid-2">
  290. <div class="card">
  291. <h3><?php echo $t['existing_groups']; ?></h3>
  292. <table>
  293. <thead>
  294. <tr>
  295. <th><?php echo $t['group_name']; ?></th>
  296. <th style="width: 120px;"><?php echo $t['actions']; ?></th>
  297. </tr>
  298. </thead>
  299. <tbody>
  300. <?php if (count($groups) > 0): ?>
  301. <?php foreach($groups as $grp): ?>
  302. <tr>
  303. <td>
  304. <strong><?php echo htmlspecialchars($grp['name']); ?></strong><br>
  305. <span class="lang-hint"><?php echo htmlspecialchars($grp['name_en'] ?? ''); ?></span>
  306. </td>
  307. <td>
  308. <a href="attributes.php?edit_group=<?php echo $grp['id']; ?>" class="action-link action-edit">Edit</a>
  309. <a href="attributes.php?delete_group=<?php echo $grp['id']; ?>" class="action-link action-delete" onclick="return confirm('<?php echo $t['confirm_del_grp']; ?>');">Del</a>
  310. </td>
  311. </tr>
  312. <?php endforeach; ?>
  313. <?php else: ?>
  314. <tr><td colspan="2" style="text-align:center; color:var(--text-muted);"><?php echo $t['no_groups']; ?></td></tr>
  315. <?php endif; ?>
  316. </tbody>
  317. </table>
  318. </div>
  319. <div class="card">
  320. <h3><?php echo $t['existing_attrs']; ?></h3>
  321. <table>
  322. <thead>
  323. <tr>
  324. <th>Group</th>
  325. <th><?php echo $t['attr_name']; ?></th>
  326. <th>Unit</th>
  327. <th style="width: 120px;"><?php echo $t['actions']; ?></th>
  328. </tr>
  329. </thead>
  330. <tbody>
  331. <?php if (count($attributes) > 0): ?>
  332. <?php foreach($attributes as $attr): ?>
  333. <tr>
  334. <td><?php echo htmlspecialchars($attr['group_name']); ?></td>
  335. <td>
  336. <strong><?php echo htmlspecialchars($attr['name']); ?></strong><br>
  337. <span class="lang-hint"><?php echo htmlspecialchars($attr['name_en'] ?? ''); ?></span>
  338. </td>
  339. <td><?php echo htmlspecialchars($attr['unit'] ? $attr['unit'] : '-'); ?></td>
  340. <td>
  341. <a href="attributes.php?edit_attr=<?php echo $attr['id']; ?>" class="action-link action-edit">Edit</a>
  342. <a href="attributes.php?delete_attr=<?php echo $attr['id']; ?>" class="action-link action-delete" onclick="return confirm('<?php echo $t['confirm_del_attr']; ?>');">Del</a>
  343. </td>
  344. </tr>
  345. <?php endforeach; ?>
  346. <?php else: ?>
  347. <tr><td colspan="4" style="text-align:center; color:var(--text-muted);"><?php echo $t['no_attrs']; ?></td></tr>
  348. <?php endif; ?>
  349. </tbody>
  350. </table>
  351. </div>
  352. </div>
  353. </div>
  354. </body>
  355. </html>