X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/cb0d674a71449de883713db2fcdccb6e108992ad..refs/pull/3918/head:/app/Entities/Tools/BookContents.php diff --git a/app/Entities/Tools/BookContents.php b/app/Entities/Tools/BookContents.php index 99602de41..f45bdfcc1 100644 --- a/app/Entities/Tools/BookContents.php +++ b/app/Entities/Tools/BookContents.php @@ -11,22 +11,15 @@ use Illuminate\Support\Collection; class BookContents { - /** - * @var Book - */ - protected $book; + protected Book $book; - /** - * BookContents constructor. - */ public function __construct(Book $book) { $this->book = $book; } /** - * Get the current priority of the last item - * at the top-level of the book. + * Get the current priority of the last item at the top-level of the book. */ public function getLastPriority(): int { @@ -119,9 +112,10 @@ class BookContents // Sort our changes from our map to be chapters first // Since they need to be process to ensure book alignment for child page changes. $sortMapItems = $sortMap->all(); - usort($sortMapItems, function(BookSortMapItem $itemA, BookSortMapItem $itemB) { + usort($sortMapItems, function (BookSortMapItem $itemA, BookSortMapItem $itemB) { $aScore = $itemA->type === 'page' ? 2 : 1; $bScore = $itemB->type === 'page' ? 2 : 1; + return $aScore - $bScore; }); @@ -167,9 +161,9 @@ class BookContents return; } - $currentParentKey = 'book:' . $model->book_id; + $currentParentKey = 'book:' . $model->book_id; if ($model instanceof Page && $model->chapter_id) { - $currentParentKey = 'chapter:' . $model->chapter_id; + $currentParentKey = 'chapter:' . $model->chapter_id; } $currentParent = $modelMap[$currentParentKey] ?? null; @@ -187,7 +181,7 @@ class BookContents $model->changeBook($newBook->id); } - if ($chapterChanged) { + if ($model instanceof Page && $chapterChanged) { $model->chapter_id = $newChapter->id ?? 0; } @@ -241,7 +235,7 @@ class BookContents } $hasPageEditPermission = userCan('page-update', $model); - $newParentInRightLocation = ($newParent instanceof Book || $newParent->book_id === $newBook->id); + $newParentInRightLocation = ($newParent instanceof Book || ($newParent instanceof Chapter && $newParent->book_id === $newBook->id)); $newParentPermission = ($newParent instanceof Chapter) ? 'chapter-update' : 'book-update'; $hasNewParentPermission = userCan($newParentPermission, $newParent); @@ -265,6 +259,7 @@ class BookContents /** * Load models from the database into the given sort map. + * * @return array */ protected function loadModelsFromSortMap(BookSortMap $sortMap): array @@ -272,8 +267,8 @@ class BookContents $modelMap = []; $ids = [ 'chapter' => [], - 'page' => [], - 'book' => [], + 'page' => [], + 'book' => [], ]; foreach ($sortMap->all() as $sortMapItem) {