| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-18 00:56:55 +08:00
										 |  |  | namespace BookStack\Entities\Controllers; | 
					
						
							| 
									
										
										
										
											2018-08-27 21:18:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-18 00:56:55 +08:00
										 |  |  | use BookStack\Activity\ActivityQueries; | 
					
						
							|  |  |  | use BookStack\Activity\Models\View; | 
					
						
							| 
									
										
										
										
											2024-02-08 00:37:36 +08:00
										 |  |  | use BookStack\Entities\Queries\BookQueries; | 
					
						
							| 
									
										
										
										
											2024-02-05 03:32:19 +08:00
										 |  |  | use BookStack\Entities\Queries\BookshelfQueries; | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | use BookStack\Entities\Repos\BookshelfRepo; | 
					
						
							| 
									
										
										
										
											2020-11-22 07:20:54 +08:00
										 |  |  | use BookStack\Entities\Tools\ShelfContext; | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  | use BookStack\Exceptions\ImageUploadException; | 
					
						
							|  |  |  | use BookStack\Exceptions\NotFoundException; | 
					
						
							| 
									
										
										
										
											2023-05-19 03:53:39 +08:00
										 |  |  | use BookStack\Http\Controller; | 
					
						
							| 
									
										
										
										
											2022-08-20 19:07:38 +08:00
										 |  |  | use BookStack\References\ReferenceFetcher; | 
					
						
							| 
									
										
										
										
											2022-10-30 23:16:06 +08:00
										 |  |  | use BookStack\Util\SimpleListOptions; | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  | use Exception; | 
					
						
							| 
									
										
										
										
											2018-08-27 21:18:09 +08:00
										 |  |  | use Illuminate\Http\Request; | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  | use Illuminate\Validation\ValidationException; | 
					
						
							| 
									
										
										
										
											2018-08-27 21:18:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class BookshelfController extends Controller | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-12-17 23:02:15 +08:00
										 |  |  |     public function __construct( | 
					
						
							|  |  |  |         protected BookshelfRepo $shelfRepo, | 
					
						
							| 
									
										
										
										
											2024-02-05 03:32:19 +08:00
										 |  |  |         protected BookshelfQueries $queries, | 
					
						
							| 
									
										
										
										
											2024-02-08 00:37:36 +08:00
										 |  |  |         protected BookQueries $bookQueries, | 
					
						
							| 
									
										
										
										
											2023-12-17 23:02:15 +08:00
										 |  |  |         protected ShelfContext $shelfContext, | 
					
						
							| 
									
										
										
										
											2024-02-05 03:32:19 +08:00
										 |  |  |         protected ReferenceFetcher $referenceFetcher, | 
					
						
							| 
									
										
										
										
											2023-12-17 23:02:15 +08:00
										 |  |  |     ) { | 
					
						
							| 
									
										
										
										
											2018-08-27 21:18:09 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-23 23:42:40 +08:00
										 |  |  |      * Display a listing of bookshelves. | 
					
						
							| 
									
										
										
										
											2018-08-27 21:18:09 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-10-30 23:16:06 +08:00
										 |  |  |     public function index(Request $request) | 
					
						
							| 
									
										
										
										
											2018-08-27 21:18:09 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-02-08 07:12:05 +08:00
										 |  |  |         $view = setting()->getForCurrentUser('bookshelves_view_type'); | 
					
						
							| 
									
										
										
										
											2022-10-30 23:16:06 +08:00
										 |  |  |         $listOptions = SimpleListOptions::fromRequest($request, 'bookshelves')->withSortOptions([ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'name'       => trans('common.sort_name'), | 
					
						
							| 
									
										
										
										
											2019-02-03 21:45:45 +08:00
										 |  |  |             'created_at' => trans('common.sort_created_at'), | 
					
						
							|  |  |  |             'updated_at' => trans('common.sort_updated_at'), | 
					
						
							| 
									
										
										
										
											2022-10-30 23:16:06 +08:00
										 |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2019-02-03 21:45:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-05 03:32:19 +08:00
										 |  |  |         $shelves = $this->queries->visibleForListWithCover() | 
					
						
							|  |  |  |             ->orderBy($listOptions->getSort(), $listOptions->getOrder()) | 
					
						
							|  |  |  |             ->paginate(18); | 
					
						
							|  |  |  |         $recents = $this->isSignedIn() ? $this->queries->recentlyViewedForCurrentUser()->get() : false; | 
					
						
							|  |  |  |         $popular = $this->queries->popularForList()->get(); | 
					
						
							|  |  |  |         $new = $this->queries->visibleForList() | 
					
						
							|  |  |  |             ->orderBy('created_at', 'desc') | 
					
						
							|  |  |  |             ->take(4) | 
					
						
							|  |  |  |             ->get(); | 
					
						
							| 
									
										
										
										
											2019-02-03 21:45:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-24 19:23:25 +08:00
										 |  |  |         $this->shelfContext->clearShelfContext(); | 
					
						
							| 
									
										
										
										
											2018-08-27 21:18:09 +08:00
										 |  |  |         $this->setPageTitle(trans('entities.shelves')); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-02 19:41:41 +08:00
										 |  |  |         return view('shelves.index', [ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'shelves'     => $shelves, | 
					
						
							|  |  |  |             'recents'     => $recents, | 
					
						
							|  |  |  |             'popular'     => $popular, | 
					
						
							|  |  |  |             'new'         => $new, | 
					
						
							|  |  |  |             'view'        => $view, | 
					
						
							| 
									
										
										
										
											2022-10-30 23:16:06 +08:00
										 |  |  |             'listOptions' => $listOptions, | 
					
						
							| 
									
										
										
										
											2018-08-27 21:18:09 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Show the form for creating a new bookshelf. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function create() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->checkPermission('bookshelf-create-all'); | 
					
						
							| 
									
										
										
										
											2024-02-08 00:37:36 +08:00
										 |  |  |         $books = $this->bookQueries->visibleForList()->orderBy('name')->get(['name', 'id', 'slug', 'created_at', 'updated_at']); | 
					
						
							| 
									
										
										
										
											2018-09-17 02:34:09 +08:00
										 |  |  |         $this->setPageTitle(trans('entities.shelves_create')); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-02 19:41:41 +08:00
										 |  |  |         return view('shelves.create', ['books' => $books]); | 
					
						
							| 
									
										
										
										
											2018-08-27 21:18:09 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-09-17 02:34:09 +08:00
										 |  |  |      * Store a newly created bookshelf in storage. | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |      * @throws ValidationException | 
					
						
							|  |  |  |      * @throws ImageUploadException | 
					
						
							| 
									
										
										
										
											2018-08-27 21:18:09 +08:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function store(Request $request) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->checkPermission('bookshelf-create-all'); | 
					
						
							| 
									
										
										
										
											2022-06-14 00:20:21 +08:00
										 |  |  |         $validated = $this->validate($request, [ | 
					
						
							| 
									
										
										
										
											2023-12-17 23:02:15 +08:00
										 |  |  |             'name'             => ['required', 'string', 'max:255'], | 
					
						
							|  |  |  |             'description_html' => ['string', 'max:2000'], | 
					
						
							|  |  |  |             'image'            => array_merge(['nullable'], $this->getImageValidationRules()), | 
					
						
							|  |  |  |             'tags'             => ['array'], | 
					
						
							| 
									
										
										
										
											2018-08-27 21:18:09 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         $bookIds = explode(',', $request->get('books', '')); | 
					
						
							| 
									
										
										
										
											2022-07-24 19:23:25 +08:00
										 |  |  |         $shelf = $this->shelfRepo->create($validated, $bookIds); | 
					
						
							| 
									
										
										
										
											2018-08-27 21:18:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-27 21:55:23 +08:00
										 |  |  |         return redirect($shelf->getUrl()); | 
					
						
							| 
									
										
										
										
											2018-08-27 21:18:09 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 02:34:09 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |      * Display the bookshelf of the given slug. | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |      * @throws NotFoundException | 
					
						
							| 
									
										
										
										
											2018-09-17 02:34:09 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-10-30 23:16:06 +08:00
										 |  |  |     public function show(Request $request, ActivityQueries $activities, string $slug) | 
					
						
							| 
									
										
										
										
											2018-09-17 02:34:09 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-02-06 01:35:49 +08:00
										 |  |  |         $shelf = $this->queries->findVisibleBySlugOrFail($slug); | 
					
						
							| 
									
										
										
										
											2022-07-17 03:55:32 +08:00
										 |  |  |         $this->checkOwnablePermission('bookshelf-view', $shelf); | 
					
						
							| 
									
										
										
										
											2018-09-17 02:34:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-30 23:16:06 +08:00
										 |  |  |         $listOptions = SimpleListOptions::fromRequest($request, 'shelf_books')->withSortOptions([ | 
					
						
							|  |  |  |             'default' => trans('common.sort_default'), | 
					
						
							|  |  |  |             'name' => trans('common.sort_name'), | 
					
						
							|  |  |  |             'created_at' => trans('common.sort_created_at'), | 
					
						
							|  |  |  |             'updated_at' => trans('common.sort_updated_at'), | 
					
						
							|  |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2021-01-31 11:25:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-30 23:16:06 +08:00
										 |  |  |         $sort = $listOptions->getSort(); | 
					
						
							| 
									
										
										
										
											2023-06-23 23:42:40 +08:00
										 |  |  |         $sortedVisibleShelfBooks = $shelf->visibleBooks() | 
					
						
							|  |  |  |             ->reorder($sort === 'default' ? 'order' : $sort, $listOptions->getOrder()) | 
					
						
							|  |  |  |             ->get() | 
					
						
							| 
									
										
										
										
											2021-01-31 11:25:31 +08:00
										 |  |  |             ->values() | 
					
						
							|  |  |  |             ->all(); | 
					
						
							| 
									
										
										
										
											2021-01-29 14:58:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-16 17:49:37 +08:00
										 |  |  |         View::incrementFor($shelf); | 
					
						
							| 
									
										
										
										
											2022-07-24 19:23:25 +08:00
										 |  |  |         $this->shelfContext->setShelfContext($shelf->id); | 
					
						
							| 
									
										
										
										
											2021-02-08 07:12:05 +08:00
										 |  |  |         $view = setting()->getForCurrentUser('bookshelf_view_type'); | 
					
						
							| 
									
										
										
										
											2018-09-17 02:34:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-27 21:55:23 +08:00
										 |  |  |         $this->setPageTitle($shelf->getShortName()); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-02 19:41:41 +08:00
										 |  |  |         return view('shelves.show', [ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'shelf'                   => $shelf, | 
					
						
							| 
									
										
										
										
											2021-01-31 11:25:31 +08:00
										 |  |  |             'sortedVisibleShelfBooks' => $sortedVisibleShelfBooks, | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'view'                    => $view, | 
					
						
							| 
									
										
										
										
											2021-12-12 01:29:33 +08:00
										 |  |  |             'activity'                => $activities->entityActivity($shelf, 20, 1), | 
					
						
							| 
									
										
										
										
											2022-10-30 23:16:06 +08:00
										 |  |  |             'listOptions'             => $listOptions, | 
					
						
							| 
									
										
										
										
											2023-12-19 00:23:40 +08:00
										 |  |  |             'referenceCount'          => $this->referenceFetcher->getReferenceCountToEntity($shelf), | 
					
						
							| 
									
										
										
										
											2018-09-17 02:34:09 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Show the form for editing the specified bookshelf. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function edit(string $slug) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-02-06 01:35:49 +08:00
										 |  |  |         $shelf = $this->queries->findVisibleBySlugOrFail($slug); | 
					
						
							| 
									
										
										
										
											2019-04-27 21:55:23 +08:00
										 |  |  |         $this->checkOwnablePermission('bookshelf-update', $shelf); | 
					
						
							| 
									
										
										
										
											2018-09-17 02:34:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         $shelfBookIds = $shelf->books()->get(['id'])->pluck('id'); | 
					
						
							| 
									
										
										
										
											2024-02-08 00:37:36 +08:00
										 |  |  |         $books = $this->bookQueries->visibleForList() | 
					
						
							|  |  |  |             ->whereNotIn('id', $shelfBookIds) | 
					
						
							|  |  |  |             ->orderBy('name') | 
					
						
							|  |  |  |             ->get(['name', 'id', 'slug', 'created_at', 'updated_at']); | 
					
						
							| 
									
										
										
										
											2018-09-17 02:34:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-27 21:55:23 +08:00
										 |  |  |         $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $shelf->getShortName()])); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-02 19:41:41 +08:00
										 |  |  |         return view('shelves.edit', [ | 
					
						
							| 
									
										
										
										
											2019-04-27 21:55:23 +08:00
										 |  |  |             'shelf' => $shelf, | 
					
						
							| 
									
										
										
										
											2018-09-17 02:34:09 +08:00
										 |  |  |             'books' => $books, | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Update the specified bookshelf in storage. | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |      * @throws ValidationException | 
					
						
							|  |  |  |      * @throws ImageUploadException | 
					
						
							|  |  |  |      * @throws NotFoundException | 
					
						
							| 
									
										
										
										
											2018-09-17 02:34:09 +08:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function update(Request $request, string $slug) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-02-06 01:35:49 +08:00
										 |  |  |         $shelf = $this->queries->findVisibleBySlugOrFail($slug); | 
					
						
							| 
									
										
										
										
											2018-09-17 02:34:09 +08:00
										 |  |  |         $this->checkOwnablePermission('bookshelf-update', $shelf); | 
					
						
							| 
									
										
										
										
											2022-06-14 00:20:21 +08:00
										 |  |  |         $validated = $this->validate($request, [ | 
					
						
							| 
									
										
										
										
											2023-12-17 23:02:15 +08:00
										 |  |  |             'name'             => ['required', 'string', 'max:255'], | 
					
						
							|  |  |  |             'description_html' => ['string', 'max:2000'], | 
					
						
							|  |  |  |             'image'            => array_merge(['nullable'], $this->getImageValidationRules()), | 
					
						
							|  |  |  |             'tags'             => ['array'], | 
					
						
							| 
									
										
										
										
											2018-09-17 02:34:09 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-14 00:20:21 +08:00
										 |  |  |         if ($request->has('image_reset')) { | 
					
						
							|  |  |  |             $validated['image'] = null; | 
					
						
							| 
									
										
										
										
											2022-06-20 01:14:53 +08:00
										 |  |  |         } elseif (array_key_exists('image', $validated) && is_null($validated['image'])) { | 
					
						
							| 
									
										
										
										
											2022-06-14 00:20:21 +08:00
										 |  |  |             unset($validated['image']); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         $bookIds = explode(',', $request->get('books', '')); | 
					
						
							| 
									
										
										
										
											2022-07-24 19:23:25 +08:00
										 |  |  |         $shelf = $this->shelfRepo->update($shelf, $validated, $bookIds); | 
					
						
							| 
									
										
										
										
											2018-09-17 02:34:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         return redirect($shelf->getUrl()); | 
					
						
							| 
									
										
										
										
											2018-09-17 02:34:09 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |      * Shows the page to confirm deletion. | 
					
						
							| 
									
										
										
										
											2018-09-17 02:34:09 +08:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function showDelete(string $slug) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-02-06 01:35:49 +08:00
										 |  |  |         $shelf = $this->queries->findVisibleBySlugOrFail($slug); | 
					
						
							| 
									
										
										
										
											2019-04-27 21:55:23 +08:00
										 |  |  |         $this->checkOwnablePermission('bookshelf-delete', $shelf); | 
					
						
							| 
									
										
										
										
											2018-09-17 02:34:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-27 21:55:23 +08:00
										 |  |  |         $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()])); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-27 21:55:23 +08:00
										 |  |  |         return view('shelves.delete', ['shelf' => $shelf]); | 
					
						
							| 
									
										
										
										
											2018-09-17 02:34:09 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Remove the specified bookshelf from storage. | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |      * @throws Exception | 
					
						
							| 
									
										
										
										
											2018-09-17 02:34:09 +08:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function destroy(string $slug) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-02-06 01:35:49 +08:00
										 |  |  |         $shelf = $this->queries->findVisibleBySlugOrFail($slug); | 
					
						
							| 
									
										
										
										
											2019-04-27 21:55:23 +08:00
										 |  |  |         $this->checkOwnablePermission('bookshelf-delete', $shelf); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-24 19:23:25 +08:00
										 |  |  |         $this->shelfRepo->destroy($shelf); | 
					
						
							| 
									
										
										
										
											2019-04-27 21:55:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 02:34:09 +08:00
										 |  |  |         return redirect('/shelves'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-08-27 21:18:09 +08:00
										 |  |  | } |