Auto commit: 2025-12-18T06:36:58.434Z

This commit is contained in:
Flatlogic Bot 2025-12-18 06:36:58 +00:00
parent cff6fa6b35
commit de3473c411

View File

@ -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
<tr><td colspan="7">No links submitted yet.</td></tr>
<?php else: ?>
<?php foreach ($links as $link): ?>
<tr data-id="<?php echo $link['id']; ?>">
<tr data-id="<?php echo $link['id']; ?>" data-thumbnail-url="<?php echo htmlspecialchars($link['thumbnail_url']); ?>">
<td class="link-title"><?php echo htmlspecialchars($link['title']); ?></td>
<td class="link-url"><a href="<?php echo htmlspecialchars($link['url']); ?>" target="_blank"><?php echo htmlspecialchars(substr($link['url'], 0, 50)); ?>...</a></td>
<td><?php echo htmlspecialchars($link['category_name']); ?> > <span class="link-subcategory-name" data-id="<?php echo $link['subcategory_id']; ?>"><?php echo htmlspecialchars($link['subcategory_name']); ?></span></td>
@ -229,6 +269,19 @@ $subcategories = $pdo->query("SELECT sc.id, sc.name AS subcategory_name, c.name
<option value="paused">Paused</option>
</select>
</div>
<div class="mb-3">
<label for="editLinkThumbnail" class="form-label">Current Thumbnail</label>
<div id="currentThumbnailPreview" class="mb-2">
<!-- Image will be loaded here by JS -->
</div>
<input type="file" class="form-control" id="editLinkThumbnail" name="thumbnail" accept="image/*">
<div class="form-check mt-2">
<input class="form-check-input" type="checkbox" value="true" id="removeCurrentThumbnail" name="remove_image">
<label class="form-check-label" for="removeCurrentThumbnail">
Remove current thumbnail
</label>
</div>
</div>
<button type="submit" class="btn btn-primary">Save changes</button>
</form>
</div>
@ -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,8 +318,13 @@ 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 = '<small>No thumbnail set.</small>';
}
});
});
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 => {