BookStack/app/Http/Controllers/BookController.php

227 lines
7.2 KiB
PHP
Raw Normal View History

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