]> BookStack Code Mirror - bookstack/blobdiff - app/Entities/Repos/PageRepo.php
Show bookshelves that a book belongs to on a book view
[bookstack] / app / Entities / Repos / PageRepo.php
index ed142eb611b53f3d715722ded3bf12bdb2e4455f..0e0585a8549ab95f755f29143c9c267b4f0cee54 100644 (file)
@@ -9,7 +9,6 @@ use Carbon\Carbon;
 use DOMDocument;
 use DOMElement;
 use DOMXPath;
-use Illuminate\Support\Collection;
 
 class PageRepo extends EntityRepo
 {
@@ -21,9 +20,9 @@ class PageRepo extends EntityRepo
      * @return Page
      * @throws \BookStack\Exceptions\NotFoundException
      */
-    public function getPageBySlug(string $pageSlug, string $bookSlug)
+    public function getBySlug(string $pageSlug, string $bookSlug)
     {
-        return $this->getBySlug('page', $pageSlug, $bookSlug);
+        return $this->getEntityBySlug('page', $pageSlug, $bookSlug);
     }
 
     /**
@@ -60,11 +59,6 @@ class PageRepo extends EntityRepo
         $oldHtml = $page->html;
         $oldName = $page->name;
 
-        // Prevent slug being updated if no name change
-        if ($page->name !== $input['name']) {
-            $page->slug = $this->findSuitableSlug('page', $input['name'], $page->id, $book_id);
-        }
-
         // Save page tags if present
         if (isset($input['tags'])) {
             $this->tagRepo->saveTagsToEntity($page, $input['tags']);
@@ -79,11 +73,17 @@ class PageRepo extends EntityRepo
         $page->fill($input);
         $page->html = $this->formatHtml($input['html']);
         $page->text = $this->pageToPlainText($page);
+        $page->updated_by = $userId;
+        $page->revision_count++;
+
         if (setting('app-editor') !== 'markdown') {
             $page->markdown = '';
         }
-        $page->updated_by = $userId;
-        $page->revision_count++;
+
+        if ($page->isDirty('name')) {
+            $page->refreshSlug();
+        }
+
         $page->save();
 
         // Remove all update drafts for this user & page.
@@ -242,8 +242,7 @@ class PageRepo extends EntityRepo
         }
 
         $book->pages()->save($page);
-        $page = $this->entityProvider->page->find($page->id);
-        $this->permissionService->buildJointPermissionsForEntity($page);
+        $page->refresh()->rebuildPermissions();
         return $page;
     }
 
@@ -310,12 +309,11 @@ class PageRepo extends EntityRepo
             $draftPage->template = ($input['template'] === 'true');
         }
 
-        $draftPage->slug = $this->findSuitableSlug('page', $draftPage->name, false, $draftPage->book->id);
         $draftPage->html = $this->formatHtml($input['html']);
         $draftPage->text = $this->pageToPlainText($draftPage);
         $draftPage->draft = false;
         $draftPage->revision_count = 1;
-
+        $draftPage->refreshSlug();
         $draftPage->save();
         $this->savePageRevision($draftPage, trans('entities.pages_initial_revision'));
         $this->searchService->indexEntity($draftPage);
@@ -432,7 +430,7 @@ class PageRepo extends EntityRepo
             return [];
         }
 
-        $tree = collect($headers)->map(function($header) {
+        $tree = collect($headers)->map(function ($header) {
             $text = trim(str_replace("\xc2\xa0", '', $header->nodeValue));
             $text = mb_substr($text, 0, 100);
 
@@ -442,7 +440,7 @@ class PageRepo extends EntityRepo
                 'link' => '#' . $header->getAttribute('id'),
                 'text' => $text,
             ];
-        })->filter(function($header) {
+        })->filter(function ($header) {
             return mb_strlen($header['text']) > 0;
         });
 
@@ -468,12 +466,14 @@ class PageRepo extends EntityRepo
     {
         $page->revision_count++;
         $this->savePageRevision($page);
+
         $revision = $page->revisions()->where('id', '=', $revisionId)->first();
         $page->fill($revision->toArray());
-        $page->slug = $this->findSuitableSlug('page', $page->name, $page->id, $book->id);
         $page->text = $this->pageToPlainText($page);
         $page->updated_by = user()->id;
+        $page->refreshSlug();
         $page->save();
+
         $this->searchService->indexEntity($page);
         return $page;
     }
@@ -482,18 +482,19 @@ class PageRepo extends EntityRepo
      * Change the page's parent to the given entity.
      * @param Page $page
      * @param Entity $parent
-     * @throws \Throwable
      */
     public function changePageParent(Page $page, Entity $parent)
     {
         $book = $parent->isA('book') ? $parent : $parent->book;
         $page->chapter_id = $parent->isA('chapter') ? $parent->id : 0;
         $page->save();
+
         if ($page->book->id !== $book->id) {
-            $page = $this->changeBook('page', $book->id, $page);
+            $page = $this->changeBook($page, $book->id);
         }
+
         $page->load('book');
-        $this->permissionService->buildJointPermissionsForEntity($book);
+        $book->rebuildPermissions();
     }
 
     /**
@@ -541,12 +542,12 @@ class PageRepo extends EntityRepo
      * @param string $search
      * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
      */
-    public function getPageTemplates(int $count = 10, int $page = 1,  string $search = '')
+    public function getPageTemplates(int $count = 10, int $page = 1, string $search = '')
     {
         $query = $this->entityQuery('page')
             ->where('template', '=', true)
             ->orderBy('name', 'asc')
-            ->skip( ($page - 1) * $count)
+            ->skip(($page - 1) * $count)
             ->take($count);
 
         if ($search) {