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