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