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