- $bookChildren = $this->bookRepo->getChildren($book);
- return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
- }
-
- /**
- * Saves an array of sort mapping to pages and chapters.
- * @param string $bookSlug
- * @param Request $request
- * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
- */
- public function saveSort($bookSlug, Request $request)
- {
- $book = $this->bookRepo->getBySlug($bookSlug);
- $this->checkOwnablePermission('book-update', $book);
-
- // Return if no map sent
- if (!$request->has('sort-tree')) {
- return redirect($book->getUrl());
- }
-
- $sortedBooks = [];
- // Sort pages and chapters
- $sortMap = json_decode($request->get('sort-tree'));
- $defaultBookId = $book->id;
- foreach ($sortMap as $index => $bookChild) {
- $id = $bookChild->id;
- $isPage = $bookChild->type == 'page';
- $bookId = $this->bookRepo->exists($bookChild->book) ? $bookChild->book : $defaultBookId;
- $model = $isPage ? $this->pageRepo->getById($id) : $this->chapterRepo->getById($id);
- $isPage ? $this->pageRepo->changeBook($bookId, $model) : $this->chapterRepo->changeBook($bookId, $model);
- $model->priority = $index;
- if ($isPage) {
- $model->chapter_id = ($bookChild->parentChapter === false) ? 0 : $bookChild->parentChapter;
- }
- $model->save();
- if (!in_array($bookId, $sortedBooks)) {
- $sortedBooks[] = $bookId;
- }
- }