edit.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. $pubId = $_GET['id'] ?? null;
  15. $pub = null;
  16. $categories = [];
  17. $selectedCategories = [];
  18. // Get all available categories
  19. $allCategories = $publication->getCategories();
  20. if ($pubId) {
  21. $pub = $publication->getById($pubId);
  22. if (!$pub) {
  23. die('Publication not found');
  24. }
  25. }
  26. // Handle form submission
  27. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  28. $title = trim($_POST['title'] ?? '');
  29. $content = trim($_POST['content'] ?? '');
  30. $summary = trim($_POST['summary'] ?? '');
  31. $author = trim($_POST['author'] ?? $user['username']);
  32. $status = $_POST['status'] ?? 'draft';
  33. $categoryIds = $_POST['categories'] ?? [];
  34. $errors = [];
  35. if (empty($title)) $errors[] = 'Title is required';
  36. if (empty($content)) $errors[] = 'Content is required';
  37. if (empty($author)) $errors[] = 'Author is required';
  38. if (empty($errors)) {
  39. $data = [
  40. 'title' => $title,
  41. 'content' => $content,
  42. 'summary' => $summary,
  43. 'author' => $author,
  44. 'status' => $status
  45. ];
  46. try {
  47. if ($pubId) {
  48. $publication->update($pubId, $data, $categoryIds);
  49. $message = 'Publication updated successfully';
  50. } else {
  51. $publication->create($data, $categoryIds);
  52. $message = 'Publication created successfully';
  53. header('Location: index.php?message=' . urlencode($message));
  54. exit;
  55. }
  56. } catch (Exception $e) {
  57. $errors[] = 'Error saving publication: ' . $e->getMessage();
  58. }
  59. }
  60. // Preserve form data on error
  61. $pub = [
  62. 'title' => $title,
  63. 'content' => $content,
  64. 'summary' => $summary,
  65. 'author' => $author,
  66. 'status' => $status
  67. ];
  68. $selectedCategories = $categoryIds;
  69. } elseif ($pub) {
  70. // Get selected categories for existing publication
  71. $sql = "SELECT category_id FROM publication_categories WHERE publication_id = ?";
  72. $selectedCategories = array_column($publication->db->fetchAll($sql, [$pubId]), 'category_id');
  73. }
  74. ?>
  75. <!DOCTYPE html>
  76. <html lang="en">
  77. <head>
  78. <meta charset="UTF-8">
  79. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  80. <title><?php echo $pubId ? 'Edit' : 'Create'; ?> Publication - <?php echo SITE_TITLE; ?></title>
  81. <link rel="stylesheet" href="../css/style.css">
  82. </head>
  83. <body>
  84. <div class="admin-layout">
  85. <header class="admin-header">
  86. <div class="header-content">
  87. <h1><?php echo SITE_TITLE; ?></h1>
  88. <nav class="admin-nav">
  89. <a href="index.php" class="nav-link">Dashboard</a>
  90. <a href="publications.php" class="nav-link">Publications</a>
  91. <a href="categories.php" class="nav-link">Categories</a>
  92. <?php if (LDAP_ENABLED): ?>
  93. <a href="ldap-users.php" class="nav-link">LDAP Users</a>
  94. <?php endif; ?>
  95. <a href="logout.php" class="nav-link">Logout</a>
  96. </nav>
  97. <div class="user-info">
  98. Welcome, <?php echo htmlspecialchars($user['username']); ?>
  99. </div>
  100. </div>
  101. </header>
  102. <main class="admin-main">
  103. <h2><?php echo $pubId ? 'Edit' : 'Create'; ?> Publication</h2>
  104. <?php if (!empty($errors)): ?>
  105. <div class="alert alert-error">
  106. <?php foreach ($errors as $error): ?>
  107. <p><?php echo htmlspecialchars($error); ?></p>
  108. <?php endforeach; ?>
  109. </div>
  110. <?php endif; ?>
  111. <?php if (isset($message)): ?>
  112. <div class="alert alert-success">
  113. <?php echo htmlspecialchars($message); ?>
  114. </div>
  115. <?php endif; ?>
  116. <form method="post" class="publication-form">
  117. <div class="form-group">
  118. <label for="title">Title *</label>
  119. <input type="text" id="title" name="title"
  120. value="<?php echo htmlspecialchars($pub['title'] ?? ''); ?>" required>
  121. </div>
  122. <div class="form-group">
  123. <label for="summary">Summary</label>
  124. <textarea id="summary" name="summary" rows="3"><?php echo htmlspecialchars($pub['summary'] ?? ''); ?></textarea>
  125. </div>
  126. <div class="form-group">
  127. <label for="author">Author *</label>
  128. <input type="text" id="author" name="author"
  129. value="<?php echo htmlspecialchars($pub['author'] ?? $user['username']); ?>" required>
  130. </div>
  131. <div class="form-group">
  132. <label for="status">Status</label>
  133. <select id="status" name="status">
  134. <option value="draft" <?php echo ($pub['status'] ?? 'draft') === 'draft' ? 'selected' : ''; ?>>Draft</option>
  135. <option value="published" <?php echo ($pub['status'] ?? '') === 'published' ? 'selected' : ''; ?>>Published</option>
  136. <option value="archived" <?php echo ($pub['status'] ?? '') === 'archived' ? 'selected' : ''; ?>>Archived</option>
  137. </select>
  138. </div>
  139. <div class="form-group">
  140. <label>Categories</label>
  141. <div class="category-checkboxes">
  142. <?php foreach ($allCategories as $category): ?>
  143. <label class="checkbox-label">
  144. <input type="checkbox" name="categories[]" value="<?php echo $category['id']; ?>"
  145. <?php echo in_array($category['id'], $selectedCategories) ? 'checked' : ''; ?>>
  146. <?php echo htmlspecialchars($category['name']); ?>
  147. </label>
  148. <?php endforeach; ?>
  149. </div>
  150. </div>
  151. <div class="form-group">
  152. <label for="content">Content *</label>
  153. <textarea id="content" name="content" rows="20" required><?php echo htmlspecialchars($pub['content'] ?? ''); ?></textarea>
  154. </div>
  155. <div class="form-actions">
  156. <button type="submit" class="btn btn-primary">
  157. <?php echo $pubId ? 'Update' : 'Create'; ?> Publication
  158. </button>
  159. <a href="index.php" class="btn btn-secondary">Cancel</a>
  160. </div>
  161. </form>
  162. </main>
  163. </div>
  164. </body>
  165. </html>