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