| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- <?php
- // Start session for language preference
- if (session_status() === PHP_SESSION_NONE) {
- session_start();
- }
- require_once '../includes/config.php';
- require_once '../includes/database.php';
- require_once '../includes/auth.php';
- require_once '../includes/publication.php';
- require_once '../includes/translation.php';
- // Include LDAP class if LDAP is enabled
- if (LDAP_ENABLED) {
- require_once '../includes/ldap.php';
- }
- $auth = new Auth();
- $auth->requireAuth();
- $publication = new Publication();
- $user = $auth->getUser();
- // Translation system is auto-initialized when translation.php is included
- // Handle actions
- $action = $_GET['action'] ?? '';
- $message = '';
- $category = null;
- if ($action === 'edit' && isset($_GET['id'])) {
- $id = (int)$_GET['id'];
- $category = $publication->db->fetch("SELECT * FROM categories WHERE id = ?", [$id]);
- if (!$category) {
- die(t('admin_category_not_found'));
- }
- }
- if ($action === 'delete' && isset($_GET['id'])) {
- $id = (int)$_GET['id'];
- try {
- // Check if category has publications
- $pubCount = $publication->db->fetch("SELECT COUNT(*) as count FROM publication_categories WHERE category_id = ?", [$id])['count'];
- if ($pubCount > 0) {
- $message = t('admin_category_cannot_delete_with_pubs');
- } else {
- $publication->db->delete('categories', 'id = ?', [$id]);
- $message = t('admin_category_deleted_success');
- }
- } catch (Exception $e) {
- $message = t('admin_category_delete_error') . ' ' . $e->getMessage();
- }
- }
- // Handle form submission
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- $name = trim($_POST['name'] ?? '');
- $description = trim($_POST['description'] ?? '');
- $categoryId = (int)($_POST['category_id'] ?? 0);
-
- $errors = [];
-
- if (empty($name)) $errors[] = t('admin_category_name_required');
-
- if (empty($errors)) {
- try {
- if ($categoryId > 0) {
- // Update existing category
- $publication->db->update('categories', [
- 'name' => $name,
- 'description' => $description
- ], 'id = ?', [$categoryId]);
- $message = t('admin_category_updated_success');
- } else {
- // Create new category
- $publication->db->insert('categories', [
- 'name' => $name,
- 'description' => $description
- ]);
- $message = t('admin_category_created_success');
- }
-
- // Redirect to avoid form resubmission
- header('Location: categories.php?message=' . urlencode($message));
- exit;
-
- } catch (Exception $e) {
- if (strpos($e->getMessage(), 'Duplicate') !== false) {
- $errors[] = t('admin_category_name_exists');
- } else {
- $errors[] = t('admin_category_save_error') . ' ' . $e->getMessage();
- }
- }
- }
-
- // Preserve form data on error
- $category = [
- 'name' => $name,
- 'description' => $description,
- 'id' => $categoryId
- ];
- }
- // Get all categories
- $categories = $publication->getCategories();
- // Handle message from redirect
- if (isset($_GET['message'])) {
- $message = htmlspecialchars($_GET['message']);
- }
- ?>
- <!DOCTYPE html>
- <html lang="<?php echo Translation::getCurrentLang(); ?>">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title><?php echo t('categories'); ?> - <?php echo SITE_TITLE; ?></title>
- <link rel="stylesheet" href="../css/style.css">
- </head>
- <body>
- <div class="admin-layout">
- <header class="admin-header">
- <div class="header-content">
- <h1><a href="/index.php"><?php echo SITE_TITLE; ?></a></h1>
- <nav class="admin-nav">
- <a href="index.php" class="nav-link"><?php echo t('admin_nav_dashboard'); ?></a>
- <a href="publications.php" class="nav-link"><?php echo t('admin_nav_publications'); ?></a>
- <a href="categories.php" class="nav-link active"><?php echo t('admin_nav_categories'); ?></a>
- <a href="users.php" class="nav-link"><?php echo t('manage_users'); ?></a>
- <?php if (LDAP_ENABLED): ?>
- <a href="ldap-users.php" class="nav-link"><?php echo t('admin_nav_ldap_users'); ?></a>
- <?php endif; ?>
- <a href="logout.php" class="nav-link"><?php echo t('admin_nav_logout'); ?></a>
- </nav>
- <div class="user-info">
- <?php echo t('welcome'); ?>, <?php echo htmlspecialchars($user['username']); ?>
- </div>
- <?php echo Translation::getLanguageSwitcher('categories.php'); ?>
- </div>
- </header>
- <main class="admin-main">
- <div class="page-header">
- <h2><?php echo t('categories'); ?></h2>
- <a href="categories.php?action=edit" class="btn btn-primary"><?php echo t('create_new_category'); ?></a>
- </div>
-
- <?php if ($message): ?>
- <div class="alert alert-<?php echo strpos($message, 'Error') === false ? 'success' : 'error'; ?>">
- <?php echo $message; ?>
- </div>
- <?php endif; ?>
- <?php if ($action === 'edit' || !empty($errors)): ?>
- <div class="form-section">
- <h3><?php echo $category && $category['id'] > 0 ? t('admin_edit_category') : t('admin_create_category'); ?></h3>
-
- <?php if (!empty($errors)): ?>
- <div class="alert alert-error">
- <?php foreach ($errors as $error): ?>
- <p><?php echo htmlspecialchars($error); ?></p>
- <?php endforeach; ?>
- </div>
- <?php endif; ?>
-
- <form method="post" class="category-form">
- <input type="hidden" name="category_id" value="<?php echo $category['id'] ?? 0; ?>">
-
- <div class="form-group">
- <label for="name"><?php echo t('admin_category_name'); ?> *</label>
- <input type="text" id="name" name="name"
- value="<?php echo htmlspecialchars($category['name'] ?? ''); ?>" required>
- </div>
-
- <div class="form-group">
- <label for="description"><?php echo t('admin_category_description'); ?></label>
- <textarea id="description" name="description" rows="3"><?php echo htmlspecialchars($category['description'] ?? ''); ?></textarea>
- </div>
-
- <div class="form-actions">
- <button type="submit" class="btn btn-primary">
- <?php echo $category && $category['id'] > 0 ? t('admin_update_category') : t('admin_create_category'); ?>
- </button>
- <a href="categories.php" class="btn btn-secondary"><?php echo t('admin_cancel'); ?></a>
- </div>
- </form>
- </div>
- <?php endif; ?>
- <div class="categories-list">
- <h3><?php echo t('admin_existing_categories'); ?></h3>
-
- <?php if (empty($categories)): ?>
- <p class="text-center"><?php echo t('admin_no_categories_found'); ?></p>
- <?php else: ?>
- <div class="category-grid">
- <?php foreach ($categories as $cat): ?>
- <div class="category-card">
- <div class="category-header">
- <h4><?php echo htmlspecialchars($cat['name']); ?></h4>
- <div class="category-actions">
- <a href="categories.php?action=edit&id=<?php echo $cat['id']; ?>" class="btn btn-sm"><?php echo t('admin_edit'); ?></a>
- <a href="categories.php?action=delete&id=<?php echo $cat['id']; ?>"
- class="btn btn-sm btn-danger"
- onclick="return confirm('<?php echo t('admin_delete_category_confirm'); ?>')">
- <?php echo t('admin_delete'); ?>
- </a>
- </div>
- </div>
-
- <?php if ($cat['description']): ?>
- <p class="category-description"><?php echo htmlspecialchars($cat['description']); ?></p>
- <?php endif; ?>
-
- <div class="category-stats">
- <span class="publication-count">
- <?php echo $cat['publication_count']; ?> <?php echo t('admin_publications'); ?>
- </span>
- <span class="created-date">
- <?php echo t('admin_created'); ?> <?php echo date('M j, Y', strtotime($cat['created_at'])); ?>
- </span>
- </div>
- </div>
- <?php endforeach; ?>
- </div>
- <?php endif; ?>
- </div>
- </main>
- </div>
- <style>
- .page-header {
- display: flex;
- justify-content: space-between;
- align-items: center;
- margin-bottom: 2rem;
- }
-
- .form-section {
- background: white;
- padding: 2rem;
- border-radius: 0.5rem;
- box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
- margin-bottom: 2rem;
- }
-
- .category-form {
- max-width: 600px;
- }
-
- .categories-list {
- background: white;
- padding: 2rem;
- border-radius: 0.5rem;
- box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
- }
-
- .category-grid {
- display: grid;
- grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
- gap: 1.5rem;
- margin-top: 1.5rem;
- }
-
- .category-card {
- border: 1px solid #dee2e6;
- border-radius: 0.5rem;
- padding: 1.5rem;
- background: #f8f9fa;
- }
-
- .category-header {
- display: flex;
- justify-content: space-between;
- align-items: center;
- margin-bottom: 1rem;
- }
-
- .category-header h4 {
- margin: 0;
- color: #495057;
- }
-
- .category-actions {
- display: flex;
- gap: 0.5rem;
- }
-
- .category-description {
- color: #6c757d;
- margin-bottom: 1rem;
- font-size: 0.875rem;
- }
-
- .category-stats {
- display: flex;
- justify-content: space-between;
- align-items: center;
- font-size: 0.75rem;
- color: #6c757d;
- }
-
- .publication-count {
- font-weight: 500;
- }
-
- .text-center {
- text-align: center;
- padding: 2rem;
- color: #6c757d;
- }
-
- @media (max-width: 768px) {
- .page-header {
- flex-direction: column;
- gap: 1rem;
- align-items: stretch;
- }
-
- .category-grid {
- grid-template-columns: 1fr;
- }
-
- .category-header {
- flex-direction: column;
- gap: 1rem;
- align-items: stretch;
- }
-
- .category-actions {
- justify-content: center;
- }
- }
- </style>
- </body>
- </html>
|