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