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

106 lines
3.2 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;
2024-02-10 15:14:02 +03:00
use App\Models\Documents;
2024-01-26 11:15:11 +03:00
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 DocumentController extends Controller
2024-01-26 11:15:11 +03:00
{
public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
abort_if(Auth::guest(), 403);
2024-02-10 15:14:02 +03:00
$files = Documents::all()->sortBy('position');
return view('documents.index', compact('files'));
2024-01-26 11:15:11 +03:00
}
public function create($idReceptionScreen = null): View
{
abort_if(Auth::guest(), 403);
$receptionScreens = ReceptionScreen::pluck('name', 'id');
2024-02-10 15:14:02 +03:00
$files = Documents::where('reception_screen_id', '=', $idReceptionScreen)->get();
return view('files.create', compact('receptionScreens', 'idReceptionScreen', 'files'));
2024-01-26 11:15:11 +03:00
}
public function store(StoreFileRequest $request)
{
abort_if(Auth::guest(), 403);
2024-01-31 08:47:05 +03:00
$nameFile = $request->file('url')->getClientOriginalName();
$name = Storage::put('public', $request->file('url'));
2024-01-30 10:55:31 +03:00
2024-01-26 11:15:11 +03:00
$validated = $request->validated();
2024-02-10 15:14:02 +03:00
$file = new Documents();
2024-01-26 11:15:11 +03:00
$file->name = $validated['name'];
2024-01-31 08:47:05 +03:00
$file->file_name = $nameFile;
$file->url = Storage::url($name);
2024-01-26 11:15:11 +03:00
$file->position = $validated['position'];
$file->reception_screen_id = $validated['idReceptionScreen'];
$file->save();
2024-01-31 08:47:05 +03:00
2024-01-26 11:15:11 +03:00
return redirect()->route('files.index');
}
2024-01-31 08:47:05 +03:00
public function download($id)
{
2024-02-10 15:14:02 +03:00
$file = (new Documents())->find($id);
2024-01-31 08:47:05 +03:00
return Storage::url($file->url);
}
2024-01-26 11:15:11 +03:00
public function edit(int $idFile)
{
abort_if(Auth::guest(), 403);
2024-02-10 15:14:02 +03:00
$file = (new Documents())->find($idFile);
$files = Documents::where('reception_screen_id', '=', $file->reception_screen_id)->get();
2024-01-26 11:15:11 +03:00
$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
}
2024-02-10 15:14:02 +03:00
public function update(UpdateFileRequest $request, Documents $file)
2024-01-26 11:15:11 +03:00
{
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);
2024-02-10 15:14:02 +03:00
$file = (new Documents())->find($idFile);
2024-01-26 11:15:11 +03:00
$file->delete();
return redirect()->route('admin-reception-screen.index');
}
}