diff --git a/app/Http/Controllers/Admin/AdminContentController.php b/app/Http/Controllers/Admin/AdminContentController.php index 2e8d1104c..63b899619 100644 --- a/app/Http/Controllers/Admin/AdminContentController.php +++ b/app/Http/Controllers/Admin/AdminContentController.php @@ -146,15 +146,36 @@ class AdminContentController extends BasePageController /** * Delete content by ID. */ - public function destroy(Request $request): \Illuminate\Routing\Redirector|RedirectResponse|\Illuminate\Contracts\Foundation\Application + public function destroy(Request $request) { if ($request->has('id')) { - Content::query()->where('id', $request->input('id'))->delete(); + $content = Content::query()->find($request->input('id')); + + if ($content) { + $content->delete(); + + // If AJAX request, return JSON + if ($request->wantsJson() || $request->ajax()) { + return response()->json([ + 'success' => true, + 'message' => 'Content deleted successfully' + ]); + } + + return redirect()->route('admin.content-list')->with('success', 'Content deleted successfully'); + } + + // If AJAX request, return error JSON + if ($request->wantsJson() || $request->ajax()) { + return response()->json([ + 'success' => false, + 'message' => 'Content not found' + ], 404); + } } $referrer = $request->server('HTTP_REFERER'); - - return redirect()->to($referrer); + return redirect()->to($referrer)->with('error', 'Invalid request'); } /** diff --git a/resources/js/csp-safe.js b/resources/js/csp-safe.js index da832e659..8ebaa444f 100644 --- a/resources/js/csp-safe.js +++ b/resources/js/csp-safe.js @@ -41,6 +41,7 @@ document.addEventListener('DOMContentLoaded', function() { initAdminSpecificFeatures(); initRecentActivityRefresh(); initContentToggle(); + initContentDelete(); // Initialize page-specific functionality from inline scripts initMyMovies(); @@ -4824,3 +4825,86 @@ function initContentToggle() { }); } +// Admin Content List - Delete Content +function initContentDelete() { + document.addEventListener('click', function(e) { + const deleteBtn = e.target.closest('.content-delete'); + if (!deleteBtn) return; + + e.preventDefault(); + const contentId = deleteBtn.getAttribute('data-content-id'); + const contentTitle = deleteBtn.getAttribute('data-content-title'); + const csrfToken = document.querySelector('meta[name="csrf-token"]')?.content; + + if (!contentId || !csrfToken) { + if (typeof showToast === 'function') { + showToast('Error: Missing required data', 'error'); + } + return; + } + + // Show styled confirmation modal + showConfirm({ + title: 'Delete Content', + message: `Are you sure you want to delete "${contentTitle}"?`, + details: 'This action cannot be undone.', + type: 'danger', + confirmText: 'Delete', + cancelText: 'Cancel', + onConfirm: function() { + // Disable button during request + deleteBtn.disabled = true; + deleteBtn.style.opacity = '0.6'; + + fetch('/admin/content-delete', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-CSRF-TOKEN': csrfToken, + 'X-Requested-With': 'XMLHttpRequest' + }, + body: JSON.stringify({ id: contentId }) + }) + .then(response => response.json()) + .then(data => { + if (data.success) { + // Remove the row from the table + const row = deleteBtn.closest('tr'); + if (row) { + row.style.transition = 'opacity 0.3s'; + row.style.opacity = '0'; + setTimeout(() => { + row.remove(); + + // Check if table is empty now + const tbody = document.querySelector('tbody'); + if (tbody && tbody.children.length === 0) { + location.reload(); // Reload to show "no content found" message + } + }, 300); + } + + if (typeof showToast === 'function') { + showToast(data.message, 'success'); + } + } else { + if (typeof showToast === 'function') { + showToast(data.message || 'Failed to delete content', 'error'); + } + deleteBtn.disabled = false; + deleteBtn.style.opacity = '1'; + } + }) + .catch(error => { + console.error('Error deleting content:', error); + if (typeof showToast === 'function') { + showToast('An error occurred while deleting content', 'error'); + } + deleteBtn.disabled = false; + deleteBtn.style.opacity = '1'; + }); + } + }); + }); +} + diff --git a/resources/views/admin/content/index.blade.php b/resources/views/admin/content/index.blade.php index 5154f34a0..70679d3a4 100644 --- a/resources/views/admin/content/index.blade.php +++ b/resources/views/admin/content/index.blade.php @@ -101,10 +101,18 @@ title="{{ $item->status == 1 ? 'Disable' : 'Enable' }}"> + + @endforeach - data-confirm-delete> + @else diff --git a/routes/web.php b/routes/web.php index 2c51ac26e..34c9b7cff 100644 --- a/routes/web.php +++ b/routes/web.php @@ -201,6 +201,7 @@ Route::middleware('role:Admin', '2fa')->prefix('admin')->group(function () { Route::get('content-list', [AdminContentController::class, 'index'])->name('admin.content-list'); Route::match(['GET', 'POST'], 'content-add', [AdminContentController::class, 'create'])->name('admin.content-add'); Route::post('content-toggle-status', [AdminContentController::class, 'toggleStatus'])->name('admin.content-toggle-status'); + Route::post('content-delete', [AdminContentController::class, 'destroy'])->name('admin.content-delete'); Route::get('category_regexes-list', [AdminCategoryRegexesController::class, 'index'])->name('admin.category-regexes-list'); Route::match(['GET', 'POST'], 'category_regexes-edit', [AdminCategoryRegexesController::class, 'edit'])->name('admin.category-regexes-edit'); Route::get('collection_regexes-list', [AdminCollectionRegexesController::class, 'index'])->name('admin.collection-regexes-list');