]> BookStack Code Mirror - bookstack/blob - app/Entities/Models/Deletion.php
Added more complexity in an attempt to make ldap host failover fit
[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 int       $id
14  * @property int       $deleted_by
15  * @property string    $deletable_type
16  * @property int       $deletable_id
17  * @property Deletable $deletable
18  */
19 class Deletion extends Model implements Loggable
20 {
21     protected $hidden = [];
22
23     /**
24      * Get the related deletable record.
25      */
26     public function deletable(): MorphTo
27     {
28         return $this->morphTo('deletable')->withTrashed();
29     }
30
31     /**
32      * Get the user that performed the deletion.
33      */
34     public function deleter(): BelongsTo
35     {
36         return $this->belongsTo(User::class, 'deleted_by');
37     }
38
39     /**
40      * Create a new deletion record for the provided entity.
41      */
42     public static function createForEntity(Entity $entity): self
43     {
44         $record = (new self())->forceFill([
45             'deleted_by'     => user()->id,
46             'deletable_type' => $entity->getMorphClass(),
47             'deletable_id'   => $entity->id,
48         ]);
49         $record->save();
50
51         return $record;
52     }
53
54     public function logDescriptor(): string
55     {
56         $deletable = $this->deletable()->first();
57
58         if ($deletable instanceof Entity) {
59             return "Deletion ({$this->id}) for {$deletable->getType()} ({$deletable->id}) {$deletable->name}";
60         }
61
62         return "Deletion ({$this->id})";
63     }
64
65     /**
66      * Get a URL for this specific deletion.
67      */
68     public function getUrl(string $path = 'restore'): string
69     {
70         return url("/settings/recycle-bin/{$this->id}/" . ltrim($path, '/'));
71     }
72 }