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