refactor(templates): plantillas globales + asignación por proyecto (pivot)

- Migración: tabla pivot inspection_template_project; drop de phase_id (asociación
  a fase descartada); project_id se mantiene en inspection_templates por compat.
  Migra automáticamente las plantillas existentes (project_id → pivot).
- Modelos: Project::inspectionTemplates() ↔ InspectionTemplate::projects() (BTM);
  retirada la relación phase() de InspectionTemplate.
- GlobalTemplateManager (nuevo, ruta /inspection-templates, permiso manage templates):
  catálogo único global, CRUD + import CSV; sin asociación a fase ni a proyecto.
- ProjectTemplatesPicker (nuevo, ruta projects.templates, permiso edit projects):
  lista global con checkbox para asignar/desasignar plantillas al proyecto.
- ProjectMap: el selector de plantillas del mapa lee SOLO las asignadas al proyecto
  vía pivot. API móvil (bundle + /templates) adaptado al pivot.
- Eliminado el viejo TemplateManager por-proyecto, su vista wrapper y la tabla
  ImportTemplatesTable (ya no aplica: todas son globales).
- Acceso "Plantillas" añadido al menú principal (gate manage templates).

Tests: GlobalTemplatesTest (4). Suite 89 passing.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-06-25 17:26:12 +02:00
co-authored by Claude Opus 4.7
parent 7256c87182
commit f8a68312a3
17 changed files with 386 additions and 639 deletions
@@ -1,12 +1,11 @@
<div>
<div class="max-w-5xl mx-auto">
<div class="bg-base-100 p-4 rounded shadow">
<div class="flex justify-between items-center mb-4">
<h2 class="text-xl font-bold">📋 {{ __('Inspection templates') }}</h2>
<div>
<h1 class="text-xl font-bold">📋 {{ __('Inspection templates') }}</h1>
<p class="text-sm text-base-content/60">{{ __('Global catalogue, reusable across all projects.') }}</p>
</div>
<div class="flex items-center gap-2">
<button wire:click="openImportProjectModal" class="btn btn-outline btn-sm gap-1" title="{{ __('Import from another project') }}">
<x-heroicon-o-arrow-down-on-square-stack class="w-4 h-4" />
{{ __('Import from project') }}
</button>
<button wire:click="openImportFileModal" class="btn btn-outline btn-sm gap-1" title="{{ __('Import from CSV/Excel') }}">
<x-heroicon-o-document-arrow-up class="w-4 h-4" />
{{ __('Import CSV/Excel') }}
@@ -17,93 +16,41 @@
</div>
</div>
@if(session()->has('message'))
<div class="alert alert-success mb-4">{{ session('message') }}</div>
@endif
{{-- Formulario de creación/edición con diseño de dos columnas --}}
@if($showForm)
<form wire:submit.prevent="saveTemplate" class="border p-4 rounded mb-6 bg-base-200">
<table class="w-full mb-8">
<tbody>
{{-- Nombre del template --}}
<tr>
<td class="w-1/4 py-3 pr-4 align-top">
{{ __('Template name') }}
</td>
<td class="py-3">
<input type="text" wire:model="form.name"
class="input w-full"
required>
</td>
<td class="w-1/4 py-3 pr-4 align-top">{{ __('Template name') }}</td>
<td class="py-3"><input type="text" wire:model="form.name" class="input w-full" required></td>
</tr>
{{-- Descripción --}}
<tr>
<td class="w-1/4 py-3 pr-4 align-top">
{{ __('Description') }}
</td>
<td class="py-3">
<textarea wire:model="form.description" class="textarea textarea-bordered w-full" rows="2"></textarea>
</td>
</tr>
{{-- Fase asociada (opcional) --}}
<tr>
<td class="w-1/4 py-3 pr-4 align-top">
{{ __('Associated phase (optional)') }}
</td>
<td class="py-3">
<select wire:model="form.phase_id" class="select select-bordered w-full" @disabled($form['is_global'] ?? false)>
<option value="">{{ __('Global project') }}</option>
@foreach($phases as $phase)
<option value="{{ $phase->id }}" {{ old('form.phase_id') == $phase->id ? 'selected' : '' }}>
{{ $phase->name }}
</option>
@endforeach
</select>
</td>
</tr>
{{-- Plantilla global (disponible en todos los proyectos) --}}
<tr>
<td class="w-1/4 py-3 pr-4 align-top">{{ __('Global template') }}</td>
<td class="py-3">
<label class="flex items-center gap-2 cursor-pointer">
<input type="checkbox" wire:model.live="form.is_global" class="toggle toggle-primary toggle-sm" />
<span class="text-sm text-base-content/70">{{ __('Available in all projects') }}</span>
</label>
</td>
<td class="w-1/4 py-3 pr-4 align-top">{{ __('Description') }}</td>
<td class="py-3"><textarea wire:model="form.description" class="textarea textarea-bordered w-full" rows="2"></textarea></td>
</tr>
</tbody>
</table>
{{-- Campos dinámicos --}}
<div class="border-t pt-4 mt-2">
<h3 class="font-bold mb-3">{{ __('Form fields') }}</h3>
@foreach($form['fields'] as $index => $field)
<div class="border p-3 rounded mb-3 bg-base-100">
{{-- Fila: grupo / sección --}}
<div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-2">
<div class="font-medium">{{ __('Group / section') }}</div>
<div><input type="text" wire:model="form.fields.{{ $index }}.group" placeholder="ej: Geometría" class="input input-sm w-full"></div>
</div>
{{-- Fila: nombre interno --}}
<div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-2">
<div class="font-medium">{{ __('Internal name') }}</div>
<div><input type="text" wire:model="form.fields.{{ $index }}.name" placeholder="ej: altura_medida" class="input input-sm w-full"></div>
</div>
{{-- Fila: etiqueta --}}
<div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-2">
<div class="font-medium">{{ __('Visible label') }}</div>
<div><input type="text" wire:model="form.fields.{{ $index }}.label" placeholder="ej: Altura medida (m)" class="input input-sm w-full"></div>
</div>
{{-- Fila: pregunta / revisar (texto corto) --}}
<div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-2">
<div class="font-medium">{{ __('Question / check') }}</div>
<div><input type="text" wire:model="form.fields.{{ $index }}.question" placeholder="ej: ¿Cumple la cota prevista?" class="input input-sm w-full"></div>
<div><input type="text" wire:model="form.fields.{{ $index }}.question" placeholder="ej: ¿Cumple la cota?" class="input input-sm w-full"></div>
</div>
{{-- Fila: tipo --}}
<div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-2">
<div class="font-medium">{{ __('Field type') }}</div>
<div>
@@ -114,7 +61,6 @@
</select>
</div>
</div>
{{-- Fila: requerido y botón eliminar --}}
<div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-2">
<div class="font-medium">{{ __('Required') }}</div>
<div class="flex justify-between items-center">
@@ -122,8 +68,6 @@
<button type="button" wire:click="removeField({{ $index }})" class="btn btn-xs btn-error">{{ __('Remove field') }}</button>
</div>
</div>
{{-- Campos adicionales según tipo --}}
@if(in_array($field['type'], ['integer', 'decimal', 'percentage']))
<div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-2">
<div class="font-medium">{{ __('Min') }} / {{ __('Max') }} / {{ __('Step') }}</div>
@@ -140,7 +84,6 @@
</div>
@endif
{{-- Fila: comentarios / ayuda (texto largo) --}}
<div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-2">
<div class="font-medium">{{ __('Comments / help') }}</div>
<div><textarea wire:model="form.fields.{{ $index }}.help" rows="2" placeholder="{{ __('Notes or instructions for this field') }}" class="textarea textarea-bordered textarea-sm w-full"></textarea></div>
@@ -157,93 +100,68 @@
</form>
@endif
{{-- Tabla de templates existentes --}}
{{-- Tabla --}}
<div class="overflow-x-auto">
<table class="table table-zebra">
<thead>
<tr>
<th>{{ __('Name') }}</th>
<th>{{ __('Description') }}</th>
<th>{{ __('Phase') }}</th>
<th>{{ __('Fields') }}</th>
<th>{{ __('Used in projects') }}</th>
<th>{{ __('Actions') }}</th>
</tr>
</thead>
<tbody>
@forelse($templates as $template)
<tr>
<td>
{{ $template->name }}
@if(is_null($template->project_id))
<span class="badge badge-info badge-sm ml-1">{{ __('Global') }}</span>
@endif
</td>
<td class="font-medium">{{ $template->name }}</td>
<td>{{ $template->description ?? '-' }}</td>
<td>{{ $template->phase ? $template->phase->name : __('Global project') }}</td>
<td>{{ count($template->fields) }}</td>
<td>{{ count($template->fields ?? []) }}</td>
<td><span class="badge badge-ghost badge-sm">{{ $template->projects()->count() }}</span></td>
<td>
<button wire:click="editTemplate({{ $template->id }})" class="btn btn-xs btn-warning">
{{ __('Edit') }}
</button>
<button wire:click="editTemplate({{ $template->id }})" class="btn btn-xs btn-warning">{{ __('Edit') }}</button>
<button wire:click="deleteTemplate({{ $template->id }})"
wire:confirm="{{ __('Delete template confirmation') }}"
class="btn btn-xs btn-error">{{ __('Delete') }}</button>
</td>
</tr>
@empty
<tr>
<td colspan="5" class="text-center">{{ __('No templates yet (table)') }}</td>
</tr>
<tr><td colspan="5" class="text-center text-base-content/40 py-6">{{ __('No templates yet (table)') }}</td></tr>
@endforelse
</tbody>
</table>
</div>
</div>
{{-- ════════════════════════════════════════════════════════════
MODAL: Importar desde CSV/Excel
════════════════════════════════════════════════════════════ --}}
{{-- MODAL: Importar desde CSV/Excel --}}
@if($showImportFileModal)
<div class="fixed inset-0 z-40 bg-black/50" wire:click="$set('showImportFileModal', false)"></div>
<div class="fixed inset-0 z-50 flex items-center justify-center p-4">
<div class="bg-base-100 rounded-box shadow-2xl w-full max-w-2xl max-h-[90vh] overflow-y-auto">
<div class="flex items-center justify-between p-4 border-b border-base-300">
<h3 class="font-bold">{{ __('Import from CSV/Excel') }}</h3>
<button wire:click="$set('showImportFileModal', false)" class="btn btn-sm btn-ghost btn-circle">
<x-heroicon-o-x-mark class="w-4 h-4" />
</button>
<button wire:click="$set('showImportFileModal', false)" class="btn btn-sm btn-ghost btn-circle"><x-heroicon-o-x-mark class="w-4 h-4" /></button>
</div>
<div class="p-4 space-y-3">
<p class="text-xs text-base-content/60">
{{ __('Columns: name, label, type, required, options, min, max, step') }}.
<button type="button" wire:click="downloadExampleCsv" class="link link-primary">{{ __('Download example CSV') }}</button>
</p>
<div class="form-control">
<label class="label"><span class="label-text font-medium">{{ __('Template name') }} <span class="text-error">*</span></span></label>
<input type="text" wire:model="importTemplateName" class="input input-bordered w-full"
placeholder="{{ __('e.g. Concrete reception') }}" />
<input type="text" wire:model="importTemplateName" class="input input-bordered w-full" />
@error('importTemplateName')<span class="text-xs text-error">{{ $message }}</span>@enderror
</div>
<div class="form-control">
<label class="label"><span class="label-text font-medium">{{ __('File (CSV / Excel)') }} <span class="text-error">*</span></span></label>
<input type="file" wire:model="importFile" accept=".csv,.txt,.xlsx,.xls" class="file-input file-input-bordered w-full" />
<div wire:loading wire:target="importFile" class="text-xs text-base-content/50 mt-1">{{ __('Uploading…') }}</div>
@error('importFile')<span class="text-xs text-error">{{ $message }}</span>@enderror
</div>
@if($importError)
<div class="alert alert-error text-sm py-2">{{ $importError }}</div>
@endif
@if($importError)<div class="alert alert-error text-sm py-2">{{ $importError }}</div>@endif
<div class="flex justify-end">
<button wire:click="parseImportFile" class="btn btn-sm btn-secondary gap-1" wire:loading.attr="disabled" wire:target="parseImportFile,importFile">
<x-heroicon-o-eye class="w-4 h-4" /> {{ __('Preview') }}
</button>
<button wire:click="parseImportFile" class="btn btn-sm btn-secondary gap-1"><x-heroicon-o-eye class="w-4 h-4" /> {{ __('Preview') }}</button>
</div>
{{-- Vista previa de campos detectados --}}
@if(!empty($importPreviewFields))
<div class="border border-base-300 rounded-box p-2">
<p class="text-sm font-medium mb-2">{{ count($importPreviewFields) }} {{ __('fields detected') }}:</p>
@@ -251,14 +169,9 @@
<table class="table table-xs">
<thead><tr><th>{{ __('Label') }}</th><th>{{ __('Name') }}</th><th>{{ __('Type') }}</th><th>{{ __('Required') }}</th></tr></thead>
<tbody>
@foreach($importPreviewFields as $f)
<tr>
<td>{{ $f['label'] }}</td>
<td class="font-mono text-xs">{{ $f['name'] }}</td>
<td><span class="badge badge-ghost badge-sm">{{ $f['type'] }}</span></td>
<td>{{ !empty($f['required']) ? '✓' : '' }}</td>
</tr>
@endforeach
@foreach($importPreviewFields as $f)
<tr><td>{{ $f['label'] }}</td><td class="font-mono text-xs">{{ $f['name'] }}</td><td><span class="badge badge-ghost badge-sm">{{ $f['type'] }}</span></td><td>{{ !empty($f['required']) ? '✓' : '' }}</td></tr>
@endforeach
</tbody>
</table>
</div>
@@ -267,32 +180,7 @@
</div>
<div class="flex justify-end gap-2 p-4 border-t border-base-300">
<button wire:click="$set('showImportFileModal', false)" class="btn btn-ghost btn-sm">{{ __('Cancel') }}</button>
<button wire:click="confirmImportFile" class="btn btn-primary btn-sm" @disabled(empty($importPreviewFields))>
{{ __('Create template') }}
</button>
</div>
</div>
</div>
@endif
{{-- ════════════════════════════════════════════════════════════
MODAL: Importar desde otro proyecto
════════════════════════════════════════════════════════════ --}}
@if($showImportProjectModal)
<div class="fixed inset-0 z-40 bg-black/50" wire:click="$set('showImportProjectModal', false)"></div>
<div class="fixed inset-0 z-50 flex items-center justify-center p-4">
<div class="bg-base-100 rounded-box shadow-2xl w-full max-w-4xl max-h-[90vh] overflow-y-auto">
<div class="flex items-center justify-between p-4 border-b border-base-300">
<div>
<h3 class="font-bold">{{ __('Import from another project') }}</h3>
<p class="text-xs text-base-content/60">{{ __('Mark the templates and use "Import selected".') }}</p>
</div>
<button wire:click="$set('showImportProjectModal', false)" class="btn btn-sm btn-ghost btn-circle">
<x-heroicon-o-x-mark class="w-4 h-4" />
</button>
</div>
<div class="p-4">
<livewire:projects.import-templates-table :project-id="$project->id" :key="'import-tpl-'.$project->id" />
<button wire:click="confirmImportFile" class="btn btn-primary btn-sm" @disabled(empty($importPreviewFields))>{{ __('Create template') }}</button>
</div>
</div>
</div>
@@ -56,6 +56,14 @@ new class extends Component
</x-nav-link>
</div>
@endcan
@can('manage templates')
<div class="hidden space-x-8 sm:-my-px sm:ms-10 sm:flex">
<x-nav-link :href="route('inspection-templates')" :active="request()->routeIs('inspection-templates')" wire:navigate>
{{ __('Templates') }}
</x-nav-link>
</div>
@endcan
</div>
<!-- Language Switcher -->
@@ -0,0 +1,64 @@
<div class="max-w-4xl mx-auto">
<a href="{{ route('projects.dashboard', $project) }}" wire:navigate class="btn btn-ghost btn-sm gap-1 mb-3">
<x-heroicon-o-arrow-left class="w-4 h-4" /> Volver
</a>
<div class="flex flex-wrap items-center justify-between gap-3 mb-5">
<div>
<h1 class="text-xl font-bold">Plantillas del proyecto</h1>
<p class="text-sm text-base-content/60">{{ $project->name }} · marca las plantillas globales que quieres usar al inspeccionar este proyecto</p>
</div>
@can('manage templates')
<a href="{{ route('inspection-templates') }}" wire:navigate class="btn btn-outline btn-sm gap-1">
<x-heroicon-o-cog-6-tooth class="w-4 h-4" /> Gestionar catálogo
</a>
@endcan
</div>
<div class="card bg-base-100 border border-base-300">
<div class="card-body p-4">
<div class="form-control mb-3">
<input type="text" wire:model.live.debounce.300ms="search" class="input input-bordered input-sm" placeholder="Buscar plantilla por nombre…" />
</div>
@if($templates->isEmpty())
<p class="text-sm text-base-content/40 py-6 text-center">
No hay plantillas en el catálogo.
@can('manage templates')
<a href="{{ route('inspection-templates') }}" wire:navigate class="link link-primary">Crear una</a>.
@endcan
</p>
@else
<div class="overflow-x-auto rounded-lg border border-base-300">
<table class="table table-sm">
<thead class="bg-base-200">
<tr>
<th class="w-10">Asignada</th>
<th>Plantilla</th>
<th>Descripción</th>
<th class="text-center">Campos</th>
</tr>
</thead>
<tbody>
@foreach($templates as $tpl)
@php $assigned = in_array($tpl->id, $assignedIds, true); @endphp
<tr wire:key="tpl-{{ $tpl->id }}" class="hover">
<td>
<input type="checkbox"
wire:click="toggle({{ $tpl->id }})"
@checked($assigned)
class="checkbox checkbox-sm checkbox-primary" />
</td>
<td class="font-medium">{{ $tpl->name }}</td>
<td class="text-sm text-base-content/60">{{ $tpl->description ?? '—' }}</td>
<td class="text-center"><span class="badge badge-ghost badge-sm">{{ count($tpl->fields ?? []) }}</span></td>
</tr>
@endforeach
</tbody>
</table>
</div>
<p class="text-xs text-base-content/50 mt-2">{{ count($assignedIds) }} plantilla(s) asignadas a este proyecto.</p>
@endif
</div>
</div>
</div>