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