]> BookStack Code Mirror - bookstack/blob - app/Entities/Models/Deletion.php
2295a5e214bbf055293386be366aed2817c688dd
[bookstack] / app / Entities / Models / Deletion.php
1 <?php namespace BookStack\Entities\Models;
2
3 use BookStack\Auth\User;
4 use BookStack\Entities\Models\Entity;
5 use BookStack\Interfaces\Loggable;
6 use Illuminate\Database\Eloquent\Model;
7 use Illuminate\Database\Eloquent\Relations\BelongsTo;
8 use Illuminate\Database\Eloquent\Relations\MorphTo;
9
10 /**
11  * @property Model deletable
12  */
13 class Deletion extends Model implements Loggable
14 {
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         return $record;
44     }
45
46     public function logDescriptor(): string
47     {
48         $deletable = $this->deletable()->first();
49         return "Deletion ({$this->id}) for {$deletable->getType()} ({$deletable->id}) {$deletable->name}";
50     }
51
52     /**
53      * Get a URL for this specific deletion.
54      */
55     public function getUrl($path): string
56     {
57         return url("/settings/recycle-bin/{$this->id}/" . ltrim($path, '/'));
58     }
59 }