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