2021-06-26 17:23:15 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Entities\Models;
|
2020-09-28 00:24:33 +02:00
|
|
|
|
2023-05-17 18:56:55 +02:00
|
|
|
use BookStack\Activity\Models\Loggable;
|
|
|
|
use BookStack\Users\Models\User;
|
2020-09-28 00:24:33 +02:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
|
|
|
|
2021-06-26 13:12:11 +02:00
|
|
|
/**
|
2022-04-25 19:42:31 +02:00
|
|
|
* @property int $id
|
|
|
|
* @property int $deleted_by
|
|
|
|
* @property string $deletable_type
|
|
|
|
* @property int $deletable_id
|
2021-11-20 15:03:56 +01:00
|
|
|
* @property Deletable $deletable
|
2021-06-26 13:12:11 +02:00
|
|
|
*/
|
2020-11-19 00:38:44 +01:00
|
|
|
class Deletion extends Model implements Loggable
|
2020-09-28 00:24:33 +02:00
|
|
|
{
|
2022-04-25 18:54:59 +02:00
|
|
|
protected $hidden = [];
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-06 01:32:01 +01:00
|
|
|
* Get the user that performed the deletion.
|
2020-09-28 00:24:33 +02:00
|
|
|
*/
|
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.
|
|
|
|
*/
|
2021-10-26 23:04:18 +02:00
|
|
|
public static function createForEntity(Entity $entity): self
|
2020-09-28 00:24:33 +02:00
|
|
|
{
|
|
|
|
$record = (new self())->forceFill([
|
2021-06-26 17:23:15 +02:00
|
|
|
'deleted_by' => user()->id,
|
2020-09-28 00:24:33 +02:00
|
|
|
'deletable_type' => $entity->getMorphClass(),
|
2021-06-26 17:23:15 +02:00
|
|
|
'deletable_id' => $entity->id,
|
2020-09-28 00:24:33 +02:00
|
|
|
]);
|
|
|
|
$record->save();
|
2021-06-26 17:23:15 +02:00
|
|
|
|
2020-09-28 00:24:33 +02:00
|
|
|
return $record;
|
|
|
|
}
|
|
|
|
|
2020-11-19 00:38:44 +01:00
|
|
|
public function logDescriptor(): string
|
|
|
|
{
|
|
|
|
$deletable = $this->deletable()->first();
|
2021-06-26 17:23:15 +02:00
|
|
|
|
2021-11-06 01:32:01 +01:00
|
|
|
if ($deletable instanceof Entity) {
|
|
|
|
return "Deletion ({$this->id}) for {$deletable->getType()} ({$deletable->id}) {$deletable->name}";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "Deletion ({$this->id})";
|
2020-11-19 00:38:44 +01:00
|
|
|
}
|
2021-06-26 13:12:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a URL for this specific deletion.
|
|
|
|
*/
|
2022-01-10 18:46:17 +01:00
|
|
|
public function getUrl(string $path = 'restore'): string
|
2021-06-26 13:12:11 +02:00
|
|
|
{
|
|
|
|
return url("/settings/recycle-bin/{$this->id}/" . ltrim($path, '/'));
|
|
|
|
}
|
2020-09-28 00:24:33 +02:00
|
|
|
}
|