3 namespace BookStack\Http\Controllers;
5 use BookStack\Actions\ActivityType;
6 use BookStack\Entities\Book;
7 use BookStack\Entities\Managers\BookContents;
8 use BookStack\Entities\Repos\BookRepo;
9 use BookStack\Exceptions\SortOperationException;
10 use BookStack\Facades\Activity;
11 use Illuminate\Http\Request;
13 class BookSortController extends Controller
19 * BookSortController constructor.
22 public function __construct(BookRepo $bookRepo)
24 $this->bookRepo = $bookRepo;
25 parent::__construct();
29 * Shows the view which allows pages to be re-ordered and sorted.
31 public function show(string $bookSlug)
33 $book = $this->bookRepo->getBySlug($bookSlug);
34 $this->checkOwnablePermission('book-update', $book);
36 $bookChildren = (new BookContents($book))->getTree(false);
38 $this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()]));
39 return view('books.sort', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
43 * Shows the sort box for a single book.
44 * Used via AJAX when loading in extra books to a sort.
46 public function showItem(string $bookSlug)
48 $book = $this->bookRepo->getBySlug($bookSlug);
49 $bookChildren = (new BookContents($book))->getTree();
50 return view('books.sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
54 * Sorts a book using a given mapping array.
56 public function update(Request $request, string $bookSlug)
58 $book = $this->bookRepo->getBySlug($bookSlug);
59 $this->checkOwnablePermission('book-update', $book);
61 // Return if no map sent
62 if (!$request->filled('sort-tree')) {
63 return redirect($book->getUrl());
66 $sortMap = collect(json_decode($request->get('sort-tree')));
67 $bookContents = new BookContents($book);
68 $booksInvolved = collect();
71 $booksInvolved = $bookContents->sortUsingMap($sortMap);
72 } catch (SortOperationException $exception) {
73 $this->showPermissionError();
76 // Rebuild permissions and add activity for involved books.
77 $booksInvolved->each(function (Book $book) {
78 Activity::add($book, ActivityType::BOOK_SORT, $book->id);
81 return redirect($book->getUrl());