3 namespace BookStack\Entities\Models;
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;
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
27 class Page extends BookChild
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'];
34 protected $fillable = ['name', 'priority'];
36 public $textField = 'text';
38 protected $hidden = ['html', 'markdown', 'text', 'restricted', 'pivot', 'deleted_at'];
42 'template' => 'boolean',
46 * Get the entities that are visible to the current user.
48 public function scopeVisible(Builder $query): Builder
50 $query = Permissions::enforceDraftVisibilityOnQuery($query);
52 return parent::scopeVisible($query);
56 * Get the chapter that this page is in, If applicable.
60 public function chapter()
62 return $this->belongsTo(Chapter::class);
66 * Check if this page has a chapter.
68 public function hasChapter(): bool
70 return $this->chapter()->count() > 0;
74 * Get the associated page revisions, ordered by created date.
75 * Only provides actual saved page revision instances, Not drafts.
77 public function revisions(): HasMany
79 return $this->allRevisions()
80 ->where('type', '=', 'version')
81 ->orderBy('created_at', 'desc')
82 ->orderBy('id', 'desc');
86 * Get all revision instances assigned to this page.
87 * Includes all types of revisions.
89 public function allRevisions(): HasMany
91 return $this->hasMany(PageRevision::class);
95 * Get the attachments assigned to this page.
99 public function attachments()
101 return $this->hasMany(Attachment::class, 'uploaded_to')->orderBy('order', 'asc');
105 * Get the url of this page.
107 public function getUrl(string $path = ''): string
111 urlencode($this->book_slug ?? $this->book->slug),
112 $this->draft ? 'draft' : 'page',
113 $this->draft ? $this->id : urlencode($this->slug),
117 return url('/' . implode('/', $parts));
121 * Get the current revision for the page if existing.
123 * @return PageRevision|null
125 public function getCurrentRevision()
127 return $this->revisions()->first();
131 * Get this page for JSON display.
133 public function forJsonDisplay(): self
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();