X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/d9cde4123def07f77e2a230ee9a10a5991226e44..refs/pull/2023/head:/app/Entities/Book.php diff --git a/app/Entities/Book.php b/app/Entities/Book.php index 77cacf632..38b7d4a8c 100644 --- a/app/Entities/Book.php +++ b/app/Entities/Book.php @@ -1,21 +1,25 @@ slug) . '/' . trim($path, '/')); + return url('/http/source.bookstackapp.com/books/' . urlencode($this->slug) . '/' . trim($path, '/')); } - return baseUrl('/books/' . urlencode($this->slug)); + return url('/http/source.bookstackapp.com/books/' . urlencode($this->slug)); } /** @@ -44,8 +48,8 @@ class Book extends Entity } try { - $cover = $this->cover ? baseUrl($this->cover->getThumb($width, $height, false)) : $default; - } catch (\Exception $err) { + $cover = $this->cover ? url($this->cover->getThumb($width, $height, false)) : $default; + } catch (Exception $err) { $cover = $default; } return $cover; @@ -53,16 +57,23 @@ class Book extends Entity /** * Get the cover image of the book - * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ - public function cover() + public function cover(): BelongsTo { return $this->belongsTo(Image::class, 'image_id'); } + /** + * Get the type of the image model that is used when storing a cover image. + */ + public function coverImageTypeKey(): string + { + return 'cover_book'; + } + /** * Get all pages within this book. - * @return \Illuminate\Database\Eloquent\Relations\HasMany + * @return HasMany */ public function pages() { @@ -71,7 +82,7 @@ class Book extends Entity /** * Get the direct child pages of this book. - * @return \Illuminate\Database\Eloquent\Relations\HasMany + * @return HasMany */ public function directPages() { @@ -80,7 +91,7 @@ class Book extends Entity /** * Get all chapters within this book. - * @return \Illuminate\Database\Eloquent\Relations\HasMany + * @return HasMany */ public function chapters() { @@ -89,7 +100,7 @@ class Book extends Entity /** * Get the shelves this book is contained within. - * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany + * @return BelongsToMany */ public function shelves() { @@ -97,22 +108,24 @@ class Book extends Entity } /** - * Get an excerpt of this book's description to the specified length or less. - * @param int $length - * @return string + * Get the direct child items within this book. + * @return Collection */ - public function getExcerpt(int $length = 100) + public function getDirectChildren(): Collection { - $description = $this->description; - return strlen($description) > $length ? substr($description, 0, $length-3) . '...' : $description; + $pages = $this->directPages()->visible()->get(); + $chapters = $this->chapters()->visible()->get(); + return $pages->concat($chapters)->sortBy('priority')->sortByDesc('draft'); } /** - * Return a generalised, common raw query that can be 'unioned' across entities. + * Get an excerpt of this book's description to the specified length or less. + * @param int $length * @return string */ - public function entityRawQuery() + public function getExcerpt(int $length = 100) { - return "'BookStack\\\\Book' as entity_type, id, id as entity_id, slug, name, {$this->textField} as text,'' as html, '0' as book_id, '0' as priority, '0' as chapter_id, '0' as draft, created_by, updated_by, updated_at, created_at"; + $description = $this->description; + return mb_strlen($description) > $length ? mb_substr($description, 0, $length-3) . '...' : $description; } }