Files
construprogress/app/Livewire/Projects/FeatureManager.php
T

88 lines
2.3 KiB
PHP
Raw Normal View History

<?php
namespace App\Livewire\Projects;
use App\Models\Feature;
use App\Models\FeatureType;
use App\Models\Project;
use Illuminate\Support\Facades\Auth;
use Livewire\Attributes\Layout;
use Livewire\Attributes\On;
use Livewire\Component;
#[Layout('layouts.app')]
class FeatureManager extends Component
{
public Project $project;
2026-08-28 13:04:28 +02:00
public $featureTypes = [];
// Edit modal
public bool $showForm = false;
2026-08-28 13:04:28 +02:00
public $editingId = null;
2026-08-28 13:04:28 +02:00
public string $name = '';
2026-08-28 13:04:28 +02:00
public $featureTypeId = '';
2026-08-28 13:04:28 +02:00
public bool $isActive = true;
public function mount(Project $project)
{
$this->project = $project;
abort_unless($this->canManage(), 403);
$this->featureTypes = FeatureType::orderBy('name')->get();
}
private function canManage(): bool
{
$user = Auth::user();
2026-08-28 13:04:28 +02:00
return $user->can('manage all')
|| ($user->can('edit layers') && $this->project->users()->where('user_id', $user->id)->exists());
}
#[On('feature-edit')]
public function editFeature($id): void
{
abort_unless($this->canManage(), 403);
$feature = $this->findFeature($id);
2026-08-28 13:04:28 +02:00
$this->editingId = $feature->id;
$this->name = $feature->name ?? '';
$this->featureTypeId = $feature->feature_type_id ?? '';
2026-08-28 13:04:28 +02:00
$this->isActive = (bool) $feature->is_active;
$this->resetErrorBag();
$this->showForm = true;
}
public function save(): void
{
abort_unless($this->canManage(), 403);
$this->validate([
2026-08-28 13:04:28 +02:00
'name' => 'required|string|max:255',
'featureTypeId' => 'nullable|exists:feature_types,id',
]);
$this->findFeature($this->editingId)->update([
2026-08-28 13:04:28 +02:00
'name' => $this->name,
'feature_type_id' => $this->featureTypeId ?: null,
2026-08-28 13:04:28 +02:00
'is_active' => $this->isActive,
]);
$this->showForm = false;
$this->dispatch('features-changed');
$this->dispatch('notify', 'Elemento actualizado');
}
private function findFeature($id): Feature
{
return Feature::whereHas('layer.phase', fn ($q) => $q->where('project_id', $this->project->id))->findOrFail($id);
}
public function render()
{
return view('livewire.projects.feature-manager');
}
}