Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add redirect option to delete button script #5723

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/app/Http/Controllers/Operations/DeleteOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ protected function setupDeleteDefaults()
$this->crud->operation(['list', 'show'], function () {
$this->crud->addButton('line', 'delete', 'view', 'crud::buttons.delete', 'end');
});

// setup the default redirect to where user will be redirected after delete
// if user has access to list, redirect to list, otherwise redirect to previous page
$this->crud->operation('show', function () {
$this->crud->setOperationSetting('deleteButtonRedirect', function () {
if ($this->crud->hasAccess('list')) {
return url($this->crud->route);
}

return url()->previous();
});
});
}

/**
Expand Down
141 changes: 83 additions & 58 deletions src/resources/views/crud/buttons/delete.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
@php
$redirectUrl = $crud->getOperationSetting('deleteButtonRedirect');
if($redirectUrl && $redirectUrl instanceof \Closure){
$redirectUrl = $redirectUrl();
}
$redirectUrl = filter_var($redirectUrl, FILTER_VALIDATE_URL) ? $redirectUrl : null;
@endphp
@if ($crud->hasAccess('delete', $entry))
<a href="javascript:void(0)" onclick="deleteEntry(this)" bp-button="delete" data-route="{{ url($crud->route.'/'.$entry->getKey()) }}" class="btn btn-sm btn-link" data-button-type="delete">
<a href="javascript:void(0)" onclick="deleteEntry(this)" bp-button="delete" data-redirect-route="{{ $redirectUrl }}" data-route="{{ url($crud->route.'/'.$entry->getKey()) }}" class="btn btn-sm btn-link" data-button-type="delete">
<i class="la la-trash"></i> <span>{{ trans('backpack::crud.delete') }}</span>
</a>
@endif
Expand All @@ -10,8 +17,7 @@
@push('after_scripts') @if (request()->ajax()) @endpush @endif
@bassetBlock('backpack/crud/buttons/delete-button-'.app()->getLocale().'.js')
<script>

if (typeof deleteEntry != 'function') {
if (typeof deleteEntry !== 'function') {
$("[data-button-type=delete]").unbind('click');

function deleteEntry(button) {
Expand Down Expand Up @@ -40,70 +46,89 @@ className: "bg-danger",
},
dangerMode: true,
}).then((value) => {
function showDeleteNotyAlert() {
// Show a success notification bubble
new Noty({
type: "success",
text: "{!! '<strong>'.trans('backpack::crud.delete_confirmation_title').'</strong><br>'.trans('backpack::crud.delete_confirmation_message') !!}"
}).show();
}
if (value) {
$.ajax({
url: route,
type: 'DELETE',
success: function(result) {
if (result == 1) {
// Redraw the table
if (typeof crud != 'undefined' && typeof crud.table != 'undefined') {
// Move to previous page in case of deleting the only item in table
if(crud.table.rows().count() === 1) {
crud.table.page("previous");
}
if (result == 1) {
// Redraw the table in case we are on a page with a table
if (typeof crud != 'undefined' && typeof crud.table != 'undefined') {
// Move to previous page in case of deleting the only item in table
if(crud.table.rows().count() === 1) {
crud.table.page("previous");
}

crud.table.draw(false);
}
crud.table.draw(false);

// Show a success notification bubble
new Noty({
type: "success",
text: "{!! '<strong>'.trans('backpack::crud.delete_confirmation_title').'</strong><br>'.trans('backpack::crud.delete_confirmation_message') !!}"
}).show();
// Hide the modal, if any is displayed
$('.dtr-modal-close').click();

// Hide the modal, if any
$('.modal').modal('hide');
} else {
// if the result is an array, it means
// we have notification bubbles to show
if (result instanceof Object) {
// trigger one or more bubble notifications
Object.entries(result).forEach(function(entry, index) {
var type = entry[0];
entry[1].forEach(function(message, i) {
new Noty({
type: type,
text: message
}).show();
});
});
} else {// Show an error alert
swal({
title: "{!! trans('backpack::crud.delete_confirmation_not_title') !!}",
text: "{!! trans('backpack::crud.delete_confirmation_not_message') !!}",
icon: "error",
timer: 4000,
buttons: false,
});
}
}
},
error: function(result) {
// Show an alert with the result
swal({
title: "{!! trans('backpack::crud.delete_confirmation_not_title') !!}",
text: "{!! trans('backpack::crud.delete_confirmation_not_message') !!}",
icon: "error",
timer: 4000,
buttons: false,
});
}
});
}
});
showDeleteNotyAlert();
}else{
// there is no crud table in the current page, so we will redirect the user to the defined button redirect route in data-redirect-url
let redirectRoute = $(button).data('redirect-route');
if(redirectRoute){
// queue the alert in localstorage to show it after the redirect
localStorage.setItem('backpack_alerts', JSON.stringify({
'success': [
"{!! '<strong>'.trans('backpack::crud.delete_confirmation_title').'</strong><br>'.trans('backpack::crud.delete_confirmation_message') !!}"
]
}));
window.location.href = redirectRoute;
}else{
// Show a success notification bubble, keep the previous behaviour of not redirecting
// and keeping the entry open after deletion
showDeleteNotyAlert();
}
}
} else {
// if the result is an array, it means
// we have notification bubbles to show
if (result instanceof Object) {
// trigger one or more bubble notifications
Object.entries(result).forEach(function(entry, index) {
var type = entry[0];
entry[1].forEach(function(message, i) {
new Noty({
type: type,
text: message
}).show();
});
});
} else {// Show an error alert
swal({
title: "{!! trans('backpack::crud.delete_confirmation_not_title') !!}",
text: "{!! trans('backpack::crud.delete_confirmation_not_message') !!}",
icon: "error",
timer: 4000,
buttons: false,
});
}
}
},
error: function(result) {
// Show an alert with the result
swal({
title: "{!! trans('backpack::crud.delete_confirmation_not_title') !!}",
text: "{!! trans('backpack::crud.delete_confirmation_not_message') !!}",
icon: "error",
timer: 4000,
buttons: false,
});
}
});
}
});

}
}
}

// make it so that the function above is run after each DataTable draw event
Expand Down