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
{
// 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;
});
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;
$model->changeBook($newBook->id);
}
- if ($chapterChanged) {
+ if ($model instanceof Page && $chapterChanged) {
$model->chapter_id = $newChapter->id ?? 0;
}
}
$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);
/**
* Load models from the database into the given sort map.
+ *
* @return array<string, Entity>
*/
protected function loadModelsFromSortMap(BookSortMap $sortMap): array
$modelMap = [];
$ids = [
'chapter' => [],
- 'page' => [],
- 'book' => [],
+ 'page' => [],
+ 'book' => [],
];
foreach ($sortMap->all() as $sortMapItem) {