]> BookStack Code Mirror - bookstack/blob - app/Entities/Controllers/BookshelfController.php
Merge branch 'BookStackApp:development' into add-priority
[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\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 bookshelves.
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()
115             ->reorder($sort === 'default' ? 'order' : $sort, $listOptions->getOrder())
116             ->get()
117             ->values()
118             ->all();
119
120         View::incrementFor($shelf);
121         $this->shelfContext->setShelfContext($shelf->id);
122         $view = setting()->getForCurrentUser('bookshelf_view_type');
123
124         $this->setPageTitle($shelf->getShortName());
125
126         return view('shelves.show', [
127             'shelf'                   => $shelf,
128             'sortedVisibleShelfBooks' => $sortedVisibleShelfBooks,
129             'view'                    => $view,
130             'activity'                => $activities->entityActivity($shelf, 20, 1),
131             'listOptions'             => $listOptions,
132             'referenceCount'          => $this->referenceFetcher->getPageReferenceCountToEntity($shelf),
133         ]);
134     }
135
136     /**
137      * Show the form for editing the specified bookshelf.
138      */
139     public function edit(string $slug)
140     {
141         $shelf = $this->shelfRepo->getBySlug($slug);
142         $this->checkOwnablePermission('bookshelf-update', $shelf);
143
144         $shelfBookIds = $shelf->books()->get(['id'])->pluck('id');
145         $books = Book::visible()->whereNotIn('id', $shelfBookIds)->orderBy('name')->get(['name', 'id', 'slug', 'created_at', 'updated_at']);
146
147         $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $shelf->getShortName()]));
148
149         return view('shelves.edit', [
150             'shelf' => $shelf,
151             'books' => $books,
152         ]);
153     }
154
155     /**
156      * Update the specified bookshelf in storage.
157      *
158      * @throws ValidationException
159      * @throws ImageUploadException
160      * @throws NotFoundException
161      */
162     public function update(Request $request, string $slug)
163     {
164         $shelf = $this->shelfRepo->getBySlug($slug);
165         $this->checkOwnablePermission('bookshelf-update', $shelf);
166         $validated = $this->validate($request, [
167             'name'        => ['required', 'string', 'max:255'],
168             'description' => ['string', 'max:1000'],
169             'image'       => array_merge(['nullable'], $this->getImageValidationRules()),
170             'tags'        => ['array'],
171         ]);
172
173         if ($request->has('image_reset')) {
174             $validated['image'] = null;
175         } elseif (array_key_exists('image', $validated) && is_null($validated['image'])) {
176             unset($validated['image']);
177         }
178
179         $bookIds = explode(',', $request->get('books', ''));
180         $shelf = $this->shelfRepo->update($shelf, $validated, $bookIds);
181
182         return redirect($shelf->getUrl());
183     }
184
185     /**
186      * Shows the page to confirm deletion.
187      */
188     public function showDelete(string $slug)
189     {
190         $shelf = $this->shelfRepo->getBySlug($slug);
191         $this->checkOwnablePermission('bookshelf-delete', $shelf);
192
193         $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()]));
194
195         return view('shelves.delete', ['shelf' => $shelf]);
196     }
197
198     /**
199      * Remove the specified bookshelf from storage.
200      *
201      * @throws Exception
202      */
203     public function destroy(string $slug)
204     {
205         $shelf = $this->shelfRepo->getBySlug($slug);
206         $this->checkOwnablePermission('bookshelf-delete', $shelf);
207
208         $this->shelfRepo->destroy($shelf);
209
210         return redirect('/shelves');
211     }
212 }