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:
@@ -1,121 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Projects;
|
||||
|
||||
use App\Models\InspectionTemplate;
|
||||
use App\Models\Project;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Rappasoft\LaravelLivewireTables\DataTableComponent;
|
||||
use Rappasoft\LaravelLivewireTables\Views\Column;
|
||||
use Rappasoft\LaravelLivewireTables\Views\Filters\SelectFilter;
|
||||
use Rappasoft\LaravelLivewireTables\Views\Filters\TextFilter;
|
||||
|
||||
class ImportTemplatesTable extends DataTableComponent
|
||||
{
|
||||
protected $model = InspectionTemplate::class;
|
||||
|
||||
public int $projectId;
|
||||
|
||||
public function configure(): void
|
||||
{
|
||||
$this->setPrimaryKey('id')
|
||||
->setDefaultSort('inspection_templates.name', 'asc')
|
||||
->setSortingPillsEnabled(false)
|
||||
->setBulkActionsEnabled(true)
|
||||
->setAdditionalSelects([
|
||||
'inspection_templates.id as id',
|
||||
'inspection_templates.project_id as project_id',
|
||||
'inspection_templates.phase_id as phase_id',
|
||||
]);
|
||||
}
|
||||
|
||||
/** Sólo plantillas de proyectos accesibles, distintos del proyecto destino. */
|
||||
private function accessibleProjectIds()
|
||||
{
|
||||
return Project::accessibleBy(Auth::user())->pluck('id');
|
||||
}
|
||||
|
||||
public function builder(): Builder
|
||||
{
|
||||
return InspectionTemplate::query()
|
||||
->whereIn('inspection_templates.project_id', $this->accessibleProjectIds())
|
||||
->where('inspection_templates.project_id', '!=', $this->projectId)
|
||||
->with(['project', 'phase']);
|
||||
}
|
||||
|
||||
public function columns(): array
|
||||
{
|
||||
return [
|
||||
Column::make('Plantilla', 'name')
|
||||
->sortable()->searchable()
|
||||
->secondaryHeaderFilter('name')
|
||||
->format(fn ($value) => '<span class="font-medium">' . e($value) . '</span>')
|
||||
->html(),
|
||||
|
||||
Column::make('Proyecto')
|
||||
->secondaryHeaderFilter('project')
|
||||
->label(fn ($row) => e($row->project?->name ?? '—')),
|
||||
|
||||
Column::make('Fase')
|
||||
->label(fn ($row) => e($row->phase?->name ?? '—')),
|
||||
|
||||
Column::make('Campos')
|
||||
->label(fn ($row) => '<span class="badge badge-ghost badge-sm">' . count($row->fields ?? []) . '</span>')
|
||||
->html(),
|
||||
];
|
||||
}
|
||||
|
||||
public function filters(): array
|
||||
{
|
||||
$projects = Project::whereIn('id', InspectionTemplate::query()
|
||||
->whereIn('project_id', $this->accessibleProjectIds())
|
||||
->where('project_id', '!=', $this->projectId)
|
||||
->distinct()->pluck('project_id'))
|
||||
->orderBy('name')->pluck('name', 'id')->toArray();
|
||||
|
||||
return [
|
||||
TextFilter::make('Plantilla', 'name')
|
||||
->config(['placeholder' => 'Buscar nombre…'])
|
||||
->filter(fn (Builder $q, string $v) => $q->where('inspection_templates.name', 'like', '%' . $v . '%')),
|
||||
|
||||
SelectFilter::make('Proyecto', 'project')
|
||||
->options(['' => 'Todos'] + $projects)
|
||||
->filter(fn (Builder $q, string $v) => $q->where('inspection_templates.project_id', $v)),
|
||||
];
|
||||
}
|
||||
|
||||
public function bulkActions(): array
|
||||
{
|
||||
return ['importSelected' => 'Importar seleccionadas'];
|
||||
}
|
||||
|
||||
public function importSelected()
|
||||
{
|
||||
$allowed = $this->accessibleProjectIds();
|
||||
$imported = 0;
|
||||
|
||||
foreach (InspectionTemplate::whereIn('id', $this->getSelected())->get() as $source) {
|
||||
if (! $allowed->contains($source->project_id) || $source->project_id === $this->projectId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$name = $source->name;
|
||||
if (InspectionTemplate::where('project_id', $this->projectId)->where('name', $name)->exists()) {
|
||||
$name .= ' (copia)';
|
||||
}
|
||||
|
||||
InspectionTemplate::create([
|
||||
'name' => $name,
|
||||
'description' => $source->description,
|
||||
'project_id' => $this->projectId,
|
||||
'phase_id' => null,
|
||||
'fields' => $source->fields,
|
||||
]);
|
||||
$imported++;
|
||||
}
|
||||
|
||||
$this->clearSelected();
|
||||
$this->dispatch('templates-imported', count: $imported);
|
||||
}
|
||||
}
|
||||
@@ -107,7 +107,10 @@ class ProjectMap extends Component
|
||||
|
||||
public function loadTemplates()
|
||||
{
|
||||
$this->templates = InspectionTemplate::where('project_id', $this->project->id)->get();
|
||||
// Las plantillas son globales; cada proyecto elige cuáles usa (pivot).
|
||||
$this->templates = $this->project->inspectionTemplates()
|
||||
->orderBy('inspection_templates.name')
|
||||
->get();
|
||||
}
|
||||
|
||||
// ─── Layer / Phase visibility ────────────────────────────────────────────────
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Projects;
|
||||
|
||||
use App\Models\InspectionTemplate;
|
||||
use App\Models\Project;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Livewire\Attributes\Layout;
|
||||
use Livewire\Component;
|
||||
|
||||
#[Layout('layouts.app')]
|
||||
class ProjectTemplatesPicker extends Component
|
||||
{
|
||||
public Project $project;
|
||||
public array $assignedIds = [];
|
||||
public string $search = '';
|
||||
|
||||
public function mount(Project $project)
|
||||
{
|
||||
$this->project = $project;
|
||||
abort_unless(Auth::user()->can('edit projects'), 403);
|
||||
$this->assignedIds = $project->inspectionTemplates()->pluck('inspection_templates.id')->map(fn ($v) => (int) $v)->all();
|
||||
}
|
||||
|
||||
public function toggle(int $templateId): void
|
||||
{
|
||||
abort_unless(Auth::user()->can('edit projects'), 403);
|
||||
InspectionTemplate::findOrFail($templateId); // valida
|
||||
|
||||
if (in_array($templateId, $this->assignedIds, true)) {
|
||||
$this->project->inspectionTemplates()->detach($templateId);
|
||||
$this->assignedIds = array_values(array_diff($this->assignedIds, [$templateId]));
|
||||
$this->dispatch('notify', 'Plantilla desasignada del proyecto');
|
||||
} else {
|
||||
$this->project->inspectionTemplates()->syncWithoutDetaching([$templateId]);
|
||||
$this->assignedIds[] = $templateId;
|
||||
$this->dispatch('notify', 'Plantilla asignada al proyecto');
|
||||
}
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
$templates = InspectionTemplate::query()
|
||||
->when($this->search !== '', fn ($q) => $q->where('name', 'like', '%' . $this->search . '%'))
|
||||
->orderBy('name')->get();
|
||||
|
||||
return view('livewire.projects.project-templates-picker', [
|
||||
'templates' => $templates,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user