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