]> BookStack Code Mirror - bookstack/blobdiff - app/Http/Controllers/BookController.php
Show bookshelves that a book belongs to on a book view
[bookstack] / app / Http / Controllers / BookController.php
index 1f9caf75641aa6c885b975946f1dcd51ec3d91f2..35f62012ab36a3a40c72813817cc7fb19c0ce101 100644 (file)
@@ -3,10 +3,9 @@
 use Activity;
 use BookStack\Auth\UserRepo;
 use BookStack\Entities\Book;
+use BookStack\Entities\Bookshelf;
 use BookStack\Entities\EntityContextManager;
 use BookStack\Entities\Repos\BookRepo;
-use BookStack\Entities\Repos\EntityRepo;
-use BookStack\Entities\ExportService;
 use BookStack\Exceptions\ImageUploadException;
 use BookStack\Exceptions\NotFoundException;
 use BookStack\Exceptions\NotifyException;
@@ -55,12 +54,12 @@ class BookController extends Controller
      */
     public function index()
     {
-        $view = setting()->getUser($this->currentUser, 'books_view_type', config('app.views.books'));
-        $sort = setting()->getUser($this->currentUser, 'books_sort', 'name');
-        $order = setting()->getUser($this->currentUser, 'books_sort_order', 'asc');
+        $view = setting()->getForCurrentUser('books_view_type', config('app.views.books'));
+        $sort = setting()->getForCurrentUser('books_sort', 'name');
+        $order = setting()->getForCurrentUser('books_sort_order', 'asc');
 
         $books = $this->bookRepo->getAllPaginated('book', 18, $sort, $order);
-        $recents = $this->signedIn ? $this->bookRepo->getRecentlyViewed('book', 4, 0) : false;
+        $recents = $this->isSignedIn() ? $this->bookRepo->getRecentlyViewed('book', 4, 0) : false;
         $popular = $this->bookRepo->getPopular('book', 4, 0);
         $new = $this->bookRepo->getRecentlyCreated('book', 4, 0);
 
@@ -108,7 +107,6 @@ class BookController extends Controller
      * @throws NotFoundException
      * @throws ImageUploadException
      * @throws ValidationException
-     * @throws Throwable
      */
     public function store(Request $request, string $shelfSlug = null)
     {
@@ -121,16 +119,18 @@ class BookController extends Controller
 
         $bookshelf = null;
         if ($shelfSlug !== null) {
+            /** @var Bookshelf $bookshelf */
             $bookshelf = $this->bookRepo->getEntityBySlug('bookshelf', $shelfSlug);
             $this->checkOwnablePermission('bookshelf-update', $bookshelf);
         }
 
+        /** @var Book $book */
         $book = $this->bookRepo->createFromInput('book', $request->all());
         $this->bookUpdateActions($book, $request);
         Activity::add($book, 'book_create', $book->id);
 
         if ($bookshelf) {
-            $this->bookRepo->appendBookToShelf($bookshelf, $book);
+            $bookshelf->appendBook($book);
             Activity::add($bookshelf, 'bookshelf_update');
         }
 
@@ -150,6 +150,7 @@ class BookController extends Controller
         $this->checkOwnablePermission('book-view', $book);
 
         $bookChildren = $this->bookRepo->getBookChildren($book);
+        $bookParentShelves = $this->bookRepo->getBookParentShelves($book);
 
         Views::add($book);
         if ($request->has('shelf')) {
@@ -161,6 +162,7 @@ class BookController extends Controller
             'book' => $book,
             'current' => $book,
             'bookChildren' => $bookChildren,
+            'bookParentShelves' => $bookParentShelves,
             'activity' => Activity::entityActivity($book, 20, 1)
         ]);
     }
@@ -199,7 +201,7 @@ class BookController extends Controller
             'image' => $this->imageRepo->getImageValidationRules(),
         ]);
 
-         $book = $this->bookRepo->updateFromInput('book', $book, $request->all());
+         $book = $this->bookRepo->updateFromInput($book, $request->all());
          $this->bookUpdateActions($book, $request);
 
          Activity::add($book, 'book_update', $book->id);
@@ -245,7 +247,7 @@ class BookController extends Controller
      * @return Factory|View
      * @throws NotFoundException
      */
-    public function getSortItem(string $bookSlug)
+    public function sortItem(string $bookSlug)
     {
         $book = $this->bookRepo->getBySlug($bookSlug);
         $bookChildren = $this->bookRepo->getBookChildren($book);
@@ -285,10 +287,12 @@ class BookController extends Controller
         // Get the books involved in the sort
         $bookIdsInvolved = $bookIdsInvolved->unique()->toArray();
         $booksInvolved = $this->bookRepo->getManyById('book', $bookIdsInvolved, false, true);
+
         // Throw permission error if invalid ids or inaccessible books given.
         if (count($bookIdsInvolved) !== count($booksInvolved)) {
             $this->showPermissionError();
         }
+
         // Check permissions of involved books
         $booksInvolved->each(function (Book $book) {
              $this->checkOwnablePermission('book-update', $book);
@@ -303,7 +307,7 @@ class BookController extends Controller
             $chapterChanged = ($mapItem->type === 'page') && intval($model->chapter_id) !== $mapItem->parentChapter;
 
             if ($bookChanged) {
-                $this->bookRepo->changeBook($mapItem->type, $mapItem->book, $model);
+                $this->bookRepo->changeBook($model, $mapItem->book);
             }
             if ($chapterChanged) {
                 $model->chapter_id = intval($mapItem->parentChapter);
@@ -317,7 +321,7 @@ class BookController extends Controller
 
         // Rebuild permissions and add activity for involved books.
         $booksInvolved->each(function (Book $book) {
-            $this->bookRepo->buildJointPermissionsForBook($book);
+            $book->rebuildPermissions();
             Activity::add($book, 'book_sort', $book->id);
         });