add file resource
This commit is contained in:
parent
df05cd21f4
commit
95deb4cefe
|
@ -0,0 +1,93 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\StoreFileRequest;
|
||||
use App\Http\Requests\StoreReceptionScreenRequest;
|
||||
use App\Http\Requests\UpdateFileRequest;
|
||||
use App\Http\Requests\UpdateReceptionScreenRequest;
|
||||
use App\Models\File;
|
||||
use App\Models\ReceptionScreen;
|
||||
use App\Services\WorkWithFiles;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Contracts\View\Factory;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class FileController extends Controller
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
}
|
||||
public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
|
||||
{
|
||||
abort_if(Auth::guest(), 403);
|
||||
|
||||
$files = File::all()->sortBy('position');
|
||||
return view('files.index', compact('files'));
|
||||
}
|
||||
|
||||
public function create($idReceptionScreen = null): View
|
||||
{
|
||||
abort_if(Auth::guest(), 403);
|
||||
|
||||
$receptionScreens = ReceptionScreen::pluck('name', 'id');
|
||||
$idsReceptionScreens = $receptionScreens->keys()->toArray();
|
||||
$files = File::where('reception_screen_id', '=', $idReceptionScreen)->get();
|
||||
return view('files.create', compact('receptionScreens', 'idsReceptionScreens', 'idReceptionScreen', 'files'));
|
||||
}
|
||||
|
||||
public function store(StoreFileRequest $request)
|
||||
{
|
||||
abort_if(Auth::guest(), 403);
|
||||
|
||||
$content = $request->file('url');
|
||||
WorkWithFiles::saveFileToUploads($content);
|
||||
$newPath = WorkWithFiles::renameFile($content);
|
||||
|
||||
$validated = $request->validated();
|
||||
$file = new File();
|
||||
$file->name = $validated['name'];
|
||||
$file->url = $newPath;
|
||||
$file->position = $validated['position'];
|
||||
$file->reception_screen_id = $validated['idReceptionScreen'];
|
||||
$file->save();
|
||||
return redirect()->route('files.index');
|
||||
}
|
||||
public function edit(int $idFile)
|
||||
{
|
||||
abort_if(Auth::guest(), 403);
|
||||
|
||||
$file = (new File())->find($idFile);
|
||||
$files = File::where('reception_screen_id', '=', $file->reception_screen_id)->get();
|
||||
|
||||
$receptionScreens = ReceptionScreen::pluck('name', 'id');
|
||||
$idsReceptionScreens = $receptionScreens->keys()->toArray();
|
||||
$idReceptionScreen = $file->reception_screen_id;
|
||||
return view('files.edit', compact('receptionScreens', 'idsReceptionScreens', 'idReceptionScreen', 'files', 'file'));
|
||||
}
|
||||
|
||||
public function update(UpdateFileRequest $request, File $file)
|
||||
{
|
||||
abort_if(Auth::guest(), 403);
|
||||
|
||||
$validated = $request->validated();
|
||||
|
||||
$file->name = $validated['name'];
|
||||
$file->position = $validated['position'];
|
||||
$file->reception_screen_id = $validated['idReceptionScreen'];
|
||||
$file->save();
|
||||
return redirect()->route('admin-reception-screen.index');
|
||||
}
|
||||
|
||||
public function destroy($idFile)
|
||||
{
|
||||
abort_if(Auth::guest(), 403);
|
||||
|
||||
$file = (new File())->find($idFile);
|
||||
$file->delete();
|
||||
return redirect()->route('admin-reception-screen.index');
|
||||
}
|
||||
}
|
|
@ -18,6 +18,8 @@ class ReceptionScreenController extends Controller
|
|||
}
|
||||
public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
|
||||
{
|
||||
abort_if(Auth::guest(), 403);
|
||||
|
||||
$receptionScreens = ReceptionScreen::all()->sortBy('position');
|
||||
return view('admin-reception-screen.index', compact('receptionScreens'));
|
||||
}
|
||||
|
@ -32,6 +34,8 @@ class ReceptionScreenController extends Controller
|
|||
|
||||
public function store(StoreReceptionScreenRequest $request)
|
||||
{
|
||||
abort_if(Auth::guest(), 403);
|
||||
|
||||
$validated = $request->validated();
|
||||
$receptionScreen = new ReceptionScreen();
|
||||
$receptionScreen->name = $validated['name'];
|
||||
|
@ -52,6 +56,8 @@ class ReceptionScreenController extends Controller
|
|||
|
||||
public function update(UpdateReceptionScreenRequest $request, $id)
|
||||
{
|
||||
abort_if(Auth::guest(), 403);
|
||||
|
||||
$validated = $request->validated();
|
||||
$receptionScreen = new ReceptionScreen();
|
||||
$currentReceptionScreen = $receptionScreen->find($id);
|
||||
|
|
|
@ -6,10 +6,6 @@ use Illuminate\Http\Request;
|
|||
|
||||
class UploadFileController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
return view('uploadfile');
|
||||
}
|
||||
public function showUploadFile(Request $request)
|
||||
{
|
||||
$file = $request->file('image');
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class StoreFileRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => 'max:255',
|
||||
'position' => 'int|max:255',
|
||||
'url' => 'file',
|
||||
'idReceptionScreen' => 'int|max:255',
|
||||
];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class UpdateFileRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => 'required|max:255',
|
||||
'position' => 'required||int|max:255',
|
||||
'idReceptionScreen' => 'int|max:255',
|
||||
];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Carbon\Carbon;
|
||||
|
||||
class WorkWithFiles
|
||||
{
|
||||
public static function saveFileToUploads($content)
|
||||
{
|
||||
$destinationPath = 'uploads';
|
||||
$content->move($destinationPath, $content->getClientOriginalName());
|
||||
}
|
||||
|
||||
public static function renameFile($content)
|
||||
{
|
||||
$nameChunks = explode('.', $content->getClientOriginalName());
|
||||
array_splice($nameChunks, 1);
|
||||
$timestamp = Carbon::now();
|
||||
$name = implode('.', $nameChunks);
|
||||
$newName = "{$name}{$timestamp}.{$content->getClientOriginalExtension()}";
|
||||
|
||||
$dir = __DIR__;
|
||||
$path = "{$dir}/../../public/uploads/";
|
||||
$oldName = $content->getClientOriginalName();
|
||||
rename("{$path}{$oldName}", "{$path}{$newName}");
|
||||
return "{$path}{$newName}";
|
||||
}
|
||||
}
|
|
@ -43,11 +43,11 @@
|
|||
<tr>
|
||||
<th scope="row">{{ $file->position }}</th>
|
||||
<td>{{ $file->name }}</td>
|
||||
<td><a href="{{ route("admin-reception-screen.edit", $file) }}"
|
||||
<td><a href="{{ route("files.edit", $file) }}"
|
||||
class="btn btn-secondary">редактировать</a>
|
||||
<a rel="nofollow" data-method="delete"
|
||||
data-confirm="Вы действительно хотите удалить?"
|
||||
href="{{ route('admin-reception-screen.destroy', $file) }}"
|
||||
href="{{ route('files.destroy', $file) }}"
|
||||
class="btn btn-danger">
|
||||
удалить
|
||||
</a>
|
||||
|
@ -58,7 +58,7 @@
|
|||
</tbody>
|
||||
</table>
|
||||
<div class="mb-2">
|
||||
<a href="{{ route('admin-reception-screen.create') }}"
|
||||
<a href="{{ route('files.create', $receptionScreen->id) }}"
|
||||
class="btn btn-primary">
|
||||
Добавить файл
|
||||
</a>
|
||||
|
@ -68,7 +68,8 @@
|
|||
@else
|
||||
<tr>
|
||||
<td colspan="3">
|
||||
<a href="{{ route('admin-reception-screen.create') }}" class="btn btn-primary">Добавить
|
||||
@php($idReceptionScreen = $receptionScreen->id)
|
||||
<a href="{{ route('files.create', $idReceptionScreen) }}" class="btn btn-primary">Добавить
|
||||
файл</a>
|
||||
</td>
|
||||
</tr>
|
||||
|
@ -78,11 +79,5 @@
|
|||
</table>
|
||||
<br>
|
||||
<br>
|
||||
|
||||
{{ Form::open(array('url' => route('uploadfile'), 'method' => 'POST', 'files'=>'true')) }}
|
||||
Select the file to upload.
|
||||
{{ Form::file('image') }}
|
||||
{{ Form::submit('Upload File')}}
|
||||
{{Form::close()}}
|
||||
</div>
|
||||
@endsection
|
||||
|
|
|
@ -0,0 +1,79 @@
|
|||
@extends('layouts.admin-layout')
|
||||
@section('content')
|
||||
@auth()
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<h1 class="">Прикрепить файл</h1>
|
||||
{{ Form::open(array('url' => route('files.store'), 'method' => 'POST', 'files'=>'true')) }}
|
||||
<div class="mt-2">
|
||||
{{ Form::label('url', 'Путь к файлу') }}
|
||||
</div>
|
||||
<div class="mt-2">
|
||||
{{ Form::file('url', ['class' => 'form-control']) }}
|
||||
</div>
|
||||
<div>
|
||||
@if ($errors->any())
|
||||
{{ $errors->first('url') }}
|
||||
@endif
|
||||
</div>
|
||||
<div class="mt-2">
|
||||
{{ Form::label('name', 'Имя файла') }}
|
||||
</div>
|
||||
<div class="mt-2">
|
||||
{{ Form::text('name', '', ['class' => 'form-control']) }}
|
||||
</div>
|
||||
<div>
|
||||
@if ($errors->any())
|
||||
{{ $errors->first('name') }}
|
||||
@endif
|
||||
</div>
|
||||
<div class="mt-2">
|
||||
{{ Form::label('position', 'Позиция') }}
|
||||
</div>
|
||||
<div class="mt-2">
|
||||
{{ Form::text('position', 0, ['class' => 'form-control']) }}
|
||||
</div>
|
||||
<div>
|
||||
@if ($errors->any())
|
||||
{{ $errors->first('name') }}
|
||||
@endif
|
||||
</div>
|
||||
<div class="mt-2">
|
||||
{{ Form::label('idReceptionScreen', 'Пункт экрана приема') }}
|
||||
</div>
|
||||
<div class="mt-2">
|
||||
{{ Form::select('idReceptionScreen', $receptionScreens, $idReceptionScreen, $idsReceptionScreens,['class' => 'form-select']) }}
|
||||
</div>
|
||||
<div>
|
||||
@if ($errors->any())
|
||||
{{ $errors->first('idReceptionScreen') }}
|
||||
@endif
|
||||
</div>
|
||||
<div class="mt-4">
|
||||
{{ Form::submit('Загрузить файл', ['class' => 'btn btn-primary'])}}
|
||||
</div>
|
||||
{{Form::close()}}
|
||||
</div>
|
||||
@if($idReceptionScreen !== null)
|
||||
<div class="col">
|
||||
<h2>Файлы пункта Экрана Приема: {{ $receptionScreens[$idReceptionScreen] }}</h2>
|
||||
<table class="table">
|
||||
<thead class="border-b-2 border-solid border-black text-left" style="text-align: left">
|
||||
<tr>
|
||||
<th scope="col">Позиция</th>
|
||||
<th scope="col">Название</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach($files as $file)
|
||||
<tr>
|
||||
<th scope="row">{{ $file->position }}</th>
|
||||
<th scope="row">{{ $file->name }}</th>
|
||||
@endforeach
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
@endif
|
||||
</div>
|
||||
@endauth
|
||||
@endsection
|
|
@ -0,0 +1,68 @@
|
|||
@extends('layouts.admin-layout')
|
||||
@section('content')
|
||||
@auth()
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<h1 class="">Изменить файл</h1>
|
||||
{{ Form::open(array('url' => route('files.update', $file), 'method' => 'PATCH', 'files'=>'true')) }}
|
||||
<div class="mt-2">
|
||||
{{ Form::label('name', 'Имя файла') }}
|
||||
</div>
|
||||
<div class="mt-2">
|
||||
{{ Form::text('name', $file->name, ['class' => 'form-control']) }}
|
||||
</div>
|
||||
<div>
|
||||
@if ($errors->any())
|
||||
{{ $errors->first('name') }}
|
||||
@endif
|
||||
</div>
|
||||
<div class="mt-2">
|
||||
{{ Form::label('position', 'Позиция') }}
|
||||
</div>
|
||||
<div class="mt-2">
|
||||
{{ Form::text('position', $file->position, ['class' => 'form-control']) }}
|
||||
</div>
|
||||
<div>
|
||||
@if ($errors->any())
|
||||
{{ $errors->first('name') }}
|
||||
@endif
|
||||
</div>
|
||||
<div class="mt-2">
|
||||
{{ Form::label('idReceptionScreen', 'Пункт экрана приема') }}
|
||||
</div>
|
||||
<div class="mt-2">
|
||||
{{ Form::select('idReceptionScreen', $receptionScreens, $idReceptionScreen, $idsReceptionScreens,['class' => 'form-select']) }}
|
||||
</div>
|
||||
<div>
|
||||
@if ($errors->any())
|
||||
{{ $errors->first('idReceptionScreen') }}
|
||||
@endif
|
||||
</div>
|
||||
<div class="mt-4">
|
||||
{{ Form::submit('Загрузить файл', ['class' => 'btn btn-primary'])}}
|
||||
</div>
|
||||
{{Form::close()}}
|
||||
</div>
|
||||
@if($idReceptionScreen !== null)
|
||||
<div class="col">
|
||||
<h2>Файлы пункта Экрана Приема: {{ $receptionScreens[$idReceptionScreen] }}</h2>
|
||||
<table class="table">
|
||||
<thead class="border-b-2 border-solid border-black text-left" style="text-align: left">
|
||||
<tr>
|
||||
<th scope="col">Позиция</th>
|
||||
<th scope="col">Название</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach($files as $file)
|
||||
<tr>
|
||||
<th scope="row">{{ $file->position }}</th>
|
||||
<th scope="row">{{ $file->name }}</th>
|
||||
@endforeach
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
@endif
|
||||
</div>
|
||||
@endauth
|
||||
@endsection
|
|
@ -0,0 +1,36 @@
|
|||
@extends('layouts.admin-layout')
|
||||
@section('content')
|
||||
<div class="container">
|
||||
<h2>Файлы</h2>
|
||||
<br>
|
||||
<a href="{{ route('files.create') }}" class="btn btn-primary">Добавить файл</a>
|
||||
<br>
|
||||
<br>
|
||||
<table class="table">
|
||||
<thead class="border-b-2 border-solid border-black text-left" style="text-align: left">
|
||||
<tr>
|
||||
<th scope="col">Позиция</th>
|
||||
<th scope="col">Название файла</th>
|
||||
<th scope="col">Экран Приема</th>
|
||||
<th scope="col">действия</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach($files as $file)
|
||||
<tr>
|
||||
<th scope="row">{{ $file->position }}</th>
|
||||
<td>{{ $file->name }}</td>
|
||||
<td>{{ $file->receptionScreen->name }}</td>
|
||||
<td><a href="{{ route("files.edit", $file) }}" class="btn btn-secondary">редактировать</a>
|
||||
<a rel="nofollow" data-method="delete" data-confirm="Вы действительно хотите удалить?"
|
||||
href="{{ route('files.destroy', $file) }}" class="btn btn-danger">
|
||||
удалить
|
||||
</a>
|
||||
</td>
|
||||
@endforeach
|
||||
</tbody>
|
||||
</table>
|
||||
<br>
|
||||
<br>
|
||||
</div>
|
||||
@endsection
|
|
@ -1,5 +1,6 @@
|
|||
<?php
|
||||
|
||||
use App\Http\Controllers\FileController;
|
||||
use App\Http\Controllers\ReceptionScreenController;
|
||||
use App\Http\Controllers\ProfileController;
|
||||
use App\Http\Controllers\UploadFileController;
|
||||
|
@ -21,10 +22,15 @@ Route::get('/', function () {
|
|||
return view('home');
|
||||
})->name('home');
|
||||
|
||||
Route::resources([
|
||||
'/users' => UserController::class,
|
||||
'/admin-reception-screen' => ReceptionScreenController::class
|
||||
]);
|
||||
Route::resource('/users', UserController::class)->middleware(['auth', 'verified']);
|
||||
Route::resource('/admin-reception-screen', ReceptionScreenController::class)->middleware(['auth', 'verified']);
|
||||
//Route::resource('/files', FileController::class)->middleware(['auth', 'verified']);
|
||||
Route::get('/files', [FileController::class, 'index'])->name('files.index');
|
||||
Route::post('/files', [FileController::class, 'store'])->name('files.store');
|
||||
Route::get('/files/create/{file?}', [FileController::class, 'create'])->name('files.create');
|
||||
Route::patch('/files/{file}', [FileController::class, 'update'])->name('files.update');
|
||||
Route::delete('files/{file}', [FileController::class, 'destroy'])->name('files.destroy');
|
||||
Route::get('/files/edit/{file}', [FileController::class, 'edit'])->name('files.edit');
|
||||
|
||||
Route::get('/course', function () {
|
||||
return view('menu.course');
|
||||
|
@ -62,7 +68,6 @@ Route::get('/video-materials-for-applicants', function () {
|
|||
return view('menu.abitur.videomaterialy-dlya-postupayushchikh');
|
||||
})->name('videomaterialy-dlya-postupayushchikh');
|
||||
|
||||
Route::post('/uploadfile', [UploadFileController::class, 'showUploadFile'])->name('uploadfile');
|
||||
|
||||
Route::get('/dashboard', function () {
|
||||
return view('admin');
|
||||
|
|
Loading…
Reference in New Issue