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