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