Ajoute dossiers et association de site aux schémas d'infrastructure

- Navigation par dossiers dans la section Infrastructure (créer, parcourir,
  fil d'ariane), isolée du navigateur Documents comme les schémas eux-mêmes.
- Un schéma peut être associé à un site de l'organisation, avec filtre par
  site dans la liste.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
mrKamoo
2026-08-24 07:57:45 +02:00
co-authored by Claude Sonnet 5
parent 3e5878192e
commit c1c6d56d1e
6 changed files with 241 additions and 24 deletions
+6 -1
View File
@@ -8,7 +8,7 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use League\CommonMark\CommonMarkConverter;
#[Fillable(['organization_id', 'folder_id', 'name', 'type', 'is_folder', 'content', 'file_path', 'office_extension', 'office_key', 'current_version', 'created_by'])]
#[Fillable(['organization_id', 'folder_id', 'location_id', 'name', 'type', 'is_folder', 'content', 'file_path', 'office_extension', 'office_key', 'current_version', 'created_by'])]
class Document extends Model implements Linkable
{
use SoftDeletes;
@@ -60,6 +60,11 @@ class Document extends Model implements Linkable
return $this->belongsTo(Document::class, 'folder_id');
}
public function location()
{
return $this->belongsTo(Location::class);
}
public function children()
{
return $this->hasMany(Document::class, 'folder_id');
@@ -0,0 +1,22 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('documents', function (Blueprint $table) {
$table->foreignId('location_id')->nullable()->after('folder_id')->constrained()->nullOnDelete();
});
}
public function down(): void
{
Schema::table('documents', function (Blueprint $table) {
$table->dropConstrainedForeignId('location_id');
});
}
};
@@ -9,17 +9,19 @@ new class extends Component
public Organization $organization;
public Document $document;
public string $backRoute = 'organizations.documents.index';
public ?int $folderId = null;
public bool $renaming = false;
public string $name = '';
public function mount(Organization $organization, Document $document, string $backRoute = 'organizations.documents.index'): void
public function mount(Organization $organization, Document $document, string $backRoute = 'organizations.documents.index', ?int $folderId = null): void
{
abort_unless($document->isDiagram(), 404);
$this->organization = $organization;
$this->document = $document;
$this->backRoute = $backRoute;
$this->folderId = $folderId;
$this->name = $document->name;
}
@@ -31,6 +33,11 @@ new class extends Component
$this->renaming = false;
}
public function updateLocation(?int $locationId): void
{
$this->document->update(['location_id' => $locationId]);
}
public function saveDiagram(string $xml): void
{
$contentChanged = $this->document->content !== $xml;
@@ -46,6 +53,13 @@ new class extends Component
]);
}
}
public function with(): array
{
return [
'locations' => $this->organization->locations()->orderBy('name')->get(),
];
}
};
?>
@@ -66,7 +80,18 @@ new class extends Component
<span class="text-sm text-slate-400 opacity-0 group-hover:opacity-100">Renommer</span>
</button>
@endif
<a href="{{ route($backRoute, $organization) }}" class="text-sm text-slate-500 hover:text-slate-900">Retour</a>
<a href="{{ route($backRoute, [$organization, 'folderId' => $folderId]) }}" class="text-sm text-slate-500 hover:text-slate-900">Retour</a>
</div>
<div class="mb-4 flex items-center gap-2">
<label class="text-sm font-medium text-slate-700">Site associé</label>
<select wire:change="updateLocation($event.target.value === '' ? null : $event.target.value)"
class="rounded border-slate-300 text-sm shadow-sm focus:border-slate-500 focus:ring-slate-500">
<option value="">Aucun</option>
@foreach ($locations as $location)
<option value="{{ $location->id }}" @selected($document->location_id === $location->id)>{{ $location->name }}</option>
@endforeach
</select>
</div>
<div
@@ -2,15 +2,41 @@
use App\Models\Document;
use App\Models\Organization;
use Livewire\Attributes\Url;
use Livewire\Component;
new class extends Component
{
public Organization $organization;
#[Url]
public ?int $folderId = null;
#[Url]
public ?int $locationId = null;
public string $newDiagramName = '';
public bool $showNewDiagram = false;
public string $newFolderName = '';
public bool $showNewFolder = false;
public function createFolder(): void
{
$this->validate(['newFolderName' => 'required|string|max:255']);
$this->organization->documents()->create([
'folder_id' => $this->folderId,
'name' => $this->newFolderName,
'type' => 'diagram',
'is_folder' => true,
'created_by' => auth()->id(),
]);
$this->newFolderName = '';
$this->showNewFolder = false;
}
public function createDiagram(): void
{
$this->validate(['newDiagramName' => 'required|string|max:255']);
@@ -29,6 +55,7 @@ new class extends Component
XML;
$document = $this->organization->documents()->create([
'folder_id' => $this->folderId,
'name' => $this->newDiagramName,
'type' => 'diagram',
'content' => $emptyDiagram,
@@ -42,7 +69,7 @@ new class extends Component
'created_by' => auth()->id(),
]);
$this->redirectRoute('organizations.infrastructure.edit', [$this->organization, $document], navigate: true);
$this->redirectRoute('organizations.infrastructure.edit', [$this->organization, $document, 'folderId' => $this->folderId], navigate: true);
}
public function delete(int $documentId): void
@@ -50,15 +77,49 @@ new class extends Component
$document = $this->organization->documents()->where('type', 'diagram')->findOrFail($documentId);
$document->delete();
session()->flash('status', "Schéma « {$document->name} » supprimé.");
session()->flash('status', "« {$document->name} » supprimé.");
}
private function folderTree(\Illuminate\Support\Collection $allFolders): \Illuminate\Support\Collection
{
$byParent = $allFolders->groupBy('folder_id');
$walk = function ($parentId, $depth) use (&$walk, $byParent) {
return ($byParent->get($parentId) ?? collect())
->sortBy('name')
->flatMap(fn ($folder) => collect([['folder' => $folder, 'depth' => $depth]])
->concat($walk($folder->id, $depth + 1)));
};
return $walk(null, 0);
}
public function with(): array
{
$currentFolder = $this->folderId
? $this->organization->documents()->where('type', 'diagram')->find($this->folderId)
: null;
$breadcrumbs = collect();
$walk = $currentFolder;
while ($walk) {
$breadcrumbs->prepend($walk);
$walk = $walk->folder;
}
$allFolders = $this->organization->documents()->where('type', 'diagram')->where('is_folder', true)->get();
return [
'diagrams' => $this->organization->documents()
'currentFolder' => $currentFolder,
'breadcrumbs' => $breadcrumbs,
'folderTree' => $this->folderTree($allFolders),
'locations' => $this->organization->locations()->orderBy('name')->get(),
'items' => $this->organization->documents()
->where('type', 'diagram')
->with('creator')
->where('folder_id', $this->folderId)
->when($this->locationId, fn ($query) => $query->where('location_id', $this->locationId))
->with('location')
->orderByDesc('is_folder')
->orderBy('name')
->get(),
];
@@ -69,10 +130,16 @@ new class extends Component
<div>
<div class="mb-6 flex items-center justify-between">
<h1 class="text-2xl font-semibold text-slate-900">Infrastructure</h1>
<button type="button" wire:click="$toggle('showNewDiagram')"
class="cursor-pointer rounded bg-slate-900 px-4 py-2 text-sm font-medium text-white hover:bg-slate-700">
+ Nouveau schéma
</button>
<div class="flex gap-2">
<button type="button" wire:click="$set('showNewFolder', true)" title="Nouveau dossier"
class="flex h-10 w-10 cursor-pointer items-center justify-center rounded border border-slate-300 text-lg hover:bg-slate-50">
📁
</button>
<button type="button" wire:click="$set('showNewDiagram', true)"
class="cursor-pointer rounded bg-slate-900 px-4 py-2 text-sm font-medium text-white hover:bg-slate-700">
+ Nouveau schéma
</button>
</div>
</div>
@if (session('status'))
@@ -81,6 +148,35 @@ new class extends Component
</div>
@endif
<div class="mb-4 flex items-center justify-between gap-4">
<div class="flex items-center gap-1 text-sm text-slate-500">
<a wire:click="$set('folderId', null)" href="#" class="hover:text-slate-900">Infrastructure</a>
@foreach ($breadcrumbs as $crumb)
<span>/</span>
<a wire:click="$set('folderId', {{ $crumb->id }})" href="#" class="hover:text-slate-900">{{ $crumb->name }}</a>
@endforeach
</div>
@if ($locations->isNotEmpty())
<select wire:model.live="locationId" class="rounded border-slate-300 text-sm shadow-sm focus:border-slate-500 focus:ring-slate-500">
<option value="">Tous les sites</option>
@foreach ($locations as $location)
<option value="{{ $location->id }}">{{ $location->name }}</option>
@endforeach
</select>
@endif
</div>
@if ($showNewFolder)
<form wire:submit="createFolder" class="mb-4 flex items-center gap-2 rounded border border-slate-200 bg-white p-3">
<input type="text" wire:model="newFolderName" placeholder="Nom du dossier" autofocus
class="flex-1 rounded border-slate-300 text-sm shadow-sm focus:border-slate-500 focus:ring-slate-500">
<button type="submit" class="cursor-pointer rounded bg-slate-900 px-3 py-1.5 text-sm text-white hover:bg-slate-700">Créer</button>
<button type="button" wire:click="$set('showNewFolder', false)" class="cursor-pointer text-sm text-slate-500 hover:text-slate-900">Annuler</button>
@error('newFolderName') <p class="text-sm text-red-600">{{ $message }}</p> @enderror
</form>
@endif
@if ($showNewDiagram)
<form wire:submit="createDiagram" class="mb-4 flex items-start gap-2 rounded border border-slate-200 bg-white p-3">
<div class="flex-1">
@@ -99,26 +195,35 @@ new class extends Component
<thead class="bg-slate-50 text-left text-xs uppercase tracking-wide text-slate-500">
<tr>
<th class="px-4 py-3">Nom</th>
<th class="px-4 py-3">Créé par</th>
<th class="px-4 py-3">Site</th>
<th class="px-4 py-3">Mis à jour</th>
<th class="px-4 py-3"></th>
</tr>
</thead>
<tbody class="divide-y divide-slate-100">
@forelse ($diagrams as $diagram)
<tr wire:key="diagram-{{ $diagram->id }}">
@forelse ($items as $item)
@php $isFolder = $item->isFolder(); @endphp
<tr wire:key="diagram-{{ $item->id }}">
<td class="px-4 py-3">
<a href="{{ route('organizations.infrastructure.edit', [$organization, $diagram]) }}" class="font-medium text-slate-900 hover:underline">
🗺️ {{ $diagram->name }}
</a>
@if ($isFolder)
<a wire:click="$set('folderId', {{ $item->id }})" href="#" class="font-medium text-slate-900 hover:underline">
📁 {{ $item->name }}
</a>
@else
<a href="{{ route('organizations.infrastructure.edit', [$organization, $item, 'folderId' => $this->folderId]) }}" class="font-medium text-slate-900 hover:underline">
🗺️ {{ $item->name }}
</a>
@endif
</td>
<td class="px-4 py-3 text-slate-600">{{ $diagram->creator?->name ?? '—' }}</td>
<td class="px-4 py-3 text-slate-600">{{ $diagram->updated_at->diffForHumans() }}</td>
<td class="px-4 py-3 text-slate-600">{{ $isFolder ? '—' : ($item->location?->name ?? '—') }}</td>
<td class="px-4 py-3 text-slate-600">{{ $item->updated_at->diffForHumans() }}</td>
<td class="px-4 py-3 text-right">
<a href="{{ route('organizations.infrastructure.edit', [$organization, $diagram]) }}" class="text-slate-500 hover:text-slate-900">Ouvrir</a>
@unless ($isFolder)
<a href="{{ route('organizations.infrastructure.edit', [$organization, $item, 'folderId' => $this->folderId]) }}" class="text-slate-500 hover:text-slate-900">Ouvrir</a>
@endunless
<button type="button"
wire:click="delete({{ $diagram->id }})"
wire:confirm="Supprimer ce schéma ? Cette action est irréversible."
wire:click="delete({{ $item->id }})"
wire:confirm="Supprimer ce {{ $isFolder ? 'dossier' : 'schéma' }} ? Cette action est irréversible."
class="ml-3 cursor-pointer text-red-500 hover:text-red-700">
Supprimer
</button>
@@ -126,7 +231,7 @@ new class extends Component
</tr>
@empty
<tr>
<td colspan="4" class="px-4 py-6 text-center text-slate-400">Aucun schéma pour le moment.</td>
<td colspan="4" class="px-4 py-6 text-center text-slate-400">Dossier vide.</td>
</tr>
@endforelse
</tbody>
@@ -1,3 +1,4 @@
<x-layouts.app :title="$document->name">
<livewire:documents.diagram-editor :organization="$organization" :document="$document" back-route="organizations.infrastructure.index" />
<livewire:documents.diagram-editor :organization="$organization" :document="$document"
back-route="organizations.infrastructure.index" :folder-id="request()->integer('folderId') ?: null" />
</x-layouts.app>
+59
View File
@@ -2,6 +2,7 @@
namespace Tests\Feature;
use App\Models\Location;
use App\Models\Organization;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -100,4 +101,62 @@ class InfrastructureTest extends TestCase
->assertDontSee('Schéma réseau - Mairie')
->assertSee('Compte-rendu.md');
}
public function test_can_create_a_folder_and_a_diagram_inside_it(): void
{
$user = User::factory()->create();
$org = Organization::create(['name' => 'Test Commune', 'slug' => 'test-commune', 'org_type' => 'client']);
$this->actingAs($user);
$component = Livewire::test('infrastructure.index', ['organization' => $org])
->set('newFolderName', 'Réseau')
->call('createFolder')
->assertHasNoErrors();
$folder = $org->documents()->where('type', 'diagram')->where('is_folder', true)->firstOrFail();
$this->assertSame('Réseau', $folder->name);
$component->set('folderId', $folder->id)
->set('newDiagramName', 'Schéma coeur de réseau')
->call('createDiagram')
->assertHasNoErrors();
$diagram = $org->documents()->where('type', 'diagram')->where('name', 'Schéma coeur de réseau')->firstOrFail();
$this->assertSame($folder->id, $diagram->folder_id);
// The folder itself must not leak into the general Documents browser.
Livewire::test('documents.index', ['organization' => $org])->assertDontSee('Réseau');
}
public function test_a_diagram_can_be_associated_with_a_site(): void
{
$user = User::factory()->create();
$org = Organization::create(['name' => 'Test Commune', 'slug' => 'test-commune', 'org_type' => 'client']);
$location = Location::create(['organization_id' => $org->id, 'name' => 'Mairie']);
$document = $org->documents()->create([
'name' => 'Schéma test',
'type' => 'diagram',
'content' => '<mxfile></mxfile>',
'current_version' => 1,
'created_by' => $user->id,
]);
$this->actingAs($user);
Livewire::test('documents.diagram-editor', ['organization' => $org, 'document' => $document])
->call('updateLocation', $location->id)
->assertHasNoErrors();
$this->assertSame($location->id, $document->refresh()->location_id);
Livewire::test('infrastructure.index', ['organization' => $org, 'locationId' => $location->id])
->assertSee('Schéma test');
$otherLocation = Location::create(['organization_id' => $org->id, 'name' => 'Annexe']);
Livewire::test('infrastructure.index', ['organization' => $org, 'locationId' => $otherLocation->id])
->assertDontSee('Schéma test');
}
}