]> BookStack Code Mirror - bookstack/blobdiff - app/Entities/Book.php
Update settings.php
[bookstack] / app / Entities / Book.php
index 77cacf632e03dad04c6b9dcec98843170e046224..38b7d4a8c29d3361652192d703a8362029bd62e4 100644 (file)
@@ -1,21 +1,25 @@
 <?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.
@@ -25,9 +29,9 @@ class Book extends Entity
     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));
     }
 
     /**
@@ -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;
     }
 }