]> BookStack Code Mirror - bookstack/blob - app/Entities/Page.php
Default OpenID display name set to standard value
[bookstack] / app / Entities / Page.php
1 <?php namespace BookStack\Entities;
2
3 use BookStack\Uploads\Attachment;
4 use Illuminate\Database\Eloquent\Builder;
5 use Illuminate\Database\Eloquent\Collection;
6 use Illuminate\Database\Eloquent\Relations\BelongsTo;
7 use Illuminate\Database\Eloquent\Relations\HasMany;
8 use Permissions;
9
10 /**
11  * Class Page
12  * @property int $chapter_id
13  * @property string $html
14  * @property string $markdown
15  * @property string $text
16  * @property bool $template
17  * @property bool $draft
18  * @property int $revision_count
19  * @property Chapter $chapter
20  * @property Collection $attachments
21  */
22 class Page extends BookChild
23 {
24     protected $fillable = ['name', 'priority', 'markdown'];
25
26     protected $simpleAttributes = ['name', 'id', 'slug'];
27
28     public $textField = 'text';
29
30     protected $hidden = ['html', 'markdown', 'text', 'restricted', 'pivot'];
31
32     /**
33      * Get the entities that are visible to the current user.
34      */
35     public function scopeVisible(Builder $query)
36     {
37         $query = Permissions::enforceDraftVisiblityOnQuery($query);
38         return parent::scopeVisible($query);
39     }
40
41     /**
42      * Converts this page into a simplified array.
43      * @return mixed
44      */
45     public function toSimpleArray()
46     {
47         $array = array_intersect_key($this->toArray(), array_flip($this->simpleAttributes));
48         $array['url'] = $this->getUrl();
49         return $array;
50     }
51
52     /**
53      * Get the parent item
54      */
55     public function parent(): Entity
56     {
57         return $this->chapter_id ? $this->chapter : $this->book;
58     }
59
60     /**
61      * Get the chapter that this page is in, If applicable.
62      * @return BelongsTo
63      */
64     public function chapter()
65     {
66         return $this->belongsTo(Chapter::class);
67     }
68
69     /**
70      * Check if this page has a chapter.
71      * @return bool
72      */
73     public function hasChapter()
74     {
75         return $this->chapter()->count() > 0;
76     }
77
78     /**
79      * Get the associated page revisions, ordered by created date.
80      * @return mixed
81      */
82     public function revisions()
83     {
84         return $this->hasMany(PageRevision::class)->where('type', '=', 'version')->orderBy('created_at', 'desc')->orderBy('id', 'desc');
85     }
86
87     /**
88      * Get the attachments assigned to this page.
89      * @return HasMany
90      */
91     public function attachments()
92     {
93         return $this->hasMany(Attachment::class, 'uploaded_to')->orderBy('order', 'asc');
94     }
95
96     /**
97      * Get the url for this page.
98      * @param string|bool $path
99      * @return string
100      */
101     public function getUrl($path = false)
102     {
103         $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
104         $midText = $this->draft ? '/draft/' : '/page/';
105         $idComponent = $this->draft ? $this->id : urlencode($this->slug);
106
107         $url = '/books/' . urlencode($bookSlug) . $midText . $idComponent;
108         if ($path !== false) {
109             $url .= '/' . trim($path, '/');
110         }
111
112         return url($url);
113     }
114
115     /**
116      * Get the current revision for the page if existing
117      * @return PageRevision|null
118      */
119     public function getCurrentRevision()
120     {
121         return $this->revisions()->first();
122     }
123 }