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