]> BookStack Code Mirror - bookstack/blob - app/Entities/Controllers/BookshelfController.php
Played around with a new app structure
[bookstack] / app / Entities / Controllers / BookshelfController.php
1 <?php
2
3 namespace BookStack\Entities\Controllers;
4
5 use BookStack\Activity\ActivityQueries;
6 use BookStack\Activity\Models\View;
7 use BookStack\Entities\Models\Book;
8 use BookStack\Entities\Repos\BookshelfRepo;
9 use BookStack\Entities\Tools\ShelfContext;
10 use BookStack\Exceptions\ImageUploadException;
11 use BookStack\Exceptions\NotFoundException;
12 use BookStack\Http\Controllers\Controller;
13 use BookStack\References\ReferenceFetcher;
14 use BookStack\Util\SimpleListOptions;
15 use Exception;
16 use Illuminate\Http\Request;
17 use Illuminate\Validation\ValidationException;
18
19 class BookshelfController extends Controller
20 {
21     protected BookshelfRepo $shelfRepo;
22     protected ShelfContext $shelfContext;
23     protected ReferenceFetcher $referenceFetcher;
24
25     public function __construct(BookshelfRepo $shelfRepo, ShelfContext $shelfContext, ReferenceFetcher $referenceFetcher)
26     {
27         $this->shelfRepo = $shelfRepo;
28         $this->shelfContext = $shelfContext;
29         $this->referenceFetcher = $referenceFetcher;
30     }
31
32     /**
33      * Display a listing of the book.
34      */
35     public function index(Request $request)
36     {
37         $view = setting()->getForCurrentUser('bookshelves_view_type');
38         $listOptions = SimpleListOptions::fromRequest($request, 'bookshelves')->withSortOptions([
39             'name'       => trans('common.sort_name'),
40             'created_at' => trans('common.sort_created_at'),
41             'updated_at' => trans('common.sort_updated_at'),
42         ]);
43
44         $shelves = $this->shelfRepo->getAllPaginated(18, $listOptions->getSort(), $listOptions->getOrder());
45         $recents = $this->isSignedIn() ? $this->shelfRepo->getRecentlyViewed(4) : false;
46         $popular = $this->shelfRepo->getPopular(4);
47         $new = $this->shelfRepo->getRecentlyCreated(4);
48
49         $this->shelfContext->clearShelfContext();
50         $this->setPageTitle(trans('entities.shelves'));
51
52         return view('shelves.index', [
53             'shelves'     => $shelves,
54             'recents'     => $recents,
55             'popular'     => $popular,
56             'new'         => $new,
57             'view'        => $view,
58             'listOptions' => $listOptions,
59         ]);
60     }
61
62     /**
63      * Show the form for creating a new bookshelf.
64      */
65     public function create()
66     {
67         $this->checkPermission('bookshelf-create-all');
68         $books = Book::visible()->orderBy('name')->get(['name', 'id', 'slug', 'created_at', 'updated_at']);
69         $this->setPageTitle(trans('entities.shelves_create'));
70
71         return view('shelves.create', ['books' => $books]);
72     }
73
74     /**
75      * Store a newly created bookshelf in storage.
76      *
77      * @throws ValidationException
78      * @throws ImageUploadException
79      */
80     public function store(Request $request)
81     {
82         $this->checkPermission('bookshelf-create-all');
83         $validated = $this->validate($request, [
84             'name'        => ['required', 'string', 'max:255'],
85             'description' => ['string', 'max:1000'],
86             'image'       => array_merge(['nullable'], $this->getImageValidationRules()),
87             'tags'        => ['array'],
88         ]);
89
90         $bookIds = explode(',', $request->get('books', ''));
91         $shelf = $this->shelfRepo->create($validated, $bookIds);
92
93         return redirect($shelf->getUrl());
94     }
95
96     /**
97      * Display the bookshelf of the given slug.
98      *
99      * @throws NotFoundException
100      */
101     public function show(Request $request, ActivityQueries $activities, string $slug)
102     {
103         $shelf = $this->shelfRepo->getBySlug($slug);
104         $this->checkOwnablePermission('bookshelf-view', $shelf);
105
106         $listOptions = SimpleListOptions::fromRequest($request, 'shelf_books')->withSortOptions([
107             'default' => trans('common.sort_default'),
108             'name' => trans('common.sort_name'),
109             'created_at' => trans('common.sort_created_at'),
110             'updated_at' => trans('common.sort_updated_at'),
111         ]);
112
113         $sort = $listOptions->getSort();
114         $sortedVisibleShelfBooks = $shelf->visibleBooks()->get()
115             ->sortBy($sort === 'default' ? 'pivot.order' : $sort, SORT_REGULAR, $listOptions->getOrder() === 'desc')
116             ->values()
117             ->all();
118
119         View::incrementFor($shelf);
120         $this->shelfContext->setShelfContext($shelf->id);
121         $view = setting()->getForCurrentUser('bookshelf_view_type');
122
123         $this->setPageTitle($shelf->getShortName());
124
125         return view('shelves.show', [
126             'shelf'                   => $shelf,
127             'sortedVisibleShelfBooks' => $sortedVisibleShelfBooks,
128             'view'                    => $view,
129             'activity'                => $activities->entityActivity($shelf, 20, 1),
130             'listOptions'             => $listOptions,
131             'referenceCount'          => $this->referenceFetcher->getPageReferenceCountToEntity($shelf),
132         ]);
133     }
134
135     /**
136      * Show the form for editing the specified bookshelf.
137      */
138     public function edit(string $slug)
139     {
140         $shelf = $this->shelfRepo->getBySlug($slug);
141         $this->checkOwnablePermission('bookshelf-update', $shelf);
142
143         $shelfBookIds = $shelf->books()->get(['id'])->pluck('id');
144         $books = Book::visible()->whereNotIn('id', $shelfBookIds)->orderBy('name')->get(['name', 'id', 'slug', 'created_at', 'updated_at']);
145
146         $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $shelf->getShortName()]));
147
148         return view('shelves.edit', [
149             'shelf' => $shelf,
150             'books' => $books,
151         ]);
152     }
153
154     /**
155      * Update the specified bookshelf in storage.
156      *
157      * @throws ValidationException
158      * @throws ImageUploadException
159      * @throws NotFoundException
160      */
161     public function update(Request $request, string $slug)
162     {
163         $shelf = $this->shelfRepo->getBySlug($slug);
164         $this->checkOwnablePermission('bookshelf-update', $shelf);
165         $validated = $this->validate($request, [
166             'name'        => ['required', 'string', 'max:255'],
167             'description' => ['string', 'max:1000'],
168             'image'       => array_merge(['nullable'], $this->getImageValidationRules()),
169             'tags'        => ['array'],
170         ]);
171
172         if ($request->has('image_reset')) {
173             $validated['image'] = null;
174         } elseif (array_key_exists('image', $validated) && is_null($validated['image'])) {
175             unset($validated['image']);
176         }
177
178         $bookIds = explode(',', $request->get('books', ''));
179         $shelf = $this->shelfRepo->update($shelf, $validated, $bookIds);
180
181         return redirect($shelf->getUrl());
182     }
183
184     /**
185      * Shows the page to confirm deletion.
186      */
187     public function showDelete(string $slug)
188     {
189         $shelf = $this->shelfRepo->getBySlug($slug);
190         $this->checkOwnablePermission('bookshelf-delete', $shelf);
191
192         $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()]));
193
194         return view('shelves.delete', ['shelf' => $shelf]);
195     }
196
197     /**
198      * Remove the specified bookshelf from storage.
199      *
200      * @throws Exception
201      */
202     public function destroy(string $slug)
203     {
204         $shelf = $this->shelfRepo->getBySlug($slug);
205         $this->checkOwnablePermission('bookshelf-delete', $shelf);
206
207         $this->shelfRepo->destroy($shelf);
208
209         return redirect('/shelves');
210     }
211 }