1 <?php namespace BookStack;
3 class Chapter extends Entity
5 public $searchFactor = 1.3;
7 protected $fillable = ['name', 'description', 'priority', 'book_id'];
9 protected $with = ['book'];
12 * Get the book this chapter is within.
13 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
15 public function book()
17 return $this->belongsTo(Book::class);
21 * Get the pages that this chapter contains.
25 public function pages($dir = 'ASC')
27 return $this->hasMany(Page::class)->orderBy('priority', $dir);
31 * Get the url of this chapter.
32 * @param string|bool $path
35 public function getUrl($path = false)
37 $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
38 if ($path !== false) {
39 return baseUrl('/books/' . urlencode($bookSlug) . '/chapter/' . urlencode($this->slug) . '/' . trim($path, '/'));
41 return baseUrl('/books/' . urlencode($bookSlug) . '/chapter/' . urlencode($this->slug));
45 * Get an excerpt of this chapter's description to the specified length or less.
49 public function getExcerpt($length = 100)
51 $description = $this->description;
52 return strlen($description) > $length ? substr($description, 0, $length-3) . '...' : $description;
56 * Return a generalised, common raw query that can be 'unioned' across entities.
59 public function entityRawQuery()
61 return "'BookStack\\\\Chapter' as entity_type, id, id as entity_id, slug, name, {$this->textField} as text, '' as html, book_id, priority, '0' as chapter_id, '0' as draft, created_by, updated_by, updated_at, created_at";