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