2021-06-26 11:23:15 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Entities\Models;
|
2020-09-27 18:24:33 -04:00
|
|
|
|
|
|
|
use BookStack\Auth\User;
|
2020-11-18 18:38:44 -05:00
|
|
|
use BookStack\Interfaces\Loggable;
|
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
|
|
|
/**
|
|
|
|
* @property Model deletable
|
|
|
|
*/
|
2020-11-18 18:38:44 -05:00
|
|
|
class Deletion extends Model implements Loggable
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The the user that performed the deletion.
|
|
|
|
*/
|
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.
|
|
|
|
*/
|
2020-10-03 13:44:12 -04:00
|
|
|
public static function createForEntity(Entity $entity): Deletion
|
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
|
|
|
|
2020-11-18 18:38:44 -05:00
|
|
|
return "Deletion ({$this->id}) for {$deletable->getType()} ({$deletable->id}) {$deletable->name}";
|
|
|
|
}
|
2021-06-26 07:12:11 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a URL for this specific deletion.
|
|
|
|
*/
|
|
|
|
public function getUrl($path): string
|
|
|
|
{
|
|
|
|
return url("/settings/recycle-bin/{$this->id}/" . ltrim($path, '/'));
|
|
|
|
}
|
2020-09-27 18:24:33 -04:00
|
|
|
}
|