categories.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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="<?php echo Translation::getCurrentLang(); ?>">
  97. <head>
  98. <meta charset="UTF-8">
  99. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  100. <title><?php echo t('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. <?php echo t('welcome'); ?>, <?php echo htmlspecialchars($user['username']); ?>
  120. </div>
  121. <?php echo Translation::getLanguageSwitcher('categories.php'); ?>
  122. </div>
  123. </header>
  124. <main class="admin-main">
  125. <div class="page-header">
  126. <h2><?php echo t('categories'); ?></h2>
  127. <a href="categories.php?action=edit" class="btn btn-primary"><?php echo t('create_new_category'); ?></a>
  128. </div>
  129. <?php if ($message): ?>
  130. <div class="alert alert-<?php echo strpos($message, 'Error') === false ? 'success' : 'error'; ?>">
  131. <?php echo $message; ?>
  132. </div>
  133. <?php endif; ?>
  134. <?php if ($action === 'edit' || !empty($errors)): ?>
  135. <div class="form-section">
  136. <h3><?php echo $category && $category['id'] > 0 ? t('admin_edit_category') : t('admin_create_category'); ?></h3>
  137. <?php if (!empty($errors)): ?>
  138. <div class="alert alert-error">
  139. <?php foreach ($errors as $error): ?>
  140. <p><?php echo htmlspecialchars($error); ?></p>
  141. <?php endforeach; ?>
  142. </div>
  143. <?php endif; ?>
  144. <form method="post" class="category-form">
  145. <input type="hidden" name="category_id" value="<?php echo $category['id'] ?? 0; ?>">
  146. <div class="form-group">
  147. <label for="name"><?php echo t('admin_category_name'); ?> *</label>
  148. <input type="text" id="name" name="name"
  149. value="<?php echo htmlspecialchars($category['name'] ?? ''); ?>" required>
  150. </div>
  151. <div class="form-group">
  152. <label for="description"><?php echo t('admin_category_description'); ?></label>
  153. <textarea id="description" name="description" rows="3"><?php echo htmlspecialchars($category['description'] ?? ''); ?></textarea>
  154. </div>
  155. <div class="form-actions">
  156. <button type="submit" class="btn btn-primary">
  157. <?php echo $category && $category['id'] > 0 ? t('admin_update_category') : t('admin_create_category'); ?>
  158. </button>
  159. <a href="categories.php" class="btn btn-secondary"><?php echo t('admin_cancel'); ?></a>
  160. </div>
  161. </form>
  162. </div>
  163. <?php endif; ?>
  164. <div class="categories-list">
  165. <h3><?php echo t('admin_existing_categories'); ?></h3>
  166. <?php if (empty($categories)): ?>
  167. <p class="text-center"><?php echo t('admin_no_categories_found'); ?></p>
  168. <?php else: ?>
  169. <div class="category-grid">
  170. <?php foreach ($categories as $cat): ?>
  171. <div class="category-card">
  172. <div class="category-header">
  173. <h4><?php echo htmlspecialchars($cat['name']); ?></h4>
  174. <div class="category-actions">
  175. <a href="categories.php?action=edit&id=<?php echo $cat['id']; ?>" class="btn btn-sm"><?php echo t('admin_edit'); ?></a>
  176. <a href="categories.php?action=delete&id=<?php echo $cat['id']; ?>"
  177. class="btn btn-sm btn-danger"
  178. onclick="return confirm('<?php echo t('admin_delete_category_confirm'); ?>')">
  179. <?php echo t('admin_delete'); ?>
  180. </a>
  181. </div>
  182. </div>
  183. <?php if ($cat['description']): ?>
  184. <p class="category-description"><?php echo htmlspecialchars($cat['description']); ?></p>
  185. <?php endif; ?>
  186. <div class="category-stats">
  187. <span class="publication-count">
  188. <?php echo $cat['publication_count']; ?> <?php echo t('admin_publications'); ?>
  189. </span>
  190. <span class="created-date">
  191. <?php echo t('admin_created'); ?> <?php echo date('M j, Y', strtotime($cat['created_at'])); ?>
  192. </span>
  193. </div>
  194. </div>
  195. <?php endforeach; ?>
  196. </div>
  197. <?php endif; ?>
  198. </div>
  199. </main>
  200. </div>
  201. <style>
  202. .page-header {
  203. display: flex;
  204. justify-content: space-between;
  205. align-items: center;
  206. margin-bottom: 2rem;
  207. }
  208. .form-section {
  209. background: white;
  210. padding: 2rem;
  211. border-radius: 0.5rem;
  212. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  213. margin-bottom: 2rem;
  214. }
  215. .category-form {
  216. max-width: 600px;
  217. }
  218. .categories-list {
  219. background: white;
  220. padding: 2rem;
  221. border-radius: 0.5rem;
  222. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  223. }
  224. .category-grid {
  225. display: grid;
  226. grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  227. gap: 1.5rem;
  228. margin-top: 1.5rem;
  229. }
  230. .category-card {
  231. border: 1px solid #dee2e6;
  232. border-radius: 0.5rem;
  233. padding: 1.5rem;
  234. background: #f8f9fa;
  235. }
  236. .category-header {
  237. display: flex;
  238. justify-content: space-between;
  239. align-items: center;
  240. margin-bottom: 1rem;
  241. }
  242. .category-header h4 {
  243. margin: 0;
  244. color: #495057;
  245. }
  246. .category-actions {
  247. display: flex;
  248. gap: 0.5rem;
  249. }
  250. .category-description {
  251. color: #6c757d;
  252. margin-bottom: 1rem;
  253. font-size: 0.875rem;
  254. }
  255. .category-stats {
  256. display: flex;
  257. justify-content: space-between;
  258. align-items: center;
  259. font-size: 0.75rem;
  260. color: #6c757d;
  261. }
  262. .publication-count {
  263. font-weight: 500;
  264. }
  265. .text-center {
  266. text-align: center;
  267. padding: 2rem;
  268. color: #6c757d;
  269. }
  270. @media (max-width: 768px) {
  271. .page-header {
  272. flex-direction: column;
  273. gap: 1rem;
  274. align-items: stretch;
  275. }
  276. .category-grid {
  277. grid-template-columns: 1fr;
  278. }
  279. .category-header {
  280. flex-direction: column;
  281. gap: 1rem;
  282. align-items: stretch;
  283. }
  284. .category-actions {
  285. justify-content: center;
  286. }
  287. }
  288. </style>
  289. </body>
  290. </html>