X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/c7a2d568bf693add30c8402d68d1f46f09a44c5b..refs/pull/2522/head:/app/Entities/Models/Page.php diff --git a/app/Entities/Models/Page.php b/app/Entities/Models/Page.php index 8ad05e7aa..739927aff 100644 --- a/app/Entities/Models/Page.php +++ b/app/Entities/Models/Page.php @@ -1,5 +1,6 @@ - 'boolean', + 'template' => 'boolean', + ]; /** * Get the entities that are visible to the current user. */ - public function scopeVisible(Builder $query) + public function scopeVisible(Builder $query): Builder { $query = Permissions::enforceDraftVisiblityOnQuery($query); return parent::scopeVisible($query); @@ -86,22 +92,19 @@ class Page extends BookChild } /** - * Get the url for this page. - * @param string|bool $path - * @return string + * Get the url of this page. */ - public function getUrl($path = false) + public function getUrl($path = ''): string { - $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug; - $midText = $this->draft ? '/draft/' : '/page/'; - $idComponent = $this->draft ? $this->id : urlencode($this->slug); - - $url = '/books/' . urlencode($bookSlug) . $midText . $idComponent; - if ($path !== false) { - $url .= '/' . trim($path, '/'); - } + $parts = [ + 'books', + urlencode($this->getAttribute('bookSlug') ?? $this->book->slug), + $this->draft ? 'draft' : 'page', + $this->draft ? $this->id : urlencode($this->slug), + trim($path, '/'), + ]; - return url($url); + return url('/' . implode('/', $parts)); } /** @@ -112,4 +115,15 @@ class Page extends BookChild { return $this->revisions()->first(); } + + /** + * Get this page for JSON display. + */ + public function forJsonDisplay(): Page + { + $refreshed = $this->refresh()->unsetRelations()->load(['tags', 'createdBy', 'updatedBy', 'ownedBy']); + $refreshed->setHidden(array_diff($refreshed->getHidden(), ['html', 'markdown'])); + $refreshed->html = (new PageContent($refreshed))->render(); + return $refreshed; + } }