-<?php namespace BookStack\Entities;
+<?php namespace BookStack\Entities\Models;
+use BookStack\Entities\Tools\PageContent;
use BookStack\Uploads\Attachment;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
public $textField = 'text';
- protected $hidden = ['html', 'markdown', 'text', 'restricted', 'pivot'];
+ protected $hidden = ['html', 'markdown', 'text', 'restricted', 'pivot', 'deleted_at'];
+
+ protected $casts = [
+ 'draft' => '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);
}
/**
- * 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));
}
/**
{
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;
+ }
}