+ $book = $this->bookRepo->updateFromInput($book, $request->all());
+ Activity::add($book, 'book_update', $book->id);
+ return redirect($book->getUrl());
+ }
+
+ /**
+ * Shows the page to confirm deletion
+ * @param $bookSlug
+ * @return \Illuminate\View\View
+ */
+ public function showDelete($bookSlug)
+ {
+ $book = $this->bookRepo->getBySlug($bookSlug);
+ $this->checkOwnablePermission('book-delete', $book);
+ $this->setPageTitle(trans('entities.books_delete_named', ['bookName'=>$book->getShortName()]));
+ return view('books/delete', ['book' => $book, 'current' => $book]);
+ }
+
+ /**
+ * Shows the view which allows pages to be re-ordered and sorted.
+ * @param string $bookSlug
+ * @return \Illuminate\View\View
+ */
+ public function sort($bookSlug)
+ {
+ $book = $this->bookRepo->getBySlug($bookSlug);
+ $this->checkOwnablePermission('book-update', $book);
+ $bookChildren = $this->bookRepo->getChildren($book, true);
+ $books = $this->bookRepo->getAll(false);
+ $this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()]));
+ return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
+ }
+
+ /**
+ * Shows the sort box for a single book.
+ * Used via AJAX when loading in extra books to a sort.
+ * @param $bookSlug
+ * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
+ */
+ public function getSortItem($bookSlug)
+ {
+ $book = $this->bookRepo->getBySlug($bookSlug);
+ $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());
+ }
+
+ // Sort pages and chapters
+ $sortedBooks = [];
+ $updatedModels = collect();
+ $sortMap = json_decode($request->get('sort-tree'));
+ $defaultBookId = $book->id;
+
+ // Loop through contents of provided map and update entities accordingly
+ foreach ($sortMap as $bookChild) {
+ $priority = $bookChild->sort;
+ $id = intval($bookChild->id);
+ $isPage = $bookChild->type == 'page';
+ $bookId = $this->bookRepo->exists($bookChild->book) ? intval($bookChild->book) : $defaultBookId;
+ $chapterId = ($isPage && $bookChild->parentChapter === false) ? 0 : intval($bookChild->parentChapter);
+ $model = $isPage ? $this->pageRepo->getById($id) : $this->chapterRepo->getById($id);
+
+ // Update models only if there's a change in parent chain or ordering.
+ if ($model->priority !== $priority || $model->book_id !== $bookId || ($isPage && $model->chapter_id !== $chapterId)) {
+ $isPage ? $this->pageRepo->changeBook($bookId, $model) : $this->chapterRepo->changeBook($bookId, $model);
+ $model->priority = $priority;
+ if ($isPage) $model->chapter_id = $chapterId;
+ $model->save();
+ $updatedModels->push($model);
+ }
+
+ // Store involved books to be sorted later
+ if (!in_array($bookId, $sortedBooks)) {
+ $sortedBooks[] = $bookId;
+ }