use Activity;
use BookStack\Auth\UserRepo;
use BookStack\Entities\Book;
+use BookStack\Entities\EntityContextManager;
use BookStack\Entities\Repos\EntityRepo;
use BookStack\Entities\ExportService;
use Illuminate\Http\Request;
protected $entityRepo;
protected $userRepo;
protected $exportService;
+ protected $entityContextManager;
/**
* BookController constructor.
* @param EntityRepo $entityRepo
- * @param \BookStack\Auth\UserRepo $userRepo
- * @param \BookStack\Entities\ExportService $exportService
+ * @param UserRepo $userRepo
+ * @param ExportService $exportService
+ * @param EntityContextManager $entityContextManager
*/
- public function __construct(EntityRepo $entityRepo, UserRepo $userRepo, ExportService $exportService)
- {
+ public function __construct(
+ EntityRepo $entityRepo,
+ UserRepo $userRepo,
+ ExportService $exportService,
+ EntityContextManager $entityContextManager
+ ) {
$this->entityRepo = $entityRepo;
$this->userRepo = $userRepo;
$this->exportService = $exportService;
+ $this->entityContextManager = $entityContextManager;
parent::__construct();
}
$popular = $this->entityRepo->getPopular('book', 4, 0);
$new = $this->entityRepo->getRecentlyCreated('book', 4, 0);
+ $this->entityContextManager->clearShelfContext();
+
$this->setPageTitle(trans('entities.books'));
return view('books.index', [
'books' => $books,
/**
* 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());
}
/**
* Display the specified book.
* @param $slug
+ * @param Request $request
* @return Response
+ * @throws \BookStack\Exceptions\NotFoundException
*/
- public function show($slug)
+ public function show($slug, Request $request)
{
$book = $this->entityRepo->getBySlug('book', $slug);
$this->checkOwnablePermission('book-view', $book);
+
$bookChildren = $this->entityRepo->getBookChildren($book);
+
Views::add($book);
+ if ($request->has('shelf')) {
+ $this->entityContextManager->setShelfContext(intval($request->get('shelf')));
+ }
+
$this->setPageTitle($book->getShortName());
return view('books.show', [
'book' => $book,