69 lines
2.1 KiB
PHP
69 lines
2.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Modules\CustomField\Actions;
|
||
|
|
|
||
|
|
use App\Models\User;
|
||
|
|
use App\Modules\Client\Models\Client;
|
||
|
|
use App\Modules\CustomField\Models\CustomFieldDefinition;
|
||
|
|
use App\Modules\CustomField\Models\CustomFieldValue;
|
||
|
|
use App\Modules\History\Models\HistoryEntry;
|
||
|
|
use App\Modules\History\Models\HistoryEntryType;
|
||
|
|
use Illuminate\Support\Facades\DB;
|
||
|
|
|
||
|
|
final class UpdateCustomFieldValueAction
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Upsert a client's value for one custom field. When the value actually
|
||
|
|
* changes, append a read-only entry to the Verlauf log (history_entries)
|
||
|
|
* so every base-data change is audited alongside the manual notes.
|
||
|
|
*/
|
||
|
|
public function handle(
|
||
|
|
Client $client,
|
||
|
|
CustomFieldDefinition $definition,
|
||
|
|
?string $value,
|
||
|
|
User $author
|
||
|
|
): CustomFieldValue {
|
||
|
|
$new = $value === null || $value === '' ? null : (string) $value;
|
||
|
|
|
||
|
|
return DB::transaction(function () use ($client, $definition, $new, $author) {
|
||
|
|
$record = CustomFieldValue::firstOrNew([
|
||
|
|
'client_id' => $client->id,
|
||
|
|
'custom_field_definition_id' => $definition->id,
|
||
|
|
]);
|
||
|
|
|
||
|
|
$old = $record->value;
|
||
|
|
|
||
|
|
if ($old === $new) {
|
||
|
|
return $record;
|
||
|
|
}
|
||
|
|
|
||
|
|
$record->value = $new;
|
||
|
|
$record->author_id = $author->id;
|
||
|
|
$record->save();
|
||
|
|
|
||
|
|
HistoryEntry::create([
|
||
|
|
'client_id' => $client->id,
|
||
|
|
'type' => HistoryEntryType::FieldChange,
|
||
|
|
'body' => $this->changeBody($definition->label, $old, $new),
|
||
|
|
'custom_field_definition_id' => $definition->id,
|
||
|
|
'author_id' => $author->id,
|
||
|
|
]);
|
||
|
|
|
||
|
|
return $record;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
private function changeBody(string $label, ?string $old, ?string $new): string
|
||
|
|
{
|
||
|
|
if ($old === null) {
|
||
|
|
return "{$label} gesetzt: „{$new}“";
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($new === null) {
|
||
|
|
return "{$label} entfernt (war „{$old}“)";
|
||
|
|
}
|
||
|
|
|
||
|
|
return "{$label} geändert: „{$old}“ → „{$new}“";
|
||
|
|
}
|
||
|
|
}
|