Sindbad~EG File Manager

Current Path : /home/copmadinaarea/.trash/news/
Upload File :
Current File : /home/copmadinaarea/.trash/news/edit.php

<?php
require_once '../config/config.php';
require_once '../classes/Location.php';
require_login();

$database = new Database();
$conn = $database->getConnection();
$news = new News($conn);
$category = new Category($conn);
$location = new Location($conn);

$id = intval($_GET['id'] ?? 0);
if (!$id) {
    flash_message('Invalid news article ID', 'error');
    redirect('news/index.php');
}

$article = $news->getById($id);
if (!$article) {
    flash_message('News article not found', 'error');
    redirect('news/index.php');
}

// Check permissions
if ($_SESSION['account_type'] === 'user' && $article['user_id'] != $_SESSION['user_id']) {
    flash_message('You do not have permission to edit this article', 'error');
    redirect('news/index.php');
}

$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $title = sanitize_input($_POST['title'] ?? '');
    $location_id = $_POST['location'] ?? '';
    $description = sanitize_input($_POST['description'] ?? '');
    $content = $_POST['content'] ?? '';
    $written_by = sanitize_input($_POST['written_by'] ?? '');
    $category_id = $_POST['category_id'] ?? null;
    $status = $_POST['status'] ?? 'draft';
    
    if (empty($title) || empty($location_id) || empty($content) || empty($written_by)) {
        $error = 'Please fill in all required fields';
    } else {
        $update_data = [
            'title' => $title,
            'location_id' => $location_id,
            'description' => $description,
            'content' => $content,
            'written_by' => $written_by,
            'category_id' => $category_id ?: null,
            'status' => $status
        ];
        
        if ($news->update($id, $update_data)) {
            flash_message('News article updated successfully!', 'success');
            redirect('news/view.php?id=' . $id);
        } else {
            $error = 'Failed to update news article. Please try again.';
        }
    }
}

$categories = $category->getAll();
$locations = $location->getAll();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit News Article - COP News Portal</title>
    <link rel="stylesheet" href="../assets/css/style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <script src="https://cdn.tiny.cloud/1/<?php echo get_setting('tinymce_api_key', 'no-api-key'); ?>/tinymce/6/tinymce.min.js" referrerpolicy="origin"></script>
