3 namespace BookStack\Entities\Controllers;
5 use BookStack\Activity\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 BookStack\Http\Controller;
11 use Illuminate\Http\Request;
13 class BookSortController extends Controller
17 public function __construct(BookRepo $bookRepo)
19 $this->bookRepo = $bookRepo;
23 * Shows the view which allows pages to be re-ordered and sorted.
25 public function show(string $bookSlug)
27 $book = $this->bookRepo->getBySlug($bookSlug);
28 $this->checkOwnablePermission('book-update', $book);
30 $bookChildren = (new BookContents($book))->getTree(false);
32 $this->setPageTitle(trans('entities.books_sort_named', ['bookName' => $book->getShortName()]));
34 return view('books.sort', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
38 * Shows the sort box for a single book.
39 * Used via AJAX when loading in extra books to a sort.
41 public function showItem(string $bookSlug)
43 $book = $this->bookRepo->getBySlug($bookSlug);
44 $bookChildren = (new BookContents($book))->getTree();
46 return view('books.parts.sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
50 * Sorts a book using a given mapping array.
52 public function update(Request $request, string $bookSlug)
54 $book = $this->bookRepo->getBySlug($bookSlug);
55 $this->checkOwnablePermission('book-update', $book);
57 // Return if no map sent
58 if (!$request->filled('sort-tree')) {
59 return redirect($book->getUrl());
62 $sortMap = BookSortMap::fromJson($request->get('sort-tree'));
63 $bookContents = new BookContents($book);
64 $booksInvolved = $bookContents->sortUsingMap($sortMap);
66 // Rebuild permissions and add activity for involved books.
67 foreach ($booksInvolved as $bookInvolved) {
68 Activity::add(ActivityType::BOOK_SORT, $bookInvolved);
71 return redirect($book->getUrl());