categories.php 13 KB

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