feat(templates): grupos de campos, pregunta/comentarios por campo y plantillas globales

Builder de plantillas (parte 1 del bloque):
- Cada campo admite "Grupo / sección" (título), "Pregunta / revisar" (texto corto,
  tras la etiqueta) y "Comentarios / ayuda" (texto largo, al final).
- Plantillas híbridas: toggle "Global" → project_id null; el listado de un proyecto
  muestra sus plantillas + las globales (badge "Global").

Tests: TemplateCreationTest (grupo/pregunta/ayuda persisten; global sin proyecto y
visible en otros proyectos). Suite 90 passing.

Pendiente (parte 2): adaptar el formulario de inspección (grupos, comentarios, fotos)
y el visor de inspección.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-25 16:45:21 +02:00
co-authored by Claude Opus 4.8
parent a52be91dd4
commit 44f125293b
3 changed files with 98 additions and 5 deletions
+13 -3
View File
@@ -25,6 +25,7 @@ class TemplateManager extends Component
'name' => '',
'description' => '',
'phase_id' => null,
'is_global' => false,
'fields' => [],
];
@@ -67,8 +68,12 @@ class TemplateManager extends Component
public function loadTemplates()
{
$this->templates = InspectionTemplate::where('project_id', $this->project->id)
// Plantillas del proyecto + globales (project_id null)
$this->templates = InspectionTemplate::where(function ($q) {
$q->where('project_id', $this->project->id)->orWhereNull('project_id');
})
->with('phase')
->orderBy('name')
->get();
}
@@ -87,6 +92,7 @@ class TemplateManager extends Component
'name' => $template->name,
'description' => $template->description ?? '',
'phase_id' => $template->phase_id,
'is_global' => is_null($template->project_id),
'fields' => $template->fields ?? [],
];
$this->editingTemplate = $id;
@@ -105,6 +111,7 @@ class TemplateManager extends Component
'name' => '',
'description' => '',
'phase_id' => null,
'is_global' => false,
'fields' => [],
];
$this->editingTemplate = null;
@@ -113,14 +120,17 @@ class TemplateManager extends Component
public function addField()
{
$this->form['fields'][] = [
'group' => '',
'name' => '',
'label' => '',
'question' => '',
'type' => 'text',
'options' => '',
'required' => false,
'min' => null,
'max' => null,
'step' => null,
'help' => '',
];
}
@@ -141,8 +151,8 @@ class TemplateManager extends Component
$data = [
'name' => $this->form['name'],
'description' => $this->form['description'],
'project_id' => $this->project->id,
'phase_id' => $this->form['phase_id'] ?: null,
'project_id' => ($this->form['is_global'] ?? false) ? null : $this->project->id,
'phase_id' => ($this->form['is_global'] ?? false) ? null : ($this->form['phase_id'] ?: null),
'fields' => array_values($this->form['fields']),
];
@@ -54,7 +54,7 @@
{{ __('Associated phase (optional)') }}
</td>
<td class="py-3">
<select wire:model="form.phase_id" class="select select-bordered w-full">
<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' : '' }}>
@@ -64,6 +64,17 @@
</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>
</tr>
</tbody>
</table>
@@ -72,6 +83,11 @@
<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>
@@ -82,6 +98,11 @@
<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>
{{-- Fila: tipo --}}
<div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-2">
<div class="font-medium">{{ __('Field type') }}</div>
@@ -118,6 +139,12 @@
<div><input type="text" wire:model="form.fields.{{ $index }}.options" placeholder="ej: Bueno,Regular,Malo" class="input input-sm w-full"></div>
</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>
</div>
</div>
@endforeach
<button type="button" wire:click="addField" class="btn btn-sm btn-secondary mt-2">+ {{ __('Add field') }}</button>
@@ -145,7 +172,12 @@
<tbody>
@forelse($templates as $template)
<tr>
<td>{{ $template->name }}</td>
<td>
{{ $template->name }}
@if(is_null($template->project_id))
<span class="badge badge-info badge-sm ml-1">{{ __('Global') }}</span>
@endif
</td>
<td>{{ $template->description ?? '-' }}</td>
<td>{{ $template->phase ? $template->phase->name : __('Global project') }}</td>
<td>{{ count($template->fields) }}</td>
+51
View File
@@ -56,4 +56,55 @@ class TemplateCreationTest extends TestCase
'name' => 'Recepción de hormigón',
]);
}
public function test_field_keeps_group_question_and_help(): void
{
[$user, $project] = $this->setup_();
Livewire::actingAs($user)
->test(TemplateManager::class, ['project' => $project])
->call('newTemplate')
->set('form.name', 'Con grupos')
->call('addField')
->set('form.fields.0.group', 'Geometría')
->set('form.fields.0.name', 'altura')
->set('form.fields.0.label', 'Altura (m)')
->set('form.fields.0.question', '¿Cumple la cota?')
->set('form.fields.0.help', 'Medir con láser')
->call('saveTemplate')
->assertHasNoErrors();
$tpl = \App\Models\InspectionTemplate::where('name', 'Con grupos')->first();
$this->assertSame('Geometría', $tpl->fields[0]['group']);
$this->assertSame('¿Cumple la cota?', $tpl->fields[0]['question']);
$this->assertSame('Medir con láser', $tpl->fields[0]['help']);
}
public function test_global_template_has_no_project_and_shows_in_other_projects(): void
{
[$user, $project] = $this->setup_();
Livewire::actingAs($user)
->test(TemplateManager::class, ['project' => $project])
->call('newTemplate')
->set('form.name', 'Plantilla global')
->set('form.is_global', true)
->call('saveTemplate')
->assertHasNoErrors();
$this->assertDatabaseHas('inspection_templates', [
'name' => 'Plantilla global', 'project_id' => null,
]);
// Otro proyecto del mismo usuario ve la plantilla global en su listado
$other = \App\Models\Project::create([
'reference' => 'OTH', 'name' => 'Otro', 'address' => 'x', 'lat' => 40, 'lng' => -3,
'start_date' => now()->toDateString(), 'end_date_estimated' => now()->addMonth()->toDateString(),
'status' => 'in_progress', 'created_by' => $user->id,
]);
Livewire::actingAs($user)
->test(TemplateManager::class, ['project' => $other])
->assertSee('Plantilla global');
}
}