dress/app/Models/GeneralSetting.php

112 lines
3.2 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Attributes\Appends;
use Illuminate\Database\Eloquent\Attributes\Guarded;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Models\Activity;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
#[Guarded(['id'])]
#[Appends(['logo_light_url', 'logo_dark_url', 'icon_light_url', 'icon_dark_url'])]
class GeneralSetting extends Model implements HasMedia
{
use HasFactory, InteractsWithMedia, LogsActivity;
public function registerMediaCollections(): void
{
$this->addMediaCollection('logo_light')
->singleFile();
$this->addMediaCollection('logo_dark')
->singleFile();
$this->addMediaCollection('icon_light')
->singleFile();
$this->addMediaCollection('icon_dark')
->singleFile();
}
protected function logoLightUrl(): Attribute
{
return Attribute::make(
get: fn () => $this->getFirstMediaUrl('logo_light') ?: null,
);
}
protected function logoDarkUrl(): Attribute
{
return Attribute::make(
get: fn () => $this->getFirstMediaUrl('logo_dark') ?: null,
);
}
protected function iconLightUrl(): Attribute
{
return Attribute::make(
get: fn () => $this->getFirstMediaUrl('icon_light') ?: null,
);
}
protected function iconDarkUrl(): Attribute
{
return Attribute::make(
get: fn () => $this->getFirstMediaUrl('icon_dark') ?: null,
);
}
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logOnly(['name', 'description', 'address', 'phone'])
->logOnlyDirty()
->useLogName('Pengaturan Umum');
}
public function tapActivity(Activity $activity, string $eventName)
{
$activity->description = match ($eventName) {
'created' => 'TAMBAH',
'updated' => 'UBAH',
'deleted' => 'HAPUS',
default => $activity->description,
};
if (isset($activity->properties['attributes'])) {
$attributeMap = [
'name' => 'Nama Aplikasi',
'description' => 'Deskripsi',
'address' => 'Alamat',
'phone' => 'No. Telepon',
];
$properties = $activity->properties->toArray();
$localizeValues = function ($attrs) use ($attributeMap) {
$newAttrs = [];
foreach ($attrs as $key => $value) {
$label = $attributeMap[$key] ?? $key;
$newAttrs[$label] = $value;
}
return $newAttrs;
};
$properties['attributes'] = $localizeValues($properties['attributes']);
if (isset($properties['old'])) {
$properties['old'] = $localizeValues($properties['old']);
}
$activity->properties = collect($properties);
}
}
}