Skip to content
This repository has been archived by the owner on May 7, 2019. It is now read-only.

Commit

Permalink
Add button to remove all uploads (#27)
Browse files Browse the repository at this point in the history
* Add button to remove all user uploads

* Fix N+1 problem when deleting all uploads
  • Loading branch information
X11 authored and TheReverend403 committed Feb 5, 2017
1 parent 180a6f6 commit 88fd233
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
20 changes: 20 additions & 0 deletions app/Http/Controllers/Account/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Http\Requests;
use App\Models\Upload;
use App\Models\User;
use DB;
use Illuminate\Http\Request;
use Illuminate\Mail\Message;
use Mail;
Expand Down Expand Up @@ -53,6 +54,25 @@ public function deleteUpload(Request $request, Upload $upload)
return redirect()->route('account.uploads');
}

public function deleteAllUploads(Request $request)
{
$uploads = $request->user()->uploads()->get();
$uploadIds = [];
foreach ($uploads as $upload) {
$upload->deleteDirs();
$upload->invalidateCache();
$uploadIds[] = $upload->id;
}

DB::table('uploads')->whereIn('id', $uploadIds)->delete();

$request->user()->invalidateCache();

flash()->success(trans('messages.all_uploads_deleted'));

return redirect()->route('account.uploads');
}

public function postResetKey(Request $request)
{
do {
Expand Down
3 changes: 3 additions & 0 deletions app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@
Route::get('/', [
'as' => 'account.uploads', 'uses' => 'Account\AccountController@getUploads']);

Route::post('/delete', [
'as' => 'account.uploads.delete_all', 'uses' => 'Account\AccountController@deleteAllUploads']);

Route::post('{upload}/delete', [
'as' => 'account.uploads.delete', 'uses' => 'Account\AccountController@deleteUpload']);

Expand Down
5 changes: 4 additions & 1 deletion resources/assets/js/global.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
$(function () {
$('#flash-overlay-modal').modal();
$('div.alert').not('.alert-important').delay(3000).slideUp();
});
$('form[confirm]').on('submit', function(){
return window.confirm(this.getAttribute('confirm'));
});
});
3 changes: 3 additions & 0 deletions resources/lang/en/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
'confirmation_pending' => 'Please check your email for a confirmation code from ' . config('mail.from.address'),
'not_confirmed' => 'Your account has not been confirmed. Please check your email for a confirmation code from ' . config('mail.from.address'),
'no_such_confirmation_code' => 'We couldn\'t find a pending registration with that confirmation code.',
'delete_all_uploads' => 'Delete uploads',
'confirm_deletion' => 'Are you sure you want to delete all your uploads?',
'all_uploads_deleted' => 'All your uploads have been deleted',
'admin' => [
'no_uploads_found' => ':name has no uploads.',
'unbanned_user' => 'Unbanned :name.',
Expand Down
7 changes: 7 additions & 0 deletions resources/views/account/uploads.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

@section('content')
@if(count($uploads))
<div class="actions-area text-right">
<form action="{{ route('account.uploads.delete_all') }}" confirm="{{ trans('messages.confirm_deletion') }}" method="POST">
<button type="submit" class="btn btn-danger btn-sm" title="Delete" >{{ trans('messages.delete_all_uploads') }} <i class="fa fa-remove"></i></button>
{!! csrf_field() !!}
</form>
</div>
<br>
@foreach ($uploads->chunk(2) as $chunk)
<div class="row">
@foreach ($chunk as $upload)
Expand Down

0 comments on commit 88fd233

Please sign in to comment.