| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <?php
- require_once '../includes/config.php';
- require_once '../includes/database.php';
- require_once '../includes/auth.php';
- require_once '../includes/publication.php';
- $auth = new Auth();
- $auth->requireAuth();
- $publication = new Publication();
- $user = $auth->getUser();
- $pubId = $_GET['id'] ?? null;
- $pub = null;
- $categories = [];
- $selectedCategories = [];
- // Get all available categories
- $allCategories = $publication->getCategories();
- if ($pubId) {
- $pub = $publication->getById($pubId);
- if (!$pub) {
- die('Publication not found');
- }
- }
- // Handle form submission
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- $title = trim($_POST['title'] ?? '');
- $content = trim($_POST['content'] ?? '');
- $summary = trim($_POST['summary'] ?? '');
- $author = trim($_POST['author'] ?? $user['username']);
- $status = $_POST['status'] ?? 'draft';
- $categoryIds = $_POST['categories'] ?? [];
-
- $errors = [];
-
- if (empty($title)) $errors[] = 'Title is required';
- if (empty($content)) $errors[] = 'Content is required';
- if (empty($author)) $errors[] = 'Author is required';
-
- if (empty($errors)) {
- $data = [
- 'title' => $title,
- 'content' => $content,
- 'summary' => $summary,
- 'author' => $author,
- 'status' => $status
- ];
-
- try {
- if ($pubId) {
- $publication->update($pubId, $data, $categoryIds);
- $message = 'Publication updated successfully';
- } else {
- $publication->create($data, $categoryIds);
- $message = 'Publication created successfully';
- header('Location: index.php?message=' . urlencode($message));
- exit;
- }
- } catch (Exception $e) {
- $errors[] = 'Error saving publication: ' . $e->getMessage();
- }
- }
-
- // Preserve form data on error
- $pub = [
- 'title' => $title,
- 'content' => $content,
- 'summary' => $summary,
- 'author' => $author,
- 'status' => $status
- ];
- $selectedCategories = $categoryIds;
- } elseif ($pub) {
- // Get selected categories for existing publication
- $sql = "SELECT category_id FROM publication_categories WHERE publication_id = ?";
- $selectedCategories = array_column($publication->db->fetchAll($sql, [$pubId]), 'category_id');
- }
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title><?php echo $pubId ? 'Edit' : 'Create'; ?> Publication - <?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><?php echo SITE_TITLE; ?></h1>
- <nav class="admin-nav">
- <a href="index.php" class="nav-link">Dashboard</a>
- <a href="publications.php" class="nav-link">Publications</a>
- <a href="categories.php" class="nav-link">Categories</a>
- <a href="logout.php" class="nav-link">Logout</a>
- </nav>
- <div class="user-info">
- Welcome, <?php echo htmlspecialchars($user['username']); ?>
- </div>
- </div>
- </header>
- <main class="admin-main">
- <h2><?php echo $pubId ? 'Edit' : 'Create'; ?> Publication</h2>
-
- <?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; ?>
-
- <?php if (isset($message)): ?>
- <div class="alert alert-success">
- <?php echo htmlspecialchars($message); ?>
- </div>
- <?php endif; ?>
- <form method="post" class="publication-form">
- <div class="form-group">
- <label for="title">Title *</label>
- <input type="text" id="title" name="title"
- value="<?php echo htmlspecialchars($pub['title'] ?? ''); ?>" required>
- </div>
- <div class="form-group">
- <label for="summary">Summary</label>
- <textarea id="summary" name="summary" rows="3"><?php echo htmlspecialchars($pub['summary'] ?? ''); ?></textarea>
- </div>
- <div class="form-group">
- <label for="author">Author *</label>
- <input type="text" id="author" name="author"
- value="<?php echo htmlspecialchars($pub['author'] ?? $user['username']); ?>" required>
- </div>
- <div class="form-group">
- <label for="status">Status</label>
- <select id="status" name="status">
- <option value="draft" <?php echo ($pub['status'] ?? 'draft') === 'draft' ? 'selected' : ''; ?>>Draft</option>
- <option value="published" <?php echo ($pub['status'] ?? '') === 'published' ? 'selected' : ''; ?>>Published</option>
- <option value="archived" <?php echo ($pub['status'] ?? '') === 'archived' ? 'selected' : ''; ?>>Archived</option>
- </select>
- </div>
- <div class="form-group">
- <label>Categories</label>
- <div class="category-checkboxes">
- <?php foreach ($allCategories as $category): ?>
- <label class="checkbox-label">
- <input type="checkbox" name="categories[]" value="<?php echo $category['id']; ?>"
- <?php echo in_array($category['id'], $selectedCategories) ? 'checked' : ''; ?>>
- <?php echo htmlspecialchars($category['name']); ?>
- </label>
- <?php endforeach; ?>
- </div>
- </div>
- <div class="form-group">
- <label for="content">Content *</label>
- <textarea id="content" name="content" rows="20" required><?php echo htmlspecialchars($pub['content'] ?? ''); ?></textarea>
- </div>
- <div class="form-actions">
- <button type="submit" class="btn btn-primary">
- <?php echo $pubId ? 'Update' : 'Create'; ?> Publication
- </button>
- <a href="index.php" class="btn btn-secondary">Cancel</a>
- </div>
- </form>
- </main>
- </div>
- </body>
- </html>
|