]> BookStack Code Mirror - bookstack/blob - app/Sorting/BookSortController.php
Merge pull request #5689 from BookStackApp/permission_table_locking
[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 BookStack\Util\DatabaseTransaction;
11 use Illuminate\Http\Request;
12
13 class BookSortController extends Controller
14 {
15     public function __construct(
16         protected BookQueries $queries,
17     ) {
18     }
19
20     /**
21      * Shows the view which allows pages to be re-ordered and sorted.
22      */
23     public function show(string $bookSlug)
24     {
25         $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
26         $this->checkOwnablePermission('book-update', $book);
27
28         $bookChildren = (new BookContents($book))->getTree(false);
29
30         $this->setPageTitle(trans('entities.books_sort_named', ['bookName' => $book->getShortName()]));
31
32         return view('books.sort', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
33     }
34
35     /**
36      * Shows the sort box for a single book.
37      * Used via AJAX when loading in extra books to a sort.
38      */
39     public function showItem(string $bookSlug)
40     {
41         $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
42         $bookChildren = (new BookContents($book))->getTree();
43
44         return view('books.parts.sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
45     }
46
47     /**
48      * Update the sort options of a book, setting the auto-sort and/or updating
49      * child order via mapping.
50      */
51     public function update(Request $request, BookSorter $sorter, string $bookSlug)
52     {
53         $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
54         $this->checkOwnablePermission('book-update', $book);
55         $loggedActivityForBook = false;
56
57         // Sort via map
58         if ($request->filled('sort-tree')) {
59             (new DatabaseTransaction(function () use ($book, $request, $sorter, &$loggedActivityForBook) {
60                 $sortMap = BookSortMap::fromJson($request->get('sort-tree'));
61                 $booksInvolved = $sorter->sortUsingMap($sortMap);
62
63                 // Add activity for involved books.
64                 foreach ($booksInvolved as $bookInvolved) {
65                     Activity::add(ActivityType::BOOK_SORT, $bookInvolved);
66                     if ($bookInvolved->id === $book->id) {
67                         $loggedActivityForBook = true;
68                     }
69                 }
70             }))->run();
71         }
72
73         if ($request->filled('auto-sort')) {
74             $sortSetId = intval($request->get('auto-sort')) ?: null;
75             if ($sortSetId && SortRule::query()->find($sortSetId) === null) {
76                 $sortSetId = null;
77             }
78             $book->sort_rule_id = $sortSetId;
79             $book->save();
80             $sorter->runBookAutoSort($book);
81             if (!$loggedActivityForBook) {
82                 Activity::add(ActivityType::BOOK_SORT, $book);
83             }
84         }
85
86         return redirect($book->getUrl());
87     }
88 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.