Skip to content

Commit

Permalink
Attachment file update
Browse files Browse the repository at this point in the history
  • Loading branch information
cuneytsenturk committed Dec 29, 2017
1 parent 94b7e9e commit 3e04f12
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 5 deletions.
26 changes: 26 additions & 0 deletions app/Http/Controllers/Common/Uploads.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers\Common;

use App\Http\Controllers\Controller;
use App\Models\Common\Media;
use Storage;

class Uploads extends Controller
Expand Down Expand Up @@ -41,6 +42,31 @@ public function download($folder, $file)
return response()->download($path);
}

/**
* Destroy the specified resource.
*
* @param $folder
* @param $file
* @return callable
*/
public function destroy($folder, $id)
{
$media = Media::find($id);

// Get file path
/*if (!$path = $this->getPath($folder, $id)) {
$message = trans('messages.warning.deleted', ['name' => $file, 'text' => $file]);
flash($message)->warning();
return back();
}*/

$media->delete(); //will not delete files

return back();
}

/**
* Get the full path of resource.
*
Expand Down
14 changes: 14 additions & 0 deletions app/Models/Common/Media.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Models\Common;

use Illuminate\Database\Eloquent\SoftDeletes;
use Plank\Mediable\Media as PMedia;

class Media extends PMedia
{
use SoftDeletes;

protected $dates = ['deleted_at'];

}
2 changes: 1 addition & 1 deletion app/Models/Income/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,6 @@ public function getAttachmentAttribute()
return false;
}

return $this->getMedia('attachment')->first();
return $this->getMedia('attachment')->last();
}
}
2 changes: 1 addition & 1 deletion config/mediable.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,5 +224,5 @@
/**
* Detach associated media when mediable model is soft deleted.
*/
'detach_on_soft_delete' => true,
'detach_on_soft_delete' => false,
];
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function up()
$table->string('aggregate_type', 32);
$table->integer('size')->unsigned();
$table->timestamps();
$table->softDeletes();

$table->index(['disk', 'directory']);
$table->unique(['disk', 'directory', 'filename', 'extension']);
Expand Down
24 changes: 22 additions & 2 deletions resources/views/incomes/invoices/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,23 @@
</div>

@if($invoice->attachment)
<span>
<a href=""><i class="fa fa-file-{{ $invoice->attachment->aggregate_type }}-o"></i> {{ $invoice->attachment->basename }}</a> <i class="fa fa fa-times"></i>
<span class="attachment">
<a href="{{ url('uploads/invoices/' . $invoice->attachment->basename . '/download') }}">
<span id="download-attachment" class="text-primary">
<i class="fa fa-file-{{ $invoice->attachment->aggregate_type }}-o"></i> {{ $invoice->attachment->basename }}
</span>
</a>
{!! Form::open([
'id' => 'attachment-' . $invoice->attachment->id,
'method' => 'DELETE',
'url' => [url('uploads/invoices/' . $invoice->attachment->id)],
'style' => 'display:inline'
]) !!}
{{ Form::hidden('id', $invoice->id) }}
<a id="remove-attachment" href="javascript:void();">
<span class="text-danger"><i class="fa fa fa-times"></i></span>
</a>
{!! Form::close() !!}
</span>
@endif
</div>
Expand Down Expand Up @@ -420,6 +435,11 @@
$('#email-modal').modal('show');
});
@if($invoice->attachment)
$(document).on('click', '#remove-attachment', function (e) {
confirmDelete("#attachment-{!! $invoice->attachment->id !!}", "{!! trans('general.attachment') !!}", "{!! trans('general.delete_confirm', ['name' => '<strong>' . $invoice->attachment->basename . '</strong>', 'type' => strtolower(trans('general.attachment'))]) !!}", "{!! trans('general.cancel') !!}", "{!! trans('general.delete') !!}");
});
@endif
});
function addPayment() {
Expand Down
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
Route::group(['prefix' => 'uploads'], function () {
Route::get('{folder}/{file}', 'Common\Uploads@get');
Route::get('{folder}/{file}/download', 'Common\Uploads@download');
Route::get('{folder}/{file}/destroy', 'Common\Uploads@destroy');
Route::delete('{folder}/{id}', 'Common\Uploads@destroy');
});

Route::group(['middleware' => ['adminmenu', 'permission:read-admin-panel']], function () {
Expand Down

0 comments on commit 3e04f12

Please sign in to comment.