<?php namespace BookStack\Entities;
use BookStack\Uploads\Image;
+use Exception;
+use Illuminate\Database\Eloquent\Relations\BelongsTo;
+use Illuminate\Database\Eloquent\Relations\BelongsToMany;
+use Illuminate\Database\Eloquent\Relations\HasMany;
+use Illuminate\Support\Collection;
-class Book extends Entity
+/**
+ * Class Book
+ * @property string $description
+ * @property int $image_id
+ * @property Image|null $cover
+ * @package BookStack\Entities
+ */
+class Book extends Entity implements HasCoverImage
{
public $searchFactor = 2;
- protected $fillable = ['name', 'description', 'image_id'];
-
- /**
- * Get the morph class for this model.
- * @return string
- */
- public function getMorphClass()
- {
- return 'BookStack\\Book';
- }
+ protected $fillable = ['name', 'description'];
+ protected $hidden = ['restricted', 'pivot'];
/**
* Get the url for this book.
public function getUrl($path = false)
{
if ($path !== false) {
- return baseUrl('/books/' . urlencode($this->slug) . '/' . trim($path, '/'));
+ return url('/books/' . urlencode($this->slug) . '/' . trim($path, '/'));
}
- return baseUrl('/books/' . urlencode($this->slug));
+ return url('/books/' . urlencode($this->slug));
}
/**
}
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;
/**
* 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()
{
return $this->hasMany(Page::class);
}
+ /**
+ * Get the direct child pages of this book.
+ * @return HasMany
+ */
+ public function directPages()
+ {
+ return $this->pages()->where('chapter_id', '=', '0');
+ }
+
/**
* Get all chapters within this book.
- * @return \Illuminate\Database\Eloquent\Relations\HasMany
+ * @return HasMany
*/
public function chapters()
{
/**
* Get the shelves this book is contained within.
- * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
+ * @return BelongsToMany
*/
public function shelves()
{
}
/**
- * 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($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;
}
}