Update Log aktivitas

This commit is contained in:
2026-11-05 16:34:33 +07:00
parent a3ec431d49
commit 7a279ad5d2
84 changed files with 725 additions and 67 deletions
+50
View File
@@ -0,0 +1,50 @@
<?php
namespace App\Models\Concerns;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Spatie\Activitylog\Models\Concerns\LogsActivity;
use Spatie\Activitylog\Support\LogOptions;
trait CatatAktivitas
{
use LogsActivity;
protected static array $kolomTidakDicatat = [
'created_by', 'updated_by', 'deleted_by', 'created_at', 'updated_at', 'deleted_at',
'password', 'remember_token', 'sync_status',
'foto', 'foto_url', 'path', 'path_kk', 'path_ktp', 'file_kk', 'file_ktp',
];
protected static array $kataEvent = [
'created' => 'ditambahkan',
'updated' => 'diubah',
'deleted' => 'dihapus',
'restored' => 'dipulihkan',
];
// Aturan log: kolom fillable yang berubah saja
public function getActivitylogOptions(): LogOptions
{
$label = property_exists($this, 'labelAktivitas') ? $this->labelAktivitas : Str::headline(class_basename($this));
$logName = property_exists($this, 'logAktivitas') ? $this->logAktivitas : Str::snake(explode('\\', static::class)[2]);
return LogOptions::defaults()
->useLogName($logName)
->logFillable()
->logExcept([...self::$kolomTidakDicatat, ...$this->getHidden()])
->dontLogIfAttributesChangedOnly(self::$kolomTidakDicatat)
->logOnlyDirty()
->dontLogEmptyChanges()
->setDescriptionForEvent(fn (string $event) => $label . ' ' . (self::$kataEvent[$event] ?? $event));
}
// Tandai petani terkait
public function beforeActivityLogged(Model $activity, string $event): void
{
if ($petaniId = $this->getAttribute('petani_id')) {
$activity->properties = $activity->properties->put('petani_id', (int) $petaniId);
}
}
}
@@ -54,7 +54,15 @@ trait KembalikanStatusPetaniKeDraf
->where('status', StatusDataPetani::LENGKAP->value)
->update(['status' => StatusDataPetani::DRAFT->value]);
if ($diubah && request()->hasSession()) {
if (! $diubah) {
return true;
}
// Log status (tanpa query ulang petani)
(new Petani)->forceFill(['id' => $petaniId, 'status' => StatusDataPetani::DRAFT])
->catatPerubahanStatus(StatusDataPetani::LENGKAP, 'Otomatis kembali ke draf karena data diubah', ['sumber' => class_basename($this)]);
if (request()->hasSession()) {
request()->session()->flash('status_petani_info', 'Data petani diubah, status kembali ke Draf. Tandai lengkap lagi setelah selesai.');
}