-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_article.php
More file actions
126 lines (105 loc) · 4.2 KB
/
Copy pathadd_article.php
File metadata and controls
126 lines (105 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
$editMode = false;
$errors = [];
$title = $author = $content = "";
$imagePath = "";
$id = null;
// Check if editing
if (isset($_GET['edit'])) {
$id = intval($_GET['edit']);
$file = "articles/article_$id.json";
if (file_exists($file)) {
$data = json_decode(file_get_contents($file), true);
if ($data) {
$editMode = true;
$title = $data['title'];
$author = $data['author'];
$content = $data['content'];
$imagePath = $data['image'];
}
}
}
// On form submit
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$title = trim($_POST["title"]);
$author = trim($_POST["author"]);
$content = trim($_POST["content"]);
$id = isset($_POST['article_id']) ? intval($_POST['article_id']) : time();
if (strlen($title) < 5) {
$errors[] = "Article title must be at least 5 characters.";
}
if (strlen($author) < 3) {
$errors[] = "Author name must be at least 3 characters.";
}
if (strlen($content) < 400) {
$errors[] = "Content must be at least 400 characters.";
}
// Handle image upload
if (isset($_FILES["image"]) && $_FILES["image"]["error"] == 0) {
$imageName = basename($_FILES["image"]["name"]);
$targetDir = "uploads/";
$targetFile = $targetDir . time() . "_" . $imageName;
move_uploaded_file($_FILES["image"]["tmp_name"], $targetFile);
$imagePath = $targetFile;
}
if (!$imagePath) {
$errors[] = "Please upload a featured image.";
}
if (empty($errors)) {
$article = [
"id" => $id,
"title" => $title,
"author" => $author,
"content" => $content,
"image" => $imagePath,
"date" => date("Y-m-d H:i"),
];
file_put_contents("articles/article_$id.json", json_encode($article, JSON_PRETTY_PRINT));
header("Location: index.php");
exit;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?= $editMode ? "Edit" : "Add" ?> Article</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100">
<div class="max-w-2xl mx-auto bg-white mt-10 p-6 rounded shadow">
<h1 class="text-2xl font-bold mb-4 text-green-700"><?= $editMode ? "Edit Article" : "Add New Article" ?></h1>
<!-- Show errors -->
<?php if (!empty($errors)): ?>
<div class="bg-red-100 text-red-800 p-3 rounded mb-4">
<ul class="list-disc list-inside">
<?php foreach ($errors as $error): ?>
<li><?= htmlspecialchars($error) ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="article_id" value="<?= htmlspecialchars($id) ?>">
<label class="block mb-2 font-semibold">Article Title</label>
<input type="text" name="title" value="<?= htmlspecialchars($title) ?>" class="w-full p-2 border rounded mb-4">
<label class="block mb-2 font-semibold">Author Name</label>
<input type="text" name="author" value="<?= htmlspecialchars($author) ?>" class="w-full p-2 border rounded mb-4">
<label class="block mb-2 font-semibold">Featured Image</label>
<input type="file" name="image" class="mb-4">
<?php if ($imagePath): ?>
<img src="<?= htmlspecialchars($imagePath) ?>" class="w-32 mb-4 rounded shadow">
<?php endif; ?>
<label class="block mb-2 font-semibold">Article Content</label>
<textarea name="content" rows="10" class="w-full p-2 border rounded mb-4"><?= htmlspecialchars($content) ?></textarea>
<div class="flex justify-between">
<a href="index.php" class="bg-gray-300 text-gray-800 px-4 py-2 rounded hover:bg-gray-400">Cancel</a>
<button type="submit" class="bg-green-600 text-white px-4 py-2 rounded hover:bg-green-700">
<?= $editMode ? "Update Article" : "Submit Article" ?>
</button>
</div>
</form>
</div>
</body>
</html>