categories.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. // Start session for language preference
  3. if (session_status() === PHP_SESSION_NONE) {
  4. session_start();
  5. }
  6. require_once '../includes/config.php';
  7. require_once '../includes/database.php';
  8. require_once '../includes/auth.php';
  9. require_once '../includes/publication.php';
  10. require_once '../includes/translation.php';
  11. // Include LDAP class if LDAP is enabled
  12. if (LDAP_ENABLED) {
  13. require_once '../includes/ldap.php';
  14. }
  15. $auth = new Auth();
  16. $auth->requireAuth();
  17. $publication = new Publication();
  18. $user = $auth->getUser();
  19. // Translation system is auto-initialized when translation.php is included
  20. // Handle actions
  21. $action = $_GET['action'] ?? '';
  22. $message = '';
  23. $category = null;
  24. if ($action === 'edit' && isset($_GET['id'])) {
  25. $id = (int)$_GET['id'];
  26. $category = $publication->db->fetch("SELECT * FROM categories WHERE id = ?", [$id]);
  27. if (!$category) {
  28. die(t('admin_category_not_found'));
  29. }
  30. }
  31. if ($action === 'delete' && isset($_GET['id'])) {
  32. $id = (int)$_GET['id'];
  33. try {
  34. // Check if category has publications
  35. $pubCount = $publication->db->fetch("SELECT COUNT(*) as count FROM publication_categories WHERE category_id = ?", [$id])['count'];
  36. if ($pubCount > 0) {
  37. $message = t('admin_category_cannot_delete_with_pubs');
  38. } else {
  39. $publication->db->delete('categories', 'id = ?', [$id]);
  40. $message = t('admin_category_deleted_success');
  41. }
  42. } catch (Exception $e) {
  43. $message = t('admin_category_delete_error') . ' ' . $e->getMessage();
  44. }
  45. }
  46. // Handle form submission
  47. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  48. $name = trim($_POST['name'] ?? '');
  49. $description = trim($_POST['description'] ?? '');
  50. $categoryId = (int)($_POST['category_id'] ?? 0);
  51. $errors = [];
  52. if (empty($name)) $errors[] = t('admin_category_name_required');
  53. if (empty($errors)) {
  54. try {
  55. if ($categoryId > 0) {
  56. // Update existing category
  57. $publication->db->update('categories', [
  58. 'name' => $name,
  59. 'description' => $description
  60. ], 'id = ?', [$categoryId]);
  61. $message = t('admin_category_updated_success');
  62. } else {
  63. // Create new category
  64. $publication->db->insert('categories', [
  65. 'name' => $name,
  66. 'description' => $description
  67. ]);
  68. $message = t('admin_category_created_success');
  69. }
  70. // Redirect to avoid form resubmission
  71. header('Location: categories.php?message=' . urlencode($message));
  72. exit;
  73. } catch (Exception $e) {
  74. if (strpos($e->getMessage(), 'Duplicate') !== false) {
  75. $errors[] = t('admin_category_name_exists');
  76. } else {
  77. $errors[] = t('admin_category_save_error') . ' ' . $e->getMessage();
  78. }
  79. }
  80. }
  81. // Preserve form data on error
  82. $category = [
  83. 'name' => $name,
  84. 'description' => $description,
  85. 'id' => $categoryId
  86. ];
  87. }
  88. // Get all categories
  89. $categories = $publication->getCategories();
  90. // Handle message from redirect
  91. if (isset($_GET['message'])) {
  92. $message = htmlspecialchars($_GET['message']);
  93. }
  94. ?>
  95. <!DOCTYPE html>
  96. <html lang="en">
  97. <head>
  98. <meta charset="UTF-8">
  99. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  100. <title>Categories - <?php echo SITE_TITLE; ?></title>
  101. <link rel="stylesheet" href="../css/style.css">
  102. </head>
  103. <body>
  104. <div class="admin-layout">
  105. <header class="admin-header">
  106. <div class="header-content">
  107. <h1><a href="/index.php"><?php echo SITE_TITLE; ?></a></h1>
  108. <nav class="admin-nav">
  109. <a href="index.php" class="nav-link"><?php echo t('admin_nav_dashboard'); ?></a>
  110. <a href="publications.php" class="nav-link"><?php echo t('admin_nav_publications'); ?></a>
  111. <a href="categories.php" class="nav-link active"><?php echo t('admin_nav_categories'); ?></a>
  112. <a href="users.php" class="nav-link"><?php echo t('manage_users'); ?></a>
  113. <?php if (LDAP_ENABLED): ?>
  114. <a href="ldap-users.php" class="nav-link"><?php echo t('admin_nav_ldap_users'); ?></a>
  115. <?php endif; ?>
  116. <a href="logout.php" class="nav-link"><?php echo t('admin_nav_logout'); ?></a>
  117. </nav>
  118. <div class="user-info">
  119. Welcome, <?php echo htmlspecialchars($user['username']); ?>
  120. </div>
  121. </div>
  122. </header>
  123. <main class="admin-main">
  124. <div class="page-header">
  125. <h2>Categories</h2>
  126. <a href="categories.php?action=edit" class="btn btn-primary">Create New Category</a>
  127. </div>
  128. <?php if ($message): ?>
  129. <div class="alert alert-<?php echo strpos($message, 'Error') === false ? 'success' : 'error'; ?>">
  130. <?php echo $message; ?>
  131. </div>
  132. <?php endif; ?>
  133. <?php if ($action === 'edit' || !empty($errors)): ?>
  134. <div class="form-section">
  135. <h3><?php echo $category && $category['id'] > 0 ? t('admin_edit_category') : t('admin_create_category'); ?></h3>
  136. <?php if (!empty($errors)): ?>
  137. <div class="alert alert-error">
  138. <?php foreach ($errors as $error): ?>
  139. <p><?php echo htmlspecialchars($error); ?></p>
  140. <?php endforeach; ?>
  141. </div>
  142. <?php endif; ?>
  143. <form method="post" class="category-form">
  144. <input type="hidden" name="category_id" value="<?php echo $category['id'] ?? 0; ?>">
  145. <div class="form-group">
  146. <label for="name"><?php echo t('admin_category_name'); ?> *</label>
  147. <input type="text" id="name" name="name"
  148. value="<?php echo htmlspecialchars($category['name'] ?? ''); ?>" required>
  149. </div>
  150. <div class="form-group">
  151. <label for="description"><?php echo t('admin_category_description'); ?></label>
  152. <textarea id="description" name="description" rows="3"><?php echo htmlspecialchars($category['description'] ?? ''); ?></textarea>
  153. </div>
  154. <div class="form-actions">
  155. <button type="submit" class="btn btn-primary">
  156. <?php echo $category && $category['id'] > 0 ? t('admin_update_category') : t('admin_create_category'); ?>
  157. </button>
  158. <a href="categories.php" class="btn btn-secondary"><?php echo t('admin_cancel'); ?></a>
  159. </div>
  160. </form>
  161. </div>
  162. <?php endif; ?>
  163. <div class="categories-list">
  164. <h3><?php echo t('admin_existing_categories'); ?></h3>
  165. <?php if (empty($categories)): ?>
  166. <p class="text-center"><?php echo t('admin_no_categories_found'); ?></p>
  167. <?php else: ?>
  168. <div class="category-grid">
  169. <?php foreach ($categories as $cat): ?>
  170. <div class="category-card">
  171. <div class="category-header">
  172. <h4><?php echo htmlspecialchars($cat['name']); ?></h4>
  173. <div class="category-actions">
  174. <a href="categories.php?action=edit&id=<?php echo $cat['id']; ?>" class="btn btn-sm"><?php echo t('admin_edit'); ?></a>
  175. <a href="categories.php?action=delete&id=<?php echo $cat['id']; ?>"
  176. class="btn btn-sm btn-danger"
  177. onclick="return confirm('<?php echo t('admin_delete_category_confirm'); ?>')">
  178. <?php echo t('admin_delete'); ?>
  179. </a>
  180. </div>
  181. </div>
  182. <?php if ($cat['description']): ?>
  183. <p class="category-description"><?php echo htmlspecialchars($cat['description']); ?></p>
  184. <?php endif; ?>
  185. <div class="category-stats">
  186. <span class="publication-count">
  187. <?php echo $cat['publication_count']; ?> <?php echo t('admin_publications'); ?>
  188. </span>
  189. <span class="created-date">
  190. <?php echo t('admin_created'); ?> <?php echo date('M j, Y', strtotime($cat['created_at'])); ?>
  191. </span>
  192. </div>
  193. </div>
  194. <?php endforeach; ?>
  195. </div>
  196. <?php endif; ?>
  197. </div>
  198. </main>
  199. </div>
  200. <style>
  201. .page-header {
  202. display: flex;
  203. justify-content: space-between;
  204. align-items: center;
  205. margin-bottom: 2rem;
  206. }
  207. .form-section {
  208. background: white;
  209. padding: 2rem;
  210. border-radius: 0.5rem;
  211. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  212. margin-bottom: 2rem;
  213. }
  214. .category-form {
  215. max-width: 600px;
  216. }
  217. .categories-list {
  218. background: white;
  219. padding: 2rem;
  220. border-radius: 0.5rem;
  221. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  222. }
  223. .category-grid {
  224. display: grid;
  225. grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  226. gap: 1.5rem;
  227. margin-top: 1.5rem;
  228. }
  229. .category-card {
  230. border: 1px solid #dee2e6;
  231. border-radius: 0.5rem;
  232. padding: 1.5rem;
  233. background: #f8f9fa;
  234. }
  235. .category-header {
  236. display: flex;
  237. justify-content: space-between;
  238. align-items: center;
  239. margin-bottom: 1rem;
  240. }
  241. .category-header h4 {
  242. margin: 0;
  243. color: #495057;
  244. }
  245. .category-actions {
  246. display: flex;
  247. gap: 0.5rem;
  248. }
  249. .category-description {
  250. color: #6c757d;
  251. margin-bottom: 1rem;
  252. font-size: 0.875rem;
  253. }
  254. .category-stats {
  255. display: flex;
  256. justify-content: space-between;
  257. align-items: center;
  258. font-size: 0.75rem;
  259. color: #6c757d;
  260. }
  261. .publication-count {
  262. font-weight: 500;
  263. }
  264. .text-center {
  265. text-align: center;
  266. padding: 2rem;
  267. color: #6c757d;
  268. }
  269. @media (max-width: 768px) {
  270. .page-header {
  271. flex-direction: column;
  272. gap: 1rem;
  273. align-items: stretch;
  274. }
  275. .category-grid {
  276. grid-template-columns: 1fr;
  277. }
  278. .category-header {
  279. flex-direction: column;
  280. gap: 1rem;
  281. align-items: stretch;
  282. }
  283. .category-actions {
  284. justify-content: center;
  285. }
  286. }
  287. </style>
  288. </body>
  289. </html>