Laravel 13 + Livewire/Volt app for managing client organizations, documents, passwords, flexible assets, virtual machines, contacts, domains, todos, shortcuts, and role-based user administration.
281 lines
12 KiB
PHP
281 lines
12 KiB
PHP
<?php
|
|
|
|
use App\Models\FlexibleAsset;
|
|
use App\Models\FlexibleAssetType;
|
|
use App\Models\Organization;
|
|
use Livewire\Component;
|
|
|
|
new class extends Component
|
|
{
|
|
public Organization $organization;
|
|
public FlexibleAssetType $type;
|
|
public ?FlexibleAsset $asset = null;
|
|
|
|
/** @var array<string, mixed> */
|
|
public array $data = [];
|
|
|
|
public string $newLogNote = '';
|
|
|
|
public function mount(Organization $organization, FlexibleAssetType $type, ?FlexibleAsset $asset = null): void
|
|
{
|
|
$this->organization = $organization;
|
|
$this->type = $type;
|
|
|
|
if ($asset?->exists) {
|
|
$this->asset = $asset;
|
|
}
|
|
|
|
foreach ($type->fields as $field) {
|
|
$existing = $this->asset?->data[$field->name] ?? null;
|
|
|
|
$this->data[$field->name] = match ($field->field_type) {
|
|
'checkbox' => (bool) $existing,
|
|
'tag' => $existing ?? [],
|
|
default => $existing ?? '',
|
|
};
|
|
}
|
|
}
|
|
|
|
public function save(): void
|
|
{
|
|
$rules = [];
|
|
|
|
foreach ($this->type->fields as $field) {
|
|
$rule = $field->is_required ? 'required' : 'nullable';
|
|
|
|
if ($field->field_type === 'checkbox') {
|
|
$rule = 'boolean';
|
|
}
|
|
|
|
$rules["data.{$field->name}"] = $rule;
|
|
}
|
|
|
|
$this->validate($rules);
|
|
|
|
$primaryField = $this->type->fields->firstWhere('field_type', 'text') ?? $this->type->fields->first();
|
|
$name = $primaryField ? (string) ($this->data[$primaryField->name] ?? '') : $this->type->name;
|
|
|
|
if ($this->asset?->exists) {
|
|
$previousData = $this->asset->data ?? [];
|
|
|
|
$this->asset->update(['name' => $name ?: $this->asset->name, 'data' => $this->data]);
|
|
|
|
$this->logFieldChanges($previousData, $this->data);
|
|
|
|
session()->flash('status', "\"{$this->asset->name}\" mis à jour.");
|
|
} else {
|
|
$this->organization->flexibleAssets()->create([
|
|
'flexible_asset_type_id' => $this->type->id,
|
|
'name' => $name ?: $this->type->name,
|
|
'data' => $this->data,
|
|
'created_by' => auth()->id(),
|
|
]);
|
|
session()->flash('status', "\"{$name}\" créé.");
|
|
}
|
|
|
|
$this->redirectRoute('organizations.assets.index', [$this->organization, $this->type], navigate: true);
|
|
}
|
|
|
|
public function addLogEntry(): void
|
|
{
|
|
$this->validate(['newLogNote' => 'required|string']);
|
|
|
|
$this->asset->logs()->create([
|
|
'note' => $this->newLogNote,
|
|
'created_by' => auth()->id(),
|
|
]);
|
|
|
|
$this->newLogNote = '';
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $previousData
|
|
* @param array<string, mixed> $newData
|
|
*/
|
|
private function logFieldChanges(array $previousData, array $newData): void
|
|
{
|
|
$changes = [];
|
|
|
|
foreach ($this->type->fields as $field) {
|
|
$before = $previousData[$field->name] ?? null;
|
|
$after = $newData[$field->name] ?? null;
|
|
|
|
if ($this->normalizeFieldValue($before) === $this->normalizeFieldValue($after)) {
|
|
continue;
|
|
}
|
|
|
|
if ($field->field_type === 'password') {
|
|
$changes[] = "{$field->label} modifié";
|
|
continue;
|
|
}
|
|
|
|
$changes[] = "{$field->label} : « ".$this->formatFieldValue($field, $before)." » → « ".$this->formatFieldValue($field, $after)." »";
|
|
}
|
|
|
|
if (empty($changes)) {
|
|
return;
|
|
}
|
|
|
|
$this->asset->logs()->create([
|
|
'note' => "Modification automatique :\n".implode("\n", $changes),
|
|
'created_by' => auth()->id(),
|
|
]);
|
|
}
|
|
|
|
private function normalizeFieldValue(mixed $value): mixed
|
|
{
|
|
if (is_array($value)) {
|
|
sort($value);
|
|
|
|
return $value;
|
|
}
|
|
|
|
return blank($value) ? null : $value;
|
|
}
|
|
|
|
private function formatFieldValue(\App\Models\FlexibleAssetField $field, mixed $value): string
|
|
{
|
|
if (blank($value) && $value !== false) {
|
|
return '—';
|
|
}
|
|
|
|
return match ($field->field_type) {
|
|
'checkbox' => $value ? 'Oui' : 'Non',
|
|
'tag' => is_array($value) ? implode(', ', $value) : (string) $value,
|
|
'related_asset' => $field->relatedOptions($this->organization)->firstWhere('id', (int) $value)['label'] ?? '—',
|
|
default => (string) $value,
|
|
};
|
|
}
|
|
};
|
|
?>
|
|
|
|
<div class="max-w-6xl">
|
|
<p class="text-sm text-slate-500">{{ $organization->name }} · {{ $type->name }}</p>
|
|
<h1 class="mb-6 text-2xl font-semibold text-slate-900">
|
|
{{ $asset?->exists ? 'Modifier l\'enregistrement' : 'Nouvel enregistrement' }}
|
|
</h1>
|
|
|
|
<div class="grid grid-cols-1 gap-6 {{ $asset?->exists ? 'lg:grid-cols-2' : '' }}">
|
|
<div>
|
|
<form wire:submit="save" class="space-y-4 rounded border border-slate-200 bg-white p-6">
|
|
@foreach ($type->fields as $field)
|
|
<div>
|
|
<label class="block text-sm font-medium text-slate-700">
|
|
{{ $field->label }}
|
|
@if ($field->is_required) <span class="text-red-500">*</span> @endif
|
|
</label>
|
|
|
|
@switch($field->field_type)
|
|
@case('textarea')
|
|
<textarea wire:model="data.{{ $field->name }}" rows="3"
|
|
class="mt-1 w-full rounded border-slate-300 text-sm shadow-sm focus:border-slate-500 focus:ring-slate-500"></textarea>
|
|
@break
|
|
|
|
@case('password')
|
|
<input type="password" wire:model="data.{{ $field->name }}" autocomplete="new-password"
|
|
class="mt-1 w-full rounded border-slate-300 font-mono text-sm shadow-sm focus:border-slate-500 focus:ring-slate-500">
|
|
@break
|
|
|
|
@case('checkbox')
|
|
<div class="mt-1">
|
|
<input type="checkbox" wire:model="data.{{ $field->name }}" class="rounded border-slate-300">
|
|
</div>
|
|
@break
|
|
|
|
@case('date')
|
|
<input type="date" wire:model="data.{{ $field->name }}"
|
|
class="mt-1 w-full rounded border-slate-300 text-sm shadow-sm focus:border-slate-500 focus:ring-slate-500">
|
|
@break
|
|
|
|
@case('select')
|
|
<select wire:model="data.{{ $field->name }}"
|
|
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 ($field->options['choices'] ?? [] as $choice)
|
|
<option value="{{ $choice }}">{{ $choice }}</option>
|
|
@endforeach
|
|
</select>
|
|
@break
|
|
|
|
@case('tag')
|
|
<select wire:model="data.{{ $field->name }}" multiple
|
|
class="mt-1 w-full rounded border-slate-300 text-sm shadow-sm focus:border-slate-500 focus:ring-slate-500">
|
|
@foreach ($field->options['choices'] ?? [] as $choice)
|
|
<option value="{{ $choice }}">{{ $choice }}</option>
|
|
@endforeach
|
|
</select>
|
|
@break
|
|
|
|
@case('related_asset')
|
|
<select wire:model="data.{{ $field->name }}"
|
|
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 ($field->relatedOptions($organization) as $option)
|
|
<option value="{{ $option['id'] }}">{{ $option['label'] }}</option>
|
|
@endforeach
|
|
</select>
|
|
@break
|
|
|
|
@default
|
|
<input type="text" wire:model="data.{{ $field->name }}"
|
|
class="mt-1 w-full rounded border-slate-300 text-sm shadow-sm focus:border-slate-500 focus:ring-slate-500">
|
|
@endswitch
|
|
|
|
@error("data.{$field->name}") <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
|
|
</div>
|
|
@endforeach
|
|
|
|
<div class="flex items-center gap-3 pt-2">
|
|
<button type="submit"
|
|
class="rounded bg-slate-900 px-4 py-2 text-sm font-medium text-white hover:bg-slate-700">
|
|
Enregistrer
|
|
</button>
|
|
<a href="{{ route('organizations.assets.index', [$organization, $type]) }}" class="text-sm text-slate-500 hover:text-slate-900">Annuler</a>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
|
|
@if ($asset?->exists)
|
|
<div>
|
|
<h2 class="mb-3 text-sm font-semibold uppercase tracking-wide text-slate-500">Journal MCO</h2>
|
|
|
|
<form wire:submit="addLogEntry" class="mb-4 flex items-start gap-2 rounded border border-slate-200 bg-white p-3">
|
|
<textarea wire:model="newLogNote" rows="2" placeholder="Ajouter une entrée au journal (maintenance, incident, mise à jour...)"
|
|
class="flex-1 rounded border-slate-300 text-sm shadow-sm focus:border-slate-500 focus:ring-slate-500"></textarea>
|
|
<button type="submit" class="rounded bg-slate-900 px-3 py-1.5 text-sm text-white hover:bg-slate-700">Ajouter</button>
|
|
</form>
|
|
@error('newLogNote') <p class="mb-4 text-sm text-red-600">{{ $message }}</p> @enderror
|
|
|
|
<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-2">Note</th>
|
|
<th class="px-4 py-2">Par</th>
|
|
<th class="px-4 py-2">Quand</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody class="divide-y divide-slate-100">
|
|
@forelse ($asset->logs as $log)
|
|
<tr wire:key="log-{{ $log->id }}">
|
|
<td class="px-4 py-2 whitespace-pre-line text-slate-900">{{ $log->note }}</td>
|
|
<td class="px-4 py-2 text-slate-600">{{ $log->creator?->name ?? 'système' }}</td>
|
|
<td class="px-4 py-2 text-slate-500">{{ $log->created_at->diffForHumans() }}</td>
|
|
</tr>
|
|
@empty
|
|
<tr>
|
|
<td colspan="3" class="px-4 py-6 text-center text-slate-400">Aucune entrée pour le moment.</td>
|
|
</tr>
|
|
@endforelse
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
<div class="mt-8">
|
|
<livewire:related-items.panel :organization="$organization" :item="$asset" :key="'related-'.$asset->id" />
|
|
</div>
|
|
</div>
|
|
@endif
|
|
</div>
|
|
</div>
|