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