Backend for file uploads attached to any generic model, initially on aircraft and airports #226
This commit is contained in:
parent
793b3e7134
commit
e358b8706f
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class CreateFilesTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Create the files table. Acts as a morphable
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('files', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('name');
|
||||
$table->string('description')->nullable();
|
||||
$table->string('path');
|
||||
$table->boolean('public')->default(true);
|
||||
$table->string('ref_model', 50)->nullable();
|
||||
$table->string('ref_model_id', 36)->nullable();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('files');
|
||||
}
|
||||
}
|
88
app/Http/Controllers/Admin/FilesController.php
Normal file
88
app/Http/Controllers/Admin/FilesController.php
Normal file
@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Requests\CreateFilesRequest;
|
||||
use App\Interfaces\Controller;
|
||||
use App\Models\File;
|
||||
use Flash;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Class FilesController
|
||||
* @package App\Http\Controllers\Admin
|
||||
*/
|
||||
class FilesController extends Controller
|
||||
{
|
||||
/**
|
||||
* Store a newly file
|
||||
* @param CreateFilesRequest $request
|
||||
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
||||
*/
|
||||
public function store(CreateFilesRequest $request)
|
||||
{
|
||||
$attrs = $request->post();
|
||||
|
||||
Log::info('Uploading files', $attrs);
|
||||
|
||||
/**
|
||||
* @var $file \Illuminate\Http\UploadedFile
|
||||
*/
|
||||
$file = $request->file('file');
|
||||
$file_path = $file->storeAs(
|
||||
'files',
|
||||
$file->getClientOriginalName(),
|
||||
config('filesystems.public_files')
|
||||
);
|
||||
|
||||
$asset = new File();
|
||||
$asset->name = $attrs['name'];
|
||||
$asset->path = $file_path;
|
||||
$asset->public = true;
|
||||
$asset->ref_model = $attrs['ref_model'];
|
||||
$asset->ref_model_id = $attrs['ref_model_id'];
|
||||
|
||||
$asset->save();
|
||||
|
||||
/*foreach($files as $file) {
|
||||
$file_path = $file->store('files', $file->getClientOriginalName(), 'public');
|
||||
}*/
|
||||
|
||||
# Where do we go now? OooOOoOoOo sweet child
|
||||
$redirect = $attrs['redirect'];
|
||||
if(!$redirect) {
|
||||
$redirect = route('admin.dashboard.index');
|
||||
}
|
||||
|
||||
Flash::success('Files uploaded successfully.');
|
||||
return redirect($redirect);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the file from storage.
|
||||
* @param Request $request
|
||||
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function delete($id, Request $request)
|
||||
{
|
||||
$redirect = $request->post('redirect');
|
||||
if (!$redirect) {
|
||||
$redirect = route('admin.dashboard.index');
|
||||
}
|
||||
|
||||
$file = File::find($id);
|
||||
if (!$file) {
|
||||
Flash::error('File doesn\'t exist');
|
||||
return redirect($redirect);
|
||||
}
|
||||
|
||||
Storage::disk(config('filesystems.public_files'))->delete($file->path);
|
||||
$file->delete();
|
||||
|
||||
Flash::success('File deleted successfully.');
|
||||
return redirect($redirect);
|
||||
}
|
||||
}
|
35
app/Http/Requests/CreateFilesRequest.php
Normal file
35
app/Http/Requests/CreateFilesRequest.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
/**
|
||||
* Class CreateFilesRequest
|
||||
* @package App\Http\Requests
|
||||
*/
|
||||
class CreateFilesRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
* @return array
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => 'required',
|
||||
'file' => 'required|file',
|
||||
|
||||
#'files.*' => 'required|file',
|
||||
];
|
||||
}
|
||||
}
|
35
app/Http/Requests/UpdateFilesRequest.php
Normal file
35
app/Http/Requests/UpdateFilesRequest.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
/**
|
||||
* Class CreateFilesRequest
|
||||
* @package App\Http\Requests
|
||||
*/
|
||||
class UpdateFilesRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
* @return array
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => 'required',
|
||||
'file' => 'nullable|file',
|
||||
|
||||
#'files.*' => 'required|file',
|
||||
];
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ namespace App\Interfaces;
|
||||
|
||||
/**
|
||||
* Class Model
|
||||
* @property mixed $id
|
||||
* @package App\Interfaces
|
||||
*/
|
||||
abstract class Model extends \Illuminate\Database\Eloquent\Model
|
||||
|
@ -5,6 +5,7 @@ namespace App\Models;
|
||||
use App\Interfaces\Model;
|
||||
use App\Models\Enums\AircraftStatus;
|
||||
use App\Models\Traits\ExpensableTrait;
|
||||
use App\Models\Traits\FilesTrait;
|
||||
|
||||
/**
|
||||
* @property int id
|
||||
@ -22,6 +23,7 @@ use App\Models\Traits\ExpensableTrait;
|
||||
class Aircraft extends Model
|
||||
{
|
||||
use ExpensableTrait;
|
||||
use FilesTrait;
|
||||
|
||||
public $table = 'aircraft';
|
||||
|
||||
|
@ -4,6 +4,7 @@ namespace App\Models;
|
||||
|
||||
use App\Interfaces\Model;
|
||||
use App\Models\Traits\ExpensableTrait;
|
||||
use App\Models\Traits\FilesTrait;
|
||||
|
||||
/**
|
||||
* Class Airport
|
||||
@ -16,6 +17,7 @@ use App\Models\Traits\ExpensableTrait;
|
||||
class Airport extends Model
|
||||
{
|
||||
use ExpensableTrait;
|
||||
use FilesTrait;
|
||||
|
||||
public $table = 'airports';
|
||||
public $timestamps = false;
|
||||
|
53
app/Models/File.php
Normal file
53
app/Models/File.php
Normal file
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
|
||||
use App\Interfaces\Model;
|
||||
use App\Models\Traits\ReferenceTrait;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
/**
|
||||
* File property
|
||||
* @property string $name
|
||||
* @property string $description
|
||||
* @property string $path
|
||||
* @property boolean $public
|
||||
* @package App\Models
|
||||
*/
|
||||
class File extends Model
|
||||
{
|
||||
use ReferenceTrait;
|
||||
|
||||
protected $table = 'files';
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'description',
|
||||
'path',
|
||||
'public',
|
||||
'ref_model',
|
||||
'ref_model_id',
|
||||
];
|
||||
|
||||
public static $rules = [
|
||||
'name' => 'required',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the full URL to this attribute
|
||||
* @return string
|
||||
*/
|
||||
public function getUrlAttribute(): string
|
||||
{
|
||||
$disk = config('filesystems.public_files');
|
||||
if ($disk !== 'public') {
|
||||
return Storage::disk(config('filesystems.public_files'))
|
||||
->url($this->path);
|
||||
}
|
||||
|
||||
return public_asset(Storage::disk('public')
|
||||
->url($this->path)
|
||||
);
|
||||
}
|
||||
}
|
@ -7,6 +7,7 @@
|
||||
namespace App\Models;
|
||||
|
||||
use App\Interfaces\Model;
|
||||
use App\Models\Traits\ReferenceTrait;
|
||||
|
||||
/**
|
||||
* @property string id UUID type
|
||||
@ -22,6 +23,8 @@ use App\Interfaces\Model;
|
||||
*/
|
||||
class JournalTransaction extends Model
|
||||
{
|
||||
use ReferenceTrait;
|
||||
|
||||
protected $table = 'journal_transactions';
|
||||
|
||||
public $incrementing = false;
|
||||
@ -61,33 +64,6 @@ class JournalTransaction extends Model
|
||||
return $this->belongsTo(Journal::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Model $object
|
||||
* @return JournalTransaction
|
||||
*/
|
||||
public function referencesObject($object)
|
||||
{
|
||||
$this->ref_model = \get_class($object);
|
||||
$this->ref_model_id = $object->id;
|
||||
$this->save();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function getReferencedObject()
|
||||
{
|
||||
if ($classname = $this->ref_model) {
|
||||
$klass = new $this->ref_model;
|
||||
|
||||
return $klass->find($this->ref_model_id);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $currency
|
||||
*/
|
||||
|
22
app/Models/Traits/FilesTrait.php
Normal file
22
app/Models/Traits/FilesTrait.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Traits;
|
||||
|
||||
use App\Models\File;
|
||||
|
||||
trait FilesTrait
|
||||
{
|
||||
/**
|
||||
* Morph to type of File
|
||||
* @return mixed
|
||||
*/
|
||||
public function files()
|
||||
{
|
||||
return $this->morphMany(
|
||||
File::class,
|
||||
'files', # overridden by the next two anyway
|
||||
'ref_model',
|
||||
'ref_model_id'
|
||||
);
|
||||
}
|
||||
}
|
40
app/Models/Traits/ReferenceTrait.php
Normal file
40
app/Models/Traits/ReferenceTrait.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Traits;
|
||||
|
||||
/**
|
||||
* Trait ReferenceTrait
|
||||
* @property \App\Interfaces\Model $ref_model
|
||||
* @property mixed $ref_model_id
|
||||
* @package App\Models\Traits
|
||||
*/
|
||||
trait ReferenceTrait
|
||||
{
|
||||
/**
|
||||
* @param \App\Interfaces\Model $object
|
||||
* @return self
|
||||
*/
|
||||
public function referencesObject($object)
|
||||
{
|
||||
$this->ref_model = \get_class($object);
|
||||
$this->ref_model_id = $object->id;
|
||||
$this->save();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an instance of the object or null
|
||||
* @return \App\Interfaces\Model|null
|
||||
*/
|
||||
public function getReferencedObject()
|
||||
{
|
||||
if ($classname = $this->ref_model) {
|
||||
$klass = new $this->ref_model;
|
||||
|
||||
return $klass->find($this->ref_model_id);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
@ -34,6 +34,10 @@ Route::group([
|
||||
Route::match(['get', 'post'], 'fares/import', 'FareController@import')->name('fares.import');
|
||||
Route::resource('fares', 'FareController');
|
||||
|
||||
# files
|
||||
Route::post('files', 'FilesController@store')->name('files.store');
|
||||
Route::delete('files/{id}/delete', 'FilesController@delete')->name('files.delete');
|
||||
|
||||
# finances
|
||||
Route::resource('finances', 'FinanceController');
|
||||
|
||||
|
@ -2,6 +2,9 @@
|
||||
|
||||
return [
|
||||
'default' => 'local',
|
||||
|
||||
// This is the filesystem the uploaded files should go to
|
||||
'public_files' => 'public',
|
||||
'cloud' => 's3',
|
||||
'disks' => [
|
||||
|
||||
@ -12,7 +15,8 @@ return [
|
||||
|
||||
'public' => [
|
||||
'driver' => 'local',
|
||||
'root' => app_path('public/assets/upload'),
|
||||
'root' => public_path('uploads'),
|
||||
'url' => '/uploads',
|
||||
'visibility' => 'public',
|
||||
],
|
||||
|
||||
|
4
public/uploads/.gitignore
vendored
Executable file
4
public/uploads/.gitignore
vendored
Executable file
@ -0,0 +1,4 @@
|
||||
*
|
||||
!files/
|
||||
!avatars/
|
||||
!.gitignore
|
2
public/uploads/avatars/.gitignore
vendored
Executable file
2
public/uploads/avatars/.gitignore
vendored
Executable file
@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
2
public/uploads/files/.gitignore
vendored
Executable file
2
public/uploads/files/.gitignore
vendored
Executable file
@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
@ -1,19 +1,28 @@
|
||||
@extends('admin.app')
|
||||
@section('title', "Edit \"$aircraft->name\"")
|
||||
@section('content')
|
||||
<div class="card border-blue-bottom">
|
||||
<div class="content">
|
||||
{{ Form::model($aircraft, ['route' => ['admin.aircraft.update', $aircraft->id], 'method' => 'patch']) }}
|
||||
@include('admin.aircraft.fields')
|
||||
{{ Form::close() }}
|
||||
<div class="card border-blue-bottom">
|
||||
<div class="content">
|
||||
{{ Form::model($aircraft, ['route' => ['admin.aircraft.update', $aircraft->id], 'method' => 'patch']) }}
|
||||
@include('admin.aircraft.fields')
|
||||
{{ Form::close() }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card border-blue-bottom">
|
||||
<div class="content">
|
||||
@include('admin.aircraft.expenses')
|
||||
<div class="card border-blue-bottom">
|
||||
<div class="content">
|
||||
@include('admin.aircraft.expenses')
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card border-blue-bottom">
|
||||
<div class="content">
|
||||
@include('admin.common.file_upload', [
|
||||
'model' => $aircraft,
|
||||
'redirect' => route('admin.aircraft.edit', ['id' => $aircraft->id])
|
||||
])
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
|
||||
@include('admin.aircraft.script')
|
||||
|
@ -18,5 +18,14 @@
|
||||
@include('admin.airports.expenses')
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card border-blue-bottom">
|
||||
<div class="content">
|
||||
@include('admin.common.file_upload', [
|
||||
'model' => $airport,
|
||||
'redirect' => route('admin.airports.edit', ['id' => $airport->id])
|
||||
])
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
@include('admin.airports.script')
|
||||
|
75
resources/views/admin/common/file_upload.blade.php
Normal file
75
resources/views/admin/common/file_upload.blade.php
Normal file
@ -0,0 +1,75 @@
|
||||
{{--
|
||||
|
||||
Pass in:
|
||||
$model - The model instance this belongs to, e.g: ['model' => $airport]
|
||||
$redirect - Where to go to
|
||||
|
||||
--}}
|
||||
<div id="airport-files-wrapper">
|
||||
<div class="header">
|
||||
<h3>files</h3>
|
||||
</div>
|
||||
|
||||
{{-- Show all the files here --}}
|
||||
<table class="table table-hover table-responsive">
|
||||
@if(count($model->files))
|
||||
<thead>
|
||||
<tr>
|
||||
<td>Name</td>
|
||||
<td>Current File</td>
|
||||
<td class="text-right"></td>
|
||||
</tr>
|
||||
</thead>
|
||||
@endif
|
||||
<tbody>
|
||||
@foreach($model->files as $file)
|
||||
<tr>
|
||||
<td>{{ $file->name }}</td>
|
||||
<td><a href="{{ $file->url }}" target="_blank">Link to file</a></td>
|
||||
<td class="text-right">
|
||||
{{ Form::open(['route' => ['admin.files.delete', $file->id], 'method' => 'delete']) }}
|
||||
{{ Form::hidden('id', $file->id) }}
|
||||
{{ Form::hidden('redirect', $redirect) }}
|
||||
{{ Form::button('<i class="fa fa-times"></i>', [
|
||||
'type' => 'submit',
|
||||
'class' => 'btn btn-sm btn-danger btn-icon',
|
||||
'onclick' => "return confirm('Are you sure?')"])
|
||||
}}
|
||||
{{ Form::close() }}
|
||||
</td>
|
||||
</tr>
|
||||
@endforeach
|
||||
</tbody>
|
||||
</table>
|
||||
<hr>
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
<div class="text-right">
|
||||
{{ Form::open([
|
||||
'url' => route('admin.files.store'),
|
||||
'method' => 'POST',
|
||||
'class' => 'form-inline',
|
||||
'files' => true
|
||||
])
|
||||
}}
|
||||
|
||||
{{-- Fields for the model --}}
|
||||
{{ Form::hidden('ref_model', get_class($model)) }}
|
||||
{{ Form::hidden('ref_model_id', $model->id) }}
|
||||
{{ Form::hidden('redirect', $redirect) }}
|
||||
|
||||
{{ Form::label('name', 'Name:') }} <span class="required">*</span>
|
||||
{{ Form::text('name', null, ['class' => 'form-control']) }}
|
||||
|
||||
{{ Form::file('file', ['class' => 'form-control']) }}
|
||||
|
||||
{{ Form::submit('Upload', ['class' => 'btn btn-success']) }}
|
||||
<p class="text-danger">{{ $errors->first('name') }}</p>
|
||||
<p class="text-danger">{{ $errors->first('file') }}</p>
|
||||
|
||||
{{ Form::close() }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
Loading…
Reference in New Issue
Block a user