]> BookStack Code Mirror - bookstack/blob - app/Entities/Models/PageRevision.php
Added additional testing for editor switching permissions
[bookstack] / app / Entities / Models / PageRevision.php
1 <?php
2
3 namespace BookStack\Entities\Models;
4
5 use BookStack\Auth\User;
6 use BookStack\Model;
7 use Carbon\Carbon;
8 use Illuminate\Database\Eloquent\Relations\BelongsTo;
9
10 /**
11  * Class PageRevision.
12  *
13  * @property mixed  $id
14  * @property int    $page_id
15  * @property string $name
16  * @property string $slug
17  * @property string $book_slug
18  * @property int    $created_by
19  * @property Carbon $created_at
20  * @property Carbon $updated_at
21  * @property string $type
22  * @property string $summary
23  * @property string $markdown
24  * @property string $html
25  * @property string $text
26  * @property int    $revision_number
27  * @property Page   $page
28  * @property-read ?User $createdBy
29  */
30 class PageRevision extends Model
31 {
32     protected $fillable = ['name', 'text', 'summary'];
33     protected $hidden = ['html', 'markdown', 'restricted', 'text'];
34
35     /**
36      * Get the user that created the page revision.
37      */
38     public function createdBy(): BelongsTo
39     {
40         return $this->belongsTo(User::class, 'created_by');
41     }
42
43     /**
44      * Get the page this revision originates from.
45      */
46     public function page(): BelongsTo
47     {
48         return $this->belongsTo(Page::class);
49     }
50
51     /**
52      * Get the url for this revision.
53      */
54     public function getUrl(string $path = ''): string
55     {
56         return $this->page->getUrl('/revisions/' . $this->id . '/' . ltrim($path, '/'));
57     }
58
59     /**
60      * Get the previous revision for the same page if existing.
61      */
62     public function getPrevious(): ?PageRevision
63     {
64         $id = static::newQuery()->where('page_id', '=', $this->page_id)
65             ->where('id', '<', $this->id)
66             ->max('id');
67
68         if ($id) {
69             return static::query()->find($id);
70         }
71
72         return null;
73     }
74
75     /**
76      * Allows checking of the exact class, Used to check entity type.
77      * Included here to align with entities in similar use cases.
78      * (Yup, Bit of an awkward hack).
79      *
80      * @deprecated Use instanceof instead.
81      */
82     public static function isA(string $type): bool
83     {
84         return $type === 'revision';
85     }
86 }