edit.php 6.8 KB

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