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