]> BookStack Code Mirror - bookstack/blob - app/Entities/Queries/PageQueries.php
8fb02075ae37f796cf8aeb54c5cd27c98450368a
[bookstack] / app / Entities / Queries / PageQueries.php
1 <?php
2
3 namespace BookStack\Entities\Queries;
4
5 use BookStack\Entities\Models\Page;
6 use BookStack\Exceptions\NotFoundException;
7 use Illuminate\Database\Eloquent\Builder;
8
9 class PageQueries implements ProvidesEntityQueries
10 {
11     protected static array $contentAttributes = [
12         'name', 'id', 'slug', 'book_id', 'chapter_id', 'draft',
13         'template', 'html', 'text', 'created_at', 'updated_at', 'priority'
14     ];
15     protected static array $listAttributes = [
16         'name', 'id', 'slug', 'book_id', 'chapter_id', 'draft',
17         'template', 'text', 'created_at', 'updated_at', 'priority'
18     ];
19
20     public function start(): Builder
21     {
22         return Page::query();
23     }
24
25     public function findVisibleById(int $id): ?Page
26     {
27         return $this->start()->scopes('visible')->find($id);
28     }
29
30     public function findVisibleByIdOrFail(int $id): Page
31     {
32         $page = $this->findVisibleById($id);
33
34         if (is_null($page)) {
35             throw new NotFoundException(trans('errors.page_not_found'));
36         }
37
38         return $page;
39     }
40
41     public function findVisibleBySlugsOrFail(string $bookSlug, string $pageSlug): Page
42     {
43         /** @var ?Page $page */
44         $page = $this->start()->with('book')
45             ->scopes('visible')
46             ->whereHas('book', function (Builder $query) use ($bookSlug) {
47                 $query->where('slug', '=', $bookSlug);
48             })
49             ->where('slug', '=', $pageSlug)
50             ->first();
51
52         if (is_null($page)) {
53             throw new NotFoundException(trans('errors.page_not_found'));
54         }
55
56         return $page;
57     }
58
59     public function usingSlugs(string $bookSlug, string $pageSlug): Builder
60     {
61         return $this->start()
62             ->where('slug', '=', $pageSlug)
63             ->whereHas('book', function (Builder $query) use ($bookSlug) {
64                 $query->where('slug', '=', $bookSlug);
65             });
66     }
67
68     public function visibleForList(): Builder
69     {
70         return $this->start()
71             ->scopes('visible')
72             ->select($this->mergeBookSlugForSelect(static::$listAttributes));
73     }
74
75     public function visibleWithContents(): Builder
76     {
77         return $this->start()
78             ->scopes('visible')
79             ->select($this->mergeBookSlugForSelect(static::$contentAttributes));
80     }
81
82     public function currentUserDraftsForList(): Builder
83     {
84         return $this->visibleForList()
85             ->where('draft', '=', true)
86             ->where('created_by', '=', user()->id);
87     }
88
89     public function visibleTemplates(): Builder
90     {
91         return $this->visibleForList()
92             ->where('template', '=', true);
93     }
94
95     protected function mergeBookSlugForSelect(array $columns): array
96     {
97         return array_merge($columns, ['book_slug' => function ($builder) {
98             $builder->select('slug')
99                 ->from('books')
100                 ->whereColumn('books.id', '=', 'pages.book_id');
101         }]);
102     }
103 }