]> BookStack Code Mirror - bookstack/blob - app/Sorting/BookSortController.php
Sorting: Reorganised book sort code to its own directory
[bookstack] / app / Sorting / BookSortController.php
1 <?php
2
3 namespace BookStack\Sorting;
4
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;
11
12 class BookSortController extends Controller
13 {
14     public function __construct(
15         protected BookQueries $queries,
16     ) {
17     }
18
19     /**
20      * Shows the view which allows pages to be re-ordered and sorted.
21      */
22     public function show(string $bookSlug)
23     {
24         $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
25         $this->checkOwnablePermission('book-update', $book);
26
27         $bookChildren = (new BookContents($book))->getTree(false);
28
29         $this->setPageTitle(trans('entities.books_sort_named', ['bookName' => $book->getShortName()]));
30
31         return view('books.sort', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
32     }
33
34     /**
35      * Shows the sort box for a single book.
36      * Used via AJAX when loading in extra books to a sort.
37      */
38     public function showItem(string $bookSlug)
39     {
40         $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
41         $bookChildren = (new BookContents($book))->getTree();
42
43         return view('books.parts.sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
44     }
45
46     /**
47      * Sorts a book using a given mapping array.
48      */
49     public function update(Request $request, BookSorter $sorter, string $bookSlug)
50     {
51         $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
52         $this->checkOwnablePermission('book-update', $book);
53
54         // Return if no map sent
55         if (!$request->filled('sort-tree')) {
56             return redirect($book->getUrl());
57         }
58
59         $sortMap = BookSortMap::fromJson($request->get('sort-tree'));
60         $booksInvolved = $sorter->sortUsingMap($sortMap);
61
62         // Rebuild permissions and add activity for involved books.
63         foreach ($booksInvolved as $bookInvolved) {
64             Activity::add(ActivityType::BOOK_SORT, $bookInvolved);
65         }
66
67         return redirect($book->getUrl());
68     }
69 }