2019-10-05 12:55:01 +01:00
|
|
|
<?php
|
|
|
|
|
|
2025-01-29 16:40:11 +00:00
|
|
|
namespace BookStack\Sorting;
|
2019-10-05 12:55:01 +01:00
|
|
|
|
2023-05-17 17:56:55 +01:00
|
|
|
use BookStack\Activity\ActivityType;
|
2024-02-04 17:35:16 +00:00
|
|
|
use BookStack\Entities\Queries\BookQueries;
|
2021-06-26 15:23:15 +00:00
|
|
|
use BookStack\Entities\Tools\BookContents;
|
2019-10-05 12:55:01 +01:00
|
|
|
use BookStack\Facades\Activity;
|
2023-05-18 20:53:39 +01:00
|
|
|
use BookStack\Http\Controller;
|
2019-10-05 12:55:01 +01:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
|
|
class BookSortController extends Controller
|
|
|
|
|
{
|
2024-02-04 17:35:16 +00:00
|
|
|
public function __construct(
|
|
|
|
|
protected BookQueries $queries,
|
|
|
|
|
) {
|
2019-10-05 12:55:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Shows the view which allows pages to be re-ordered and sorted.
|
|
|
|
|
*/
|
|
|
|
|
public function show(string $bookSlug)
|
|
|
|
|
{
|
2024-02-05 17:35:49 +00:00
|
|
|
$book = $this->queries->findVisibleBySlugOrFail($bookSlug);
|
2019-10-05 12:55:01 +01:00
|
|
|
$this->checkOwnablePermission('book-update', $book);
|
|
|
|
|
|
|
|
|
|
$bookChildren = (new BookContents($book))->getTree(false);
|
|
|
|
|
|
2022-09-18 01:25:20 +01:00
|
|
|
$this->setPageTitle(trans('entities.books_sort_named', ['bookName' => $book->getShortName()]));
|
2021-06-26 15:23:15 +00:00
|
|
|
|
2019-10-05 12:55:01 +01:00
|
|
|
return view('books.sort', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Shows the sort box for a single book.
|
|
|
|
|
* Used via AJAX when loading in extra books to a sort.
|
|
|
|
|
*/
|
|
|
|
|
public function showItem(string $bookSlug)
|
|
|
|
|
{
|
2024-02-05 17:35:49 +00:00
|
|
|
$book = $this->queries->findVisibleBySlugOrFail($bookSlug);
|
2019-10-05 12:55:01 +01:00
|
|
|
$bookChildren = (new BookContents($book))->getTree();
|
2021-06-26 15:23:15 +00:00
|
|
|
|
2021-08-22 13:15:58 +01:00
|
|
|
return view('books.parts.sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
|
2019-10-05 12:55:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-02-09 15:16:18 +00:00
|
|
|
* Update the sort options of a book, setting the auto-sort and/or updating
|
|
|
|
|
* child order via mapping.
|
2019-10-05 12:55:01 +01:00
|
|
|
*/
|
2025-01-29 16:40:11 +00:00
|
|
|
public function update(Request $request, BookSorter $sorter, string $bookSlug)
|
2019-10-05 12:55:01 +01:00
|
|
|
{
|
2024-02-05 17:35:49 +00:00
|
|
|
$book = $this->queries->findVisibleBySlugOrFail($bookSlug);
|
2019-10-05 12:55:01 +01:00
|
|
|
$this->checkOwnablePermission('book-update', $book);
|
2025-02-09 15:16:18 +00:00
|
|
|
$loggedActivityForBook = false;
|
2019-10-05 12:55:01 +01:00
|
|
|
|
2025-02-09 15:16:18 +00:00
|
|
|
// Sort via map
|
|
|
|
|
if ($request->filled('sort-tree')) {
|
|
|
|
|
$sortMap = BookSortMap::fromJson($request->get('sort-tree'));
|
|
|
|
|
$booksInvolved = $sorter->sortUsingMap($sortMap);
|
2019-10-05 12:55:01 +01:00
|
|
|
|
2025-02-09 15:16:18 +00:00
|
|
|
// Rebuild permissions and add activity for involved books.
|
|
|
|
|
foreach ($booksInvolved as $bookInvolved) {
|
|
|
|
|
Activity::add(ActivityType::BOOK_SORT, $bookInvolved);
|
|
|
|
|
if ($bookInvolved->id === $book->id) {
|
|
|
|
|
$loggedActivityForBook = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-05 12:55:01 +01:00
|
|
|
|
2025-02-09 15:16:18 +00:00
|
|
|
if ($request->filled('auto-sort')) {
|
|
|
|
|
$sortSetId = intval($request->get('auto-sort')) ?: null;
|
2025-02-11 14:36:25 +00:00
|
|
|
if ($sortSetId && SortRule::query()->find($sortSetId) === null) {
|
2025-02-09 15:16:18 +00:00
|
|
|
$sortSetId = null;
|
|
|
|
|
}
|
2025-02-11 14:36:25 +00:00
|
|
|
$book->sort_rule_id = $sortSetId;
|
2025-02-09 15:16:18 +00:00
|
|
|
$book->save();
|
|
|
|
|
$sorter->runBookAutoSort($book);
|
|
|
|
|
if (!$loggedActivityForBook) {
|
|
|
|
|
Activity::add(ActivityType::BOOK_SORT, $book);
|
|
|
|
|
}
|
2022-01-04 21:09:34 +00:00
|
|
|
}
|
2019-10-05 12:55:01 +01:00
|
|
|
|
|
|
|
|
return redirect($book->getUrl());
|
|
|
|
|
}
|
|
|
|
|
}
|