namespace BookStack\Entities\Controllers;
use BookStack\Activity\Models\View;
+use BookStack\Activity\Tools\UserEntityWatchOptions;
use BookStack\Entities\Models\Book;
+use BookStack\Entities\Queries\ChapterQueries;
+use BookStack\Entities\Queries\EntityQueries;
use BookStack\Entities\Repos\ChapterRepo;
use BookStack\Entities\Tools\BookContents;
use BookStack\Entities\Tools\Cloner;
use BookStack\Entities\Tools\NextPreviousContentLocator;
use BookStack\Exceptions\MoveOperationException;
use BookStack\Exceptions\NotFoundException;
+use BookStack\Exceptions\NotifyException;
use BookStack\Exceptions\PermissionsException;
use BookStack\Http\Controller;
use BookStack\References\ReferenceFetcher;
class ChapterController extends Controller
{
- protected ChapterRepo $chapterRepo;
- protected ReferenceFetcher $referenceFetcher;
-
- public function __construct(ChapterRepo $chapterRepo, ReferenceFetcher $referenceFetcher)
- {
- $this->chapterRepo = $chapterRepo;
- $this->referenceFetcher = $referenceFetcher;
+ public function __construct(
+ protected ChapterRepo $chapterRepo,
+ protected ChapterQueries $queries,
+ protected EntityQueries $entityQueries,
+ protected ReferenceFetcher $referenceFetcher,
+ ) {
}
/**
*/
public function create(string $bookSlug)
{
- $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
+ $book = $this->entityQueries->books->findVisibleBySlugOrFail($bookSlug);
$this->checkOwnablePermission('chapter-create', $book);
$this->setPageTitle(trans('entities.chapters_create'));
- return view('chapters.create', ['book' => $book, 'current' => $book]);
+ return view('chapters.create', [
+ 'book' => $book,
+ 'current' => $book,
+ ]);
}
/**
*/
public function store(Request $request, string $bookSlug)
{
- $this->validate($request, [
- 'name' => ['required', 'string', 'max:255'],
+ $validated = $this->validate($request, [
+ 'name' => ['required', 'string', 'max:255'],
+ 'description_html' => ['string', 'max:2000'],
+ 'tags' => ['array'],
+ 'default_template_id' => ['nullable', 'integer'],
]);
- $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
+ $book = $this->entityQueries->books->findVisibleBySlugOrFail($bookSlug);
$this->checkOwnablePermission('chapter-create', $book);
- $chapter = $this->chapterRepo->create($request->all(), $book);
+ $chapter = $this->chapterRepo->create($validated, $book);
return redirect($chapter->getUrl());
}
*/
public function show(string $bookSlug, string $chapterSlug)
{
- $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
+ $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
$this->checkOwnablePermission('chapter-view', $chapter);
$sidebarTree = (new BookContents($chapter->book))->getTree();
- $pages = $chapter->getVisiblePages();
+ $pages = $this->entityQueries->pages->visibleForChapterList($chapter->id)->get();
+
$nextPreviousLocator = new NextPreviousContentLocator($chapter, $sidebarTree);
View::incrementFor($chapter);
'chapter' => $chapter,
'current' => $chapter,
'sidebarTree' => $sidebarTree,
+ 'watchOptions' => new UserEntityWatchOptions(user(), $chapter),
'pages' => $pages,
'next' => $nextPreviousLocator->getNext(),
'previous' => $nextPreviousLocator->getPrevious(),
- 'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($chapter),
+ 'referenceCount' => $this->referenceFetcher->getReferenceCountToEntity($chapter),
]);
}
*/
public function edit(string $bookSlug, string $chapterSlug)
{
- $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
+ $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
$this->checkOwnablePermission('chapter-update', $chapter);
$this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
*/
public function update(Request $request, string $bookSlug, string $chapterSlug)
{
- $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
+ $validated = $this->validate($request, [
+ 'name' => ['required', 'string', 'max:255'],
+ 'description_html' => ['string', 'max:2000'],
+ 'tags' => ['array'],
+ 'default_template_id' => ['nullable', 'integer'],
+ ]);
+
+ $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
$this->checkOwnablePermission('chapter-update', $chapter);
- $this->chapterRepo->update($chapter, $request->all());
+ $this->chapterRepo->update($chapter, $validated);
return redirect($chapter->getUrl());
}
*/
public function showDelete(string $bookSlug, string $chapterSlug)
{
- $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
+ $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
$this->checkOwnablePermission('chapter-delete', $chapter);
$this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
*/
public function destroy(string $bookSlug, string $chapterSlug)
{
- $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
+ $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
$this->checkOwnablePermission('chapter-delete', $chapter);
$this->chapterRepo->destroy($chapter);
*/
public function showMove(string $bookSlug, string $chapterSlug)
{
- $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
+ $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
$this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
$this->checkOwnablePermission('chapter-update', $chapter);
$this->checkOwnablePermission('chapter-delete', $chapter);
/**
* Perform the move action for a chapter.
*
- * @throws NotFoundException
+ * @throws NotFoundException|NotifyException
*/
public function move(Request $request, string $bookSlug, string $chapterSlug)
{
- $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
+ $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
$this->checkOwnablePermission('chapter-update', $chapter);
$this->checkOwnablePermission('chapter-delete', $chapter);
}
try {
- $newBook = $this->chapterRepo->move($chapter, $entitySelection);
+ $this->chapterRepo->move($chapter, $entitySelection);
} catch (PermissionsException $exception) {
$this->showPermissionError();
} catch (MoveOperationException $exception) {
$this->showErrorNotification(trans('errors.selected_book_not_found'));
- return redirect()->back();
+ return redirect($chapter->getUrl('/move'));
}
return redirect($chapter->getUrl());
*/
public function showCopy(string $bookSlug, string $chapterSlug)
{
- $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
+ $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
$this->checkOwnablePermission('chapter-view', $chapter);
session()->flashInput(['name' => $chapter->name]);
*/
public function copy(Request $request, Cloner $cloner, string $bookSlug, string $chapterSlug)
{
- $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
+ $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
$this->checkOwnablePermission('chapter-view', $chapter);
$entitySelection = $request->get('entity_selection') ?: null;
- $newParentBook = $entitySelection ? $this->chapterRepo->findParentByIdentifier($entitySelection) : $chapter->getParent();
+ $newParentBook = $entitySelection ? $this->entityQueries->findVisibleByStringIdentifier($entitySelection) : $chapter->getParent();
- if (is_null($newParentBook)) {
+ if (!$newParentBook instanceof Book) {
$this->showErrorNotification(trans('errors.selected_book_not_found'));
- return redirect()->back();
+ return redirect($chapter->getUrl('/copy'));
}
$this->checkOwnablePermission('chapter-create', $newParentBook);
*/
public function convertToBook(HierarchyTransformer $transformer, string $bookSlug, string $chapterSlug)
{
- $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
+ $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
$this->checkOwnablePermission('chapter-update', $chapter);
$this->checkOwnablePermission('chapter-delete', $chapter);
$this->checkPermission('book-create-all');