categories.php 11 KB

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