diff --git a/app/Auth/Permissions/PermissionService.php b/app/Auth/Permissions/PermissionService.php index 1e1ee3946..7e710edaf 100644 --- a/app/Auth/Permissions/PermissionService.php +++ b/app/Auth/Permissions/PermissionService.php @@ -577,7 +577,7 @@ class PermissionService $query2->where('has_permission_own', '=', 1) ->where('created_by', '=', $userId); }); - }) ; + }); if (!is_null($entityClass)) { $entityInstance = app()->make($entityClass); diff --git a/app/Entities/Bookshelf.php b/app/Entities/Bookshelf.php index 08ce8d8cb..1de767fec 100644 --- a/app/Entities/Bookshelf.php +++ b/app/Entities/Bookshelf.php @@ -26,7 +26,9 @@ class Bookshelf extends Entity */ public function books() { - return $this->belongsToMany(Book::class, 'bookshelves_books', 'bookshelf_id', 'book_id')->orderBy('order', 'asc'); + return $this->belongsToMany(Book::class, 'bookshelves_books', 'bookshelf_id', 'book_id') + ->withPivot('order') + ->orderBy('order', 'asc'); } /** diff --git a/app/Entities/Repos/EntityRepo.php b/app/Entities/Repos/EntityRepo.php index 6fc2689a5..3bf70c327 100644 --- a/app/Entities/Repos/EntityRepo.php +++ b/app/Entities/Repos/EntityRepo.php @@ -577,6 +577,21 @@ class EntityRepo $shelf->books()->sync($syncData); } + /** + * Append a Book to a BookShelf. + * @param Bookshelf $shelf + * @param Book $book + */ + public function appendBookToShelf(Bookshelf $shelf, Book $book) + { + if ($shelf->contains($book)) { + return; + } + + $maxOrder = $shelf->books()->max('order'); + $shelf->books()->attach($book->id, ['order' => $maxOrder + 1]); + } + /** * Change the book that an entity belongs to. * @param string $type diff --git a/app/Http/Controllers/BookController.php b/app/Http/Controllers/BookController.php index 9eb19ce65..24e0d784d 100644 --- a/app/Http/Controllers/BookController.php +++ b/app/Http/Controllers/BookController.php @@ -75,30 +75,55 @@ class BookController extends Controller /** * Show the form for creating a new book. + * @param string $shelfSlug * @return Response + * @throws \BookStack\Exceptions\NotFoundException */ - public function create() + public function create(string $shelfSlug = null) { + $bookshelf = null; + if ($shelfSlug !== null) { + $bookshelf = $this->entityRepo->getBySlug('bookshelf', $shelfSlug); + $this->checkOwnablePermission('bookshelf-update', $bookshelf); + } + $this->checkPermission('book-create-all'); $this->setPageTitle(trans('entities.books_create')); - return view('books.create'); + return view('books.create', [ + 'bookshelf' => $bookshelf + ]); } /** * Store a newly created book in storage. * - * @param Request $request + * @param Request $request + * @param string $shelfSlug * @return Response + * @throws \BookStack\Exceptions\NotFoundException */ - public function store(Request $request) + public function store(Request $request, string $shelfSlug = null) { $this->checkPermission('book-create-all'); $this->validate($request, [ 'name' => 'required|string|max:255', 'description' => 'string|max:1000' ]); + + $bookshelf = null; + if ($shelfSlug !== null) { + $bookshelf = $this->entityRepo->getBySlug('bookshelf', $shelfSlug); + $this->checkOwnablePermission('bookshelf-update', $bookshelf); + } + $book = $this->entityRepo->createFromInput('book', $request->all()); Activity::add($book, 'book_create', $book->id); + + if ($bookshelf) { + $this->entityRepo->appendBookToShelf($bookshelf, $book); + Activity::add($bookshelf, 'bookshelf_update'); + } + return redirect($book->getUrl()); } diff --git a/resources/lang/en/entities.php b/resources/lang/en/entities.php index c2c15d72f..8a3aeb022 100644 --- a/resources/lang/en/entities.php +++ b/resources/lang/en/entities.php @@ -74,6 +74,7 @@ return [ 'shelves_create' => 'Create New Shelf', 'shelves_popular' => 'Popular Shelves', 'shelves_new' => 'New Shelves', + 'shelves_new_action' => 'New Shelf', 'shelves_popular_empty' => 'The most popular shelves will appear here.', 'shelves_new_empty' => 'The most recently created shelves will appear here.', 'shelves_save' => 'Save Shelf', @@ -104,6 +105,7 @@ return [ 'books_popular' => 'Popular Books', 'books_recent' => 'Recent Books', 'books_new' => 'New Books', + 'books_new_action' => 'New Book', 'books_popular_empty' => 'The most popular books will appear here.', 'books_new_empty' => 'The most recently created books will appear here.', 'books_create' => 'Create New Book', diff --git a/resources/views/books/create.blade.php b/resources/views/books/create.blade.php index 9079a3bf9..882ce556a 100644 --- a/resources/views/books/create.blade.php +++ b/resources/views/books/create.blade.php @@ -3,21 +3,31 @@ @section('body')
{{ trans('entities.shelves_empty_contents') }}