-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin-edit.php
More file actions
218 lines (183 loc) · 9.44 KB
/
Copy pathadmin-edit.php
File metadata and controls
218 lines (183 loc) · 9.44 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/*
┏┓┏┓┳┓┏┓┓ ┓┏┏┓┏┓
┃ ┃┃┃┃┣ ┃ ┗┫┣ ┣
┗┛┗┛┻┛┗┛┗┛┗┛┻ ┗┛
*/
?>
<?php
session_start();
if (!isset($_SESSION['UserData']['Username'])) {
header("location:login.php");
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>(ADMIN) Stupid Simple CMS</title>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
<style>
body {
background: #161616;
color: white;
}
.article {
margin-bottom: 20px;
background: white;
color: black;
border-radius: 10px;
}
</style>
<?php require_once 'layout/header-admin.php'; ?>
</head>
<body class="text-center">
<?php require_once 'layout/navbar-admin.php'; ?>
<br /><br />
<h1 class="mb-4"><i class="fa-solid fa-pen-to-square" style="color: #ffc107;"></i> Article Editor <br />
<a href="<?php echo $websiteUrl; ?>add-article.php" class="btn btn-dark"><i class="fa-solid fa-feather" style="color: #ffc107;"></i> Submit an Article</a>
<a href="<?php echo $websiteUrl; ?>index.php" class="btn btn-dark"><i class='fa-regular fa-rectangle-list' style="color: #ffc107;"></i> <?php echo $blogbutton; ?></a></h1>
<div class="mx-auto" style="max-width: 800px;">
<!-- Search Bar -->
<div class="input-group mb-3" style="padding: 32px;">
<input type="text" class="form-control" placeholder="Search articles" id="searchInputAdmin">
<button class="btn btn-outline-warning" type="button" id="searchButtonAdmin"><?php echo $searchbutton; ?></button>
</div>
<?php
$articlesDir = 'blog-posts';
// Handle article deletion
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete'])) {
$fileToDelete = $_POST['delete'];
// Validate and sanitize the filename
$fileToDelete = preg_replace('/[^a-zA-Z0-9_.]/', '', $fileToDelete);
$filePath = "$articlesDir/$fileToDelete.json"; // Update file extension to json
if (file_exists($filePath)) {
unlink($filePath);
echo '<div class="alert alert-success">File deleted successfully.</div>';
} else {
echo '<div class="alert alert-danger">File not found or could not be deleted.</div>';
}
}
// Fetch articles from the directory
$articleFiles = glob("$articlesDir/*.json"); // Update file extension to json
// Sort articles based on file modification time (most recent first)
usort($articleFiles, function ($a, $b) {
return filemtime($b) - filemtime($a);
});
foreach ($articleFiles as $file) {
$content = file_get_contents($file);
$article = json_decode($content, true);
$articleId = pathinfo($file, PATHINFO_FILENAME); // Extract article ID from filename
?>
<div class="article border p-3 text-left" style="margin: 32px;" data-article-id="<?php echo $articleId; ?>">
<h2 class="editable" data-field="title" data-article-id="<?php echo $articleId; ?>">
<?php echo $article['title']; ?>
</h2>
<p class="mt-2">
Category:
<span class="editable" data-field="category" data-article-id="<?php echo $articleId; ?>">
<?php echo $article['category']; ?>
</span>
</p>
<?php if (!empty($article['image_url'])) : ?>
<img src="<?php echo $article['image_url']; ?>" alt="Article Image" class="img-fluid mb-3">
<p class="mt-2">
Image URL:
<span class="editable" data-field="image_url" data-article-id="<?php echo $articleId; ?>">
<?php echo $article['image_url']; ?>
</span>
</p>
<?php endif; ?>
<p class="editable" data-field="content" data-article-id="<?php echo $articleId; ?>">
<?php echo $article['content']; ?>
</p>
<p class="text-muted">Created at: <?php echo $article['created_at']; ?></p>
<button class="btn btn-primary edit-btn" data-article-id="<?php echo $articleId; ?>">Edit <i class="fa-solid fa-file-pen"></i></button>
<button class="btn btn-success save-btn" data-article-id="<?php echo $articleId; ?>" style="display: none;">Save</button>
<br /><br />
<!-- Delete button for the current article -->
<form method="post" action="">
<input type="hidden" name="delete" value="<?php echo $articleId; ?>">
<button type="submit" class="btn btn-danger">Delete <i class="fa-solid fa-trash-can"></i></button>
</form>
<br /><br />
</div>
<?php
}
?>
</div>
<!-- Bootstrap JS (optional) -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<!-- jQuery -->
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<!-- Custom JavaScript for AJAX editing -->
<script>
$(document).ready(function () {
// Handle search button click
$("#searchButtonAdmin").on("click", function () {
var searchTermAdmin = $("#searchInputAdmin").val().toLowerCase();
// Loop through articles and hide/show based on the search term
$(".article").each(function () {
var articleTextAdmin = $(this).text().toLowerCase();
if (articleTextAdmin.includes(searchTermAdmin)) {
$(this).show();
} else {
$(this).hide();
}
});
});
// Enable editing when the edit button is clicked
$(".edit-btn").on("click", function () {
var articleId = $(this).data("article-id");
enableEditing(articleId);
});
// Save changes when the save button is clicked
$(".save-btn").on("click", function () {
var articleId = $(this).data("article-id");
saveChanges(articleId);
});
// Function to enable editing for a specific article
function enableEditing(articleId) {
$(".editable[data-article-id='" + articleId + "']").attr("contenteditable", "true");
$(".edit-btn[data-article-id='" + articleId + "']").hide();
$(".save-btn[data-article-id='" + articleId + "']").show();
}
// Function to save changes for a specific article
function saveChanges(articleId) {
var title = $(".editable[data-article-id='" + articleId + "'][data-field='title']").text().trim();
var content = $(".editable[data-article-id='" + articleId + "'][data-field='content']").html().trim(); // Use .html() to preserve formatting
var imageUrl = $(".editable[data-article-id='" + articleId + "'][data-field='image_url']").text().trim(); // Get the image URL from the editable field
var category = $(".editable[data-article-id='" + articleId + "'][data-field='category']").text().trim(); // Get the category from the editable field
// AJAX request to update the article
$.ajax({
url: "update-article.php",
type: "POST",
data: {
id: articleId,
title: title,
content: content,
image_url: imageUrl,
category: category // Include the category in the data
},
success: function (response) {
// You can handle the response here, e.g., show a success message
console.log(response);
// Disable editing after saving changes
$(".editable[data-article-id='" + articleId + "']").attr("contenteditable", "false");
$(".edit-btn[data-article-id='" + articleId + "']").show();
$(".save-btn[data-article-id='" + articleId + "']").hide();
},
error: function (error) {
// Handle the error, e.g., show an error message
console.error(error);
}
});
}
});
</script>
<?php require_once 'layout/footer.php'; ?>
</body>
</html>