]> BookStack Code Mirror - bookstack/blob - app/Entities/Models/Page.php
Add prev and next button to navigate through different pages
[bookstack] / app / Entities / Models / Page.php
1 <?php namespace BookStack\Entities\Models;
2
3 use BookStack\Entities\Tools\PageContent;
4 use BookStack\Uploads\Attachment;
5 use Illuminate\Database\Eloquent\Builder;
6 use Illuminate\Database\Eloquent\Collection;
7 use Illuminate\Database\Eloquent\Relations\BelongsTo;
8 use Illuminate\Database\Eloquent\Relations\HasMany;
9 use Permissions;
10
11 /**
12  * Class Page
13  * @property int $chapter_id
14  * @property string $html
15  * @property string $markdown
16  * @property string $text
17  * @property bool $template
18  * @property bool $draft
19  * @property int $revision_count
20  * @property Chapter $chapter
21  * @property Collection $attachments
22  */
23 class Page extends BookChild
24 {
25     protected $fillable = ['name', 'priority', 'markdown'];
26
27     protected $simpleAttributes = ['name', 'id', 'slug'];
28
29     public $textField = 'text';
30
31     protected $hidden = ['html', 'markdown', 'text', 'restricted', 'pivot', 'deleted_at'];
32
33     protected $casts = [
34         'draft' => 'boolean',
35         'template' => 'boolean',
36     ];
37
38     /**
39      * Get the entities that are visible to the current user.
40      */
41     public function scopeVisible(Builder $query): Builder
42     {
43         $query = Permissions::enforceDraftVisiblityOnQuery($query);
44         return parent::scopeVisible($query);
45     }
46
47     /**
48      * Converts this page into a simplified array.
49      * @return mixed
50      */
51     public function toSimpleArray()
52     {
53         $array = array_intersect_key($this->toArray(), array_flip($this->simpleAttributes));
54         $array['url'] = $this->getUrl();
55         return $array;
56     }
57
58     /**
59      * Get the chapter that this page is in, If applicable.
60      * @return BelongsTo
61      */
62     public function chapter()
63     {
64         return $this->belongsTo(Chapter::class);
65     }
66
67     /**
68      * Check if this page has a chapter.
69      * @return bool
70      */
71     public function hasChapter()
72     {
73         return $this->chapter()->count() > 0;
74     }
75
76     /**
77      * Get the associated page revisions, ordered by created date.
78      * @return mixed
79      */
80     public function revisions()
81     {
82         return $this->hasMany(PageRevision::class)->where('type', '=', 'version')->orderBy('created_at', 'desc')->orderBy('id', 'desc');
83     }
84
85     /**
86      * Get the attachments assigned to this page.
87      * @return HasMany
88      */
89     public function attachments()
90     {
91         return $this->hasMany(Attachment::class, 'uploaded_to')->orderBy('order', 'asc');
92     }
93
94     /**
95      * Get the url of this page.
96      */
97     public function getUrl($path = ''): string
98     {
99         $parts = [
100             'books',
101             urlencode($this->getAttribute('bookSlug') ?? $this->book->slug),
102             $this->draft ? 'draft' : 'page',
103             $this->draft ? $this->id : urlencode($this->slug),
104             trim($path, '/'),
105         ];
106
107         return url('/' . implode('/', $parts));
108     }
109
110     /**
111      * Get the current revision for the page if existing
112      * @return PageRevision|null
113      */
114     public function getCurrentRevision()
115     {
116         return $this->revisions()->first();
117     }
118
119     /**
120      * Get this page for JSON display.
121      */
122     public function forJsonDisplay(): Page
123     {
124         $refreshed = $this->refresh()->unsetRelations()->load(['tags', 'createdBy', 'updatedBy', 'ownedBy']);
125         $refreshed->setHidden(array_diff($refreshed->getHidden(), ['html', 'markdown']));
126         $refreshed->html = (new PageContent($refreshed))->render();
127         return $refreshed;
128     }
129     /**
130      * Get the parent chapter ID.
131      */
132     public function getParentChapter()
133     {
134         $chapterId = $this->chapter()->visible()
135         ->get('id');
136         return $chapterId;
137     }
138 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.