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