8025fa6d05
Permissions now actually govern access instead of the hard-coded Admin role:
- Super-admin bypass (see all projects / full access) -> can('manage all')
in Project::scopeAccessibleBy, ProjectMap, ProjectDashboard, PhaseGantt,
LayerManager, ProjectReportController.
- Redundant '|| hasRole(Admin)' fallbacks dropped (Gate::before already lets
manage-all through can()): LayerManager (upload/delete layers), MediaManager
(upload), ProjectMap (update progress), ProjectUsers/ProjectCompanies
(assign users).
- Admin-only screens now gated by the matching permission: AdminUsers/UserView
-> can('view users'), UserForm -> can('create users')|can('edit users'),
CompanyView -> can('view companies').
- MediaManager delete: can('delete media') OR owner.
- Kept UserForm's domain guard (can't remove your own Admin role).
Note: the /admin route group still has middleware can:manage all, so admin
screens stay super-admin-only until that group is relaxed per-route.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
450 lines
18 KiB
PHP
450 lines
18 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
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,
|
|
]);
|
|
}
|
|
}
|