Sindbad~EG File Manager
<?php
require_once '../config/config.php';
require_once '../config/database.php';
require_once '../classes/News.php';
require_once '../classes/Category.php';
require_once '../classes/Location.php';
require_once '../includes/image_handler.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 {
$featured_image = $article['featured_image']; // Keep existing image by default
// Handle image upload if provided
if (isset($_FILES['featured_image']) && $_FILES['featured_image']['error'] !== UPLOAD_ERR_NO_FILE) {
$imageHandler = new ImageHandler();
$upload_result = $imageHandler->uploadImage($_FILES['featured_image'], $id);
if ($upload_result['success']) {
// Delete old image if it exists
if (!empty($article['featured_image'])) {
$imageHandler->deleteImage($article['featured_image']);
}
$featured_image = $upload_result['path'];
} else {
$error = 'Image upload failed: ' . $upload_result['error'];
}
}
if (!$error) {
$update_data = [
'title' => $title,
'location_id' => $location_id,
'description' => $description,
'content' => $content,
'written_by' => $written_by,
'category_id' => $category_id ?: null,
'status' => $status,
'featured_image' => $featured_image
];
if ($news->update($id, $update_data)) {
flash_message('News article updated successfully!', 'success');
redirect('view.php?id=' . $id);
} else {
$error = 'Failed to update news article. Please try again.';
}
}
}
}
$categories = $category->getAll();
$locations = $location->getAll();
// Set page variables for header
$page_title = 'Edit News Article';
$home_path = '../dashboard.php';
$dashboard_path = '../dashboard.php';
$news_path = 'index.php';
$create_path = 'create.php';
$editorial_path = '../editorial/dashboard.php';
$admin_path = '../admin/';
$profile_path = '../profile.php';
$logout_path = '../logout.php';
$css_path = '../assets/css/style.css';
include '../includes/header.php';
?>
<!-- TinyMCE Script -->
<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>
<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="" enctype="multipart/form-data">
<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="featured_image" class="form-label">Featured Image</label>
<?php if (!empty($article['featured_image'])): ?>
<div class="current-image mb-2">
<p><strong>Current Image:</strong></p>
<img src="../<?php echo htmlspecialchars($article['featured_image']); ?>"
alt="Current featured image" style="max-width: 200px; height: auto; border: 1px solid #ddd; border-radius: 4px;">
</div>
<?php endif; ?>
<input type="file" id="featured_image" name="featured_image" class="form-control"
accept="image/jpeg,image/jpg,image/png,image/gif,image/webp">
<small style="color: var(--primary-grey);">
Optional. Upload a new image to replace the current one. Supported formats: JPG, PNG, GIF, WebP. Max size: 5MB.
</small>
</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>
<?php include '../includes/footer.php'; ?>
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists