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