categories.php 12 KB

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