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

108 lines
3.3 KiB
PHP
Raw Normal View History

2024-01-26 11:15:11 +03:00
<?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;
2024-01-30 10:55:31 +03:00
use Illuminate\Support\Facades\Storage;
2024-01-26 11:15:11 +03:00
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);
2024-01-30 10:55:31 +03:00
$url = $request->file('file');
$return = Storage::put('file.jpg', $content);
dd($return);
2024-01-26 11:15:11 +03:00
$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;
2024-01-29 09:44:53 +03:00
return view(
'files.edit',
compact(
'receptionScreens',
'idsReceptionScreens',
'idReceptionScreen',
'files',
'file'
)
);
2024-01-26 11:15:11 +03:00
}
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');
}
}