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