Sindbad~EG File Manager
<?php
session_start();
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';
// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
header('Location: ../index.php');
exit();
}
// Initialize database connection
$database = new Database();
$pdo = $database->getConnection();
// Initialize classes
$news = new News($pdo);
$category = new Category($pdo);
$location = new Location($pdo);
$error = '';
$success = '';
// Handle form submission
if ($_POST) {
$title = trim($_POST['title'] ?? '');
$content = trim($_POST['content'] ?? '');
$description = trim($_POST['description'] ?? '');
$category_id = $_POST['category'] ?? null;
$location_id = $_POST['location'] ?? null;
$status = $_POST['status'] ?? 'draft';
$written_by = trim($_POST['written_by'] ?? '');
// Validation
if (empty($title)) {
$error = 'Title is required.';
} elseif (empty($content)) {
$error = 'Content is required.';
} elseif (empty($location_id)) {
$error = 'Location is required.';
} elseif (empty($written_by)) {
$error = 'Author name is required.';
} else {
// Handle image upload
$featured_image = null;
if (isset($_FILES['featured_image']) && $_FILES['featured_image']['error'] === UPLOAD_ERR_OK) {
$upload_dir = '../images/news/';
$allowed_types = ['image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/webp'];
$max_size = 5 * 1024 * 1024; // 5MB
if (!in_array($_FILES['featured_image']['type'], $allowed_types)) {
$error = 'Invalid image format. Please use JPG, PNG, GIF, or WebP.';
} elseif ($_FILES['featured_image']['size'] > $max_size) {
$error = 'Image size too large. Maximum size is 5MB.';
} else {
$file_extension = pathinfo($_FILES['featured_image']['name'], PATHINFO_EXTENSION);
$featured_image = 'news_' . time() . '_' . rand(1000, 9999) . '.' . $file_extension;
$upload_path = $upload_dir . $featured_image;
if (!move_uploaded_file($_FILES['featured_image']['tmp_name'], $upload_path)) {
$error = 'Failed to upload image.';
}
}
}
if (!$error) {
try {
$article_id = $news->create([
'title' => $title,
'content' => $content,
'description' => $description,
'category_id' => $category_id ?: null,
'location_id' => $location_id,
'user_id' => $_SESSION['user_id'],
'status' => $status,
'featured_image' => $featured_image,
'written_by' => $written_by
]);
$_SESSION['flash'] = [
'type' => 'success',
'message' => 'News article created successfully!'
];
header('Location: index.php');
exit();
} catch (Exception $e) {
$error = 'Error creating article: ' . $e->getMessage();
}
}
}
}
// Get categories and locations for dropdowns
$categories = $category->getAll();
$locations = $location->getAll();
// Set page title and navigation
$page_title = 'Create News Article';
$css_path = '../assets/css/style.css';
$dashboard_path = '../dashboard.php';
$news_path = 'index.php';
$editorial_path = '../editorial/dashboard.php';
$admin_path = '../admin/';
$profile_path = '../profile.php';
$logout_path = '../logout.php';
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 mb-6">
<div class="card-header">
<h2 class="text-xl font-semibold text-cop-dark flex items-center">
<i class="fas fa-plus mr-2"></i> Create News Article
</h2>
</div>
<div class="card-body">
<?php if ($error): ?>
<div class="alert alert-error mb-6">
<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-cols-1 md:grid-cols-2 gap-6">
<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'] ?? ''); ?>"
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 $location): ?>
<option value="<?php echo $location['id']; ?>"
<?php echo ($_POST['location'] ?? '') == $location['id'] ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($location['name']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<label for="category" class="form-label">Category</label>
<select id="category" name="category" class="form-control form-select">
<option value="">Select Category</option>
<?php foreach ($categories as $category): ?>
<option value="<?php echo $category['id']; ?>"
<?php echo ($_POST['category'] ?? '') == $category['id'] ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($category['name']); ?>
</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'] ?? $_SESSION['user_name']); ?>"
placeholder="Author name" required>
</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 or summary..."><?php echo htmlspecialchars($_POST['description'] ?? ''); ?></textarea>
</div>
<div class="form-group">
<label for="content" class="form-label">Content *</label>
<textarea id="content" name="content" class="form-control" rows="10"><?php echo htmlspecialchars($_POST['content'] ?? ''); ?></textarea>
</div>
<div class="form-group">
<label for="featured_image" class="form-label">Featured Image</label>
<input type="file" id="featured_image" name="featured_image" class="form-control"
accept="image/jpeg,image/jpg,image/png,image/gif,image/webp">
<small class="text-gray-600 text-sm">
Optional. Supported formats: JPG, PNG, GIF, WebP. Max size: 5MB. Images will be automatically resized.
</small>
</div>
<div class="form-group">
<label for="status" class="form-label">Publication Options</label>
<div class="space-y-3">
<div class="flex items-center">
<input type="radio" id="status_draft" name="status" value="draft"
<?php echo ($_POST['status'] ?? 'draft') === 'draft' ? 'checked' : ''; ?>
class="mr-2">
<label for="status_draft" class="text-sm">
<span class="font-medium">Save as Draft</span>
<span class="block text-gray-600">Only visible to you for editing</span>
</label>
</div>
<div class="flex items-center">
<input type="radio" id="status_published" name="status" value="published"
<?php echo ($_POST['status'] ?? '') === 'published' ? 'checked' : ''; ?>
class="mr-2">
<label for="status_published" class="text-sm">
<span class="font-medium">Publish Immediately</span>
<span class="block text-gray-600">Make visible to all users</span>
</label>
</div>
<?php if (class_exists('Editorial')): ?>
<div class="flex items-center">
<input type="radio" id="status_review" name="status" value="pending_review"
<?php echo ($_POST['status'] ?? '') === 'pending_review' ? 'checked' : ''; ?>
class="mr-2">
<label for="status_review" class="text-sm">
<span class="font-medium">Submit for Editorial Review</span>
<span class="block text-gray-600">Send to editors for approval before publishing</span>
</label>
</div>
<?php endif; ?>
</div>
</div>
<div class="flex flex-wrap gap-3 mt-6">
<button type="submit" class="btn btn-primary">
<i class="fas fa-save mr-2"></i> Create Article
</button>
<a href="index.php" class="btn btn-secondary">
<i class="fas fa-times mr-2"></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:Helvetica,Arial,sans-serif; font-size:14px }',
setup: function (editor) {
editor.on('change', function () {
editor.save();
});
}
});
// Form validation
document.querySelector('form').addEventListener('submit', function(e) {
const title = document.getElementById('title').value.trim();
const content = tinymce.get('content').getContent().trim();
const location = document.getElementById('location').value;
const writtenBy = document.getElementById('written_by').value.trim();
if (!title) {
alert('Please enter a title for the news article.');
e.preventDefault();
return false;
}
if (!content) {
alert('Please enter content for the news article.');
e.preventDefault();
return false;
}
if (!location) {
alert('Please select a location for the news article.');
e.preventDefault();
return false;
}
if (!writtenBy) {
alert('Please enter the author name.');
e.preventDefault();
return false;
}
});
</script>
<?php include '../includes/footer.php'; ?>
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists