3 namespace BookStack\Entities\Models;
5 use BookStack\Auth\User;
6 use BookStack\Interfaces\Deletable;
7 use BookStack\Interfaces\Loggable;
8 use Illuminate\Database\Eloquent\Model;
9 use Illuminate\Database\Eloquent\Relations\BelongsTo;
10 use Illuminate\Database\Eloquent\Relations\MorphTo;
14 * @property int $deleted_by
15 * @property string $deletable_type
16 * @property int $deletable_id
17 * @property Deletable $deletable
19 class Deletion extends Model implements Loggable
21 protected $hidden = [];
24 * Get the related deletable record.
26 public function deletable(): MorphTo
28 return $this->morphTo('deletable')->withTrashed();
32 * Get the user that performed the deletion.
34 public function deleter(): BelongsTo
36 return $this->belongsTo(User::class, 'deleted_by');
40 * Create a new deletion record for the provided entity.
42 public static function createForEntity(Entity $entity): self
44 $record = (new self())->forceFill([
45 'deleted_by' => user()->id,
46 'deletable_type' => $entity->getMorphClass(),
47 'deletable_id' => $entity->id,
54 public function logDescriptor(): string
56 $deletable = $this->deletable()->first();
58 if ($deletable instanceof Entity) {
59 return "Deletion ({$this->id}) for {$deletable->getType()} ({$deletable->id}) {$deletable->name}";
62 return "Deletion ({$this->id})";
66 * Get a URL for this specific deletion.
68 public function getUrl(string $path = 'restore'): string
70 return url("/settings/recycle-bin/{$this->id}/" . ltrim($path, '/'));