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