X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/a6633642232efd164d4708967ab59e498fbff896..refs/pull/3751/head:/app/Http/Controllers/ChapterController.php diff --git a/app/Http/Controllers/ChapterController.php b/app/Http/Controllers/ChapterController.php index 1d69df2a2..6695c2868 100644 --- a/app/Http/Controllers/ChapterController.php +++ b/app/Http/Controllers/ChapterController.php @@ -1,27 +1,32 @@ -chapterRepo = $chapterRepo; + $this->referenceFetcher = $referenceFetcher; } /** @@ -33,17 +38,19 @@ class ChapterController extends Controller $this->checkOwnablePermission('chapter-create', $book); $this->setPageTitle(trans('entities.chapters_create')); + return view('chapters.create', ['book' => $book, 'current' => $book]); } /** * Store a newly created chapter in storage. + * * @throws ValidationException */ public function store(Request $request, string $bookSlug) { $this->validate($request, [ - 'name' => 'required|string|max:255' + 'name' => ['required', 'string', 'max:255'], ]); $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail(); @@ -64,15 +71,20 @@ class ChapterController extends Controller $sidebarTree = (new BookContents($chapter->book))->getTree(); $pages = $chapter->getVisiblePages(); - Views::add($chapter); + $nextPreviousLocator = new NextPreviousContentLocator($chapter, $sidebarTree); + View::incrementFor($chapter); $this->setPageTitle($chapter->getShortName()); + return view('chapters.show', [ - 'book' => $chapter->book, - 'chapter' => $chapter, - 'current' => $chapter, - 'sidebarTree' => $sidebarTree, - 'pages' => $pages + 'book' => $chapter->book, + 'chapter' => $chapter, + 'current' => $chapter, + 'sidebarTree' => $sidebarTree, + 'pages' => $pages, + 'next' => $nextPreviousLocator->getNext(), + 'previous' => $nextPreviousLocator->getPrevious(), + 'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($chapter), ]); } @@ -85,11 +97,13 @@ class ChapterController extends Controller $this->checkOwnablePermission('chapter-update', $chapter); $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()])); + return view('chapters.edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]); } /** * Update the specified chapter in storage. + * * @throws NotFoundException */ public function update(Request $request, string $bookSlug, string $chapterSlug) @@ -104,6 +118,7 @@ class ChapterController extends Controller /** * Shows the page to confirm deletion of this chapter. + * * @throws NotFoundException */ public function showDelete(string $bookSlug, string $chapterSlug) @@ -112,11 +127,13 @@ class ChapterController extends Controller $this->checkOwnablePermission('chapter-delete', $chapter); $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()])); + return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]); } /** * Remove the specified chapter from storage. + * * @throws NotFoundException * @throws Throwable */ @@ -132,6 +149,7 @@ class ChapterController extends Controller /** * Show the page for moving a chapter. + * * @throws NotFoundException */ public function showMove(string $bookSlug, string $chapterSlug) @@ -143,12 +161,13 @@ class ChapterController extends Controller return view('chapters.move', [ 'chapter' => $chapter, - 'book' => $chapter->book + 'book' => $chapter->book, ]); } /** * Perform the move action for a chapter. + * * @throws NotFoundException */ public function move(Request $request, string $bookSlug, string $chapterSlug) @@ -164,17 +183,69 @@ class ChapterController extends Controller try { $newBook = $this->chapterRepo->move($chapter, $entitySelection); + } catch (PermissionsException $exception) { + $this->showPermissionError(); } catch (MoveOperationException $exception) { $this->showErrorNotification(trans('errors.selected_book_not_found')); + return redirect()->back(); } $this->showSuccessNotification(trans('entities.chapter_move_success', ['bookName' => $newBook->name])); + return redirect($chapter->getUrl()); } + /** + * Show the view to copy a chapter. + * + * @throws NotFoundException + */ + public function showCopy(string $bookSlug, string $chapterSlug) + { + $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug); + $this->checkOwnablePermission('chapter-view', $chapter); + + session()->flashInput(['name' => $chapter->name]); + + return view('chapters.copy', [ + 'book' => $chapter->book, + 'chapter' => $chapter, + ]); + } + + /** + * Create a copy of a chapter within the requested target destination. + * + * @throws NotFoundException + * @throws Throwable + */ + public function copy(Request $request, Cloner $cloner, string $bookSlug, string $chapterSlug) + { + $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug); + $this->checkOwnablePermission('chapter-view', $chapter); + + $entitySelection = $request->get('entity_selection') ?: null; + $newParentBook = $entitySelection ? $this->chapterRepo->findParentByIdentifier($entitySelection) : $chapter->getParent(); + + if (is_null($newParentBook)) { + $this->showErrorNotification(trans('errors.selected_book_not_found')); + + return redirect()->back(); + } + + $this->checkOwnablePermission('chapter-create', $newParentBook); + + $newName = $request->get('name') ?: $chapter->name; + $chapterCopy = $cloner->cloneChapter($chapter, $newParentBook, $newName); + $this->showSuccessNotification(trans('entities.chapters_copy_success')); + + return redirect($chapterCopy->getUrl()); + } + /** * Show the Restrictions view. + * * @throws NotFoundException */ public function showPermissions(string $bookSlug, string $chapterSlug) @@ -189,6 +260,7 @@ class ChapterController extends Controller /** * Set the restrictions for this chapter. + * * @throws NotFoundException */ public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug, string $chapterSlug) @@ -199,6 +271,22 @@ class ChapterController extends Controller $permissionsUpdater->updateFromPermissionsForm($chapter, $request); $this->showSuccessNotification(trans('entities.chapters_permissions_success')); + return redirect($chapter->getUrl()); } + + /** + * Convert the chapter to a book. + */ + public function convertToBook(HierarchyTransformer $transformer, string $bookSlug, string $chapterSlug) + { + $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug); + $this->checkOwnablePermission('chapter-update', $chapter); + $this->checkOwnablePermission('chapter-delete', $chapter); + $this->checkPermission('book-create-all'); + + $book = $transformer->transformChapterToBook($chapter); + + return redirect($book->getUrl()); + } }