]> BookStack Code Mirror - bookstack/blob - app/Sorting/BookSortController.php
Merge pull request #5626 from BookStackApp/rubentalstra-development
[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      * Update the sort options of a book, setting the auto-sort and/or updating
48      * child order via mapping.
49      */
50     public function update(Request $request, BookSorter $sorter, string $bookSlug)
51     {
52         $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
53         $this->checkOwnablePermission('book-update', $book);
54         $loggedActivityForBook = false;
55
56         // Sort via map
57         if ($request->filled('sort-tree')) {
58             $sortMap = BookSortMap::fromJson($request->get('sort-tree'));
59             $booksInvolved = $sorter->sortUsingMap($sortMap);
60
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;
66                 }
67             }
68         }
69
70         if ($request->filled('auto-sort')) {
71             $sortSetId = intval($request->get('auto-sort')) ?: null;
72             if ($sortSetId && SortRule::query()->find($sortSetId) === null) {
73                 $sortSetId = null;
74             }
75             $book->sort_rule_id = $sortSetId;
76             $book->save();
77             $sorter->runBookAutoSort($book);
78             if (!$loggedActivityForBook) {
79                 Activity::add(ActivityType::BOOK_SORT, $book);
80             }
81         }
82
83         return redirect($book->getUrl());
84     }
85 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.