3 namespace BookStack\Http\Controllers;
5 use BookStack\Actions\ActivityType;
6 use BookStack\Entities\Repos\BookRepo;
7 use BookStack\Entities\Tools\BookContents;
8 use BookStack\Entities\Tools\BookSortMap;
9 use BookStack\Facades\Activity;
10 use Illuminate\Http\Request;
12 class BookSortController extends Controller
16 public function __construct(BookRepo $bookRepo)
18 $this->bookRepo = $bookRepo;
22 * Shows the view which allows pages to be re-ordered and sorted.
24 public function show(string $bookSlug)
26 $book = $this->bookRepo->getBySlug($bookSlug);
27 $this->checkOwnablePermission('book-update', $book);
29 $bookChildren = (new BookContents($book))->getTree(false);
31 $this->setPageTitle(trans('entities.books_sort_named', ['bookName' => $book->getShortName()]));
33 return view('books.sort', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
37 * Shows the sort box for a single book.
38 * Used via AJAX when loading in extra books to a sort.
40 public function showItem(string $bookSlug)
42 $book = $this->bookRepo->getBySlug($bookSlug);
43 $bookChildren = (new BookContents($book))->getTree();
45 return view('books.parts.sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
49 * Sorts a book using a given mapping array.
51 public function update(Request $request, string $bookSlug)
53 $book = $this->bookRepo->getBySlug($bookSlug);
54 $this->checkOwnablePermission('book-update', $book);
56 // Return if no map sent
57 if (!$request->filled('sort-tree')) {
58 return redirect($book->getUrl());
61 $sortMap = BookSortMap::fromJson($request->get('sort-tree'));
62 $bookContents = new BookContents($book);
63 $booksInvolved = $bookContents->sortUsingMap($sortMap);
65 // Rebuild permissions and add activity for involved books.
66 foreach ($booksInvolved as $bookInvolved) {
67 Activity::add(ActivityType::BOOK_SORT, $bookInvolved);
70 return redirect($book->getUrl());