127 lines
3.5 KiB
PHP
127 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use Livewire\Component;
|
|
use Livewire\Attributes\Title;
|
|
use Livewire\WithFileUploads;
|
|
use App\Models\Project;
|
|
use App\Models\Folder;
|
|
use App\Models\Document;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class ProjectShow extends Component
|
|
{
|
|
|
|
use WithFileUploads;
|
|
|
|
public Project $project;
|
|
public ?Folder $currentFolder = null;
|
|
public $expandedFolders = [];
|
|
public $files = [];
|
|
public $folderName = '';
|
|
|
|
public $selectedFolderId = null;
|
|
|
|
|
|
public function mount(Project $project)
|
|
{
|
|
$this->project = $project->load('rootFolders');
|
|
$this->currentFolder = $this->project->rootFolders->first() ?? null;
|
|
}
|
|
|
|
public function selectFolder($folderId)
|
|
{
|
|
$this->selectedFolderId = $folderId;
|
|
$this->currentFolder = Folder::with('children')->find($folderId);
|
|
}
|
|
|
|
public function toggleFolder($folderId): void
|
|
{
|
|
if (in_array($folderId, $this->expandedFolders)) {
|
|
$this->expandedFolders = array_diff($this->expandedFolders, [$folderId]);
|
|
} else {
|
|
$this->expandedFolders[] = $folderId;
|
|
}
|
|
}
|
|
|
|
public function createFolder(): void
|
|
{
|
|
$this->validate([
|
|
'folderName' => [
|
|
'required',
|
|
'max:255',
|
|
Rule::unique('folders', 'name')->where(function ($query) {
|
|
return $query->where('project_id', $this->project->id)
|
|
->where('parent_id', $this->currentFolder?->id);
|
|
})
|
|
]
|
|
]);
|
|
|
|
Folder::create([
|
|
'name' => $this->folderName,
|
|
'project_id' => $this->project->id,
|
|
'parent_id' => $this->currentFolder?->id
|
|
]);
|
|
|
|
$this->reset('folderName');
|
|
$this->project->load('rootFolders'); // Recargar carpetas raíz
|
|
if ($this->currentFolder) {
|
|
$this->currentFolder->load('children'); // Recargar hijos si está en una subcarpeta
|
|
}
|
|
$this->project->refresh();
|
|
}
|
|
|
|
public function uploadFiles(): void
|
|
{
|
|
$this->validate([
|
|
'files.*' => 'file|max:10240|mimes:pdf,docx,xlsx,jpg,png'
|
|
]);
|
|
|
|
foreach ($this->files as $file) {
|
|
Document::create([
|
|
'name' => $file->getClientOriginalName(),
|
|
'file_path' => $file->store("projects/{$this->project->id}/documents"),
|
|
'project_id' => $this->project->id,
|
|
'folder_id' => $this->currentFolder?->id
|
|
]);
|
|
}
|
|
|
|
$this->reset('files');
|
|
if ($this->currentFolder) {
|
|
$this->currentFolder->refresh(); // Recargar documentos
|
|
}
|
|
$this->reset('files');
|
|
}
|
|
|
|
public function getDocumentsProperty()
|
|
{
|
|
return $this->currentFolder
|
|
? $this->currentFolder->documents()->with('versions')->get()
|
|
: Document::whereNull('folder_id')->where('project_id', $this->project->id)->with('versions')->get();
|
|
}
|
|
|
|
public function getBreadcrumbsProperty()
|
|
{
|
|
if (!$this->currentFolder) return collect();
|
|
|
|
$breadcrumbs = collect();
|
|
$folder = $this->currentFolder;
|
|
|
|
while ($folder) {
|
|
$breadcrumbs->prepend($folder);
|
|
$folder = $folder->parent;
|
|
}
|
|
|
|
return $breadcrumbs;
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.project-show')
|
|
->layout('layouts.livewire-app', [
|
|
'title' => $this->project->name
|
|
]);
|
|
}
|
|
}
|