]> BookStack Code Mirror - bookstack/blob - app/Entities/Models/Deletion.php
Opensearch: Fixed XML declaration when php short tags enabled
[bookstack] / app / Entities / Models / Deletion.php
1 <?php
2
3 namespace BookStack\Entities\Models;
4
5 use BookStack\Activity\Models\Loggable;
6 use BookStack\Users\Models\User;
7 use Illuminate\Database\Eloquent\Model;
8 use Illuminate\Database\Eloquent\Relations\BelongsTo;
9 use Illuminate\Database\Eloquent\Relations\MorphTo;
10
11 /**
12  * @property int       $id
13  * @property int       $deleted_by
14  * @property string    $deletable_type
15  * @property int       $deletable_id
16  * @property Deletable $deletable
17  */
18 class Deletion extends Model implements Loggable
19 {
20     protected $hidden = [];
21
22     /**
23      * Get the related deletable record.
24      */
25     public function deletable(): MorphTo
26     {
27         return $this->morphTo('deletable')->withTrashed();
28     }
29
30     /**
31      * Get the user that performed the deletion.
32      */
33     public function deleter(): BelongsTo
34     {
35         return $this->belongsTo(User::class, 'deleted_by');
36     }
37
38     /**
39      * Create a new deletion record for the provided entity.
40      */
41     public static function createForEntity(Entity $entity): self
42     {
43         $record = (new self())->forceFill([
44             'deleted_by'     => user()->id,
45             'deletable_type' => $entity->getMorphClass(),
46             'deletable_id'   => $entity->id,
47         ]);
48         $record->save();
49
50         return $record;
51     }
52
53     public function logDescriptor(): string
54     {
55         $deletable = $this->deletable()->first();
56
57         if ($deletable instanceof Entity) {
58             return "Deletion ({$this->id}) for {$deletable->getType()} ({$deletable->id}) {$deletable->name}";
59         }
60
61         return "Deletion ({$this->id})";
62     }
63
64     /**
65      * Get a URL for this specific deletion.
66      */
67     public function getUrl(string $path = 'restore'): string
68     {
69         return url("/settings/recycle-bin/{$this->id}/" . ltrim($path, '/'));
70     }
71 }