refactor(livewire): organizar componentes y vistas por dominio en subnamespaces
- app/Livewire: 34 componentes agrupados en Issues/, Projects/, Phases/, Companies/, Users/, Admin/, Inspections/, Layers/, Media/, Common/ (Client/, Reports/, Forms/, Actions/ ya estaban). Namespaces actualizados. - resources/views/livewire: vistas sueltas movidas a subcarpetas espejo (companies/, users/, phases/, roles/, inspections/, media/, common/); render() actualizado. - Referencias actualizadas sin romper nada: rutas (FQN, nombres de ruta intactos), tags <livewire:...>/@livewire() a alias con punto, y use de los tests. - No tocado: Volt de Breeze (auth/profile/navigation), y el portal cliente (user-nav/client-projects) que ya tenía referencias inconsistentes. Verificado: 69 rutas OK, vistas compilan, suite 69 passing (solo 2 pre-existentes sqlite). autoload regenerado con --ignore-platform-reqs (PHP 8.2). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Projects;
|
||||
|
||||
use App\Models\Company;
|
||||
use App\Models\Project;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Livewire\Attributes\On;
|
||||
use Livewire\Component;
|
||||
|
||||
class ProjectCompanies extends Component
|
||||
{
|
||||
public Project $project;
|
||||
public $allCompanies = [];
|
||||
public $selectedCompanyId = '';
|
||||
public $selectedRole = 'other';
|
||||
|
||||
public function mount(Project $project)
|
||||
{
|
||||
$this->project = $project;
|
||||
$this->loadAvailable();
|
||||
}
|
||||
|
||||
/** Companies not yet assigned to the project (for the dropdown). */
|
||||
public function loadAvailable(): void
|
||||
{
|
||||
$assignedIds = $this->project->companies()->pluck('companies.id')->toArray();
|
||||
$this->allCompanies = Company::whereNotIn('id', $assignedIds)->orderBy('name')->get();
|
||||
}
|
||||
|
||||
/** Reload the dropdown when the embedded table changes assignments. */
|
||||
#[On('project-companies-changed')]
|
||||
public function onCompaniesChanged(): void
|
||||
{
|
||||
$this->loadAvailable();
|
||||
}
|
||||
|
||||
public function assignCompany()
|
||||
{
|
||||
abort_unless(Auth::user()->can('assign companies'), 403);
|
||||
|
||||
$this->validate([
|
||||
'selectedCompanyId' => 'required|exists:companies,id',
|
||||
'selectedRole' => 'required|in:' . implode(',', array_keys(ProjectCompaniesTable::ROLES)),
|
||||
]);
|
||||
|
||||
$this->project->companies()->attach($this->selectedCompanyId, [
|
||||
'role_in_project' => $this->selectedRole,
|
||||
]);
|
||||
|
||||
$this->reset(['selectedCompanyId', 'selectedRole']);
|
||||
$this->loadAvailable();
|
||||
$this->dispatch('project-companies-changed');
|
||||
$this->dispatch('notify', 'Empresa asignada al proyecto.');
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.projects.project-companies', [
|
||||
'roles' => ProjectCompaniesTable::ROLES,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Projects;
|
||||
|
||||
use App\Models\Company;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Livewire\Attributes\On;
|
||||
use Rappasoft\LaravelLivewireTables\DataTableComponent;
|
||||
use Rappasoft\LaravelLivewireTables\Views\Column;
|
||||
use Rappasoft\LaravelLivewireTables\Views\Filters\SelectFilter;
|
||||
|
||||
class ProjectCompaniesTable extends DataTableComponent
|
||||
{
|
||||
protected $model = Company::class;
|
||||
|
||||
public int $projectId;
|
||||
|
||||
/** role_in_project => label */
|
||||
public const ROLES = [
|
||||
'owner' => 'Promotor',
|
||||
'constructor' => 'Constructor',
|
||||
'subcontractor' => 'Subcontratista',
|
||||
'consultant' => 'Consultor',
|
||||
'supplier' => 'Proveedor',
|
||||
'other' => 'Otro',
|
||||
];
|
||||
|
||||
public function configure(): void
|
||||
{
|
||||
$this->setPrimaryKey('id')
|
||||
->setDefaultSort('companies.name', 'asc')
|
||||
->setSortingPillsEnabled(false)
|
||||
->setAdditionalSelects(['companies.id as id', 'company_project.role_in_project as role_in_project']);
|
||||
}
|
||||
|
||||
#[On('project-companies-changed')]
|
||||
public function refreshRows(): void
|
||||
{
|
||||
// no-op: triggers re-render so the builder re-runs.
|
||||
}
|
||||
|
||||
public function builder(): Builder
|
||||
{
|
||||
return Company::query()
|
||||
->join('company_project', 'company_project.company_id', '=', 'companies.id')
|
||||
->where('company_project.project_id', $this->projectId);
|
||||
}
|
||||
|
||||
public function columns(): array
|
||||
{
|
||||
return [
|
||||
Column::make('Empresa', 'name')
|
||||
->sortable()
|
||||
->searchable()
|
||||
->format(function ($value, $row) {
|
||||
$initial = strtoupper(mb_substr($value ?? '?', 0, 1));
|
||||
$html = '<div class="flex items-center gap-2">
|
||||
<span class="w-7 h-7 rounded-full bg-primary text-primary-content flex items-center justify-center text-xs font-bold shrink-0">'.$initial.'</span>
|
||||
<div><span class="font-medium">'.e($value).'</span>';
|
||||
if ($row->tax_id) {
|
||||
$html .= '<div class="text-xs text-base-content/50">'.e($row->tax_id).'</div>';
|
||||
}
|
||||
$html .= '</div></div>';
|
||||
return $html;
|
||||
})
|
||||
->html(),
|
||||
|
||||
Column::make('Rol', 'role_in_project')
|
||||
->label(function ($row) {
|
||||
$current = $row->role_in_project;
|
||||
if (! Auth::user()->can('assign companies')) {
|
||||
return '<span class="badge badge-sm">'.(self::ROLES[$current] ?? ucfirst((string) $current)).'</span>';
|
||||
}
|
||||
$opts = '';
|
||||
foreach (self::ROLES as $val => $label) {
|
||||
$opts .= '<option value="'.$val.'"'.($current === $val ? ' selected' : '').'>'.$label.'</option>';
|
||||
}
|
||||
return '<select wire:change="changeRole('.$row->id.', $event.target.value)" class="select select-bordered select-xs">'.$opts.'</select>';
|
||||
})
|
||||
->html(),
|
||||
|
||||
Column::make('Acciones')
|
||||
->label(function ($row) {
|
||||
if (! Auth::user()->can('assign companies')) {
|
||||
return '';
|
||||
}
|
||||
return '<div class="flex justify-end">
|
||||
<button wire:click="removeCompany('.$row->id.')" wire:confirm="¿Quitar a '.e($row->name).' del proyecto?"
|
||||
class="btn btn-xs btn-error btn-outline" title="Quitar del proyecto">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="w-3.5 h-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/></svg>
|
||||
</button>
|
||||
</div>';
|
||||
})
|
||||
->html(),
|
||||
];
|
||||
}
|
||||
|
||||
public function filters(): array
|
||||
{
|
||||
return [
|
||||
SelectFilter::make('Rol', 'role')
|
||||
->options(['' => 'Rol: todos'] + self::ROLES)
|
||||
->filter(fn (Builder $query, string $value) => $query->where('company_project.role_in_project', $value)),
|
||||
];
|
||||
}
|
||||
|
||||
public function changeRole($companyId, $role): void
|
||||
{
|
||||
abort_unless(Auth::user()->can('assign companies'), 403);
|
||||
if (! array_key_exists($role, self::ROLES)) {
|
||||
return;
|
||||
}
|
||||
\App\Models\Project::findOrFail($this->projectId)
|
||||
->companies()->updateExistingPivot($companyId, ['role_in_project' => $role]);
|
||||
$this->dispatch('project-companies-changed');
|
||||
$this->dispatch('notify', 'Rol actualizado.');
|
||||
}
|
||||
|
||||
public function removeCompany($companyId): void
|
||||
{
|
||||
abort_unless(Auth::user()->can('assign companies'), 403);
|
||||
\App\Models\Project::findOrFail($this->projectId)->companies()->detach($companyId);
|
||||
$this->dispatch('project-companies-changed');
|
||||
$this->dispatch('notify', 'Empresa eliminada del proyecto.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Projects;
|
||||
|
||||
use Livewire\Component;
|
||||
use Livewire\Attributes\Layout;
|
||||
use App\Models\Project;
|
||||
use App\Models\Phase;
|
||||
use App\Models\Feature;
|
||||
use App\Models\Inspection;
|
||||
use App\Models\Issue;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
#[Layout('layouts.app')]
|
||||
class ProjectDashboard extends Component
|
||||
{
|
||||
public Project $project;
|
||||
|
||||
// Computed stats (cached as properties after mount)
|
||||
public array $stats = [];
|
||||
public $phases;
|
||||
public $recentInspections;
|
||||
public $recentIssues;
|
||||
public $teamMembers;
|
||||
public $companies;
|
||||
|
||||
public function mount(Project $project): void
|
||||
{
|
||||
$this->project = $project;
|
||||
$this->checkAccess();
|
||||
$this->loadData();
|
||||
}
|
||||
|
||||
private function checkAccess(): void
|
||||
{
|
||||
$user = Auth::user();
|
||||
if ($user->can('manage all')) return;
|
||||
if (!$this->project->users()->where('user_id', $user->id)->exists()) abort(403);
|
||||
}
|
||||
|
||||
private function loadData(): void
|
||||
{
|
||||
$pid = $this->project->id;
|
||||
|
||||
$this->phases = Phase::where('project_id', $pid)
|
||||
->withCount('layers')
|
||||
->with(['layers' => fn($q) => $q->withCount('features')])
|
||||
->orderBy('order')
|
||||
->get();
|
||||
|
||||
$totalFeatures = Feature::whereHas('layer.phase', fn($q) => $q->where('project_id', $pid))->count();
|
||||
$completedFeatures = Feature::whereHas('layer.phase', fn($q) => $q->where('project_id', $pid))
|
||||
->where('status', 'completed')->count();
|
||||
$verifiedFeatures = Feature::whereHas('layer.phase', fn($q) => $q->where('project_id', $pid))
|
||||
->where('status', 'verified')->count();
|
||||
|
||||
$openIssues = Issue::where('project_id', $pid)->where('status', 'open')->count();
|
||||
$closedIssues = Issue::where('project_id', $pid)->where('status', 'closed')->count();
|
||||
$criticalIssues = Issue::where('project_id', $pid)->where('status', 'open')->where('priority', 'critical')->count();
|
||||
|
||||
$totalInspections = Inspection::where('project_id', $pid)->count();
|
||||
$passedInspections = Inspection::where('project_id', $pid)->where('result', 'pass')->count();
|
||||
$failedInspections = Inspection::where('project_id', $pid)->where('result', 'fail')->count();
|
||||
|
||||
$globalProgress = $this->phases->avg('progress_percent') ?? 0;
|
||||
|
||||
$delayedPhases = $this->phases->filter(fn($p) =>
|
||||
$p->planned_end && $p->planned_end < now() && $p->progress_percent < 100
|
||||
)->count();
|
||||
|
||||
$this->stats = [
|
||||
'global_progress' => round($globalProgress),
|
||||
'total_phases' => $this->phases->count(),
|
||||
'delayed_phases' => $delayedPhases,
|
||||
'total_features' => $totalFeatures,
|
||||
'completed_features' => $completedFeatures,
|
||||
'verified_features' => $verifiedFeatures,
|
||||
'open_issues' => $openIssues,
|
||||
'closed_issues' => $closedIssues,
|
||||
'critical_issues' => $criticalIssues,
|
||||
'total_inspections' => $totalInspections,
|
||||
'passed_inspections' => $passedInspections,
|
||||
'failed_inspections' => $failedInspections,
|
||||
];
|
||||
|
||||
$this->recentInspections = Inspection::where('project_id', $pid)
|
||||
->with(['feature', 'template', 'user'])
|
||||
->latest()->take(6)->get();
|
||||
|
||||
$this->recentIssues = Issue::where('project_id', $pid)
|
||||
->with(['feature', 'reporter'])
|
||||
->where('status', '!=', 'closed')
|
||||
->orderByRaw("FIELD(priority,'critical','high','medium','low')")
|
||||
->take(6)->get();
|
||||
|
||||
$this->teamMembers = $this->project->users()->with('roles')->get();
|
||||
|
||||
$this->companies = $this->project->companies()->get();
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.projects.project-dashboard', [
|
||||
'project' => $this->project,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Projects;
|
||||
|
||||
use Livewire\Component;
|
||||
use Livewire\Attributes\Layout;
|
||||
use App\Models\Project;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
|
||||
#[Layout('layouts.app')]
|
||||
class ProjectForm extends Component
|
||||
{
|
||||
public ?Project $project = null;
|
||||
|
||||
// Identification
|
||||
public string $name = '';
|
||||
public string $reference = '';
|
||||
public string $status = 'planning';
|
||||
|
||||
// Location
|
||||
public string $address = '';
|
||||
public string $country = '';
|
||||
public string $lat = '';
|
||||
public string $lng = '';
|
||||
|
||||
// Planning
|
||||
public string $startDate = '';
|
||||
public string $endDateEstimated = '';
|
||||
|
||||
public function mount(?Project $project = null): void
|
||||
{
|
||||
if ($project && $project->exists) {
|
||||
Gate::authorize('edit projects', $project);
|
||||
$this->project = $project;
|
||||
$this->name = $project->name;
|
||||
$this->reference = $project->reference ?? '';
|
||||
$this->status = $project->status;
|
||||
$this->address = $project->address;
|
||||
$this->country = $project->country ?? '';
|
||||
$this->lat = (string) ($project->lat ?? '');
|
||||
$this->lng = (string) ($project->lng ?? '');
|
||||
$this->startDate = $project->start_date->format('Y-m-d');
|
||||
$this->endDateEstimated = $project->end_date_estimated?->format('Y-m-d') ?? '';
|
||||
} else {
|
||||
Gate::authorize('create projects');
|
||||
$this->startDate = today()->format('Y-m-d');
|
||||
}
|
||||
}
|
||||
|
||||
// Called from JS after map click / marker drag + reverse geocode
|
||||
public function setLocation(string $lat, string $lng, string $address = '', string $country = ''): void
|
||||
{
|
||||
$this->lat = $lat;
|
||||
$this->lng = $lng;
|
||||
if ($address) $this->address = $address;
|
||||
if ($country) $this->country = strtolower($country);
|
||||
}
|
||||
|
||||
protected function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => 'required|string|max:255',
|
||||
'reference' => 'nullable|string|max:100',
|
||||
'status' => 'required|in:planning,in_progress,paused,completed',
|
||||
'address' => 'required|string',
|
||||
'country' => 'nullable|string|size:2',
|
||||
'lat' => 'nullable|numeric|between:-90,90',
|
||||
'lng' => 'nullable|numeric|between:-180,180',
|
||||
'startDate' => 'required|date',
|
||||
'endDateEstimated' => 'nullable|date|after_or_equal:startDate',
|
||||
];
|
||||
}
|
||||
|
||||
protected $validationAttributes = [
|
||||
'name' => 'nombre',
|
||||
'reference' => 'referencia',
|
||||
'status' => 'estado',
|
||||
'address' => 'dirección',
|
||||
'country' => 'país',
|
||||
'lat' => 'latitud',
|
||||
'lng' => 'longitud',
|
||||
'startDate' => 'fecha de inicio',
|
||||
'endDateEstimated' => 'fecha de fin estimada',
|
||||
];
|
||||
|
||||
public function save(): void
|
||||
{
|
||||
$this->validate();
|
||||
|
||||
$data = [
|
||||
'name' => $this->name,
|
||||
'reference' => $this->reference ?: null,
|
||||
'status' => $this->status,
|
||||
'address' => $this->address,
|
||||
'country' => $this->country ?: null,
|
||||
'lat' => $this->lat ?: null,
|
||||
'lng' => $this->lng ?: null,
|
||||
'start_date' => $this->startDate,
|
||||
'end_date_estimated' => $this->endDateEstimated ?: null,
|
||||
];
|
||||
|
||||
if ($this->project && $this->project->exists) {
|
||||
$this->project->update($data);
|
||||
session()->flash('notify', 'Proyecto actualizado correctamente.');
|
||||
} else {
|
||||
$project = Project::create(array_merge($data, ['created_by' => Auth::id()]));
|
||||
$project->users()->attach(Auth::id(), ['role_in_project' => 'supervisor']);
|
||||
session()->flash('notify', 'Proyecto creado correctamente.');
|
||||
}
|
||||
|
||||
$this->redirect(route('projects.index'), navigate: true);
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.projects.project-form', [
|
||||
'countryList' => $this->countryList(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* ISO alpha-2 (lowercase, matches flagcdn) => display name.
|
||||
*/
|
||||
private function countryList(): array
|
||||
{
|
||||
return [
|
||||
'es' => 'España', 'pt' => 'Portugal', 'fr' => 'Francia', 'it' => 'Italia',
|
||||
'de' => 'Alemania', 'gb' => 'Reino Unido', 'ie' => 'Irlanda', 'nl' => 'Países Bajos',
|
||||
'be' => 'Bélgica', 'ch' => 'Suiza', 'at' => 'Austria', 'lu' => 'Luxemburgo',
|
||||
'se' => 'Suecia', 'no' => 'Noruega', 'dk' => 'Dinamarca', 'fi' => 'Finlandia',
|
||||
'pl' => 'Polonia', 'cz' => 'Chequia', 'gr' => 'Grecia', 'ro' => 'Rumanía',
|
||||
'us' => 'Estados Unidos', 'ca' => 'Canadá', 'mx' => 'México', 'gt' => 'Guatemala',
|
||||
'cr' => 'Costa Rica', 'pa' => 'Panamá', 'co' => 'Colombia', 've' => 'Venezuela',
|
||||
'ec' => 'Ecuador', 'pe' => 'Perú', 'bo' => 'Bolivia', 'cl' => 'Chile',
|
||||
'ar' => 'Argentina', 'uy' => 'Uruguay', 'py' => 'Paraguay', 'br' => 'Brasil',
|
||||
'do' => 'República Dominicana', 'ma' => 'Marruecos', 'gq' => 'Guinea Ecuatorial',
|
||||
'ao' => 'Angola', 'cv' => 'Cabo Verde', 'us' => 'Estados Unidos',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Projects;
|
||||
|
||||
use Livewire\Component;
|
||||
use Livewire\WithPagination;
|
||||
use Livewire\Attributes\Layout;
|
||||
use App\Models\Project;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
#[Layout('layouts.app')]
|
||||
class ProjectList extends Component
|
||||
{
|
||||
use WithPagination;
|
||||
|
||||
public $search = '';
|
||||
public $statusFilter = '';
|
||||
|
||||
public function deleteProject($id)
|
||||
{
|
||||
$project = Project::findOrFail($id);
|
||||
if (Auth::user()->can('delete projects')) {
|
||||
$project->delete();
|
||||
session()->flash('message', 'Proyecto eliminado');
|
||||
}
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
$query = Project::accessibleBy(Auth::user());
|
||||
if ($this->search) {
|
||||
$query->where('name', 'like', '%' . $this->search . '%');
|
||||
}
|
||||
if ($this->statusFilter) {
|
||||
$query->where('status', $this->statusFilter);
|
||||
}
|
||||
$projects = $query->with('phases')->latest()->paginate(10);
|
||||
return view('livewire.projects.project-list', ['projects' => $projects]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,449 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Projects;
|
||||
|
||||
use Livewire\Component;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use App\Models\Project;
|
||||
use App\Models\Phase;
|
||||
use App\Models\Layer;
|
||||
use App\Models\Feature;
|
||||
use App\Models\Inspection;
|
||||
use App\Models\InspectionTemplate;
|
||||
use App\Models\Issue;
|
||||
|
||||
class ProjectMap extends Component
|
||||
{
|
||||
public Project $project;
|
||||
public $phases;
|
||||
public $activeLayers = []; // Now stores Layer IDs (not Phase IDs)
|
||||
public $showLayerModal = false;
|
||||
|
||||
// Editor properties
|
||||
public $selectedFeature = null;
|
||||
public $selectedPhaseId = null;
|
||||
public $editProgress = 0;
|
||||
public $editComment = '';
|
||||
public $editResponsible = '';
|
||||
public $editPhotos = [];
|
||||
public $formFullscreen = false;
|
||||
|
||||
// Tab management
|
||||
public $activeTab = 'edit';
|
||||
public $allFeatures;
|
||||
public $allInspections;
|
||||
|
||||
// Templates e inspecciones
|
||||
public $templates = [];
|
||||
public $selectedTemplateId = null;
|
||||
public $inspectionFormData = [];
|
||||
public $inspectionHistory = [];
|
||||
|
||||
// Imágenes en mapa
|
||||
public $showFeatureImages = false;
|
||||
public $featureImageMarkers = [];
|
||||
|
||||
// Filters
|
||||
public $filterStatus = '';
|
||||
public $filterResponsible = '';
|
||||
public $filterProgressMin = 0;
|
||||
public $filterProgressMax = 100;
|
||||
public $showFilters = false;
|
||||
|
||||
// Inspection workflow
|
||||
public $inspectionResult = '';
|
||||
public $inspectionNotes = '';
|
||||
|
||||
// Issues
|
||||
public $openIssuesCount = 0;
|
||||
|
||||
// Inspection viewer
|
||||
public $viewingInspection = null;
|
||||
|
||||
public function mount(Project $project)
|
||||
{
|
||||
$this->project = $project;
|
||||
$this->authorizeProjectAccess();
|
||||
|
||||
$this->phases = $project->phases()->with([
|
||||
'layers' => fn($q) => $q->withCount('features'),
|
||||
'layers.features',
|
||||
'layers.features.images',
|
||||
])->get();
|
||||
|
||||
// Initialize activeLayers with ALL layer IDs (not phase IDs)
|
||||
$this->activeLayers = $this->phases
|
||||
->flatMap(fn($p) => $p->layers->pluck('id'))
|
||||
->map(fn($id) => (int) $id)
|
||||
->toArray();
|
||||
|
||||
$this->loadTemplates();
|
||||
|
||||
$this->allFeatures = Feature::whereHas('layer.phase', function($q) use ($project) {
|
||||
$q->where('project_id', $project->id);
|
||||
})->with(['layer.phase', 'template'])->get();
|
||||
|
||||
$this->allInspections = Inspection::where('project_id', $project->id)
|
||||
->with(['feature.layer.phase', 'template', 'user'])
|
||||
->orderBy('created_at', 'desc')
|
||||
->get();
|
||||
|
||||
$this->openIssuesCount = Issue::where('project_id', $project->id)
|
||||
->where('status', 'open')
|
||||
->count();
|
||||
}
|
||||
|
||||
private function authorizeProjectAccess(): void
|
||||
{
|
||||
$user = Auth::user();
|
||||
if ($user->can('manage all')) return;
|
||||
if (!$this->project->users()->where('user_id', $user->id)->exists()) abort(403);
|
||||
}
|
||||
|
||||
public function loadTemplates()
|
||||
{
|
||||
$this->templates = InspectionTemplate::where('project_id', $this->project->id)->get();
|
||||
}
|
||||
|
||||
// ─── Layer / Phase visibility ────────────────────────────────────────────────
|
||||
|
||||
public function toggleLayer($layerId)
|
||||
{
|
||||
$layerId = (int) $layerId;
|
||||
if (in_array($layerId, $this->activeLayers)) {
|
||||
$this->activeLayers = array_values(array_diff($this->activeLayers, [$layerId]));
|
||||
} else {
|
||||
$this->activeLayers[] = $layerId;
|
||||
}
|
||||
$this->dispatch('layersUpdated', $this->activeLayers);
|
||||
}
|
||||
|
||||
public function togglePhase($phaseId)
|
||||
{
|
||||
$phase = $this->phases->find($phaseId);
|
||||
if (!$phase) return;
|
||||
$layerIds = $phase->layers->pluck('id')->map(fn($id) => (int) $id)->toArray();
|
||||
$allActive = !empty($layerIds) && collect($layerIds)->every(fn($id) => in_array($id, $this->activeLayers));
|
||||
if ($allActive) {
|
||||
$this->activeLayers = array_values(array_diff($this->activeLayers, $layerIds));
|
||||
} else {
|
||||
$this->activeLayers = array_values(array_unique(array_merge($this->activeLayers, $layerIds)));
|
||||
}
|
||||
$this->dispatch('layersUpdated', $this->activeLayers);
|
||||
}
|
||||
|
||||
public function openLayerModal() { $this->showLayerModal = true; }
|
||||
public function closeLayerModal() { $this->showLayerModal = false; }
|
||||
|
||||
// ─── Filters ────────────────────────────────────────────────────────────────
|
||||
|
||||
public function updatedFilterStatus() { $this->applyFilters(); }
|
||||
public function updatedFilterResponsible() { $this->applyFilters(); }
|
||||
public function updatedFilterProgressMin() { $this->applyFilters(); }
|
||||
public function updatedFilterProgressMax() { $this->applyFilters(); }
|
||||
|
||||
public function applyFilters()
|
||||
{
|
||||
$filtered = $this->allFeatures->filter(function($f) {
|
||||
if ($this->filterStatus && $f->status !== $this->filterStatus) return false;
|
||||
if ($this->filterResponsible && !str_contains(strtolower($f->responsible ?? ''), strtolower($this->filterResponsible))) return false;
|
||||
if ($f->progress < $this->filterProgressMin || $f->progress > $this->filterProgressMax) return false;
|
||||
return true;
|
||||
});
|
||||
$this->dispatch('filtersChanged', $filtered->pluck('id')->values()->toArray());
|
||||
}
|
||||
|
||||
public function clearFilters()
|
||||
{
|
||||
$this->filterStatus = '';
|
||||
$this->filterResponsible = '';
|
||||
$this->filterProgressMin = 0;
|
||||
$this->filterProgressMax = 100;
|
||||
$this->dispatch('filtersChanged', $this->allFeatures->pluck('id')->values()->toArray());
|
||||
}
|
||||
|
||||
// ─── Feature status ─────────────────────────────────────────────────────────
|
||||
|
||||
public function editFeatureStatus($status)
|
||||
{
|
||||
if (!$this->selectedFeature) return;
|
||||
$feature = Feature::with('layer.phase')->findOrFail($this->selectedFeature->id);
|
||||
if ($feature->layer->phase->project_id !== $this->project->id) abort(403);
|
||||
$feature->status = $status;
|
||||
if ($status === 'completed') $feature->progress = 100;
|
||||
if ($status === 'planned') $feature->progress = 0;
|
||||
$feature->save();
|
||||
$this->selectedFeature = $feature;
|
||||
$this->editProgress = $feature->progress;
|
||||
$this->allFeatures = $this->allFeatures->map(fn($f) => $f->id === $feature->id ? $feature : $f);
|
||||
$this->dispatch('featureStatusChanged', $feature->id, $feature->status, $feature->status_color);
|
||||
$this->dispatch('notify', 'Estado actualizado');
|
||||
}
|
||||
|
||||
public function updateProgress($featureId, $newProgress, $comment = null)
|
||||
{
|
||||
$feature = Feature::with('layer.phase')->findOrFail($featureId);
|
||||
$user = Auth::user();
|
||||
if (!$user->can('update progress')) {
|
||||
$this->dispatch('notify', 'Sin permisos');
|
||||
return;
|
||||
}
|
||||
if ($feature->layer->phase->project_id !== $this->project->id) abort(403);
|
||||
$feature->progress = min(100, max(0, $newProgress));
|
||||
$feature->save();
|
||||
$phase = $feature->layer->phase;
|
||||
$phase->progress_percent = $phase->features()->avg('progress') ?: 0;
|
||||
$phase->save();
|
||||
$phase->progressUpdates()->create([
|
||||
'user_id' => $user->id,
|
||||
'progress_percent' => $phase->progress_percent,
|
||||
'comment' => $comment,
|
||||
]);
|
||||
$this->dispatch('progressUpdated', $featureId, $feature->progress);
|
||||
$this->dispatch('notify', 'Progreso actualizado');
|
||||
if ($this->selectedFeature && $this->selectedFeature->id == $featureId) {
|
||||
$this->selectedFeature->progress = $feature->progress;
|
||||
$this->editProgress = $feature->progress;
|
||||
}
|
||||
}
|
||||
|
||||
public function selectFeature($featureId)
|
||||
{
|
||||
$this->selectedFeature = null;
|
||||
$feature = Feature::with(['template', 'layer.phase'])->find($featureId);
|
||||
if (!$feature) return;
|
||||
if ($feature->layer->phase->project_id !== $this->project->id) abort(403);
|
||||
|
||||
$this->selectedFeature = $feature;
|
||||
$this->selectedPhaseId = $feature->layer->phase_id;
|
||||
$this->editProgress = $feature->progress;
|
||||
$this->editResponsible = $feature->responsible ?? '';
|
||||
$this->editPhotos = $feature->properties['photos'] ?? [];
|
||||
$this->selectedTemplateId = $feature->template_id;
|
||||
$this->activeTab = 'edit';
|
||||
|
||||
$this->loadInspectionHistory();
|
||||
$this->resetInspectionForm();
|
||||
|
||||
$this->dispatch('featureSelected', $featureId, $feature->name);
|
||||
}
|
||||
|
||||
public function loadInspectionHistory()
|
||||
{
|
||||
if (!$this->selectedFeature) {
|
||||
$this->inspectionHistory = [];
|
||||
return;
|
||||
}
|
||||
$this->inspectionHistory = Inspection::where('feature_id', $this->selectedFeature->id)
|
||||
->with('user', 'template')
|
||||
->orderBy('created_at', 'desc')
|
||||
->get();
|
||||
}
|
||||
|
||||
public function resetInspectionForm()
|
||||
{
|
||||
$this->inspectionFormData = [];
|
||||
$this->inspectionResult = '';
|
||||
$this->inspectionNotes = '';
|
||||
if ($this->selectedTemplateId) {
|
||||
$template = InspectionTemplate::find($this->selectedTemplateId);
|
||||
if ($template) {
|
||||
foreach ($template->fields as $field) {
|
||||
$this->inspectionFormData[$field['name']] = '';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function saveInspection()
|
||||
{
|
||||
if (!$this->selectedFeature || !$this->selectedTemplateId) {
|
||||
$this->dispatch('notify', 'Selecciona un elemento y un template.');
|
||||
return;
|
||||
}
|
||||
$feature = Feature::with('layer.phase')->find($this->selectedFeature->id);
|
||||
if (!$feature || $feature->layer->phase->project_id !== $this->project->id) abort(403);
|
||||
|
||||
$this->validate(['selectedTemplateId' => 'required|exists:inspection_templates,id']);
|
||||
|
||||
$template = InspectionTemplate::find($this->selectedTemplateId);
|
||||
foreach ($template->fields as $field) {
|
||||
if (($field['required'] ?? false) && empty($this->inspectionFormData[$field['name']])) {
|
||||
$this->dispatch('notify', "El campo {$field['label']} es obligatorio.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$inspection = Inspection::create([
|
||||
'project_id' => $this->project->id,
|
||||
'layer_id' => $this->selectedFeature->layer_id,
|
||||
'feature_id' => $this->selectedFeature->id,
|
||||
'template_id' => $this->selectedTemplateId,
|
||||
'user_id' => auth()->id(),
|
||||
'inspector_user_id' => auth()->id(),
|
||||
'status' => 'completed',
|
||||
'completed_at' => now(),
|
||||
'result' => $this->inspectionResult ?: null,
|
||||
'notes' => $this->inspectionNotes ?: null,
|
||||
'data' => $this->inspectionFormData,
|
||||
]);
|
||||
|
||||
if ($this->inspectionResult === 'fail') {
|
||||
Issue::create([
|
||||
'project_id' => $this->project->id,
|
||||
'feature_id' => $this->selectedFeature->id,
|
||||
'inspection_id' => $inspection->id,
|
||||
'title' => 'Fallo en inspección: ' . ($template->name ?? 'Sin nombre'),
|
||||
'description' => $this->inspectionNotes,
|
||||
'priority' => 'high',
|
||||
'status' => 'open',
|
||||
'reported_by' => auth()->id(),
|
||||
]);
|
||||
$this->openIssuesCount = Issue::where('project_id', $this->project->id)
|
||||
->where('status', 'open')->count();
|
||||
$this->dispatch('notify', 'Inspección fallida — Issue creado automáticamente');
|
||||
} else {
|
||||
if (isset($this->inspectionFormData['progress'])) {
|
||||
$this->updateProgress($this->selectedFeature->id, (int)$this->inspectionFormData['progress'], 'Inspección registrada');
|
||||
}
|
||||
$this->dispatch('notify', 'Inspección guardada correctamente');
|
||||
}
|
||||
|
||||
// Reload global list
|
||||
$this->allInspections = Inspection::where('project_id', $this->project->id)
|
||||
->with(['feature.layer.phase', 'template', 'user'])
|
||||
->orderBy('created_at', 'desc')
|
||||
->get();
|
||||
|
||||
$this->loadInspectionHistory();
|
||||
$this->resetInspectionForm();
|
||||
}
|
||||
|
||||
public function assignTemplateToFeature($templateId)
|
||||
{
|
||||
if (!$this->selectedFeature) return;
|
||||
$template = InspectionTemplate::where('id', $templateId)
|
||||
->where('project_id', $this->project->id)->first();
|
||||
if (!$template) abort(403);
|
||||
$feature = Feature::findOrFail($this->selectedFeature->id);
|
||||
$feature->template_id = $templateId;
|
||||
$feature->save();
|
||||
$this->selectedFeature = $feature;
|
||||
$this->selectedTemplateId = $templateId;
|
||||
$this->resetInspectionForm();
|
||||
$this->dispatch('notify', 'Template asignado al elemento');
|
||||
}
|
||||
|
||||
public function saveFeatureProgress()
|
||||
{
|
||||
if (!$this->selectedFeature) return;
|
||||
$feature = Feature::with('layer.phase')->findOrFail($this->selectedFeature->id);
|
||||
if ($feature->layer->phase->project_id !== $this->project->id) abort(403);
|
||||
$feature->progress = min(100, max(0, (int)$this->editProgress));
|
||||
$feature->responsible = $this->editResponsible;
|
||||
$feature->save();
|
||||
$this->selectedFeature = $feature;
|
||||
$phase = $feature->layer->phase;
|
||||
$phase->progress_percent = $phase->features()->avg('progress') ?: 0;
|
||||
$phase->save();
|
||||
$this->dispatch('progressUpdated', $phase->id, $phase->progress_percent);
|
||||
$this->dispatch('notify', 'Progreso guardado');
|
||||
}
|
||||
|
||||
public function onTemplateChange()
|
||||
{
|
||||
$this->resetInspectionForm();
|
||||
}
|
||||
|
||||
// ─── Inspection viewer ───────────────────────────────────────────────────────
|
||||
|
||||
public function viewInspection($id)
|
||||
{
|
||||
$ins = Inspection::where('project_id', $this->project->id)
|
||||
->with(['feature.layer.phase', 'template', 'user'])
|
||||
->find($id);
|
||||
if (!$ins) return;
|
||||
$this->viewingInspection = [
|
||||
'id' => $ins->id,
|
||||
'feature_name' => $ins->feature?->name ?? '—',
|
||||
'layer_name' => $ins->feature?->layer?->name ?? '—',
|
||||
'phase_name' => $ins->feature?->layer?->phase?->name ?? '—',
|
||||
'template_name' => $ins->template?->name ?? '—',
|
||||
'user_name' => $ins->user?->name ?? '—',
|
||||
'date' => $ins->created_at->format('d/m/Y H:i'),
|
||||
'status' => $ins->status,
|
||||
'result' => $ins->result,
|
||||
'notes' => $ins->notes,
|
||||
'data' => $ins->data ?? [],
|
||||
'fields' => $ins->template?->fields ?? [],
|
||||
];
|
||||
}
|
||||
|
||||
public function closeViewInspection()
|
||||
{
|
||||
$this->viewingInspection = null;
|
||||
}
|
||||
|
||||
// ─── Feature images ──────────────────────────────────────────────────────────
|
||||
|
||||
public function toggleFeatureImages()
|
||||
{
|
||||
$this->showFeatureImages = !$this->showFeatureImages;
|
||||
$this->loadFeatureImageMarkers();
|
||||
$this->dispatch('featureImagesToggled', $this->showFeatureImages, $this->featureImageMarkers);
|
||||
}
|
||||
|
||||
public function loadFeatureImageMarkers()
|
||||
{
|
||||
if (!$this->showFeatureImages) { $this->featureImageMarkers = []; return; }
|
||||
$markers = [];
|
||||
foreach ($this->phases as $phase) {
|
||||
foreach ($phase->layers as $layer) {
|
||||
foreach ($layer->features as $feature) {
|
||||
$image = $feature->images->first();
|
||||
if ($image) {
|
||||
$geo = $feature->geometry;
|
||||
$coords = null;
|
||||
if ($geo && isset($geo['coordinates'])) {
|
||||
if ($geo['type'] === 'Point') {
|
||||
$coords = ['lat' => $geo['coordinates'][1], 'lng' => $geo['coordinates'][0]];
|
||||
} elseif (in_array($geo['type'], ['Polygon', 'LineString'])) {
|
||||
$coords = ['lat' => $geo['coordinates'][0][1] ?? null, 'lng' => $geo['coordinates'][0][0] ?? null];
|
||||
}
|
||||
}
|
||||
if ($coords && $coords['lat'] && $coords['lng']) {
|
||||
$markers[] = [
|
||||
'feature_id' => $feature->id,
|
||||
'name' => $feature->name,
|
||||
'lat' => $coords['lat'],
|
||||
'lng' => $coords['lng'],
|
||||
'image_url' => $image->url,
|
||||
'image_name' => $image->name,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->featureImageMarkers = $markers;
|
||||
}
|
||||
|
||||
public function toggleFullscreen()
|
||||
{
|
||||
$this->formFullscreen = !$this->formFullscreen;
|
||||
if (!$this->formFullscreen) $this->dispatch('mapResize');
|
||||
}
|
||||
|
||||
public function setActiveTab($tab)
|
||||
{
|
||||
$this->activeTab = $tab;
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.projects.project-map', [
|
||||
'project' => $this->project,
|
||||
'phases' => $this->phases,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Projects;
|
||||
|
||||
use Rappasoft\LaravelLivewireTables\DataTableComponent;
|
||||
use Rappasoft\LaravelLivewireTables\Views\Column;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use App\Models\Project;
|
||||
|
||||
class ProjectTable extends DataTableComponent
|
||||
{
|
||||
protected $model = Project::class;
|
||||
|
||||
public function configure(): void
|
||||
{
|
||||
$this->setPrimaryKey('id')
|
||||
->setDefaultSort('created_at', 'desc')
|
||||
->setSortingPillsEnabled(false)
|
||||
->setAdditionalSelects(['projects.id as id', 'projects.created_at as created_at']);
|
||||
}
|
||||
|
||||
public function builder(): Builder
|
||||
{
|
||||
return Project::accessibleBy(Auth::user())
|
||||
->with('phases');
|
||||
}
|
||||
|
||||
public function columns(): array
|
||||
{
|
||||
return [
|
||||
Column::make('Referencia', 'reference')
|
||||
->sortable()
|
||||
->searchable()
|
||||
->format(function ($value, $row) {
|
||||
$url = route('projects.dashboard', $row->id);
|
||||
return $value
|
||||
? '<a href="'.$url.'" class="font-mono text-xs text-primary hover:underline" wire:navigate>'.e($value).'</a>'
|
||||
: '<span class="text-gray-300">—</span>';
|
||||
})
|
||||
->html(),
|
||||
|
||||
Column::make(__('Name'), 'name')
|
||||
->sortable()
|
||||
->searchable(),
|
||||
|
||||
Column::make(__('Address'), 'address')
|
||||
->sortable()
|
||||
->searchable()
|
||||
->format(fn ($value) => $value
|
||||
? '<span class="truncate block max-w-xs" title="'.e($value).'">'.e($value).'</span>'
|
||||
: '<span class="text-gray-400">—</span>')
|
||||
->html(),
|
||||
|
||||
Column::make(__('Status'), 'status')
|
||||
->sortable()
|
||||
->format(function ($value) {
|
||||
$map = [
|
||||
'planning' => ['badge-ghost', 'Planificación'],
|
||||
'in_progress' => ['badge-primary', 'En progreso'],
|
||||
'paused' => ['badge-warning', 'Pausado'],
|
||||
'completed' => ['badge-success', 'Completado'],
|
||||
];
|
||||
[$cls, $label] = $map[$value] ?? ['badge-ghost', ucfirst($value)];
|
||||
return '<span class="badge '.$cls.'">'.$label.'</span>';
|
||||
})
|
||||
->html(),
|
||||
|
||||
Column::make(__('Progress'))
|
||||
->label(function ($row) {
|
||||
$avg = $row->phases->avg('progress_percent') ?? 0;
|
||||
$pct = round($avg);
|
||||
return '
|
||||
<div class="flex items-center gap-2 min-w-[100px]">
|
||||
<div class="flex-1 bg-gray-200 rounded-full h-2">
|
||||
<div class="bg-primary h-2 rounded-full" style="width:'.$pct.'%"></div>
|
||||
</div>
|
||||
<span class="text-xs text-gray-500 w-8 text-right">'.$pct.'%</span>
|
||||
</div>';
|
||||
})
|
||||
->html(),
|
||||
|
||||
Column::make(__('Start Date'), 'start_date')
|
||||
->sortable()
|
||||
->format(fn ($value) => $value ? $value->format('d/m/Y') : '—'),
|
||||
|
||||
Column::make(__('Est. End'), 'end_date_estimated')
|
||||
->sortable()
|
||||
->format(fn ($value) => $value ? $value->format('d/m/Y') : '—'),
|
||||
|
||||
Column::make(__('Actions'))
|
||||
->label(function ($row) {
|
||||
$dashboard = route('projects.dashboard', $row->id);
|
||||
$map = route('projects.map', $row->id);
|
||||
$edit = route('projects.edit', $row->id);
|
||||
|
||||
$canEdit = Auth::user()->can('edit projects');
|
||||
|
||||
$html = '<div class="flex items-center gap-1">';
|
||||
$html .= '<a href="'.$dashboard.'" class="btn btn-xs btn-outline" title="Dashboard" wire:navigate>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="w-3.5 h-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2H6a2 2 0 01-2-2V6zm10 0a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2V6zM4 16a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2H6a2 2 0 01-2-2v-2zm10 0a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2v-2z"/></svg>
|
||||
</a>';
|
||||
$html .= '<a href="'.$map.'" class="btn btn-xs btn-outline" title="Mapa" wire:navigate>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="w-3.5 h-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 20l-5.447-2.724A1 1 0 013 16.382V5.618a1 1 0 011.447-.894L9 7m0 13l6-3m-6 3V7m6 10l4.553 2.276A1 1 0 0021 18.382V7.618a1 1 0 00-.553-.894L15 4m0 13V4m0 0L9 7"/></svg>
|
||||
</a>';
|
||||
if ($canEdit) {
|
||||
$html .= '<a href="'.$edit.'" class="btn btn-xs btn-warning" title="Editar" wire:navigate>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="w-3.5 h-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/></svg>
|
||||
</a>';
|
||||
}
|
||||
$html .= '</div>';
|
||||
return $html;
|
||||
})
|
||||
->html(),
|
||||
];
|
||||
}
|
||||
|
||||
public function filters(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Projects;
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Livewire\Attributes\On;
|
||||
use Livewire\Component;
|
||||
|
||||
class ProjectUsers extends Component
|
||||
{
|
||||
public Project $project;
|
||||
public $allUsers = [];
|
||||
public $selectedUserId = '';
|
||||
public $selectedRole = 'viewer';
|
||||
|
||||
public function mount(Project $project)
|
||||
{
|
||||
$this->project = $project;
|
||||
$this->loadAvailable();
|
||||
}
|
||||
|
||||
/** Users not yet assigned to the project (for the dropdown). */
|
||||
public function loadAvailable(): void
|
||||
{
|
||||
$assignedIds = $this->project->users()->pluck('users.id')->toArray();
|
||||
$this->allUsers = User::whereNotIn('id', $assignedIds)->orderBy('name')->get();
|
||||
}
|
||||
|
||||
/** Reload the dropdown when the embedded table changes assignments. */
|
||||
#[On('project-users-changed')]
|
||||
public function onUsersChanged(): void
|
||||
{
|
||||
$this->loadAvailable();
|
||||
}
|
||||
|
||||
public function assignUser()
|
||||
{
|
||||
abort_unless(Auth::user()->can('assign users'), 403);
|
||||
|
||||
$this->validate([
|
||||
'selectedUserId' => 'required|exists:users,id',
|
||||
'selectedRole' => 'required|in:' . implode(',', array_keys(ProjectUsersTable::ROLES)),
|
||||
]);
|
||||
|
||||
$this->project->users()->attach($this->selectedUserId, [
|
||||
'role_in_project' => $this->selectedRole,
|
||||
]);
|
||||
|
||||
$this->reset(['selectedUserId', 'selectedRole']);
|
||||
$this->loadAvailable();
|
||||
$this->dispatch('project-users-changed');
|
||||
$this->dispatch('notify', 'Usuario asignado al proyecto.');
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.projects.project-users', [
|
||||
'roles' => ProjectUsersTable::ROLES,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Projects;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Livewire\Attributes\On;
|
||||
use Rappasoft\LaravelLivewireTables\DataTableComponent;
|
||||
use Rappasoft\LaravelLivewireTables\Views\Column;
|
||||
use Rappasoft\LaravelLivewireTables\Views\Filters\SelectFilter;
|
||||
|
||||
class ProjectUsersTable extends DataTableComponent
|
||||
{
|
||||
protected $model = User::class;
|
||||
|
||||
public int $projectId;
|
||||
|
||||
/** role_in_project => label */
|
||||
public const ROLES = [
|
||||
'supervisor' => 'Supervisor',
|
||||
'consultant' => 'Consultor',
|
||||
'client' => 'Cliente',
|
||||
'viewer' => 'Observador',
|
||||
];
|
||||
|
||||
public function configure(): void
|
||||
{
|
||||
$this->setPrimaryKey('id')
|
||||
->setDefaultSort('users.name', 'asc')
|
||||
->setSortingPillsEnabled(false)
|
||||
->setAdditionalSelects(['users.id as id', 'project_user.role_in_project as role_in_project']);
|
||||
}
|
||||
|
||||
#[On('project-users-changed')]
|
||||
public function refreshRows(): void
|
||||
{
|
||||
// no-op: triggers re-render so the builder re-runs.
|
||||
}
|
||||
|
||||
public function builder(): Builder
|
||||
{
|
||||
return User::query()
|
||||
->join('project_user', 'project_user.user_id', '=', 'users.id')
|
||||
->where('project_user.project_id', $this->projectId);
|
||||
}
|
||||
|
||||
public function columns(): array
|
||||
{
|
||||
return [
|
||||
Column::make('Nombre', 'name')
|
||||
->sortable()
|
||||
->searchable()
|
||||
->format(function ($value, $row) {
|
||||
$initial = strtoupper(mb_substr($value ?? '?', 0, 1));
|
||||
return '<div class="flex items-center gap-2">
|
||||
<span class="w-7 h-7 rounded-full bg-primary text-primary-content flex items-center justify-center text-xs font-bold shrink-0">'.$initial.'</span>
|
||||
<span class="font-medium">'.e($value).'</span>
|
||||
</div>';
|
||||
})
|
||||
->html(),
|
||||
|
||||
Column::make('Email', 'email')
|
||||
->sortable()
|
||||
->searchable(),
|
||||
|
||||
Column::make('Rol', 'role_in_project')
|
||||
->label(function ($row) {
|
||||
$current = $row->role_in_project;
|
||||
if (! Auth::user()->can('assign users')) {
|
||||
return '<span class="badge badge-sm">'.(self::ROLES[$current] ?? ucfirst((string) $current)).'</span>';
|
||||
}
|
||||
$opts = '';
|
||||
foreach (self::ROLES as $val => $label) {
|
||||
$opts .= '<option value="'.$val.'"'.($current === $val ? ' selected' : '').'>'.$label.'</option>';
|
||||
}
|
||||
return '<select wire:change="changeRole('.$row->id.', $event.target.value)" class="select select-bordered select-xs">'.$opts.'</select>';
|
||||
})
|
||||
->html(),
|
||||
|
||||
Column::make('Acciones')
|
||||
->label(function ($row) {
|
||||
if (! Auth::user()->can('assign users')) {
|
||||
return '';
|
||||
}
|
||||
return '<div class="flex justify-end">
|
||||
<button wire:click="removeUser('.$row->id.')" wire:confirm="¿Quitar a '.e($row->name).' del proyecto?"
|
||||
class="btn btn-xs btn-error btn-outline" title="Quitar del proyecto">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="w-3.5 h-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/></svg>
|
||||
</button>
|
||||
</div>';
|
||||
})
|
||||
->html(),
|
||||
];
|
||||
}
|
||||
|
||||
public function filters(): array
|
||||
{
|
||||
return [
|
||||
SelectFilter::make('Rol', 'role')
|
||||
->options(['' => 'Rol: todos'] + self::ROLES)
|
||||
->filter(fn (Builder $query, string $value) => $query->where('project_user.role_in_project', $value)),
|
||||
];
|
||||
}
|
||||
|
||||
public function changeRole($userId, $role): void
|
||||
{
|
||||
abort_unless(Auth::user()->can('assign users'), 403);
|
||||
if (! array_key_exists($role, self::ROLES)) {
|
||||
return;
|
||||
}
|
||||
\App\Models\Project::findOrFail($this->projectId)
|
||||
->users()->updateExistingPivot($userId, ['role_in_project' => $role]);
|
||||
$this->dispatch('project-users-changed');
|
||||
$this->dispatch('notify', 'Rol actualizado.');
|
||||
}
|
||||
|
||||
public function removeUser($userId): void
|
||||
{
|
||||
abort_unless(Auth::user()->can('assign users'), 403);
|
||||
\App\Models\Project::findOrFail($this->projectId)->users()->detach($userId);
|
||||
$this->dispatch('project-users-changed');
|
||||
$this->dispatch('notify', 'Usuario eliminado del proyecto.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user