3 namespace BookStack\Entities\Controllers;
5 use BookStack\Activity\ActivityType;
6 use BookStack\Entities\Queries\BookQueries;
7 use BookStack\Entities\Tools\BookContents;
8 use BookStack\Entities\Tools\BookSortMap;
9 use BookStack\Facades\Activity;
10 use BookStack\Http\Controller;
11 use Illuminate\Http\Request;
13 class BookSortController extends Controller
15 public function __construct(
16 protected BookQueries $queries,
21 * Shows the view which allows pages to be re-ordered and sorted.
23 public function show(string $bookSlug)
25 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
26 $this->checkOwnablePermission('book-update', $book);
28 $bookChildren = (new BookContents($book))->getTree(false);
30 $this->setPageTitle(trans('entities.books_sort_named', ['bookName' => $book->getShortName()]));
32 return view('books.sort', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
36 * Shows the sort box for a single book.
37 * Used via AJAX when loading in extra books to a sort.
39 public function showItem(string $bookSlug)
41 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
42 $bookChildren = (new BookContents($book))->getTree();
44 return view('books.parts.sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
48 * Sorts a book using a given mapping array.
50 public function update(Request $request, string $bookSlug)
52 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
53 $this->checkOwnablePermission('book-update', $book);
55 // Return if no map sent
56 if (!$request->filled('sort-tree')) {
57 return redirect($book->getUrl());
60 $sortMap = BookSortMap::fromJson($request->get('sort-tree'));
61 $bookContents = new BookContents($book);
62 $booksInvolved = $bookContents->sortUsingMap($sortMap);
64 // Rebuild permissions and add activity for involved books.
65 foreach ($booksInvolved as $bookInvolved) {
66 Activity::add(ActivityType::BOOK_SORT, $bookInvolved);
69 return redirect($book->getUrl());