Ajoute fiche d'identité informatique, catalogue prestataires et schémas draw.io
- Fiche d'identité par organisation (technicien référent, FAI, IP publiques, prestataires liés) accessible depuis un nouveau menu. - Nouveau module Prestataires dédié (catalogue léger réutilisable entre communes, avec journal de suivi des échanges) plutôt que de détourner les organisations de type "Fournisseur". - Nouveau menu Infrastructure par organisation avec schémas réseau/fonctionnel édités via draw.io embarqué (URL configurable, cf. réglages admin). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
74c0b01a2e
commit
3c87ced1e4
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Document;
|
||||
use App\Models\Organization;
|
||||
use Livewire\Component;
|
||||
|
||||
new class extends Component
|
||||
{
|
||||
public Organization $organization;
|
||||
public Document $document;
|
||||
public string $backRoute = 'organizations.documents.index';
|
||||
|
||||
public bool $renaming = false;
|
||||
public string $name = '';
|
||||
|
||||
public function mount(Organization $organization, Document $document, string $backRoute = 'organizations.documents.index'): void
|
||||
{
|
||||
abort_unless($document->isDiagram(), 404);
|
||||
|
||||
$this->organization = $organization;
|
||||
$this->document = $document;
|
||||
$this->backRoute = $backRoute;
|
||||
$this->name = $document->name;
|
||||
}
|
||||
|
||||
public function rename(): void
|
||||
{
|
||||
$validated = $this->validate(['name' => 'required|string|max:255']);
|
||||
|
||||
$this->document->update($validated);
|
||||
$this->renaming = false;
|
||||
}
|
||||
|
||||
public function saveDiagram(string $xml): void
|
||||
{
|
||||
$contentChanged = $this->document->content !== $xml;
|
||||
|
||||
$this->document->update(['content' => $xml]);
|
||||
|
||||
if ($contentChanged) {
|
||||
$this->document->increment('current_version');
|
||||
$this->document->versions()->create([
|
||||
'version' => $this->document->current_version,
|
||||
'content' => $xml,
|
||||
'created_by' => auth()->id(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
};
|
||||
?>
|
||||
|
||||
<div>
|
||||
<p class="text-sm text-slate-500">{{ $organization->name }}</p>
|
||||
<div class="mb-4 flex items-center justify-between">
|
||||
@if ($renaming)
|
||||
<form wire:submit="rename" class="flex items-center gap-2">
|
||||
<input type="text" wire:model="name" autofocus
|
||||
class="rounded border-slate-300 text-2xl font-semibold 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">Enregistrer</button>
|
||||
<button type="button" wire:click="$set('renaming', false)" class="cursor-pointer text-sm text-slate-500 hover:text-slate-900">Annuler</button>
|
||||
@error('name') <p class="text-sm text-red-600">{{ $message }}</p> @enderror
|
||||
</form>
|
||||
@else
|
||||
<button type="button" wire:click="$set('renaming', true)" class="group flex cursor-pointer items-center gap-2">
|
||||
<h1 class="text-2xl font-semibold text-slate-900">{{ $document->name }}</h1>
|
||||
<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>
|
||||
</div>
|
||||
|
||||
<div
|
||||
wire:ignore
|
||||
x-data="drawioEditor(@js($document->content ?? ''), @js(config('drawio.url')))"
|
||||
x-init="init()"
|
||||
class="w-full"
|
||||
style="height: 78vh;"
|
||||
>
|
||||
<iframe x-ref="iframe" src="{{ config('drawio.url') }}?embed=1&proto=json&spin=1&noSaveBtn=0&saveAndExit=0&noExitBtn=1"
|
||||
class="h-full w-full rounded border border-slate-200 bg-white"></iframe>
|
||||
</div>
|
||||
|
||||
<p class="mt-2 text-xs text-slate-500" x-data="{ status: '' }" x-on:diagram-status.window="status = $event.detail" x-text="status"></p>
|
||||
|
||||
<div class="mt-8 max-w-5xl">
|
||||
<x-documents.version-history :document="$document" />
|
||||
|
||||
<div class="mt-8">
|
||||
<livewire:related-items.panel :organization="$organization" :item="$document" :key="'related-'.$document->id" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@once
|
||||
<script>
|
||||
function drawioEditor(initialXml, baseUrl) {
|
||||
return {
|
||||
origin: null,
|
||||
init() {
|
||||
this.origin = new URL(baseUrl, window.location.href).origin;
|
||||
window.addEventListener('message', (evt) => this.handleMessage(evt));
|
||||
},
|
||||
handleMessage(evt) {
|
||||
if (evt.origin !== this.origin) return;
|
||||
|
||||
let msg;
|
||||
try {
|
||||
msg = JSON.parse(evt.data);
|
||||
} catch (e) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.event === 'init') {
|
||||
this.post({ action: 'load', xml: initialXml, autosave: 0 });
|
||||
} else if (msg.event === 'save') {
|
||||
this.$wire.saveDiagram(msg.xml).then(() => {
|
||||
window.dispatchEvent(new CustomEvent('diagram-status', { detail: 'Enregistré à ' + new Date().toLocaleTimeString() }));
|
||||
this.post({ action: 'status', message: 'Enregistré', modified: false });
|
||||
});
|
||||
} else if (msg.event === 'exit') {
|
||||
this.post({ action: 'status', message: '', modified: false });
|
||||
}
|
||||
},
|
||||
post(payload) {
|
||||
this.$refs.iframe.contentWindow.postMessage(JSON.stringify(payload), this.origin);
|
||||
},
|
||||
};
|
||||
}
|
||||
</script>
|
||||
@endonce
|
||||
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Document;
|
||||
use App\Models\Organization;
|
||||
use Livewire\Component;
|
||||
|
||||
new class extends Component
|
||||
{
|
||||
public Organization $organization;
|
||||
|
||||
public string $newDiagramName = '';
|
||||
public bool $showNewDiagram = false;
|
||||
|
||||
public function createDiagram(): void
|
||||
{
|
||||
$this->validate(['newDiagramName' => 'required|string|max:255']);
|
||||
|
||||
$emptyDiagram = <<<'XML'
|
||||
<mxfile host="app.diagrams.net">
|
||||
<diagram name="Page-1">
|
||||
<mxGraphModel dx="800" dy="600" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0">
|
||||
<root>
|
||||
<mxCell id="0" />
|
||||
<mxCell id="1" parent="0" />
|
||||
</root>
|
||||
</mxGraphModel>
|
||||
</diagram>
|
||||
</mxfile>
|
||||
XML;
|
||||
|
||||
$document = $this->organization->documents()->create([
|
||||
'name' => $this->newDiagramName,
|
||||
'type' => 'diagram',
|
||||
'content' => $emptyDiagram,
|
||||
'current_version' => 1,
|
||||
'created_by' => auth()->id(),
|
||||
]);
|
||||
|
||||
$document->versions()->create([
|
||||
'version' => 1,
|
||||
'content' => $emptyDiagram,
|
||||
'created_by' => auth()->id(),
|
||||
]);
|
||||
|
||||
$this->redirectRoute('organizations.infrastructure.edit', [$this->organization, $document], navigate: true);
|
||||
}
|
||||
|
||||
public function delete(int $documentId): void
|
||||
{
|
||||
$document = $this->organization->documents()->where('type', 'diagram')->findOrFail($documentId);
|
||||
$document->delete();
|
||||
|
||||
session()->flash('status', "Schéma « {$document->name} » supprimé.");
|
||||
}
|
||||
|
||||
public function with(): array
|
||||
{
|
||||
return [
|
||||
'diagrams' => $this->organization->documents()
|
||||
->where('type', 'diagram')
|
||||
->with('creator')
|
||||
->orderBy('name')
|
||||
->get(),
|
||||
];
|
||||
}
|
||||
};
|
||||
?>
|
||||
|
||||
<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>
|
||||
|
||||
@if (session('status'))
|
||||
<div class="mb-4 rounded border border-emerald-200 bg-emerald-50 px-3 py-2 text-sm text-emerald-700">
|
||||
{{ session('status') }}
|
||||
</div>
|
||||
@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">
|
||||
<input type="text" wire:model="newDiagramName" placeholder="Ex: Schéma réseau - Mairie, Schéma fonctionnel - SI RH"
|
||||
class="w-full rounded border-slate-300 text-sm shadow-sm focus:border-slate-500 focus:ring-slate-500">
|
||||
@error('newDiagramName') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
|
||||
</div>
|
||||
<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>
|
||||
</form>
|
||||
@endif
|
||||
|
||||
<div class="overflow-hidden rounded border border-slate-200 bg-white">
|
||||
<table class="min-w-full divide-y divide-slate-200 text-sm">
|
||||
<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">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 }}">
|
||||
<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>
|
||||
</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-right">
|
||||
<a href="{{ route('organizations.infrastructure.edit', [$organization, $diagram]) }}" class="text-slate-500 hover:text-slate-900">Ouvrir</a>
|
||||
<button type="button"
|
||||
wire:click="delete({{ $diagram->id }})"
|
||||
wire:confirm="Supprimer ce schéma ? Cette action est irréversible."
|
||||
class="ml-3 cursor-pointer text-red-500 hover:text-red-700">
|
||||
Supprimer
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
@empty
|
||||
<tr>
|
||||
<td colspan="4" class="px-4 py-6 text-center text-slate-400">Aucun schéma pour le moment.</td>
|
||||
</tr>
|
||||
@endforelse
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
@@ -19,6 +19,10 @@
|
||||
class="block rounded px-3 py-2 hover:bg-slate-800 {{ request()->routeIs('organizations.*') ? 'bg-slate-800 text-white' : '' }}">
|
||||
Organisations
|
||||
</a>
|
||||
<a href="{{ route('providers.index') }}"
|
||||
class="block rounded px-3 py-2 hover:bg-slate-800 {{ request()->routeIs('providers.*') ? 'bg-slate-800 text-white' : '' }}">
|
||||
Prestataires
|
||||
</a>
|
||||
|
||||
@if ($currentOrganization)
|
||||
@php
|
||||
@@ -38,6 +42,10 @@
|
||||
class="block rounded px-3 py-2 hover:bg-slate-800 {{ request()->routeIs('organizations.dashboard') ? 'bg-slate-800 text-white' : '' }}">
|
||||
Tableau de bord
|
||||
</a>
|
||||
<a href="{{ route('organizations.identity-card', $currentOrganization) }}"
|
||||
class="block rounded px-3 py-2 hover:bg-slate-800 {{ request()->routeIs('organizations.identity-card') ? 'bg-slate-800 text-white' : '' }}">
|
||||
Fiche d'identité
|
||||
</a>
|
||||
<a href="{{ route('organizations.todos.index', $currentOrganization) }}"
|
||||
class="block rounded px-3 py-2 hover:bg-slate-800 {{ request()->routeIs('organizations.todos.*') ? 'bg-slate-800 text-white' : '' }}">
|
||||
ToDo
|
||||
@@ -62,6 +70,10 @@
|
||||
class="block rounded px-3 py-2 hover:bg-slate-800 {{ request()->routeIs('organizations.documents.*') ? 'bg-slate-800 text-white' : '' }}">
|
||||
Documents
|
||||
</a>
|
||||
<a href="{{ route('organizations.infrastructure.index', $currentOrganization) }}"
|
||||
class="block rounded px-3 py-2 hover:bg-slate-800 {{ request()->routeIs('organizations.infrastructure.*') ? 'bg-slate-800 text-white' : '' }}">
|
||||
Infrastructure
|
||||
</a>
|
||||
<a href="{{ route('organizations.virtual-machines.index', $currentOrganization) }}"
|
||||
class="block rounded px-3 py-2 hover:bg-slate-800 {{ request()->routeIs('organizations.virtual-machines.*') ? 'bg-slate-800 text-white' : '' }}">
|
||||
Machines virtuelles
|
||||
|
||||
@@ -0,0 +1,198 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Organization;
|
||||
use App\Models\Provider;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Livewire\Component;
|
||||
|
||||
new class extends Component
|
||||
{
|
||||
public Organization $organization;
|
||||
|
||||
public ?int $technicianUserId = null;
|
||||
public string $ispName = '';
|
||||
public string $publicIpsText = '';
|
||||
|
||||
public ?int $newProviderId = null;
|
||||
public string $newProviderLabel = '';
|
||||
|
||||
public function mount(Organization $organization): void
|
||||
{
|
||||
$this->organization = $organization;
|
||||
$this->technicianUserId = $organization->technician_user_id;
|
||||
$this->ispName = $organization->isp_name ?? '';
|
||||
$this->publicIpsText = implode("\n", $organization->public_ips ?? []);
|
||||
}
|
||||
|
||||
public function save(): void
|
||||
{
|
||||
$validated = $this->validate([
|
||||
'technicianUserId' => 'nullable|exists:users,id',
|
||||
'ispName' => 'nullable|string|max:255',
|
||||
'publicIpsText' => 'nullable|string',
|
||||
]);
|
||||
|
||||
$ips = collect(preg_split('/[\s,]+/', (string) $validated['publicIpsText'], -1, PREG_SPLIT_NO_EMPTY))
|
||||
->unique()
|
||||
->values();
|
||||
|
||||
if ($ips->contains(fn ($ip) => filter_var($ip, FILTER_VALIDATE_IP) === false)) {
|
||||
$this->addError('publicIpsText', 'Une ou plusieurs adresses IP sont invalides.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->organization->update([
|
||||
'technician_user_id' => $validated['technicianUserId'],
|
||||
'isp_name' => $validated['ispName'] !== '' ? $validated['ispName'] : null,
|
||||
'public_ips' => $ips->all(),
|
||||
]);
|
||||
|
||||
session()->flash('status', "Fiche d'identité mise à jour.");
|
||||
}
|
||||
|
||||
public function addProvider(): void
|
||||
{
|
||||
$validated = $this->validate([
|
||||
'newProviderId' => 'required|exists:providers,id',
|
||||
'newProviderLabel' => 'nullable|string|max:255',
|
||||
]);
|
||||
|
||||
$label = $validated['newProviderLabel'] ?: null;
|
||||
|
||||
$alreadyLinked = $this->organization->providers()
|
||||
->wherePivot('provider_id', $validated['newProviderId'])
|
||||
->wherePivot('label', '=', $label)
|
||||
->exists();
|
||||
|
||||
if ($alreadyLinked) {
|
||||
$this->addError('newProviderId', 'Ce prestataire est déjà lié avec ce libellé.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->organization->providers()->attach($validated['newProviderId'], ['label' => $label]);
|
||||
|
||||
$this->reset(['newProviderId', 'newProviderLabel']);
|
||||
}
|
||||
|
||||
public function removeProvider(int $pivotId): void
|
||||
{
|
||||
DB::table('organization_provider')
|
||||
->where('id', $pivotId)
|
||||
->where('organization_id', $this->organization->id)
|
||||
->delete();
|
||||
}
|
||||
|
||||
public function with(): array
|
||||
{
|
||||
return [
|
||||
'technicians' => User::role(['Administrateur', 'Technicien'])->orderBy('name')->get(),
|
||||
'availableProviders' => Provider::orderBy('name')->get(),
|
||||
'providers' => $this->organization->providers()->orderBy('name')->get(),
|
||||
];
|
||||
}
|
||||
};
|
||||
?>
|
||||
|
||||
<div class="max-w-3xl">
|
||||
<p class="text-sm text-slate-500">{{ $organization->name }}</p>
|
||||
<h1 class="mb-6 text-2xl font-semibold text-slate-900">Fiche d'identité informatique</h1>
|
||||
|
||||
@if (session('status'))
|
||||
<div class="mb-4 rounded border border-emerald-200 bg-emerald-50 px-3 py-2 text-sm text-emerald-700">
|
||||
{{ session('status') }}
|
||||
</div>
|
||||
@endif
|
||||
|
||||
<form wire:submit="save" class="space-y-4 rounded border border-slate-200 bg-white p-6">
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-slate-700">Technicien gestionnaire</label>
|
||||
<select wire:model="technicianUserId"
|
||||
class="mt-1 w-full rounded border-slate-300 text-sm shadow-sm focus:border-slate-500 focus:ring-slate-500">
|
||||
<option value="">—</option>
|
||||
@foreach ($technicians as $technician)
|
||||
<option value="{{ $technician->id }}">{{ $technician->name }}</option>
|
||||
@endforeach
|
||||
</select>
|
||||
@error('technicianUserId') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-slate-700">Fournisseur d'accès internet</label>
|
||||
<input type="text" wire:model="ispName" placeholder="Orange, Free Pro, Telindus..."
|
||||
class="mt-1 w-full rounded border-slate-300 text-sm shadow-sm focus:border-slate-500 focus:ring-slate-500">
|
||||
@error('ispName') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-slate-700">IP(s) publiques</label>
|
||||
<textarea wire:model="publicIpsText" rows="3" placeholder="Une adresse IP par ligne"
|
||||
class="mt-1 w-full rounded border-slate-300 font-mono text-sm shadow-sm focus:border-slate-500 focus:ring-slate-500"></textarea>
|
||||
@error('publicIpsText') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
|
||||
</div>
|
||||
|
||||
<div class="pt-2">
|
||||
<button type="submit"
|
||||
class="cursor-pointer rounded bg-slate-900 px-4 py-2 text-sm font-medium text-white hover:bg-slate-700">
|
||||
Enregistrer
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<div class="mt-8 rounded border border-slate-200 bg-white p-6">
|
||||
<h2 class="mb-3 text-sm font-semibold uppercase tracking-wide text-slate-500">Prestataires</h2>
|
||||
|
||||
<ul class="mb-4 divide-y divide-slate-100">
|
||||
@forelse ($providers as $provider)
|
||||
<li wire:key="provider-{{ $provider->pivot->id }}" class="flex items-center gap-3 py-2 text-sm">
|
||||
<img src="{{ $provider->logo_url }}" alt="Logo {{ $provider->name }}"
|
||||
class="h-6 w-6 shrink-0 rounded border border-slate-200 bg-white object-contain p-0.5">
|
||||
<div class="flex-1">
|
||||
<a href="{{ route('providers.edit', $provider) }}" class="font-medium text-slate-900 hover:underline">
|
||||
{{ $provider->name }}
|
||||
</a>
|
||||
@if ($provider->pivot->label)
|
||||
<span class="text-xs text-slate-500">· {{ $provider->pivot->label }}</span>
|
||||
@endif
|
||||
</div>
|
||||
<button type="button" wire:click="removeProvider({{ $provider->pivot->id }})"
|
||||
wire:confirm="Retirer ce prestataire ?"
|
||||
class="cursor-pointer shrink-0 text-xs text-red-500 hover:text-red-700">
|
||||
Retirer
|
||||
</button>
|
||||
</li>
|
||||
@empty
|
||||
<li class="py-2 text-sm text-slate-400">Aucun prestataire lié.</li>
|
||||
@endforelse
|
||||
</ul>
|
||||
|
||||
<form wire:submit="addProvider" class="flex flex-wrap items-start gap-2 rounded border border-slate-100 bg-slate-50 p-3">
|
||||
<div class="flex-1">
|
||||
<select wire:model="newProviderId"
|
||||
class="w-full rounded border-slate-300 text-sm shadow-sm focus:border-slate-500 focus:ring-slate-500">
|
||||
<option value="">Sélectionner un prestataire...</option>
|
||||
@foreach ($availableProviders as $availableProvider)
|
||||
<option value="{{ $availableProvider->id }}">{{ $availableProvider->name }}</option>
|
||||
@endforeach
|
||||
</select>
|
||||
@error('newProviderId') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
|
||||
</div>
|
||||
<div class="flex-1">
|
||||
<input type="text" wire:model="newProviderLabel" placeholder="Libellé (ex: Téléphonie, Maintenance copieurs)"
|
||||
class="w-full rounded border-slate-300 text-sm shadow-sm focus:border-slate-500 focus:ring-slate-500">
|
||||
</div>
|
||||
<button type="submit" class="cursor-pointer rounded bg-slate-900 px-3 py-1.5 text-sm text-white hover:bg-slate-700">
|
||||
Ajouter
|
||||
</button>
|
||||
</form>
|
||||
|
||||
@if ($availableProviders->isEmpty())
|
||||
<p class="mt-3 text-xs text-slate-500">
|
||||
Aucun prestataire n'existe encore.
|
||||
<a href="{{ route('providers.create') }}" class="underline hover:text-slate-900">En créer un</a>.
|
||||
</p>
|
||||
@endif
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,217 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Provider;
|
||||
use App\Models\ProviderLogEntry;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Livewire\Component;
|
||||
use Livewire\WithFileUploads;
|
||||
|
||||
new class extends Component
|
||||
{
|
||||
use WithFileUploads;
|
||||
|
||||
public ?Provider $provider = null;
|
||||
|
||||
public string $name = '';
|
||||
public string $phone = '';
|
||||
public string $email = '';
|
||||
public string $website = '';
|
||||
public string $notes = '';
|
||||
|
||||
public $logo = null;
|
||||
|
||||
public string $newLogCategory = 'contact';
|
||||
public string $newLogNote = '';
|
||||
|
||||
public function mount(?Provider $provider = null): void
|
||||
{
|
||||
if ($provider?->exists) {
|
||||
$this->provider = $provider;
|
||||
$this->name = $provider->name;
|
||||
$this->phone = $provider->phone ?? '';
|
||||
$this->email = $provider->email ?? '';
|
||||
$this->website = $provider->website ?? '';
|
||||
$this->notes = $provider->notes ?? '';
|
||||
}
|
||||
}
|
||||
|
||||
public function save(): void
|
||||
{
|
||||
$validated = $this->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
'phone' => 'nullable|string|max:50',
|
||||
'email' => 'nullable|email|max:255',
|
||||
'website' => 'nullable|string|max:255',
|
||||
'notes' => 'nullable|string',
|
||||
'logo' => 'nullable|image|max:2048',
|
||||
]);
|
||||
|
||||
unset($validated['logo']);
|
||||
|
||||
foreach (['phone', 'email', 'website', 'notes'] as $field) {
|
||||
$validated[$field] = $validated[$field] !== '' ? $validated[$field] : null;
|
||||
}
|
||||
|
||||
if ($this->provider?->exists) {
|
||||
$this->provider->update($validated);
|
||||
} else {
|
||||
$this->provider = Provider::create($validated);
|
||||
}
|
||||
|
||||
if ($this->logo) {
|
||||
if ($this->provider->logo_path) {
|
||||
Storage::disk('public')->delete($this->provider->logo_path);
|
||||
}
|
||||
|
||||
$path = $this->logo->store("providers/{$this->provider->id}", 'public');
|
||||
$this->provider->update(['logo_path' => $path]);
|
||||
}
|
||||
|
||||
session()->flash('status', "Prestataire « {$this->provider->name} » enregistré.");
|
||||
|
||||
$this->redirectRoute('providers.index', navigate: true);
|
||||
}
|
||||
|
||||
public function addLogEntry(): void
|
||||
{
|
||||
$this->validate([
|
||||
'newLogCategory' => 'required|in:'.implode(',', array_keys(ProviderLogEntry::CATEGORIES)),
|
||||
'newLogNote' => 'required|string',
|
||||
]);
|
||||
|
||||
$this->provider->logEntries()->create([
|
||||
'category' => $this->newLogCategory,
|
||||
'note' => $this->newLogNote,
|
||||
'created_by' => auth()->id(),
|
||||
]);
|
||||
|
||||
$this->newLogNote = '';
|
||||
}
|
||||
|
||||
public function deleteLogEntry(int $entryId): void
|
||||
{
|
||||
$this->provider->logEntries()->findOrFail($entryId)->delete();
|
||||
}
|
||||
|
||||
public function with(): array
|
||||
{
|
||||
return [
|
||||
'logEntries' => $this->provider?->exists ? $this->provider->logEntries()->with('creator')->limit(30)->get() : collect(),
|
||||
'categories' => ProviderLogEntry::CATEGORIES,
|
||||
];
|
||||
}
|
||||
};
|
||||
?>
|
||||
|
||||
<div class="max-w-2xl">
|
||||
<h1 class="mb-6 text-2xl font-semibold text-slate-900">
|
||||
{{ $provider?->exists ? 'Modifier le prestataire' : 'Nouveau prestataire' }}
|
||||
</h1>
|
||||
|
||||
@if (session('status'))
|
||||
<div class="mb-4 rounded border border-emerald-200 bg-emerald-50 px-3 py-2 text-sm text-emerald-700">
|
||||
{{ session('status') }}
|
||||
</div>
|
||||
@endif
|
||||
|
||||
<form wire:submit="save" class="space-y-4 rounded border border-slate-200 bg-white p-6">
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-slate-700">Logo</label>
|
||||
<div class="mt-1 flex items-center gap-4">
|
||||
<img src="{{ $logo ? $logo->temporaryUrl() : ($provider?->logo_url ?? asset('images/default-organization-logo.svg')) }}"
|
||||
class="h-16 w-16 rounded border border-slate-200 object-contain p-1" alt="Logo">
|
||||
<input type="file" wire:model="logo" accept="image/*"
|
||||
class="block text-sm text-slate-600 file:mr-3 file:rounded file:border-0 file:bg-slate-100 file:px-3 file:py-1.5 file:text-sm file:font-medium file:text-slate-700 hover:file:bg-slate-200">
|
||||
</div>
|
||||
@error('logo') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-slate-700">Nom</label>
|
||||
<input type="text" wire:model="name"
|
||||
class="mt-1 w-full rounded border-slate-300 text-sm shadow-sm focus:border-slate-500 focus:ring-slate-500">
|
||||
@error('name') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
|
||||
</div>
|
||||
|
||||
<div class="grid grid-cols-2 gap-4">
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-slate-700">Téléphone</label>
|
||||
<input type="text" wire:model="phone"
|
||||
class="mt-1 w-full rounded border-slate-300 text-sm shadow-sm focus:border-slate-500 focus:ring-slate-500">
|
||||
</div>
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-slate-700">Email</label>
|
||||
<input type="email" wire:model="email"
|
||||
class="mt-1 w-full rounded border-slate-300 text-sm shadow-sm focus:border-slate-500 focus:ring-slate-500">
|
||||
@error('email') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-slate-700">Site web</label>
|
||||
<input type="text" wire:model="website" placeholder="https://..."
|
||||
class="mt-1 w-full rounded border-slate-300 text-sm shadow-sm focus:border-slate-500 focus:ring-slate-500">
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-slate-700">Notes</label>
|
||||
<textarea wire:model="notes" rows="3"
|
||||
class="mt-1 w-full rounded border-slate-300 text-sm shadow-sm focus:border-slate-500 focus:ring-slate-500"></textarea>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-3 pt-2">
|
||||
<button type="submit"
|
||||
class="cursor-pointer rounded bg-slate-900 px-4 py-2 text-sm font-medium text-white hover:bg-slate-700">
|
||||
Enregistrer
|
||||
</button>
|
||||
<a href="{{ route('providers.index') }}" class="text-sm text-slate-500 hover:text-slate-900">Annuler</a>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
@if ($provider?->exists)
|
||||
<div class="mt-8 rounded border border-slate-200 bg-white p-6">
|
||||
<h2 class="mb-3 text-sm font-semibold uppercase tracking-wide text-slate-500">Journal de suivi</h2>
|
||||
|
||||
<form wire:submit="addLogEntry" class="mb-4 space-y-2 rounded border border-slate-100 bg-slate-50 p-3">
|
||||
<div class="flex items-start gap-2">
|
||||
<select wire:model="newLogCategory" class="rounded border-slate-300 text-sm shadow-sm focus:border-slate-500 focus:ring-slate-500">
|
||||
@foreach ($categories as $value => $label)
|
||||
<option value="{{ $value }}">{{ $label }}</option>
|
||||
@endforeach
|
||||
</select>
|
||||
<textarea wire:model="newLogNote" rows="2" placeholder="Ex: mail envoyé le 21/08 pour demander des précisions..."
|
||||
class="flex-1 rounded border-slate-300 text-sm shadow-sm focus:border-slate-500 focus:ring-slate-500"></textarea>
|
||||
<button type="submit" class="cursor-pointer rounded bg-slate-900 px-3 py-1.5 text-sm text-white hover:bg-slate-700">Ajouter</button>
|
||||
</div>
|
||||
@error('newLogNote') <p class="text-sm text-red-600">{{ $message }}</p> @enderror
|
||||
</form>
|
||||
|
||||
<div class="space-y-3">
|
||||
@forelse ($logEntries as $entry)
|
||||
<div wire:key="log-{{ $entry->id }}" class="flex items-start justify-between gap-4 border-b border-slate-100 pb-3 last:border-0 last:pb-0">
|
||||
<div>
|
||||
<span class="mb-1 inline-block rounded-full px-2 py-0.5 text-xs font-medium
|
||||
{{ match($entry->category) {
|
||||
'contact' => 'bg-blue-100 text-blue-700',
|
||||
'observation' => 'bg-slate-100 text-slate-600',
|
||||
'incident' => 'bg-red-100 text-red-700',
|
||||
default => 'bg-slate-100 text-slate-600',
|
||||
} }}">
|
||||
{{ $entry->categoryLabel() }}
|
||||
</span>
|
||||
<p class="whitespace-pre-line text-sm text-slate-900">{{ $entry->note }}</p>
|
||||
<p class="mt-1 text-xs text-slate-500">{{ $entry->creator?->name ?? 'système' }} · {{ $entry->created_at->diffForHumans() }}</p>
|
||||
</div>
|
||||
<button type="button" wire:click="deleteLogEntry({{ $entry->id }})"
|
||||
wire:confirm="Supprimer cette entrée ?"
|
||||
class="cursor-pointer shrink-0 text-xs text-red-500 hover:text-red-700">
|
||||
Supprimer
|
||||
</button>
|
||||
</div>
|
||||
@empty
|
||||
<p class="text-sm text-slate-400">Aucune entrée dans le journal pour le moment.</p>
|
||||
@endforelse
|
||||
</div>
|
||||
</div>
|
||||
@endif
|
||||
</div>
|
||||
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Provider;
|
||||
use Livewire\Attributes\Url;
|
||||
use Livewire\Component;
|
||||
use Livewire\WithPagination;
|
||||
|
||||
new class extends Component
|
||||
{
|
||||
use WithPagination;
|
||||
|
||||
#[Url]
|
||||
public string $search = '';
|
||||
|
||||
public function delete(int $providerId): void
|
||||
{
|
||||
$provider = Provider::findOrFail($providerId);
|
||||
$provider->delete();
|
||||
|
||||
session()->flash('status', "Prestataire « {$provider->name} » supprimé.");
|
||||
}
|
||||
|
||||
public function with(): array
|
||||
{
|
||||
return [
|
||||
'providers' => Provider::query()
|
||||
->when($this->search, fn ($query) => $query->where('name', 'like', "%{$this->search}%"))
|
||||
->orderBy('name')
|
||||
->paginate(15),
|
||||
];
|
||||
}
|
||||
};
|
||||
?>
|
||||
|
||||
<div>
|
||||
<div class="mb-6 flex items-center justify-between">
|
||||
<h1 class="text-2xl font-semibold text-slate-900">Prestataires</h1>
|
||||
<a href="{{ route('providers.create') }}"
|
||||
class="rounded bg-slate-900 px-4 py-2 text-sm font-medium text-white hover:bg-slate-700">
|
||||
+ Nouveau prestataire
|
||||
</a>
|
||||
</div>
|
||||
|
||||
@if (session('status'))
|
||||
<div class="mb-4 rounded border border-emerald-200 bg-emerald-50 px-3 py-2 text-sm text-emerald-700">
|
||||
{{ session('status') }}
|
||||
</div>
|
||||
@endif
|
||||
|
||||
<input type="text" wire:model.live.debounce.300ms="search" placeholder="Filtrer par nom..."
|
||||
class="mb-4 w-full max-w-sm rounded border-slate-300 text-sm shadow-sm focus:border-slate-500 focus:ring-slate-500">
|
||||
|
||||
<div class="overflow-hidden rounded border border-slate-200 bg-white">
|
||||
<table class="min-w-full divide-y divide-slate-200 text-sm">
|
||||
<thead class="bg-slate-50 text-left text-xs uppercase tracking-wide text-slate-500">
|
||||
<tr>
|
||||
<th class="px-4 py-3"></th>
|
||||
<th class="px-4 py-3">Nom</th>
|
||||
<th class="px-4 py-3">Téléphone</th>
|
||||
<th class="px-4 py-3">Email</th>
|
||||
<th class="px-4 py-3"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="divide-y divide-slate-100">
|
||||
@forelse ($providers as $provider)
|
||||
<tr wire:key="provider-{{ $provider->id }}">
|
||||
<td class="px-4 py-3">
|
||||
<img src="{{ $provider->logo_url }}" alt="Logo {{ $provider->name }}"
|
||||
class="h-8 w-8 rounded border border-slate-200 bg-white object-contain p-0.5">
|
||||
</td>
|
||||
<td class="px-4 py-3">
|
||||
<a href="{{ route('providers.edit', $provider) }}" class="font-medium text-slate-900 hover:underline">
|
||||
{{ $provider->name }}
|
||||
</a>
|
||||
</td>
|
||||
<td class="px-4 py-3 text-slate-600">{{ $provider->phone ?? '—' }}</td>
|
||||
<td class="px-4 py-3 text-slate-600">{{ $provider->email ?? '—' }}</td>
|
||||
<td class="px-4 py-3 text-right">
|
||||
<a href="{{ route('providers.edit', $provider) }}" class="text-slate-500 hover:text-slate-900">Modifier</a>
|
||||
<button type="button"
|
||||
wire:click="delete({{ $provider->id }})"
|
||||
wire:confirm="Supprimer ce prestataire ? Cette action est irréversible."
|
||||
class="ml-3 cursor-pointer text-red-500 hover:text-red-700">
|
||||
Supprimer
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
@empty
|
||||
<tr>
|
||||
<td colspan="5" class="px-4 py-6 text-center text-slate-400">Aucun prestataire pour le moment.</td>
|
||||
</tr>
|
||||
@endforelse
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div class="mt-4">
|
||||
{{ $providers->links() }}
|
||||
</div>
|
||||
</div>
|
||||
Reference in New Issue
Block a user