]> BookStack Code Mirror - bookstack/blobdiff - app/Entities/Tools/BookContents.php
Guest create page: name field autofocus
[bookstack] / app / Entities / Tools / BookContents.php
index 99602de414034a44ca9db88a4cd47655d814254f..f45bdfcc1b98fabcdb2d4dc774c30752a7582ef1 100644 (file)
@@ -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<string, Entity>
      */
     protected function loadModelsFromSortMap(BookSortMap $sortMap): array
@@ -272,8 +267,8 @@ class BookContents
         $modelMap = [];
         $ids = [
             'chapter' => [],
-            'page' => [],
-            'book' => [],
+            'page'    => [],
+            'book'    => [],
         ];
 
         foreach ($sortMap->all() as $sortMapItem) {