}
/**
- * Sort the books content using the given map.
- * The map is a single-dimension collection of objects in the following format:
- * {
- * +"id": "294" (ID of item)
- * +"sort": 1 (Sort order index)
- * +"parentChapter": false (ID of parent chapter, as string, or false)
- * +"type": "page" (Entity type of item)
- * +"book": "1" (Id of book to place item in)
- * }.
- *
+ * Sort the books content using the given sort map.
* Returns a list of books that were involved in the operation.
*
* @throws SortOperationException
*/
- public function sortUsingMap(Collection $sortMap): Collection
+ public function sortUsingMap(BookSortMap $sortMap): Collection
{
// Load models into map
$this->loadModelsIntoSortMap($sortMap);
$booksInvolved = $this->getBooksInvolvedInSort($sortMap);
// Perform the sort
- $sortMap->each(function ($mapItem) {
- $this->applySortUpdates($mapItem);
- });
+ foreach ($sortMap->all() as $item) {
+ $this->applySortUpdates($item);
+ }
// Update permissions and activity.
$booksInvolved->each(function (Book $book) {
* Using the given sort map item, detect changes for the related model
* and update it if required.
*/
- protected function applySortUpdates(\stdClass $sortMapItem)
+ protected function applySortUpdates(BookSortMapItem $sortMapItem): void
{
- /** @var BookChild $model */
$model = $sortMapItem->model;
+ if (!$model) {
+ return;
+ }
- $priorityChanged = intval($model->priority) !== intval($sortMapItem->sort);
- $bookChanged = intval($model->book_id) !== intval($sortMapItem->book);
- $chapterChanged = ($model instanceof Page) && intval($model->chapter_id) !== $sortMapItem->parentChapter;
+ $priorityChanged = $model->priority !== $sortMapItem->sort;
+ $bookChanged = $model->book_id !== $sortMapItem->parentBookId;
+ $chapterChanged = ($model instanceof Page) && $model->chapter_id !== $sortMapItem->parentChapterId;
if ($bookChanged) {
- $model->changeBook($sortMapItem->book);
+ $model->changeBook($sortMapItem->parentBookId);
}
if ($chapterChanged) {
- $model->chapter_id = intval($sortMapItem->parentChapter);
+ $model->chapter_id = intval($sortMapItem->parentChapterId);
$model->save();
}
if ($priorityChanged) {
- $model->priority = intval($sortMapItem->sort);
+ $model->priority = $sortMapItem->sort;
$model->save();
}
}
/**
* Load models from the database into the given sort map.
*/
- protected function loadModelsIntoSortMap(Collection $sortMap): void
+ protected function loadModelsIntoSortMap(BookSortMap $sortMap): void
{
- $keyMap = $sortMap->keyBy(function (\stdClass $sortMapItem) {
+ $collection = collect($sortMap->all());
+
+ $keyMap = $collection->keyBy(function (BookSortMapItem $sortMapItem) {
return $sortMapItem->type . ':' . $sortMapItem->id;
});
- $pageIds = $sortMap->where('type', '=', 'page')->pluck('id');
- $chapterIds = $sortMap->where('type', '=', 'chapter')->pluck('id');
+
+ $pageIds = $collection->where('type', '=', 'page')->pluck('id');
+ $chapterIds = $collection->where('type', '=', 'chapter')->pluck('id');
$pages = Page::visible()->whereIn('id', $pageIds)->get();
$chapters = Chapter::visible()->whereIn('id', $chapterIds)->get();
foreach ($pages as $page) {
+ /** @var BookSortMapItem $sortItem */
$sortItem = $keyMap->get('page:' . $page->id);
$sortItem->model = $page;
}
foreach ($chapters as $chapter) {
+ /** @var BookSortMapItem $sortItem */
$sortItem = $keyMap->get('chapter:' . $chapter->id);
$sortItem->model = $chapter;
}
*
* @throws SortOperationException
*/
- protected function getBooksInvolvedInSort(Collection $sortMap): Collection
+ protected function getBooksInvolvedInSort(BookSortMap $sortMap): Collection
{
- $bookIdsInvolved = collect([$this->book->id]);
- $bookIdsInvolved = $bookIdsInvolved->concat($sortMap->pluck('book'));
- $bookIdsInvolved = $bookIdsInvolved->concat($sortMap->pluck('model.book_id'));
- $bookIdsInvolved = $bookIdsInvolved->unique()->toArray();
-
+ $collection = collect($sortMap->all());
+
+ $bookIdsInvolved = array_unique(array_merge(
+ [$this->book->id],
+ $collection->pluck('parentBookId')->values()->all(),
+ $collection->pluck('model.book_id')->values()->all(),
+ ));
+
$books = Book::hasPermission('update')->whereIn('id', $bookIdsInvolved)->get();
if (count($books) !== count($bookIdsInvolved)) {
--- /dev/null
+<?php
+
+namespace BookStack\Entities\Tools;
+
+class BookSortMap
+{
+ /**
+ * @var BookSortMapItem[]
+ */
+ protected $mapData = [];
+
+ public function addItem(BookSortMapItem $mapItem): void
+ {
+ $this->mapData[] = $mapItem;
+ }
+
+ /**
+ * @return BookSortMapItem[]
+ */
+ public function all(): array
+ {
+ return $this->mapData;
+ }
+
+ public static function fromJson(string $json): self
+ {
+ $map = new static();
+ $mapData = json_decode($json);
+
+ foreach ($mapData as $mapDataItem) {
+ $item = new BookSortMapItem(
+ intval($mapDataItem->id),
+ intval($mapDataItem->sort),
+ $mapDataItem->parentChapter ? intval($mapDataItem->parentChapter) : null,
+ $mapDataItem->type,
+ intval($mapDataItem->book)
+ );
+
+ $map->addItem($item);
+ }
+
+ return $map;
+ }
+
+}
\ No newline at end of file