BookStack/app/Entities/Controllers/BookController.php

266 lines
8.6 KiB
PHP
Raw Normal View History

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;
use BookStack\Activity\Tools\UserEntityWatchOptions;
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;
use BookStack\Entities\Tools\HierarchyTransformer;
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;
use BookStack\Http\Controller;
use BookStack\References\ReferenceFetcher;
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
{
public function __construct(
protected ShelfContext $shelfContext,
protected BookRepo $bookRepo,
protected ReferenceFetcher $referenceFetcher
) {
2015-07-12 15:01:42 -04:00
}
/**
* Display a listing of the book.
*/
public function index(Request $request)
2015-07-12 15:01:42 -04:00
{
$view = setting()->getForCurrentUser('books_view_type');
$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'),
]);
$books = $this->bookRepo->getAllPaginated(18, $listOptions->getSort(), $listOptions->getOrder());
$recents = $this->isSignedIn() ? $this->bookRepo->getRecentlyViewed(4) : false;
$popular = $this->bookRepo->getPopular(4);
$new = $this->bookRepo->getRecentlyCreated(4);
$this->shelfContext->clearShelfContext();
$this->setPageTitle(trans('entities.books'));
2021-06-26 11:23:15 -04:00
return view('books.index', [
2021-06-26 11:23:15 -04:00
'books' => $books,
'recents' => $recents,
'popular' => $popular,
2021-06-26 11:23:15 -04:00
'new' => $new,
'view' => $view,
'listOptions' => $listOptions,
]);
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'));
2021-06-26 11:23:15 -04:00
return view('books.create', [
2021-06-26 11:23:15 -04:00
'bookshelf' => $bookshelf,
]);
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
*/
public function store(Request $request, string $shelfSlug = null)
2015-07-12 15:01:42 -04:00
{
$this->checkPermission('book-create-all');
$validated = $this->validate($request, [
'name' => ['required', 'string', 'max:255'],
'description' => ['string', 'max:1000'],
'image' => array_merge(['nullable'], $this->getImageValidationRules()),
'tags' => ['array'],
'default_template_id' => ['nullable', 'integer'],
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($validated);
if ($bookshelf) {
2019-09-19 13:20:09 -04:00
$bookshelf->appendBook($book);
Activity::add(ActivityType::BOOKSHELF_UPDATE, $bookshelf);
}
return redirect($book->getUrl());
2015-07-12 15:01:42 -04:00
}
/**
* Display the specified book.
*/
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);
$bookChildren = (new BookContents($book))->getTree(true);
$bookParentShelves = $book->shelves()->scopes('visible')->get();
View::incrementFor($book);
if ($request->has('shelf')) {
$this->shelfContext->setShelfContext(intval($request->get('shelf')));
}
$this->setPageTitle($book->getShortName());
2021-06-26 11:23:15 -04:00
return view('books.show', [
2021-06-26 11:23:15 -04:00
'book' => $book,
'current' => $book,
'bookChildren' => $bookChildren,
'bookParentShelves' => $bookParentShelves,
'watchOptions' => new UserEntityWatchOptions(user(), $book),
'activity' => $activities->entityActivity($book, 20, 1),
'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);
$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
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
* @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);
$validated = $this->validate($request, [
'name' => ['required', 'string', 'max:255'],
'description' => ['string', 'max:1000'],
'image' => array_merge(['nullable'], $this->getImageValidationRules()),
'tags' => ['array'],
'default_template_id' => ['nullable', 'integer'],
2015-07-12 15:01:42 -04:00
]);
if ($request->has('image_reset')) {
$validated['image'] = null;
} elseif (array_key_exists('image', $validated) && is_null($validated['image'])) {
unset($validated['image']);
}
$book = $this->bookRepo->update($book, $validated);
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()]));
2021-06-26 11:23:15 -04:00
return view('books.delete', ['book' => $book, 'current' => $book]);
}
2015-09-06 09:35:53 -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);
$this->checkOwnablePermission('book-delete', $book);
$this->bookRepo->destroy($book);
2015-07-12 15:01:42 -04:00
return redirect('/books');
}
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());
}
/**
* 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
}