BookStack/app/Entities/Models/Deletion.php

66 lines
1.6 KiB
PHP
Raw Normal View History

2021-06-26 23:23:15 +08:00
<?php
namespace BookStack\Entities\Models;
use BookStack\Auth\User;
use BookStack\Interfaces\Loggable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
/**
2021-11-06 08:32:01 +08:00
* @property Model $deletable
*/
class Deletion extends Model implements Loggable
{
/**
* Get the related deletable record.
*/
public function deletable(): MorphTo
{
return $this->morphTo('deletable')->withTrashed();
}
/**
2021-11-06 08:32:01 +08:00
* Get the user that performed the deletion.
*/
public function deleter(): BelongsTo
{
return $this->belongsTo(User::class, 'deleted_by');
}
/**
* Create a new deletion record for the provided entity.
*/
public static function createForEntity(Entity $entity): self
{
$record = (new self())->forceFill([
2021-06-26 23:23:15 +08:00
'deleted_by' => user()->id,
'deletable_type' => $entity->getMorphClass(),
2021-06-26 23:23:15 +08:00
'deletable_id' => $entity->id,
]);
$record->save();
2021-06-26 23:23:15 +08:00
return $record;
}
public function logDescriptor(): string
{
$deletable = $this->deletable()->first();
2021-06-26 23:23:15 +08:00
2021-11-06 08:32:01 +08:00
if ($deletable instanceof Entity) {
return "Deletion ({$this->id}) for {$deletable->getType()} ({$deletable->id}) {$deletable->name}";
}
return "Deletion ({$this->id})";
}
/**
* Get a URL for this specific deletion.
*/
public function getUrl($path): string
{
return url("/settings/recycle-bin/{$this->id}/" . ltrim($path, '/'));
}
}