]> BookStack Code Mirror - bookstack/blobdiff - app/Repos/ChapterRepo.php
replace GPL diff lib with MIT lib
[bookstack] / app / Repos / ChapterRepo.php
index 530f550b1478342712ad572fb76318250346327a..3c518bde9de3f01a0c6a056ffdcc50cb5f27c772 100644 (file)
@@ -2,19 +2,32 @@
 
 
 use Activity;
+use BookStack\Book;
 use BookStack\Exceptions\NotFoundException;
 use Illuminate\Support\Str;
 use BookStack\Chapter;
 
 class ChapterRepo extends EntityRepo
 {
+    protected $pageRepo;
+
+    /**
+     * ChapterRepo constructor.
+     * @param $pageRepo
+     */
+    public function __construct(PageRepo $pageRepo)
+    {
+        $this->pageRepo = $pageRepo;
+        parent::__construct();
+    }
+
     /**
-     * Base query for getting chapters, Takes restrictions into account.
+     * Base query for getting chapters, Takes permissions into account.
      * @return mixed
      */
     private function chapterQuery()
     {
-        return $this->restrictionService->enforceChapterRestrictions($this->chapter, 'view');
+        return $this->permissionService->enforceChapterRestrictions($this->chapter, 'view');
     }
 
     /**
@@ -66,7 +79,7 @@ class ChapterRepo extends EntityRepo
      */
     public function getChildren(Chapter $chapter)
     {
-        $pages = $this->restrictionService->enforcePageRestrictions($chapter->pages())->get();
+        $pages = $this->permissionService->enforcePageRestrictions($chapter->pages())->get();
         // Sort items with drafts first then by priority.
         return $pages->sortBy(function($child, $key) {
             $score = $child->priority;
@@ -78,11 +91,18 @@ class ChapterRepo extends EntityRepo
     /**
      * Create a new chapter from request input.
      * @param $input
-     * @return $this
+     * @param Book $book
+     * @return Chapter
      */
-    public function newFromInput($input)
+    public function createFromInput($input, Book $book)
     {
-        return $this->chapter->fill($input);
+        $chapter = $this->chapter->newInstance($input);
+        $chapter->slug = $this->findSuitableSlug($chapter->name, $book->id);
+        $chapter->created_by = auth()->user()->id;
+        $chapter->updated_by = auth()->user()->id;
+        $chapter = $book->chapters()->save($chapter);
+        $this->permissionService->buildJointPermissionsForEntity($chapter);
+        return $chapter;
     }
 
     /**
@@ -99,7 +119,8 @@ class ChapterRepo extends EntityRepo
         }
         Activity::removeEntity($chapter);
         $chapter->views()->delete();
-        $chapter->restrictions()->delete();
+        $chapter->permissions()->delete();
+        $this->permissionService->deleteJointPermissionsForEntity($chapter);
         $chapter->delete();
     }
 
@@ -159,8 +180,9 @@ class ChapterRepo extends EntityRepo
     public function getBySearch($term, $whereTerms = [], $count = 20, $paginationAppends = [])
     {
         $terms = $this->prepareSearchTerms($term);
-        $chapters = $this->restrictionService->enforceChapterRestrictions($this->chapter->fullTextSearchQuery(['name', 'description'], $terms, $whereTerms))
-            ->paginate($count)->appends($paginationAppends);
+        $chapterQuery = $this->permissionService->enforceChapterRestrictions($this->chapter->fullTextSearchQuery(['name', 'description'], $terms, $whereTerms));
+        $chapterQuery = $this->addAdvancedSearchQueries($chapterQuery, $term);
+        $chapters = $chapterQuery->paginate($count)->appends($paginationAppends);
         $words = join('|', explode(' ', preg_quote(trim($term), '/')));
         foreach ($chapters as $chapter) {
             //highlight
@@ -179,12 +201,21 @@ class ChapterRepo extends EntityRepo
     public function changeBook($bookId, Chapter $chapter)
     {
         $chapter->book_id = $bookId;
+        // Update related activity
         foreach ($chapter->activity as $activity) {
             $activity->book_id = $bookId;
             $activity->save();
         }
         $chapter->slug = $this->findSuitableSlug($chapter->name, $bookId, $chapter->id);
         $chapter->save();
+        // Update all child pages
+        foreach ($chapter->pages as $page) {
+            $this->pageRepo->changeBook($bookId, $page);
+        }
+        // Update permissions
+        $chapter->load('book');
+        $this->permissionService->buildJointPermissionsForEntity($chapter->book);
+
         return $chapter;
     }