2021-06-26 23:23:15 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Http\Controllers;
|
2015-07-13 03:01:42 +08:00
|
|
|
|
2015-08-17 01:59:23 +08:00
|
|
|
use Activity;
|
2020-11-08 06:37:27 +08:00
|
|
|
use BookStack\Actions\ActivityType;
|
2021-05-16 17:49:37 +08:00
|
|
|
use BookStack\Actions\View;
|
2020-11-22 08:17:45 +08:00
|
|
|
use BookStack\Entities\Models\Bookshelf;
|
2021-06-26 23:23:15 +08:00
|
|
|
use BookStack\Entities\Repos\BookRepo;
|
|
|
|
use BookStack\Entities\Tools\BookContents;
|
2021-01-02 01:49:48 +08:00
|
|
|
use BookStack\Entities\Tools\PermissionsUpdater;
|
2020-11-22 07:20:54 +08:00
|
|
|
use BookStack\Entities\Tools\ShelfContext;
|
2019-09-16 06:28:23 +08:00
|
|
|
use BookStack\Exceptions\ImageUploadException;
|
2015-07-13 03:01:42 +08:00
|
|
|
use Illuminate\Http\Request;
|
2019-09-16 06:28:23 +08:00
|
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
use Throwable;
|
2015-07-13 03:01:42 +08:00
|
|
|
|
|
|
|
class BookController extends Controller
|
|
|
|
{
|
2019-09-16 06:28:23 +08:00
|
|
|
protected $bookRepo;
|
2019-04-08 01:28:11 +08:00
|
|
|
protected $entityContextManager;
|
2015-07-13 03:01:42 +08:00
|
|
|
|
2020-11-22 07:20:54 +08:00
|
|
|
public function __construct(ShelfContext $entityContextManager, BookRepo $bookRepo)
|
2019-10-05 19:55:01 +08:00
|
|
|
{
|
2019-09-16 06:28:23 +08:00
|
|
|
$this->bookRepo = $bookRepo;
|
2019-04-08 01:28:11 +08:00
|
|
|
$this->entityContextManager = $entityContextManager;
|
2015-07-13 03:01:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a listing of the book.
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
2021-02-08 07:12:05 +08:00
|
|
|
$view = setting()->getForCurrentUser('books_view_type');
|
2019-09-20 07:18:28 +08:00
|
|
|
$sort = setting()->getForCurrentUser('books_sort', 'name');
|
|
|
|
$order = setting()->getForCurrentUser('books_sort_order', 'asc');
|
2018-12-08 02:33:32 +08:00
|
|
|
|
2019-10-05 19:55:01 +08:00
|
|
|
$books = $this->bookRepo->getAllPaginated(18, $sort, $order);
|
|
|
|
$recents = $this->isSignedIn() ? $this->bookRepo->getRecentlyViewed(4) : false;
|
|
|
|
$popular = $this->bookRepo->getPopular(4);
|
|
|
|
$new = $this->bookRepo->getRecentlyCreated(4);
|
2018-12-08 02:33:32 +08:00
|
|
|
|
2019-04-08 01:28:11 +08:00
|
|
|
$this->entityContextManager->clearShelfContext();
|
|
|
|
|
2017-12-07 00:34:26 +08:00
|
|
|
$this->setPageTitle(trans('entities.books'));
|
2021-06-26 23:23:15 +08:00
|
|
|
|
2019-04-07 19:00:09 +08:00
|
|
|
return view('books.index', [
|
2021-06-26 23:23:15 +08:00
|
|
|
'books' => $books,
|
2017-08-26 20:24:55 +08:00
|
|
|
'recents' => $recents,
|
|
|
|
'popular' => $popular,
|
2021-06-26 23:23:15 +08:00
|
|
|
'new' => $new,
|
|
|
|
'view' => $view,
|
|
|
|
'sort' => $sort,
|
|
|
|
'order' => $order,
|
2017-08-26 20:24:55 +08:00
|
|
|
]);
|
2015-07-13 03:01:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new book.
|
|
|
|
*/
|
2019-04-16 03:43:25 +08:00
|
|
|
public function create(string $shelfSlug = null)
|
2015-07-13 03:01:42 +08:00
|
|
|
{
|
2019-10-05 19:55:01 +08:00
|
|
|
$this->checkPermission('book-create-all');
|
|
|
|
|
2019-04-16 03:43:25 +08:00
|
|
|
$bookshelf = null;
|
2019-04-02 23:35:46 +08:00
|
|
|
if ($shelfSlug !== null) {
|
2019-10-05 19:55:01 +08:00
|
|
|
$bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
|
2019-04-02 23:35:46 +08:00
|
|
|
$this->checkOwnablePermission('bookshelf-update', $bookshelf);
|
|
|
|
}
|
|
|
|
|
2016-12-05 00:51:39 +08:00
|
|
|
$this->setPageTitle(trans('entities.books_create'));
|
2021-06-26 23:23:15 +08:00
|
|
|
|
2019-04-02 23:35:46 +08:00
|
|
|
return view('books.create', [
|
2021-06-26 23:23:15 +08:00
|
|
|
'bookshelf' => $bookshelf,
|
2019-04-02 23:35:46 +08:00
|
|
|
]);
|
2015-07-13 03:01:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created book in storage.
|
2021-06-26 23:23:15 +08:00
|
|
|
*
|
2019-09-16 06:28:23 +08:00
|
|
|
* @throws ImageUploadException
|
|
|
|
* @throws ValidationException
|
2015-07-13 03:01:42 +08:00
|
|
|
*/
|
2019-04-16 03:43:25 +08:00
|
|
|
public function store(Request $request, string $shelfSlug = null)
|
2015-07-13 03:01:42 +08:00
|
|
|
{
|
2016-02-28 03:24:42 +08:00
|
|
|
$this->checkPermission('book-create-all');
|
2015-07-13 03:01:42 +08:00
|
|
|
$this->validate($request, [
|
2021-06-26 23:23:15 +08:00
|
|
|
'name' => 'required|string|max:255',
|
2019-05-04 22:48:15 +08:00
|
|
|
'description' => 'string|max:1000',
|
2021-06-26 23:23:15 +08:00
|
|
|
'image' => 'nullable|' . $this->getImageValidationRules(),
|
2015-07-13 03:01:42 +08:00
|
|
|
]);
|
2019-04-16 03:43:25 +08:00
|
|
|
|
|
|
|
$bookshelf = null;
|
|
|
|
if ($shelfSlug !== null) {
|
2019-10-05 19:55:01 +08:00
|
|
|
$bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
|
2019-04-16 03:43:25 +08:00
|
|
|
$this->checkOwnablePermission('bookshelf-update', $bookshelf);
|
|
|
|
}
|
|
|
|
|
2019-10-05 19:55:01 +08:00
|
|
|
$book = $this->bookRepo->create($request->all());
|
|
|
|
$this->bookRepo->updateCoverImage($book, $request->file('image', null));
|
2019-04-02 23:35:46 +08:00
|
|
|
|
|
|
|
if ($bookshelf) {
|
2019-09-20 01:20:09 +08:00
|
|
|
$bookshelf->appendBook($book);
|
2020-11-08 07:15:13 +08:00
|
|
|
Activity::addForEntity($bookshelf, ActivityType::BOOKSHELF_UPDATE);
|
2019-04-02 23:35:46 +08:00
|
|
|
}
|
|
|
|
|
2015-09-03 01:26:33 +08:00
|
|
|
return redirect($book->getUrl());
|
2015-07-13 03:01:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified book.
|
|
|
|
*/
|
2019-09-16 01:53:30 +08:00
|
|
|
public function show(Request $request, string $slug)
|
2015-07-13 03:01:42 +08:00
|
|
|
{
|
2019-09-16 06:28:23 +08:00
|
|
|
$book = $this->bookRepo->getBySlug($slug);
|
2019-10-05 19:55:01 +08:00
|
|
|
$bookChildren = (new BookContents($book))->getTree(true);
|
2020-04-10 00:29:22 +08:00
|
|
|
$bookParentShelves = $book->shelves()->visible()->get();
|
2019-04-08 01:28:11 +08:00
|
|
|
|
2021-05-16 17:49:37 +08:00
|
|
|
View::incrementFor($book);
|
2019-04-08 01:28:11 +08:00
|
|
|
if ($request->has('shelf')) {
|
|
|
|
$this->entityContextManager->setShelfContext(intval($request->get('shelf')));
|
|
|
|
}
|
|
|
|
|
2015-12-05 22:41:51 +08:00
|
|
|
$this->setPageTitle($book->getShortName());
|
2021-06-26 23:23:15 +08:00
|
|
|
|
2019-04-07 19:00:09 +08:00
|
|
|
return view('books.show', [
|
2021-06-26 23:23:15 +08:00
|
|
|
'book' => $book,
|
|
|
|
'current' => $book,
|
|
|
|
'bookChildren' => $bookChildren,
|
2019-09-27 07:45:10 +08:00
|
|
|
'bookParentShelves' => $bookParentShelves,
|
2021-06-26 23:23:15 +08:00
|
|
|
'activity' => Activity::entityActivity($book, 20, 1),
|
2017-08-20 20:57:25 +08:00
|
|
|
]);
|
2015-07-13 03:01:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified book.
|
|
|
|
*/
|
2019-09-16 06:28:23 +08:00
|
|
|
public function edit(string $slug)
|
2015-07-13 03:01:42 +08:00
|
|
|
{
|
2019-09-16 06:28:23 +08:00
|
|
|
$book = $this->bookRepo->getBySlug($slug);
|
2016-02-28 03:24:42 +08:00
|
|
|
$this->checkOwnablePermission('book-update', $book);
|
2018-01-29 00:58:52 +08:00
|
|
|
$this->setPageTitle(trans('entities.books_edit_named', ['bookName'=>$book->getShortName()]));
|
2021-06-26 23:23:15 +08:00
|
|
|
|
2019-04-07 19:00:09 +08:00
|
|
|
return view('books.edit', ['book' => $book, 'current' => $book]);
|
2015-07-13 03:01:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified book in storage.
|
2021-06-26 23:23:15 +08:00
|
|
|
*
|
2019-09-16 06:28:23 +08:00
|
|
|
* @throws ImageUploadException
|
|
|
|
* @throws ValidationException
|
2019-09-20 01:03:17 +08:00
|
|
|
* @throws Throwable
|
2015-07-13 03:01:42 +08:00
|
|
|
*/
|
2019-05-04 22:48:15 +08:00
|
|
|
public function update(Request $request, string $slug)
|
2015-07-13 03:01:42 +08:00
|
|
|
{
|
2019-09-16 06:28:23 +08:00
|
|
|
$book = $this->bookRepo->getBySlug($slug);
|
2016-02-28 03:24:42 +08:00
|
|
|
$this->checkOwnablePermission('book-update', $book);
|
2015-07-13 03:01:42 +08:00
|
|
|
$this->validate($request, [
|
2021-06-26 23:23:15 +08:00
|
|
|
'name' => 'required|string|max:255',
|
2019-05-04 22:48:15 +08:00
|
|
|
'description' => 'string|max:1000',
|
2021-06-26 23:23:15 +08:00
|
|
|
'image' => 'nullable|' . $this->getImageValidationRules(),
|
2015-07-13 03:01:42 +08:00
|
|
|
]);
|
2019-05-04 22:48:15 +08:00
|
|
|
|
2019-10-05 19:55:01 +08:00
|
|
|
$book = $this->bookRepo->update($book, $request->all());
|
|
|
|
$resetCover = $request->has('image_reset');
|
|
|
|
$this->bookRepo->updateCoverImage($book, $request->file('image', null), $resetCover);
|
2019-05-04 22:48:15 +08:00
|
|
|
|
2019-10-05 19:55:01 +08:00
|
|
|
return redirect($book->getUrl());
|
2015-07-13 03:01:42 +08:00
|
|
|
}
|
|
|
|
|
2015-07-29 03:57:13 +08:00
|
|
|
/**
|
2019-10-05 19:55:01 +08:00
|
|
|
* Shows the page to confirm deletion.
|
2015-07-29 03:57:13 +08:00
|
|
|
*/
|
2019-09-16 06:28:23 +08:00
|
|
|
public function showDelete(string $bookSlug)
|
2015-07-29 03:57:13 +08:00
|
|
|
{
|
2019-09-16 06:28:23 +08:00
|
|
|
$book = $this->bookRepo->getBySlug($bookSlug);
|
2016-02-28 03:24:42 +08:00
|
|
|
$this->checkOwnablePermission('book-delete', $book);
|
2019-09-16 06:28:23 +08:00
|
|
|
$this->setPageTitle(trans('entities.books_delete_named', ['bookName' => $book->getShortName()]));
|
2021-06-26 23:23:15 +08:00
|
|
|
|
2019-04-07 19:00:09 +08:00
|
|
|
return view('books.delete', ['book' => $book, 'current' => $book]);
|
2015-07-29 03:57:13 +08:00
|
|
|
}
|
|
|
|
|
2015-09-06 21:35:53 +08:00
|
|
|
/**
|
2019-10-05 19:55:01 +08:00
|
|
|
* Remove the specified book from the system.
|
2021-06-26 23:23:15 +08:00
|
|
|
*
|
2019-09-16 06:28:23 +08:00
|
|
|
* @throws Throwable
|
2015-07-13 03:01:42 +08:00
|
|
|
*/
|
2019-09-16 06:28:23 +08:00
|
|
|
public function destroy(string $bookSlug)
|
2015-07-13 03:01:42 +08:00
|
|
|
{
|
2019-09-16 06:28:23 +08:00
|
|
|
$book = $this->bookRepo->getBySlug($bookSlug);
|
2016-02-28 03:24:42 +08:00
|
|
|
$this->checkOwnablePermission('book-delete', $book);
|
2019-05-04 22:48:15 +08:00
|
|
|
|
2019-10-05 19:55:01 +08:00
|
|
|
$this->bookRepo->destroy($book);
|
2019-05-04 22:48:15 +08:00
|
|
|
|
2015-07-13 03:01:42 +08:00
|
|
|
return redirect('/books');
|
|
|
|
}
|
2016-02-28 18:49:41 +08:00
|
|
|
|
|
|
|
/**
|
2019-10-05 19:55:01 +08:00
|
|
|
* Show the permissions view.
|
2016-02-28 18:49:41 +08:00
|
|
|
*/
|
2019-09-16 06:28:23 +08:00
|
|
|
public function showPermissions(string $bookSlug)
|
2016-02-28 18:49:41 +08:00
|
|
|
{
|
2019-09-16 06:28:23 +08:00
|
|
|
$book = $this->bookRepo->getBySlug($bookSlug);
|
2016-02-28 18:49:41 +08:00
|
|
|
$this->checkOwnablePermission('restrictions-manage', $book);
|
2019-10-05 19:55:01 +08:00
|
|
|
|
2019-02-01 04:37:12 +08:00
|
|
|
return view('books.permissions', [
|
2016-02-28 18:49:41 +08:00
|
|
|
'book' => $book,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the restrictions for this book.
|
2021-06-26 23:23:15 +08:00
|
|
|
*
|
2019-09-16 06:28:23 +08:00
|
|
|
* @throws Throwable
|
2016-02-28 18:49:41 +08:00
|
|
|
*/
|
2021-01-02 01:49:48 +08:00
|
|
|
public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug)
|
2016-02-28 18:49:41 +08:00
|
|
|
{
|
2019-09-16 06:28:23 +08:00
|
|
|
$book = $this->bookRepo->getBySlug($bookSlug);
|
2016-02-28 18:49:41 +08:00
|
|
|
$this->checkOwnablePermission('restrictions-manage', $book);
|
2017-02-26 21:26:51 +08:00
|
|
|
|
2021-01-02 01:49:48 +08:00
|
|
|
$permissionsUpdater->updateFromPermissionsForm($book, $request);
|
2019-05-04 22:48:15 +08:00
|
|
|
|
2019-10-05 19:55:01 +08:00
|
|
|
$this->showSuccessNotification(trans('entities.books_permissions_updated'));
|
2021-06-26 23:23:15 +08:00
|
|
|
|
2019-10-05 19:55:01 +08:00
|
|
|
return redirect($book->getUrl());
|
2019-05-04 22:48:15 +08:00
|
|
|
}
|
2015-07-13 03:01:42 +08:00
|
|
|
}
|