]> BookStack Code Mirror - bookstack/blobdiff - app/Repos/ChapterRepo.php
replace GPL diff lib with MIT lib
[bookstack] / app / Repos / ChapterRepo.php
index 0980e93a7b69027e027e40e88700921d4dac9e22..3c518bde9de3f01a0c6a056ffdcc50cb5f27c772 100644 (file)
@@ -9,6 +9,18 @@ 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 permissions into account.
      * @return mixed
@@ -168,8 +180,9 @@ class ChapterRepo extends EntityRepo
     public function getBySearch($term, $whereTerms = [], $count = 20, $paginationAppends = [])
     {
         $terms = $this->prepareSearchTerms($term);
-        $chapters = $this->permissionService->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
@@ -188,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;
     }