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