edit.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. <link rel="stylesheet" href="../css/wysiwyg.css">
  83. </head>
  84. <body>
  85. <div class="admin-layout">
  86. <header class="admin-header">
  87. <div class="header-content">
  88. <h1><a href="/index.php"><?php echo SITE_TITLE; ?></a></h1>
  89. <nav class="admin-nav">
  90. <a href="index.php" class="nav-link"><?php echo t('admin_nav_dashboard'); ?></a>
  91. <a href="publications.php" class="nav-link active"><?php echo t('admin_nav_publications'); ?></a>
  92. <a href="categories.php" class="nav-link"><?php echo t('admin_nav_categories'); ?></a>
  93. <a href="users.php" class="nav-link"><?php echo t('manage_users'); ?></a>
  94. <?php if (LDAP_ENABLED): ?>
  95. <a href="ldap-users.php" class="nav-link"><?php echo t('admin_nav_ldap_users'); ?></a>
  96. <?php endif; ?>
  97. <a href="logout.php" class="nav-link"><?php echo t('admin_nav_logout'); ?></a>
  98. </nav>
  99. <div class="user-info">
  100. Welcome, <?php echo htmlspecialchars($user['username']); ?>
  101. </div>
  102. </div>
  103. </header>
  104. <main class="admin-main">
  105. <h2><?php echo $pubId ? 'Edit' : 'Create'; ?> Publication</h2>
  106. <?php if (!empty($errors)): ?>
  107. <div class="alert alert-error">
  108. <?php foreach ($errors as $error): ?>
  109. <p><?php echo htmlspecialchars($error); ?></p>
  110. <?php endforeach; ?>
  111. </div>
  112. <?php endif; ?>
  113. <?php if (isset($message)): ?>
  114. <div class="alert alert-success">
  115. <?php echo htmlspecialchars($message); ?>
  116. </div>
  117. <?php endif; ?>
  118. <form method="post" class="publication-form">
  119. <div class="form-group">
  120. <label for="title">Title *</label>
  121. <input type="text" id="title" name="title"
  122. value="<?php echo htmlspecialchars($pub['title'] ?? ''); ?>" required>
  123. </div>
  124. <div class="form-group">
  125. <label for="summary">Summary</label>
  126. <textarea id="summary" name="summary" rows="3"><?php echo htmlspecialchars($pub['summary'] ?? ''); ?></textarea>
  127. </div>
  128. <div class="form-group">
  129. <label for="author">Author *</label>
  130. <input type="text" id="author" name="author"
  131. value="<?php echo htmlspecialchars($pub['author'] ?? $user['username']); ?>" required>
  132. </div>
  133. <div class="form-group">
  134. <label for="status">Status</label>
  135. <select id="status" name="status">
  136. <option value="draft" <?php echo ($pub['status'] ?? 'draft') === 'draft' ? 'selected' : ''; ?>>Draft</option>
  137. <option value="published" <?php echo ($pub['status'] ?? '') === 'published' ? 'selected' : ''; ?>>Published</option>
  138. <option value="archived" <?php echo ($pub['status'] ?? '') === 'archived' ? 'selected' : ''; ?>>Archived</option>
  139. </select>
  140. </div>
  141. <div class="form-group">
  142. <label>Categories</label>
  143. <div class="category-checkboxes">
  144. <?php foreach ($allCategories as $category): ?>
  145. <label class="checkbox-label">
  146. <input type="checkbox" name="categories[]" value="<?php echo $category['id']; ?>"
  147. <?php echo in_array($category['id'], $selectedCategories) ? 'checked' : ''; ?>>
  148. <?php echo htmlspecialchars($category['name']); ?>
  149. </label>
  150. <?php endforeach; ?>
  151. </div>
  152. </div>
  153. <div class="form-group">
  154. <label for="content" class="wysiwyg-label">Content *</label>
  155. <textarea id="content" name="content" rows="20" required style="display: none;"><?php echo htmlspecialchars($pub['content'] ?? ''); ?></textarea>
  156. <div id="wysiwyg-editor" class="wysiwyg-editor"></div>
  157. </div>
  158. <div class="form-actions">
  159. <button type="submit" class="btn btn-primary">
  160. <?php echo $pubId ? 'Update' : 'Create'; ?> Publication
  161. </button>
  162. <a href="index.php" class="btn btn-secondary">Cancel</a>
  163. </div>
  164. </form>
  165. </main>
  166. </div>
  167. <!-- Image Gallery Modal -->
  168. <div class="image-gallery" id="imageGallery">
  169. <div class="gallery-container">
  170. <div class="gallery-header">
  171. <h3 class="gallery-title">Image Gallery</h3>
  172. <button class="gallery-close" onclick="wysiwygEditor.closeImageGallery()">Close</button>
  173. </div>
  174. <div class="gallery-content">
  175. <div class="gallery-tabs">
  176. <button class="gallery-tab active" onclick="wysiwygEditor.showGalleryTab('browse')">Browse Images</button>
  177. <button class="gallery-tab" onclick="wysiwygEditor.showGalleryTab('upload')">Upload New</button>
  178. </div>
  179. <!-- Browse Tab -->
  180. <div class="gallery-browse" id="galleryBrowse">
  181. <div class="gallery-grid" id="galleryGrid">
  182. <!-- Images will be loaded here -->
  183. </div>
  184. </div>
  185. <!-- Upload Tab -->
  186. <div class="gallery-upload" id="galleryUpload">
  187. <div class="upload-area" id="uploadArea">
  188. <div class="upload-icon">+</div>
  189. <div class="upload-text">Click to upload or drag and drop</div>
  190. <div class="upload-hint">Supported formats: JPG, PNG, GIF (Max 5MB)</div>
  191. <input type="file" class="file-input" id="fileInput" multiple accept="image/*">
  192. </div>
  193. <div class="upload-progress" id="uploadProgress">
  194. <div class="progress-bar">
  195. <div class="progress-fill" id="progressFill"></div>
  196. </div>
  197. <div class="upload-status" id="uploadStatus"></div>
  198. </div>
  199. </div>
  200. </div>
  201. <div class="gallery-actions">
  202. <button class="gallery-upload-btn" onclick="wysiwygEditor.showGalleryTab('upload')">Upload New Image</button>
  203. <button class="gallery-insert" id="galleryInsertBtn" disabled onclick="wysiwygEditor.insertSelectedImage()">Insert Image</button>
  204. </div>
  205. </div>
  206. </div>
  207. <script src="../js/wysiwyg.js"></script>
  208. </body>
  209. </html>