]> BookStack Code Mirror - bookstack/blobdiff - app/Http/Controllers/BookshelfController.php
Show bookshelves that a book belongs to on a book view
[bookstack] / app / Http / Controllers / BookshelfController.php
index e63cfd1d5f624ec4b9693e6372b22aae20c00e51..bef96dd01a6e41c62ec67e97d04437baf760ced4 100644 (file)
@@ -40,9 +40,9 @@ class BookshelfController extends Controller
      */
     public function index()
     {
-        $view = setting()->getUser($this->currentUser, 'bookshelves_view_type', config('app.views.bookshelves', 'grid'));
-        $sort = setting()->getUser($this->currentUser, 'bookshelves_sort', 'name');
-        $order = setting()->getUser($this->currentUser, 'bookshelves_sort_order', 'asc');
+        $view = setting()->getForCurrentUser('bookshelves_view_type', config('app.views.bookshelves', 'grid'));
+        $sort = setting()->getForCurrentUser('bookshelves_sort', 'name');
+        $order = setting()->getForCurrentUser('bookshelves_sort_order', 'asc');
         $sortOptions = [
             'name' => trans('common.sort_name'),
             'created_at' => trans('common.sort_created_at'),
@@ -54,7 +54,7 @@ class BookshelfController extends Controller
             $shelf->books = $this->entityRepo->getBookshelfChildren($shelf);
         }
 
-        $recents = $this->signedIn ? $this->entityRepo->getRecentlyViewed('bookshelf', 4, 0) : false;
+        $recents = $this->isSignedIn() ? $this->entityRepo->getRecentlyViewed('bookshelf', 4, 0) : false;
         $popular = $this->entityRepo->getPopular('bookshelf', 4, 0);
         $new = $this->entityRepo->getRecentlyCreated('bookshelf', 4, 0);
 
@@ -86,8 +86,9 @@ class BookshelfController extends Controller
 
     /**
      * Store a newly created bookshelf in storage.
-     * @param  Request $request
+     * @param Request $request
      * @return Response
+     * @throws \BookStack\Exceptions\ImageUploadException
      */
     public function store(Request $request)
     {
@@ -115,7 +116,7 @@ class BookshelfController extends Controller
     public function show(string $slug)
     {
         /** @var Bookshelf $shelf */
-        $shelf = $this->entityRepo->getBySlug('bookshelf', $slug);
+        $shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug);
         $this->checkOwnablePermission('book-view', $shelf);
 
         $books = $this->entityRepo->getBookshelfChildren($shelf);
@@ -123,6 +124,7 @@ class BookshelfController extends Controller
         $this->entityContextManager->setShelfContext($shelf->id);
 
         $this->setPageTitle($shelf->getShortName());
+
         return view('shelves.show', [
             'shelf' => $shelf,
             'books' => $books,
@@ -138,7 +140,7 @@ class BookshelfController extends Controller
      */
     public function edit(string $slug)
     {
-        $shelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $shelf Bookshelf */
+        $shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug); /** @var $shelf Bookshelf */
         $this->checkOwnablePermission('bookshelf-update', $shelf);
 
         $shelfBooks = $this->entityRepo->getBookshelfChildren($shelf);
@@ -167,7 +169,7 @@ class BookshelfController extends Controller
      */
     public function update(Request $request, string $slug)
     {
-        $shelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
+        $shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
         $this->checkOwnablePermission('bookshelf-update', $shelf);
         $this->validate($request, [
             'name' => 'required|string|max:255',
@@ -175,7 +177,7 @@ class BookshelfController extends Controller
             'image' => $this->imageRepo->getImageValidationRules(),
         ]);
 
-         $shelf = $this->entityRepo->updateFromInput('bookshelf', $shelf, $request->all());
+         $shelf = $this->entityRepo->updateFromInput($shelf, $request->all());
          $this->shelfUpdateActions($shelf, $request);
 
          Activity::add($shelf, 'bookshelf_update');
@@ -192,7 +194,7 @@ class BookshelfController extends Controller
      */
     public function showDelete(string $slug)
     {
-        $shelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $shelf Bookshelf */
+        $shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug); /** @var $shelf Bookshelf */
         $this->checkOwnablePermission('bookshelf-delete', $shelf);
 
         $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()]));
@@ -208,9 +210,9 @@ class BookshelfController extends Controller
      */
     public function destroy(string $slug)
     {
-        $shelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $shelf Bookshelf */
+        $shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug); /** @var $shelf Bookshelf */
         $this->checkOwnablePermission('bookshelf-delete', $shelf);
-        Activity::addMessage('bookshelf_delete', 0, $shelf->name);
+        Activity::addMessage('bookshelf_delete', $shelf->name);
 
         if ($shelf->cover) {
             $this->imageRepo->destroyImage($shelf->cover);
@@ -228,7 +230,7 @@ class BookshelfController extends Controller
      */
     public function showPermissions(string $slug)
     {
-        $shelf = $this->entityRepo->getBySlug('bookshelf', $slug);
+        $shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug);
         $this->checkOwnablePermission('restrictions-manage', $shelf);
 
         $roles = $this->userRepo->getRestrictableRoles();
@@ -240,19 +242,19 @@ class BookshelfController extends Controller
 
     /**
      * Set the permissions for this bookshelf.
-     * @param string $slug
      * @param Request $request
+     * @param string $slug
      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
      * @throws \BookStack\Exceptions\NotFoundException
      * @throws \Throwable
      */
-    public function permissions(string $slug, Request $request)
+    public function permissions(Request $request, string $slug)
     {
-        $shelf = $this->entityRepo->getBySlug('bookshelf', $slug);
+        $shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug);
         $this->checkOwnablePermission('restrictions-manage', $shelf);
 
         $this->entityRepo->updateEntityPermissionsFromRequest($request, $shelf);
-        session()->flash('success', trans('entities.shelves_permissions_updated'));
+        $this->showSuccessNotification( trans('entities.shelves_permissions_updated'));
         return redirect($shelf->getUrl());
     }
 
@@ -264,11 +266,11 @@ class BookshelfController extends Controller
      */
     public function copyPermissions(string $slug)
     {
-        $shelf = $this->entityRepo->getBySlug('bookshelf', $slug);
+        $shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug);
         $this->checkOwnablePermission('restrictions-manage', $shelf);
 
         $updateCount = $this->entityRepo->copyBookshelfPermissions($shelf);
-        session()->flash('success', trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
+        $this->showSuccessNotification( trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
         return redirect($shelf->getUrl());
     }
 
@@ -284,10 +286,18 @@ class BookshelfController extends Controller
         $this->entityRepo->updateShelfBooks($shelf, $request->get('books', ''));
 
         // Update the cover image if in request
-        if ($request->has('image') && userCan('image-create-all')) {
-            $image = $this->imageRepo->saveNew($request->file('image'), 'cover', $shelf->id);
+        if ($request->has('image')) {
+            $newImage = $request->file('image');
+            $this->imageRepo->destroyImage($shelf->cover);
+            $image = $this->imageRepo->saveNew($newImage, 'cover_shelf', $shelf->id, 512, 512, true);
             $shelf->image_id = $image->id;
             $shelf->save();
         }
+
+        if ($request->has('image_reset')) {
+            $this->imageRepo->destroyImage($shelf->cover);
+            $shelf->image_id = 0;
+            $shelf->save();
+        }
     }
 }