]> BookStack Code Mirror - bookstack/blob - app/Page.php
Added Italian language
[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     public function comments() {
70         return $this->hasMany(Comment::class, 'page_id')->orderBy('created_on', 'asc');
71     }
72
73     /**
74      * Get the url for this page.
75      * @param string|bool $path
76      * @return string
77      */
78     public function getUrl($path = false)
79     {
80         $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
81         $midText = $this->draft ? '/draft/' : '/page/';
82         $idComponent = $this->draft ? $this->id : urlencode($this->slug);
83
84         if ($path !== false) {
85             return baseUrl('/books/' . urlencode($bookSlug) . $midText . $idComponent . '/' . trim($path, '/'));
86         }
87
88         return baseUrl('/books/' . urlencode($bookSlug) . $midText . $idComponent);
89     }
90
91     /**
92      * Get an excerpt of this page's content to the specified length.
93      * @param int $length
94      * @return mixed
95      */
96     public function getExcerpt($length = 100)
97     {
98         $text = strlen($this->text) > $length ? substr($this->text, 0, $length-3) . '...' : $this->text;
99         return mb_convert_encoding($text, 'UTF-8');
100     }
101
102     /**
103      * Return a generalised, common raw query that can be 'unioned' across entities.
104      * @param bool $withContent
105      * @return string
106      */
107     public function entityRawQuery($withContent = false)
108     {   $htmlQuery = $withContent ? 'html' : "'' as html";
109         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";
110     }
111
112 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.