Roman_applicant-site/app/Http/Controllers/admin/DocumentController.php

100 lines
3.3 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Http\Controllers\admin;
use App\Http\Controllers\Controller;
use App\Http\Requests\admin\StoreDocumentRequest;
use App\Http\Requests\admin\UpdateDocumentRequest;
use App\Models\Admission;
use App\Models\Document;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Foundation\Application;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Storage;
class DocumentController extends Controller
{
public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$documents = Document::all();
return view('admin.documents.index', compact('documents'));
}
public function create(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$admissions = Admission::pluck('name', 'id');
return view('admin.documents.create', compact('admissions'));
}
public function store(StoreDocumentRequest $request): RedirectResponse
{
$this->saveFile($request);
return redirect()->route('documents.index');
}
public function edit(Document $document)
{
$admissions = Admission::pluck('name', 'id');
return view('admin.documents.edit', compact('admissions', 'document'));
}
public function show(Document $document): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
return view('admin.documents.show', compact('document'));
}
public function update(UpdateDocumentRequest $request, Document $document): RedirectResponse
{
$validated = $request->validated();
$document->name = $validated['name'];
$document->description = $validated['description'];
$document->position = $validated['position'];
$document->admission_id = $validated['admission_id'];
$document->save();
return redirect()->route('documents.index');
}
public function destroy(Document $document)
{
$document->delete();
return redirect()->route('documents.index');
}
public function download($id)
{
$file = (new Document())->find($id);
return Storage::url($file->url);
}
public function createFromAdmission(Admission $admission): View
{
$admissions = Admission::pluck('name', 'id');
$documents = Document::where('admission_id', '=', $admission->id)->get();
return view('admin.documents.create_from_admission', compact('admissions', 'admission', 'documents'));
}
public function storeFromAdmission(StoreDocumentRequest $request): RedirectResponse
{
$this->saveFile($request);
return redirect()->route('admissions.index');
}
private function saveFile(StoreDocumentRequest $request)
{
$fileName = $request->file('document')->getClientOriginalName();
$name = Storage::put('public', $request->file('document'));
$validated = $request->validated();
$document = new Document();
$document->name = $validated['name'];
$document->description = $validated['description'];
$document->file_name = $fileName;
$document->url = Storage::url($name);
$document->position = $validated['position'];
$document->admission_id = $validated['admission_id'];
$document->save();
}
}