2021-06-26 11:23:15 -04:00
|
|
|
<?php
|
|
|
|
|
2023-05-17 12:56:55 -04:00
|
|
|
namespace BookStack\Entities\Controllers;
|
2015-07-12 15:01:42 -04:00
|
|
|
|
2023-05-17 12:56:55 -04:00
|
|
|
use BookStack\Activity\ActivityQueries;
|
|
|
|
use BookStack\Activity\ActivityType;
|
|
|
|
use BookStack\Activity\Models\View;
|
2020-11-21 19:17:45 -05:00
|
|
|
use BookStack\Entities\Models\Bookshelf;
|
2021-06-26 11:23:15 -04:00
|
|
|
use BookStack\Entities\Repos\BookRepo;
|
|
|
|
use BookStack\Entities\Tools\BookContents;
|
2021-12-19 14:20:31 -05:00
|
|
|
use BookStack\Entities\Tools\Cloner;
|
2022-06-15 10:05:08 -04:00
|
|
|
use BookStack\Entities\Tools\HierarchyTransformer;
|
2020-11-21 18:20:54 -05:00
|
|
|
use BookStack\Entities\Tools\ShelfContext;
|
2019-09-15 18:28:23 -04:00
|
|
|
use BookStack\Exceptions\ImageUploadException;
|
2021-12-19 14:20:31 -05:00
|
|
|
use BookStack\Exceptions\NotFoundException;
|
|
|
|
use BookStack\Facades\Activity;
|
2023-05-18 15:53:39 -04:00
|
|
|
use BookStack\Http\Controller;
|
2022-08-20 07:07:38 -04:00
|
|
|
use BookStack\References\ReferenceFetcher;
|
2022-10-30 11:16:06 -04:00
|
|
|
use BookStack\Util\SimpleListOptions;
|
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-07-12 15:01:42 -04:00
|
|
|
|
|
|
|
class BookController extends Controller
|
|
|
|
{
|
2022-08-20 07:07:38 -04:00
|
|
|
protected BookRepo $bookRepo;
|
|
|
|
protected ShelfContext $shelfContext;
|
|
|
|
protected ReferenceFetcher $referenceFetcher;
|
2015-07-12 15:01:42 -04:00
|
|
|
|
2022-08-20 07:07:38 -04:00
|
|
|
public function __construct(ShelfContext $entityContextManager, BookRepo $bookRepo, ReferenceFetcher $referenceFetcher)
|
2019-10-05 07:55:01 -04:00
|
|
|
{
|
2019-09-15 18:28:23 -04:00
|
|
|
$this->bookRepo = $bookRepo;
|
2022-08-20 07:07:38 -04:00
|
|
|
$this->shelfContext = $entityContextManager;
|
|
|
|
$this->referenceFetcher = $referenceFetcher;
|
2015-07-12 15:01:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a listing of the book.
|
|
|
|
*/
|
2022-10-30 11:16:06 -04:00
|
|
|
public function index(Request $request)
|
2015-07-12 15:01:42 -04:00
|
|
|
{
|
2021-02-07 18:12:05 -05:00
|
|
|
$view = setting()->getForCurrentUser('books_view_type');
|
2022-10-30 11:16:06 -04:00
|
|
|
$listOptions = SimpleListOptions::fromRequest($request, 'books')->withSortOptions([
|
|
|
|
'name' => trans('common.sort_name'),
|
|
|
|
'created_at' => trans('common.sort_created_at'),
|
|
|
|
'updated_at' => trans('common.sort_updated_at'),
|
|
|
|
]);
|
2018-12-07 13:33:32 -05:00
|
|
|
|
2022-10-30 11:16:06 -04:00
|
|
|
$books = $this->bookRepo->getAllPaginated(18, $listOptions->getSort(), $listOptions->getOrder());
|
2019-10-05 07:55:01 -04:00
|
|
|
$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
|
|
|
|
2022-08-20 07:07:38 -04:00
|
|
|
$this->shelfContext->clearShelfContext();
|
2019-04-07 13:28:11 -04:00
|
|
|
|
2017-12-06 11:34:26 -05:00
|
|
|
$this->setPageTitle(trans('entities.books'));
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2019-04-07 07:00:09 -04:00
|
|
|
return view('books.index', [
|
2021-06-26 11:23:15 -04:00
|
|
|
'books' => $books,
|
2017-08-26 08:24:55 -04:00
|
|
|
'recents' => $recents,
|
|
|
|
'popular' => $popular,
|
2021-06-26 11:23:15 -04:00
|
|
|
'new' => $new,
|
|
|
|
'view' => $view,
|
2022-10-30 11:16:06 -04:00
|
|
|
'listOptions' => $listOptions,
|
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'));
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2019-04-02 11:35:46 -04:00
|
|
|
return view('books.create', [
|
2021-06-26 11:23:15 -04:00
|
|
|
'bookshelf' => $bookshelf,
|
2019-04-02 11:35:46 -04:00
|
|
|
]);
|
2015-07-12 15:01:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created book in storage.
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
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');
|
2022-06-25 08:46:55 -04:00
|
|
|
$validated = $this->validate($request, [
|
2021-11-04 20:26:55 -04:00
|
|
|
'name' => ['required', 'string', 'max:255'],
|
|
|
|
'description' => ['string', 'max:1000'],
|
|
|
|
'image' => array_merge(['nullable'], $this->getImageValidationRules()),
|
2022-06-25 08:46:55 -04:00
|
|
|
'tags' => ['array'],
|
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);
|
|
|
|
}
|
|
|
|
|
2022-06-25 08:46:55 -04:00
|
|
|
$book = $this->bookRepo->create($validated);
|
2019-04-02 11:35:46 -04:00
|
|
|
|
|
|
|
if ($bookshelf) {
|
2019-09-19 13:20:09 -04:00
|
|
|
$bookshelf->appendBook($book);
|
2021-12-11 12:29:33 -05:00
|
|
|
Activity::add(ActivityType::BOOKSHELF_UPDATE, $bookshelf);
|
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.
|
|
|
|
*/
|
2021-12-11 12:29:33 -05:00
|
|
|
public function show(Request $request, ActivityQueries $activities, 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);
|
2021-11-22 18:33:55 -05:00
|
|
|
$bookParentShelves = $book->shelves()->scopes('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')) {
|
2022-08-20 07:07:38 -04:00
|
|
|
$this->shelfContext->setShelfContext(intval($request->get('shelf')));
|
2019-04-07 13:28:11 -04:00
|
|
|
}
|
|
|
|
|
2015-12-05 09:41:51 -05:00
|
|
|
$this->setPageTitle($book->getShortName());
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2019-04-07 07:00:09 -04:00
|
|
|
return view('books.show', [
|
2021-06-26 11:23:15 -04:00
|
|
|
'book' => $book,
|
|
|
|
'current' => $book,
|
|
|
|
'bookChildren' => $bookChildren,
|
2019-09-26 19:45:10 -04:00
|
|
|
'bookParentShelves' => $bookParentShelves,
|
2021-12-11 12:29:33 -05:00
|
|
|
'activity' => $activities->entityActivity($book, 20, 1),
|
2022-08-20 07:07:38 -04:00
|
|
|
'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($book),
|
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);
|
2022-09-17 20:25:20 -04:00
|
|
|
$this->setPageTitle(trans('entities.books_edit_named', ['bookName' => $book->getShortName()]));
|
2021-06-26 11:23:15 -04:00
|
|
|
|
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.
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
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);
|
2022-06-13 12:20:21 -04:00
|
|
|
|
|
|
|
$validated = $this->validate($request, [
|
2021-11-04 20:26:55 -04:00
|
|
|
'name' => ['required', 'string', 'max:255'],
|
|
|
|
'description' => ['string', 'max:1000'],
|
|
|
|
'image' => array_merge(['nullable'], $this->getImageValidationRules()),
|
2022-06-25 08:46:55 -04:00
|
|
|
'tags' => ['array'],
|
2015-07-12 15:01:42 -04:00
|
|
|
]);
|
2019-05-04 10:48:15 -04:00
|
|
|
|
2022-06-13 12:20:21 -04:00
|
|
|
if ($request->has('image_reset')) {
|
|
|
|
$validated['image'] = null;
|
2022-06-19 13:14:53 -04:00
|
|
|
} elseif (array_key_exists('image', $validated) && is_null($validated['image'])) {
|
2022-06-13 12:20:21 -04:00
|
|
|
unset($validated['image']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$book = $this->bookRepo->update($book, $validated);
|
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()]));
|
2021-06-26 11:23:15 -04:00
|
|
|
|
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.
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
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
|
|
|
|
2021-12-19 14:20:31 -05:00
|
|
|
/**
|
|
|
|
* Show the view to copy a book.
|
|
|
|
*
|
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
public function showCopy(string $bookSlug)
|
|
|
|
{
|
|
|
|
$book = $this->bookRepo->getBySlug($bookSlug);
|
|
|
|
$this->checkOwnablePermission('book-view', $book);
|
|
|
|
|
|
|
|
session()->flashInput(['name' => $book->name]);
|
|
|
|
|
|
|
|
return view('books.copy', [
|
|
|
|
'book' => $book,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a copy of a book within the requested target destination.
|
|
|
|
*
|
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
public function copy(Request $request, Cloner $cloner, string $bookSlug)
|
|
|
|
{
|
|
|
|
$book = $this->bookRepo->getBySlug($bookSlug);
|
|
|
|
$this->checkOwnablePermission('book-view', $book);
|
|
|
|
$this->checkPermission('book-create-all');
|
|
|
|
|
|
|
|
$newName = $request->get('name') ?: $book->name;
|
|
|
|
$bookCopy = $cloner->cloneBook($book, $newName);
|
|
|
|
$this->showSuccessNotification(trans('entities.books_copy_success'));
|
|
|
|
|
|
|
|
return redirect($bookCopy->getUrl());
|
|
|
|
}
|
2022-06-15 10:05:08 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the chapter to a book.
|
|
|
|
*/
|
|
|
|
public function convertToShelf(HierarchyTransformer $transformer, string $bookSlug)
|
|
|
|
{
|
|
|
|
$book = $this->bookRepo->getBySlug($bookSlug);
|
|
|
|
$this->checkOwnablePermission('book-update', $book);
|
|
|
|
$this->checkOwnablePermission('book-delete', $book);
|
|
|
|
$this->checkPermission('bookshelf-create-all');
|
|
|
|
$this->checkPermission('book-create-all');
|
|
|
|
|
|
|
|
$shelf = $transformer->transformBookToShelf($book);
|
|
|
|
|
|
|
|
return redirect($shelf->getUrl());
|
|
|
|
}
|
2015-07-12 15:01:42 -04:00
|
|
|
}
|