]> BookStack Code Mirror - bookstack/blob - app/Chapter.php
Updated Spanish translation
[bookstack] / app / Chapter.php
1 <?php namespace BookStack;
2
3 class Chapter extends Entity
4 {
5     public $searchFactor = 1.3;
6
7     protected $fillable = ['name', 'description', 'priority', 'book_id'];
8
9     protected $with = ['book'];
10
11     /**
12      * Get the book this chapter is within.
13      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
14      */
15     public function book()
16     {
17         return $this->belongsTo(Book::class);
18     }
19
20     /**
21      * Get the pages that this chapter contains.
22      * @param string $dir
23      * @return mixed
24      */
25     public function pages($dir = 'ASC')
26     {
27         return $this->hasMany(Page::class)->orderBy('priority', $dir);
28     }
29
30     /**
31      * Get the url of this chapter.
32      * @param string|bool $path
33      * @return string
34      */
35     public function getUrl($path = false)
36     {
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, '/'));
40         }
41         return baseUrl('/books/' . urlencode($bookSlug) . '/chapter/' . urlencode($this->slug));
42     }
43
44     /**
45      * Get an excerpt of this chapter's description to the specified length or less.
46      * @param int $length
47      * @return string
48      */
49     public function getExcerpt($length = 100)
50     {
51         $description = $this->description;
52         return strlen($description) > $length ? substr($description, 0, $length-3) . '...' : $description;
53     }
54
55     /**
56      * Return a generalised, common raw query that can be 'unioned' across entities.
57      * @return string
58      */
59     public function entityRawQuery()
60     {
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";
62     }
63 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.