]> BookStack Code Mirror - bookstack/blob - app/Page.php
Updated Swedish translation
[bookstack] / app / Page.php
1 <?php namespace BookStack;
2
3 class Page extends Entity
4 {
5     protected $fillable = ['name', 'html', 'priority', 'markdown'];
6
7     protected $simpleAttributes = ['name', 'id', 'slug'];
8
9     protected $with = ['book'];
10     public $textField = 'text';
11
12     /**
13      * Converts this page into a simplified array.
14      * @return mixed
15      */
16     public function toSimpleArray()
17     {
18         $array = array_intersect_key($this->toArray(), array_flip($this->simpleAttributes));
19         $array['url'] = $this->getUrl();
20         return $array;
21     }
22
23     /**
24      * Get the book this page sits in.
25      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
26      */
27     public function book()
28     {
29         return $this->belongsTo(Book::class);
30     }
31
32     /**
33      * Get the chapter that this page is in, If applicable.
34      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
35      */
36     public function chapter()
37     {
38         return $this->belongsTo(Chapter::class);
39     }
40
41     /**
42      * Check if this page has a chapter.
43      * @return bool
44      */
45     public function hasChapter()
46     {
47         return $this->chapter()->count() > 0;
48     }
49
50     /**
51      * Get the associated page revisions, ordered by created date.
52      * @return mixed
53      */
54     public function revisions()
55     {
56         return $this->hasMany(PageRevision::class)->where('type', '=', 'version')->orderBy('created_at', 'desc');
57     }
58
59     /**
60      * Get the attachments assigned to this page.
61      * @return \Illuminate\Database\Eloquent\Relations\HasMany
62      */
63     public function attachments()
64     {
65         return $this->hasMany(Attachment::class, 'uploaded_to')->orderBy('order', 'asc');
66     }
67
68     /**
69      * Get the url for this page.
70      * @param string|bool $path
71      * @return string
72      */
73     public function getUrl($path = false)
74     {
75         $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
76         $midText = $this->draft ? '/draft/' : '/page/';
77         $idComponent = $this->draft ? $this->id : urlencode($this->slug);
78
79         if ($path !== false) {
80             return baseUrl('/books/' . urlencode($bookSlug) . $midText . $idComponent . '/' . trim($path, '/'));
81         }
82
83         return baseUrl('/books/' . urlencode($bookSlug) . $midText . $idComponent);
84     }
85
86     /**
87      * Get an excerpt of this page's content to the specified length.
88      * @param int $length
89      * @return mixed
90      */
91     public function getExcerpt($length = 100)
92     {
93         $text = strlen($this->text) > $length ? substr($this->text, 0, $length-3) . '...' : $this->text;
94         return mb_convert_encoding($text, 'UTF-8');
95     }
96
97     /**
98      * Return a generalised, common raw query that can be 'unioned' across entities.
99      * @param bool $withContent
100      * @return string
101      */
102     public function entityRawQuery($withContent = false)
103     {
104         $htmlQuery = $withContent ? 'html' : "'' as html";
105         return "'BookStack\\\\Page' as entity_type, id, id as entity_id, slug, name, {$this->textField} as text, {$htmlQuery}, book_id, priority, chapter_id, draft, created_by, updated_by, updated_at, created_at";
106     }
107 }