</head>
<body>
    <header class="header">
        <nav class="navbar">
            <a href="../dashboard.php" class="logo">
                <i class="fas fa-church"></i>
                COP News Portal
            </a>
            <ul class="nav-links">
                <li><a href="../dashboard.php"><i class="fas fa-tachometer-alt"></i> Dashboard</a></li>
                <li><a href="index.php"><i class="fas fa-newspaper"></i> News</a></li>
                <li><a href="create.php"><i class="fas fa-plus"></i> Add News</a></li>
                <?php if ($_SESSION['account_type'] === 'admin' || $_SESSION['account_type'] === 'superuser'): ?>
                    <li><a href="../admin/"><i class="fas fa-cog"></i> Admin</a></li>
                <?php endif; ?>
                <li><a href="../profile.php"><i class="fas fa-user"></i> Profile</a></li>
                <li><a href="../logout.php"><i class="fas fa-sign-out-alt"></i> Logout</a></li>
            </ul>
        </nav>
    </header>

    <main class="container" style="margin-top: 2rem;">
        <div class="card">
            <div class="card-header">
                <h1><i class="fas fa-edit"></i> Edit News Article</h1>
            </div>
            <div class="card-body">
                <?php if ($error): ?>
                    <div class="alert alert-error">
                        <i class="fas fa-exclamation-circle"></i> <?php echo $error; ?>
                    </div>
                <?php endif; ?>

                <form method="POST" action="">
                    <div class="grid grid-2">
                        <div class="form-group">
                            <label for="title" class="form-label">News Title *</label>
                            <input type="text" id="title" name="title" class="form-control" 
                                   value="<?php echo htmlspecialchars($_POST['title'] ?? $article['title']); ?>" 
                                   placeholder="Enter news title..." required>
                        </div>
                        
                        <div class="form-group">
                            <label for="location" class="form-label">Location *</label>
                            <select id="location" name="location" class="form-control form-select" required>
                                <option value="">Select Location</option>
                                <?php foreach ($locations as $loc): ?>
                                    <option value="<?php echo $loc['id']; ?>" 
                                        <?php echo ($_POST['location'] ?? $article['location_id']) == $loc['id'] ? 'selected' : ''; ?>>
                                    <?php echo htmlspecialchars($loc['name'] . ' (' . ucfirst($loc['type']) . ')'); ?>
                                </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        
                        <div class="form-group">
                            <label for="written_by" class="form-label">Written By *</label>
                            <input type="text" id="written_by" name="written_by" class="form-control" 
                                   value="<?php echo htmlspecialchars($_POST['written_by'] ?? $article['written_by']); ?>" 
                                   placeholder="Author name" required>
                        </div>
                        
                        <div class="form-group">
                            <label for="category_id" class="form-label">Category</label>
                            <select id="category_id" name="category_id" class="form-control form-select">
                                <option value="">Select Category</option>
                                <?php foreach ($categories as $cat): ?>
                                    <option value="<?php echo $cat['id']; ?>" 
                                            <?php echo ($_POST['category_id'] ?? $article['category_id']) == $cat['id'] ? 'selected' : ''; ?>>
                                        <?php echo htmlspecialchars($cat['name']); ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                    </div>
                    
                    <div class="form-group">
                        <label for="description" class="form-label">Description</label>
                        <textarea id="description" name="description" class="form-control" rows="3" 
                                  placeholder="Brief description of the news article..."><?php echo htmlspecialchars($_POST['description'] ?? $article['description']); ?></textarea>
                    </div>
                    
                    <div class="form-group">
                        <label for="content" class="form-label">Content *</label>
                        <textarea id="content" name="content" class="form-control" rows="15" required><?php echo htmlspecialchars($_POST['content'] ?? $article['content']); ?></textarea>
                    </div>
                    
                    <div class="form-group">
                        <label for="status" class="form-label">Status</label>
                        <select id="status" name="status" class="form-control form-select">
                            <option value="draft" <?php echo ($_POST['status'] ?? $article['status']) === 'draft' ? 'selected' : ''; ?>>Draft</option>
                            <option value="published" <?php echo ($_POST['status'] ?? $article['status']) === 'published' ? 'selected' : ''; ?>>Published</option>
                            <option value="archived" <?php echo ($_POST['status'] ?? $article['status']) === 'archived' ? 'selected' : ''; ?>>Archived</option>
                        </select>
                    </div>
                    
                    <div class="flex gap-2 mt-4">
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-save"></i> Update Article
                        </button>
                        <a href="view.php?id=<?php echo $article['id']; ?>" class="btn btn-secondary">
                            <i class="fas fa-eye"></i> View Article
                        </a>
                        <a href="index.php" class="btn btn-outline">
                            <i class="fas fa-times"></i> Cancel
                        </a>
                    </div>
                </form>
            </div>
        </div>
    </main>

    <script>
        // Initialize TinyMCE for rich text editing
        tinymce.init({
            selector: '#content',
            height: 400,
            menubar: false,
            plugins: [
                'advlist', 'autolink', 'lists', 'link', 'image', 'charmap', 'preview',
                'anchor', 'searchreplace', 'visualblocks', 'code', 'fullscreen',
                'insertdatetime', 'media', 'table', 'help', 'wordcount'
            ],
            toolbar: 'undo redo | blocks | ' +
                'bold italic forecolor | alignleft aligncenter ' +
                'alignright alignjustify | bullist numlist outdent indent | ' +
                'removeformat | help',
            content_style: 'body { font-family: Inter, sans-serif; font-size: 14px }',
            branding: false
        });

        // Form validation
        document.querySelector('form').addEventListener('submit', function(e) {
            const title = document.getElementById('title').value.trim();
            const location = document.getElementById('location').value.trim();
            const writtenBy = document.getElementById('written_by').value.trim();
            
            if (!title || !location || !writtenBy) {
                e.preventDefault();
                alert('Please fill in all required fields.');
                return false;
            }
            
            // Get content from TinyMCE (check if editor is initialized)
            const editor = tinymce.get('content');
            if (editor) {
                const content = editor.getContent();
                if (!content.trim()) {
                    e.preventDefault();
                    alert('Please enter the article content.');
                    return false;
                }
                // Sync TinyMCE content to textarea
                editor.save();
            } else {
                // Fallback to textarea value if TinyMCE not initialized
                const content = document.getElementById('content').value.trim();
                if (!content) {
                    e.preventDefault();
                    alert('Please enter the article content.');
                    return false;
                }
            }
        });
    </script>
</body>
</html>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists