diff --git a/admin/links.php b/admin/links.php
index c6a3295..db69bb0 100644
--- a/admin/links.php
+++ b/admin/links.php
@@ -79,9 +79,49 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
exit;
}
+ require_once __DIR__ . '/../includes/ImageProcessor.php';
+
+ $current_link = $pdo->prepare("SELECT thumbnail_url FROM links WHERE id = ?");
+ $current_link->execute([$link_id]);
+ $current_thumbnail_url = $current_link->fetchColumn();
+
+ $new_thumbnail_url = $current_thumbnail_url;
+ $remove_image = isset($_POST['remove_image']) && $_POST['remove_image'] === 'true';
+
+ // Handle image removal
+ if ($remove_image) {
+ if ($current_thumbnail_url && file_exists(__DIR__ . '/../' . $current_thumbnail_url)) {
+ unlink(__DIR__ . '/../' . $current_thumbnail_url);
+ }
+ $new_thumbnail_url = null;
+ }
+
+ // Handle new image upload
+ if (isset($_FILES['thumbnail']) && $_FILES['thumbnail']['error'] === UPLOAD_ERR_OK) {
+ $upload_dir = __DIR__ . '/../assets/images/uploads/';
+ if (!is_dir($upload_dir)) {
+ mkdir($upload_dir, 0777, true);
+ }
+
+ $processor = new ImageProcessor($upload_dir);
+ $uploaded_path = $processor->uploadAndResize($_FILES['thumbnail']);
+
+ if ($uploaded_path) {
+ // Delete old thumbnail if a new one is uploaded
+ if ($current_thumbnail_url && file_exists(__DIR__ . '/../' . $current_thumbnail_url)) {
+ unlink(__DIR__ . '/../' . $current_thumbnail_url);
+ }
+ $new_thumbnail_url = 'assets/images/uploads/' . basename($uploaded_path);
+ } else {
+ $response['message'] = 'Failed to upload new thumbnail.';
+ echo json_encode($response);
+ exit;
+ }
+ }
+
try {
- $stmt = $pdo->prepare("UPDATE links SET title = ?, url = ?, description = ?, subcategory_id = ?, status = ? WHERE id = ?");
- $stmt->execute([$title, $url, $description, $subcategory_id, $status, $link_id]);
+ $stmt = $pdo->prepare("UPDATE links SET title = ?, url = ?, description = ?, subcategory_id = ?, status = ?, thumbnail_url = ? WHERE id = ?");
+ $stmt->execute([$title, $url, $description, $subcategory_id, $status, $new_thumbnail_url, $link_id]);
if ($stmt->rowCount()) {
$response = ['success' => true, 'message' => 'Link updated successfully.'];
} else {
@@ -164,7 +204,7 @@ $subcategories = $pdo->query("SELECT sc.id, sc.name AS subcategory_name, c.name
| No links submitted yet. |
-
+
|
... |
> |
@@ -229,6 +269,19 @@ $subcategories = $pdo->query("SELECT sc.id, sc.name AS subcategory_name, c.name
+
+
+
+
+
+
+
+
+
+
+
@@ -249,7 +302,11 @@ document.addEventListener('DOMContentLoaded', function() {
button.addEventListener('click', function() {
const linkId = this.dataset.id;
if (confirm('Are you sure you want to delete this link?')) {
- sendAction(linkId, 'delete', {});
+ const formData = new FormData();
+ formData.append('action', 'delete');
+ formData.append('link_id', linkId);
+ formData.append('csrf_token', csrfToken);
+ sendAction(formData);
}
});
});
@@ -261,9 +318,14 @@ document.addEventListener('DOMContentLoaded', function() {
// Determine new status based on current for toggling between paused and approved
// If current status is 'paused', next is 'approved'. Otherwise, 'paused'.
const newStatus = (currentStatus === 'paused') ? 'approved' : 'paused';
- if (confirm(`Are you sure you want to ${newStatus === 'paused' ? 'pause' : 'unpause'} this link?`)) {
- sendAction(linkId, 'toggle_status', { current_status: currentStatus });
- }
+ if (confirm(`Are you sure you want to ${newStatus === 'paused' ? 'pause' : 'unpause' } this link?`)) {
+ const formData = new FormData();
+ formData.append('action', 'toggle_status');
+ formData.append('link_id', linkId);
+ formData.append('current_status', currentStatus);
+ formData.append('csrf_token', csrfToken);
+ sendAction(formData);
+ }
});
});
@@ -272,6 +334,9 @@ document.addEventListener('DOMContentLoaded', function() {
const linkId = this.dataset.id;
const row = this.closest('tr');
+ const linkId = this.dataset.id;
+ const row = this.closest('tr');
+
document.getElementById('editLinkId').value = linkId;
document.getElementById('editLinkTitle').value = row.querySelector('.link-title').textContent;
document.getElementById('editLinkUrl').value = row.querySelector('.link-url a').href;
@@ -287,32 +352,49 @@ document.addEventListener('DOMContentLoaded', function() {
break;
}
}
+
+ // Reset file input and checkbox
+ document.getElementById('editLinkThumbnail').value = '';
+ document.getElementById('removeCurrentThumbnail').checked = false;
+
+ // Display current thumbnail if it exists
+ const currentThumbnailPreview = document.getElementById('currentThumbnailPreview');
+ currentThumbnailPreview.innerHTML = ''; // Clear previous preview
+ const thumbnailUrl = row.dataset.thumbnailUrl; // Assuming data-thumbnail-url attribute on tr
+ if (thumbnailUrl && thumbnailUrl !== 'null') {
+ const img = document.createElement('img');
+ img.src = '../' + thumbnailUrl;
+ img.alt = 'Current Thumbnail';
+ img.style.maxWidth = '100px';
+ img.style.maxHeight = '100px';
+ img.style.objectFit = 'cover';
+ currentThumbnailPreview.appendChild(img);
+ } else {
+ currentThumbnailPreview.innerHTML = 'No thumbnail set.';
+ }
});
});
document.getElementById('editLinkForm').addEventListener('submit', function(e) {
e.preventDefault();
const formData = new FormData(this);
- const linkId = document.getElementById('editLinkId').value;
- const data = {};
- formData.forEach((value, key) => data[key] = value);
- sendAction(linkId, 'edit', data);
+
+ // Append the remove_image flag if checked
+ if (document.getElementById('removeCurrentThumbnail').checked) {
+ formData.append('remove_image', 'true');
+ }
+
+ // The action and link_id are already in the form as hidden inputs
+ // No need to manually add csrf_token if it's already a hidden input in the form
+
+ // sendAction now uses FormData directly
+ sendAction(formData);
});
- function sendAction(linkId, action, additionalData) {
- const data = {
- csrf_token: csrfToken,
- action: action,
- link_id: linkId,
- ...additionalData
- };
-
+ function sendAction(formData) {
fetch('links.php', {
method: 'POST',
- headers: {
- 'Content-Type': 'application/x-www-form-urlencoded',
- },
- body: new URLSearchParams(data)
+ body: formData
})
.then(response => response.json())
.then(data => {