vernova/app/Http/Controllers/DocumentController.php
2026-07-26 19:58:27 +03:30

178 lines
5.3 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Document;
use App\Models\Project;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
class DocumentController extends Controller
{
/**
* Display a listing of documents.
*/
public function index(Request $request)
{
$query = Document::with(['project', 'uploader']);
if ($request->filled('project_id')) {
$query->where('project_id', $request->project_id);
}
if ($request->filled('file_type')) {
$query->where('file_type', $request->file_type);
}
if ($request->filled('search')) {
$query->search($request->search);
}
$documents = $query->orderBy('created_at', 'desc')
->paginate(20)
->appends($request->query());
$projects = Project::all();
$fileTypes = ['pdf', 'image', 'word', 'excel', 'text', 'other'];
return view('documents.index', compact('documents', 'projects', 'fileTypes'));
}
/**
* Show the form for creating a new document.
*/
public function create()
{
$projects = Project::all();
$document = null;
return view('documents.form', compact('projects', 'document'));
}
/**
* Store a newly created document in storage.
*/
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'required|string|max:255',
'description' => 'nullable|string',
'project_id' => 'nullable|exists:projects,id',
'file' => 'required|file|max:20480',
]);
$file = $request->file('file');
$path = $file->store('documents', 'public');
$extension = strtolower($file->getClientOriginalExtension());
$fileType = match(true) {
$extension === 'pdf' => 'pdf',
in_array($extension, ['jpg', 'jpeg', 'png', 'gif', 'svg', 'webp']) => 'image',
in_array($extension, ['doc', 'docx']) => 'word',
in_array($extension, ['xls', 'xlsx', 'csv']) => 'excel',
in_array($extension, ['txt', 'rtf']) => 'text',
default => 'other',
};
Document::create([
'tenant_id' => $this->getTenantId(),
'project_id' => $validated['project_id'] ?? null,
'title' => $validated['title'],
'description' => $validated['description'] ?? null,
'file_path' => $path,
'file_type' => $fileType,
'uploaded_by' => Auth::id(),
]);
return redirect()
->route('documents.index')
->with('success', __('document.created_successfully'));
}
/**
* Display the specified document.
*/
public function show(Document $document)
{
$document->load(['project', 'uploader']);
return view('documents.show', compact('document'));
}
/**
* Show the form for editing the specified document.
*/
public function edit(Document $document)
{
$projects = Project::all();
return view('documents.form', compact('document', 'projects'));
}
/**
* Update the specified document in storage.
*/
public function update(Request $request, Document $document)
{
$validated = $request->validate([
'title' => 'required|string|max:255',
'description' => 'nullable|string',
'project_id' => 'nullable|exists:projects,id',
'file' => 'nullable|file|max:20480',
]);
if ($request->hasFile('file')) {
// Delete old file
Storage::disk('public')->delete($document->file_path);
$file = $request->file('file');
$path = $file->store('documents', 'public');
$extension = strtolower($file->getClientOriginalExtension());
$fileType = match(true) {
$extension === 'pdf' => 'pdf',
in_array($extension, ['jpg', 'jpeg', 'png', 'gif', 'svg', 'webp']) => 'image',
in_array($extension, ['doc', 'docx']) => 'word',
in_array($extension, ['xls', 'xlsx', 'csv']) => 'excel',
in_array($extension, ['txt', 'rtf']) => 'text',
default => 'other',
};
$validated['file_path'] = $path;
$validated['file_type'] = $fileType;
}
$document->update($validated);
return redirect()
->route('documents.index')
->with('success', __('document.updated_successfully'));
}
/**
* Remove the specified document from storage.
*/
public function destroy(Document $document)
{
Storage::disk('public')->delete($document->file_path);
$document->delete();
return redirect()
->route('documents.index')
->with('success', __('document.deleted_successfully'));
}
/**
* Download the specified document.
*/
public function download(Document $document)
{
if (!Storage::disk('public')->exists($document->file_path)) {
abort(404, __('document.file_not_found'));
}
return Storage::disk('public')->download($document->file_path, $document->title . '.' . pathinfo($document->file_path, PATHINFO_EXTENSION));
}
}