2020-09-28 06:24:33 +08:00
|
|
|
<?php namespace BookStack\Entities;
|
|
|
|
|
|
|
|
use BookStack\Auth\User;
|
2020-11-19 07:38:44 +08:00
|
|
|
use BookStack\Interfaces\Loggable;
|
2020-09-28 06:24:33 +08:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
|
|
|
|
2020-11-19 07:38:44 +08:00
|
|
|
class Deletion extends Model implements Loggable
|
2020-09-28 06:24:33 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the related deletable record.
|
|
|
|
*/
|
|
|
|
public function deletable(): MorphTo
|
|
|
|
{
|
2020-10-04 01:44:12 +08:00
|
|
|
return $this->morphTo('deletable')->withTrashed();
|
2020-09-28 06:24:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The the user that performed the deletion.
|
|
|
|
*/
|
2020-10-04 01:44:12 +08:00
|
|
|
public function deleter(): BelongsTo
|
2020-09-28 06:24:33 +08:00
|
|
|
{
|
2020-10-04 01:44:12 +08:00
|
|
|
return $this->belongsTo(User::class, 'deleted_by');
|
2020-09-28 06:24:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new deletion record for the provided entity.
|
|
|
|
*/
|
2020-10-04 01:44:12 +08:00
|
|
|
public static function createForEntity(Entity $entity): Deletion
|
2020-09-28 06:24:33 +08:00
|
|
|
{
|
|
|
|
$record = (new self())->forceFill([
|
|
|
|
'deleted_by' => user()->id,
|
|
|
|
'deletable_type' => $entity->getMorphClass(),
|
|
|
|
'deletable_id' => $entity->id,
|
|
|
|
]);
|
|
|
|
$record->save();
|
|
|
|
return $record;
|
|
|
|
}
|
|
|
|
|
2020-11-19 07:38:44 +08:00
|
|
|
public function logDescriptor(): string
|
|
|
|
{
|
|
|
|
$deletable = $this->deletable()->first();
|
|
|
|
return "Deletion ({$this->id}) for {$deletable->getType()} ({$deletable->id}) {$deletable->name}";
|
|
|
|
}
|
2020-09-28 06:24:33 +08:00
|
|
|
}
|