applicant-site/app/Http/Controllers/FileController.php

110 lines
3.2 KiB
PHP

<?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;
use Illuminate\Support\Facades\Storage;
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');
$files = File::where('reception_screen_id', '=', $idReceptionScreen)->get();
return view('files.create', compact('receptionScreens', 'idReceptionScreen', 'files'));
}
public function store(StoreFileRequest $request)
{
abort_if(Auth::guest(), 403);
$nameFile = $request->file('url')->getClientOriginalName();
$name = Storage::put('public', $request->file('url'));
$validated = $request->validated();
$file = new File();
$file->name = $validated['name'];
$file->file_name = $nameFile;
$file->url = Storage::url($name);
$file->position = $validated['position'];
$file->reception_screen_id = $validated['idReceptionScreen'];
$file->save();
return redirect()->route('files.index');
}
public function download($id)
{
$file = (new File())->find($id);
return Storage::url($file->url);
}
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');
}
}