258 lines
7.6 KiB
PHP
258 lines
7.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\ActivityLogs\ActivityLogsEventEnum;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
/**
|
|
* @property int $id
|
|
* @property string|null $user_id
|
|
* @property bool $is_anonymous If there is no user connected when action was realised.
|
|
* @property bool $is_console If the action was realised in console.
|
|
* @property string $model_class Target model.
|
|
* @property string $model_id Id of the target model.
|
|
* @property ActivityLogsEventEnum $event Event of this activity (ActivityLogsEventEnum).
|
|
* @property array<array-key, mixed>|null $data List of changes (old and new values).
|
|
* @property Carbon $created_at The date on which the user was published.
|
|
*
|
|
* @property-read \App\Models\User $user User BelongsTo relation.
|
|
* @property-read \App\Models\StaticPage $staticPage StaticPage BelongsTo relation.
|
|
* @property-read \App\Models\Settings $settings Settings BelongsTo relation.
|
|
* @property-read \App\Models\Range $range Range BelongsTo relation.
|
|
* @property-read \App\Models\Product $product Product BelongsTo relation.
|
|
* @property-read \App\Models\Prize $prize Prize BelongsTo relation.
|
|
* @property-read \App\Models\Participation $participation Participation BelongsTo relation.
|
|
* @property-read \App\Models\InstantWin $instantWin InstantWin BelongsTo relation.
|
|
* @property-read \App\Models\FaqTheme $faqTheme FaqTheme BelongsTo relation.
|
|
* @property-read \App\Models\FaqQuestion $faqQuestion FaqQuestion BelongsTo relation.
|
|
* @property-read \App\Models\Customer $customer Customer BelongsTo relation.
|
|
* @property-read \App\Models\Code $code Code BelongsTo relation.
|
|
* @property-read \App\Models\BannedIp $bannedIp BannedIp BelongsTo relation.
|
|
*/
|
|
class ActivityLog extends Model
|
|
{
|
|
use HasFactory;
|
|
use Notifiable;
|
|
|
|
/**
|
|
* Indicates if the model should be timestamped.
|
|
*
|
|
* @var boolean
|
|
*/
|
|
public $timestamps = false;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'is_anonymous',
|
|
'model_class',
|
|
'model_id',
|
|
'event',
|
|
'data',
|
|
'created_at',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'is_anonymous' => 'bool',
|
|
'is_console' => 'bool',
|
|
'event' => ActivityLogsEventEnum::class,
|
|
'data' => 'json',
|
|
'created_at' => 'datetime',
|
|
];
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Add new activity to the activity logs list.
|
|
*/
|
|
public static function addActivity(Model $model, ActivityLogsEventEnum $eventEnum): void
|
|
{
|
|
$isAnonymous = true;
|
|
/** @var Model|null */
|
|
$userModel = auth('backend')->user();
|
|
$user_id = optional($userModel)->id;
|
|
if ($userModel instanceof Model) {
|
|
$user_id = $userModel->getKeyForSelectQuery();
|
|
$isAnonymous = false;
|
|
}
|
|
$activity = new self();
|
|
$activity->user_id = $user_id;
|
|
$activity->is_console = app()->runningInConsole();
|
|
$activity->is_anonymous = $isAnonymous;
|
|
$activity->model_class = $model::class;
|
|
$activity->model_id = $model->getKey();
|
|
$activity->event = $eventEnum;
|
|
$activity->data = static::getChangedColumns($activity, $model, $eventEnum);
|
|
$activity->setAttribute('created_at', now());
|
|
$activity->saveOrFail();
|
|
}
|
|
|
|
/**
|
|
* Get old, new columns that has changed.
|
|
*
|
|
* @return array<string, string>|null
|
|
*/
|
|
public static function getChangedColumns(
|
|
self $activity,
|
|
Model $model,
|
|
ActivityLogsEventEnum $eventEnum
|
|
): ?array {
|
|
if ($eventEnum !== ActivityLogsEventEnum::updated) {
|
|
return null;
|
|
}
|
|
|
|
$casts = $model->getCasts();
|
|
$changes = $model->getChanges();
|
|
|
|
return collect($changes)
|
|
->map(fn($val, $key) => $casts[$key] ?? 'string')
|
|
->toArray();
|
|
}
|
|
|
|
// * RELATIONS
|
|
|
|
/**
|
|
* User BelongsTo relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<$this,\App\Models\User>
|
|
*/
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
/**
|
|
* StaticPage BelongsTo relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<$this,\App\Models\StaticPage>
|
|
*/
|
|
public function staticPage(): BelongsTo
|
|
{
|
|
return $this->belongsTo(StaticPage::class);
|
|
}
|
|
|
|
/**
|
|
* Settings BelongsTo relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<$this,\App\Models\Settings>
|
|
*/
|
|
public function settings(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Settings::class);
|
|
}
|
|
|
|
/**
|
|
* Range BelongsTo relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<$this,\App\Models\Range>
|
|
*/
|
|
public function range(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Range::class);
|
|
}
|
|
|
|
/**
|
|
* Product BelongsTo relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<$this,\App\Models\Product>
|
|
*/
|
|
public function product(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Product::class);
|
|
}
|
|
|
|
/**
|
|
* Prize BelongsTo relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<$this,\App\Models\Prize>
|
|
*/
|
|
public function prize(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Prize::class);
|
|
}
|
|
|
|
/**
|
|
* Participation BelongsTo relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<$this,\App\Models\Participation>
|
|
*/
|
|
public function participation(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Participation::class);
|
|
}
|
|
|
|
/**
|
|
* InstantWin BelongsTo relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<$this,\App\Models\InstantWin>
|
|
*/
|
|
public function instantWin(): BelongsTo
|
|
{
|
|
return $this->belongsTo(InstantWin::class);
|
|
}
|
|
|
|
/**
|
|
* FaqTheme BelongsTo relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<$this,\App\Models\FaqTheme>
|
|
*/
|
|
public function faqTheme(): BelongsTo
|
|
{
|
|
return $this->belongsTo(FaqTheme::class);
|
|
}
|
|
|
|
/**
|
|
* FaqQuestion BelongsTo relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<$this,\App\Models\FaqQuestion>
|
|
*/
|
|
public function faqQuestion(): BelongsTo
|
|
{
|
|
return $this->belongsTo(FaqQuestion::class);
|
|
}
|
|
|
|
/**
|
|
* Customer BelongsTo relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<$this,\App\Models\Customer>
|
|
*/
|
|
public function customer(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Customer::class);
|
|
}
|
|
|
|
/**
|
|
* Code BelongsTo relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<$this,\App\Models\Code>
|
|
*/
|
|
public function code(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Code::class);
|
|
}
|
|
|
|
/**
|
|
* BannedIp BelongsTo relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<$this,\App\Models\BannedIp>
|
|
*/
|
|
public function bannedIp(): BelongsTo
|
|
{
|
|
return $this->belongsTo(BannedIp::class);
|
|
}
|
|
}
|