]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookSortController.php
Added force option for update-url command
[bookstack] / app / Http / Controllers / BookSortController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Actions\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 Illuminate\Http\Request;
11
12 class BookSortController extends Controller
13 {
14     protected $bookRepo;
15
16     public function __construct(BookRepo $bookRepo)
17     {
18         $this->bookRepo = $bookRepo;
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->bookRepo->getBySlug($bookSlug);
27         $this->checkOwnablePermission('book-update', $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->bookRepo->getBySlug($bookSlug);
43         $bookChildren = (new BookContents($book))->getTree();
44
45         return view('books.parts.sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
46     }
47
48     /**
49      * Sorts a book using a given mapping array.
50      */
51     public function update(Request $request, string $bookSlug)
52     {
53         $book = $this->bookRepo->getBySlug($bookSlug);
54         $this->checkOwnablePermission('book-update', $book);
55
56         // Return if no map sent
57         if (!$request->filled('sort-tree')) {
58             return redirect($book->getUrl());
59         }
60
61         $sortMap = BookSortMap::fromJson($request->get('sort-tree'));
62         $bookContents = new BookContents($book);
63         $booksInvolved = $bookContents->sortUsingMap($sortMap);
64
65         // Rebuild permissions and add activity for involved books.
66         foreach ($booksInvolved as $bookInvolved) {
67             Activity::add(ActivityType::BOOK_SORT, $bookInvolved);
68         }
69
70         return redirect($book->getUrl());
71     }
72 }