3 namespace BookStack\Sorting;
5 use BookStack\Activity\ActivityType;
6 use BookStack\Entities\Queries\BookQueries;
7 use BookStack\Entities\Tools\BookContents;
8 use BookStack\Facades\Activity;
9 use BookStack\Http\Controller;
10 use Illuminate\Http\Request;
12 class BookSortController extends Controller
14 public function __construct(
15 protected BookQueries $queries,
20 * Shows the view which allows pages to be re-ordered and sorted.
22 public function show(string $bookSlug)
24 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
25 $this->checkOwnablePermission('book-update', $book);
27 $bookChildren = (new BookContents($book))->getTree(false);
29 $this->setPageTitle(trans('entities.books_sort_named', ['bookName' => $book->getShortName()]));
31 return view('books.sort', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
35 * Shows the sort box for a single book.
36 * Used via AJAX when loading in extra books to a sort.
38 public function showItem(string $bookSlug)
40 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
41 $bookChildren = (new BookContents($book))->getTree();
43 return view('books.parts.sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
47 * Sorts a book using a given mapping array.
49 public function update(Request $request, BookSorter $sorter, string $bookSlug)
51 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
52 $this->checkOwnablePermission('book-update', $book);
54 // Return if no map sent
55 if (!$request->filled('sort-tree')) {
56 return redirect($book->getUrl());
59 $sortMap = BookSortMap::fromJson($request->get('sort-tree'));
60 $booksInvolved = $sorter->sortUsingMap($sortMap);
62 // Rebuild permissions and add activity for involved books.
63 foreach ($booksInvolved as $bookInvolved) {
64 Activity::add(ActivityType::BOOK_SORT, $bookInvolved);
67 return redirect($book->getUrl());