]> BookStack Code Mirror - bookstack/blob - app/Entities/Models/PageRevision.php
Minor capitalisation fix for Estonian
[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 int    $page_id
14  * @property string $slug
15  * @property string $book_slug
16  * @property int    $created_by
17  * @property Carbon $created_at
18  * @property Carbon $updated_at
19  * @property string $type
20  * @property string $summary
21  * @property string $markdown
22  * @property string $html
23  * @property int    $revision_number
24  * @property Page   $page
25  */
26 class PageRevision extends Model
27 {
28     protected $fillable = ['name', 'html', 'text', 'markdown', 'summary'];
29
30     /**
31      * Get the user that created the page revision.
32      */
33     public function createdBy(): BelongsTo
34     {
35         return $this->belongsTo(User::class, 'created_by');
36     }
37
38     /**
39      * Get the page this revision originates from.
40      */
41     public function page(): BelongsTo
42     {
43         return $this->belongsTo(Page::class);
44     }
45
46     /**
47      * Get the url for this revision.
48      *
49      * @param null|string $path
50      *
51      * @return string
52      */
53     public function getUrl($path = null)
54     {
55         $url = $this->page->getUrl() . '/revisions/' . $this->id;
56         if ($path) {
57             return $url . '/' . trim($path, '/');
58         }
59
60         return $url;
61     }
62
63     /**
64      * Get the previous revision for the same page if existing.
65      *
66      * @return \BookStack\Entities\PageRevision|null
67      */
68     public function getPrevious()
69     {
70         $id = static::newQuery()->where('page_id', '=', $this->page_id)
71             ->where('id', '<', $this->id)
72             ->max('id');
73
74         if ($id) {
75             return static::query()->find($id);
76         }
77
78         return null;
79     }
80
81     /**
82      * Allows checking of the exact class, Used to check entity type.
83      * Included here to align with entities in similar use cases.
84      * (Yup, Bit of an awkward hack).
85      *
86      * @param $type
87      *
88      * @return bool
89      */
90     public static function isA($type)
91     {
92         return $type === 'revision';
93     }
94 }