+ // Stop if there's no change
+ if (!$priorityChanged && !$bookChanged && !$chapterChanged) {
+ return;
+ }
+
+ $currentParentKey = 'book:' . $model->book_id;
+ if ($model instanceof Page && $model->chapter_id) {
+ $currentParentKey = 'chapter:' . $model->chapter_id;
+ }
+
+ $currentParent = $modelMap[$currentParentKey];
+ /** @var Book $newBook */
+ $newBook = $modelMap['book:' . $sortMapItem->parentBookId] ?? null;
+ /** @var ?Chapter $newChapter */
+ $newChapter = $sortMapItem->parentChapterId ? ($modelMap['chapter:' . $sortMapItem->parentChapterId] ?? null) : null;
+
+ // Check permissions of our changes to be made
+ if (!$currentParent || !$newBook) {
+ return;
+ } else if (!userCan('chapter-update', $currentParent) && !userCan('book-update', $currentParent)) {
+ return;
+ } else if ($bookChanged && !$newChapter && !userCan('book-update', $newBook)) {
+ return;
+ } else if ($newChapter && !userCan('chapter-update', $newChapter)) {
+ return;
+ } else if (($chapterChanged || $bookChanged) && $newChapter && $newBook->id !== $newChapter->book_id) {
+ return;
+ }
+
+ // Action the required changes