2021-06-26 23:23:15 +08:00
|
|
|
<?php
|
|
|
|
|
2023-05-18 00:56:55 +08:00
|
|
|
namespace BookStack\Entities\Controllers;
|
2015-07-13 03:01:42 +08:00
|
|
|
|
2023-05-18 00:56:55 +08:00
|
|
|
use BookStack\Activity\ActivityQueries;
|
|
|
|
use BookStack\Activity\ActivityType;
|
|
|
|
use BookStack\Activity\Models\View;
|
2023-08-09 21:53:31 +08:00
|
|
|
use BookStack\Activity\Tools\UserEntityWatchOptions;
|
2024-02-05 01:35:16 +08:00
|
|
|
use BookStack\Entities\Queries\BookQueries;
|
2024-02-08 05:58:27 +08:00
|
|
|
use BookStack\Entities\Queries\BookshelfQueries;
|
2021-06-26 23:23:15 +08:00
|
|
|
use BookStack\Entities\Repos\BookRepo;
|
|
|
|
use BookStack\Entities\Tools\BookContents;
|
2021-12-20 03:20:31 +08:00
|
|
|
use BookStack\Entities\Tools\Cloner;
|
2022-06-15 22:05:08 +08:00
|
|
|
use BookStack\Entities\Tools\HierarchyTransformer;
|
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;
|
2021-12-20 03:20:31 +08:00
|
|
|
use BookStack\Exceptions\NotFoundException;
|
|
|
|
use BookStack\Facades\Activity;
|
2023-05-19 03:53:39 +08:00
|
|
|
use BookStack\Http\Controller;
|
2022-08-20 19:07:38 +08:00
|
|
|
use BookStack\References\ReferenceFetcher;
|
2022-10-30 23:16:06 +08:00
|
|
|
use BookStack\Util\SimpleListOptions;
|
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
|
|
|
|
{
|
2023-12-11 20:33:20 +08:00
|
|
|
public function __construct(
|
|
|
|
protected ShelfContext $shelfContext,
|
|
|
|
protected BookRepo $bookRepo,
|
2024-02-05 01:35:16 +08:00
|
|
|
protected BookQueries $queries,
|
2024-02-08 05:58:27 +08:00
|
|
|
protected BookshelfQueries $shelfQueries,
|
2024-02-05 01:35:16 +08:00
|
|
|
protected ReferenceFetcher $referenceFetcher,
|
2023-12-11 20:33:20 +08:00
|
|
|
) {
|
2015-07-13 03:01:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a listing of the book.
|
|
|
|
*/
|
2022-10-30 23:16:06 +08:00
|
|
|
public function index(Request $request)
|
2015-07-13 03:01:42 +08:00
|
|
|
{
|
2021-02-08 07:12:05 +08:00
|
|
|
$view = setting()->getForCurrentUser('books_view_type');
|
2022-10-30 23:16:06 +08: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-08 02:33:32 +08:00
|
|
|
|
2024-02-05 01:35:16 +08:00
|
|
|
$books = $this->queries->visibleForListWithCover()
|
|
|
|
->orderBy($listOptions->getSort(), $listOptions->getOrder())
|
|
|
|
->paginate(18);
|
|
|
|
$recents = $this->isSignedIn() ? $this->queries->recentlyViewedForCurrentUser()->take(4)->get() : false;
|
|
|
|
$popular = $this->queries->popularForList()->take(4)->get();
|
|
|
|
$new = $this->queries->visibleForList()->orderBy('created_at', 'desc')->take(4)->get();
|
2018-12-08 02:33:32 +08:00
|
|
|
|
2022-08-20 19:07:38 +08:00
|
|
|
$this->shelfContext->clearShelfContext();
|
2019-04-08 01:28:11 +08:00
|
|
|
|
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,
|
2022-10-30 23:16:06 +08:00
|
|
|
'listOptions' => $listOptions,
|
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) {
|
2024-02-08 05:58:27 +08:00
|
|
|
$bookshelf = $this->shelfQueries->findVisibleBySlugOrFail($shelfSlug);
|
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');
|
2022-06-25 20:46:55 +08:00
|
|
|
$validated = $this->validate($request, [
|
2023-12-12 20:14:00 +08:00
|
|
|
'name' => ['required', 'string', 'max:255'],
|
2023-12-17 23:02:15 +08:00
|
|
|
'description_html' => ['string', 'max:2000'],
|
2023-12-12 20:14:00 +08:00
|
|
|
'image' => array_merge(['nullable'], $this->getImageValidationRules()),
|
|
|
|
'tags' => ['array'],
|
|
|
|
'default_template_id' => ['nullable', 'integer'],
|
2015-07-13 03:01:42 +08:00
|
|
|
]);
|
2019-04-16 03:43:25 +08:00
|
|
|
|
|
|
|
$bookshelf = null;
|
|
|
|
if ($shelfSlug !== null) {
|
2024-02-08 05:58:27 +08:00
|
|
|
$bookshelf = $this->shelfQueries->findVisibleBySlugOrFail($shelfSlug);
|
2019-04-16 03:43:25 +08:00
|
|
|
$this->checkOwnablePermission('bookshelf-update', $bookshelf);
|
|
|
|
}
|
|
|
|
|
2022-06-25 20:46:55 +08:00
|
|
|
$book = $this->bookRepo->create($validated);
|
2019-04-02 23:35:46 +08:00
|
|
|
|
|
|
|
if ($bookshelf) {
|
2019-09-20 01:20:09 +08:00
|
|
|
$bookshelf->appendBook($book);
|
2021-12-12 01:29:33 +08:00
|
|
|
Activity::add(ActivityType::BOOKSHELF_UPDATE, $bookshelf);
|
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.
|
|
|
|
*/
|
2021-12-12 01:29:33 +08:00
|
|
|
public function show(Request $request, ActivityQueries $activities, string $slug)
|
2015-07-13 03:01:42 +08:00
|
|
|
{
|
2024-02-06 01:35:49 +08:00
|
|
|
$book = $this->queries->findVisibleBySlugOrFail($slug);
|
2019-10-05 19:55:01 +08:00
|
|
|
$bookChildren = (new BookContents($book))->getTree(true);
|
2021-11-23 07:33:55 +08:00
|
|
|
$bookParentShelves = $book->shelves()->scopes('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')) {
|
2022-08-20 19:07:38 +08:00
|
|
|
$this->shelfContext->setShelfContext(intval($request->get('shelf')));
|
2019-04-08 01:28:11 +08:00
|
|
|
}
|
|
|
|
|
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,
|
2023-08-09 21:53:31 +08:00
|
|
|
'watchOptions' => new UserEntityWatchOptions(user(), $book),
|
2021-12-12 01:29:33 +08:00
|
|
|
'activity' => $activities->entityActivity($book, 20, 1),
|
2023-12-19 00:23:40 +08:00
|
|
|
'referenceCount' => $this->referenceFetcher->getReferenceCountToEntity($book),
|
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
|
|
|
{
|
2024-02-06 01:35:49 +08:00
|
|
|
$book = $this->queries->findVisibleBySlugOrFail($slug);
|
2016-02-28 03:24:42 +08:00
|
|
|
$this->checkOwnablePermission('book-update', $book);
|
2022-09-18 08:25:20 +08:00
|
|
|
$this->setPageTitle(trans('entities.books_edit_named', ['bookName' => $book->getShortName()]));
|
2021-06-26 23:23:15 +08:00
|
|
|
|
2023-12-11 20:33:20 +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
|
|
|
{
|
2024-02-06 01:35:49 +08:00
|
|
|
$book = $this->queries->findVisibleBySlugOrFail($slug);
|
2016-02-28 03:24:42 +08:00
|
|
|
$this->checkOwnablePermission('book-update', $book);
|
2022-06-14 00:20:21 +08:00
|
|
|
|
|
|
|
$validated = $this->validate($request, [
|
2023-12-12 20:14:00 +08:00
|
|
|
'name' => ['required', 'string', 'max:255'],
|
2023-12-17 23:02:15 +08:00
|
|
|
'description_html' => ['string', 'max:2000'],
|
2023-12-12 20:14:00 +08:00
|
|
|
'image' => array_merge(['nullable'], $this->getImageValidationRules()),
|
|
|
|
'tags' => ['array'],
|
|
|
|
'default_template_id' => ['nullable', 'integer'],
|
2015-07-13 03:01:42 +08:00
|
|
|
]);
|
2019-05-04 22:48:15 +08:00
|
|
|
|
2022-06-14 00:20:21 +08:00
|
|
|
if ($request->has('image_reset')) {
|
|
|
|
$validated['image'] = null;
|
2022-06-20 01:14:53 +08:00
|
|
|
} elseif (array_key_exists('image', $validated) && is_null($validated['image'])) {
|
2022-06-14 00:20:21 +08:00
|
|
|
unset($validated['image']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$book = $this->bookRepo->update($book, $validated);
|
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
|
|
|
{
|
2024-02-06 01:35:49 +08:00
|
|
|
$book = $this->queries->findVisibleBySlugOrFail($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
|
|
|
{
|
2024-02-06 01:35:49 +08:00
|
|
|
$book = $this->queries->findVisibleBySlugOrFail($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
|
|
|
|
2021-12-20 03:20:31 +08:00
|
|
|
/**
|
|
|
|
* Show the view to copy a book.
|
|
|
|
*
|
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
public function showCopy(string $bookSlug)
|
|
|
|
{
|
2024-02-06 01:35:49 +08:00
|
|
|
$book = $this->queries->findVisibleBySlugOrFail($bookSlug);
|
2021-12-20 03:20:31 +08:00
|
|
|
$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)
|
|
|
|
{
|
2024-02-06 01:35:49 +08:00
|
|
|
$book = $this->queries->findVisibleBySlugOrFail($bookSlug);
|
2021-12-20 03:20:31 +08:00
|
|
|
$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 22:05:08 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the chapter to a book.
|
|
|
|
*/
|
|
|
|
public function convertToShelf(HierarchyTransformer $transformer, string $bookSlug)
|
|
|
|
{
|
2024-02-06 01:35:49 +08:00
|
|
|
$book = $this->queries->findVisibleBySlugOrFail($bookSlug);
|
2022-06-15 22:05:08 +08:00
|
|
|
$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-13 03:01:42 +08:00
|
|
|
}
|