+ 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)
+ {
+ $this->checkPermission('book-delete');
+ $book = $this->bookRepo->getBySlug($bookSlug);
+ $this->setPageTitle('Delete Book ' . $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)
+ {
+ $this->checkPermission('book-update');
+ $book = $this->bookRepo->getBySlug($bookSlug);
+ $bookChildren = $this->bookRepo->getChildren($book);
+ $books = $this->bookRepo->getAll(false);
+ $this->setPageTitle('Sort Book ' . $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)
+ {
+ $this->checkPermission('book-update');
+ $book = $this->bookRepo->getBySlug($bookSlug);
+
+ // 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;
+ }
+ }
+
+ // Add activity for books
+ foreach ($sortedBooks as $bookId) {
+ $updatedBook = $this->bookRepo->getById($bookId);
+ Activity::add($updatedBook, 'book_sort', $updatedBook->id);
+ }
+
+ return redirect($book->getUrl());