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 * Update the sort options of a book, setting the auto-sort and/or updating
48 * child order via mapping.
50 public function update(Request $request, BookSorter $sorter, string $bookSlug)
52 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
53 $this->checkOwnablePermission('book-update', $book);
54 $loggedActivityForBook = false;
57 if ($request->filled('sort-tree')) {
58 $sortMap = BookSortMap::fromJson($request->get('sort-tree'));
59 $booksInvolved = $sorter->sortUsingMap($sortMap);
61 // Rebuild permissions and add activity for involved books.
62 foreach ($booksInvolved as $bookInvolved) {
63 Activity::add(ActivityType::BOOK_SORT, $bookInvolved);
64 if ($bookInvolved->id === $book->id) {
65 $loggedActivityForBook = true;
70 if ($request->filled('auto-sort')) {
71 $sortSetId = intval($request->get('auto-sort')) ?: null;
72 if ($sortSetId && SortRule::query()->find($sortSetId) === null) {
75 $book->sort_rule_id = $sortSetId;
77 $sorter->runBookAutoSort($book);
78 if (!$loggedActivityForBook) {
79 Activity::add(ActivityType::BOOK_SORT, $book);
83 return redirect($book->getUrl());