]> BookStack Code Mirror - bookstack/blobdiff - app/Repos/ChapterRepo.php
Merge branch 'master' into translations
[bookstack] / app / Repos / ChapterRepo.php
index 0980e93a7b69027e027e40e88700921d4dac9e22..4106f93ee32e27ee7f3bfde0b759b11c02014a08 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
@@ -57,7 +69,7 @@ class ChapterRepo extends EntityRepo
     public function getBySlug($slug, $bookId)
     {
         $chapter = $this->chapterQuery()->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
-        if ($chapter === null) throw new NotFoundException('Chapter not found');
+        if ($chapter === null) throw new NotFoundException(trans('errors.chapter_not_found'));
         return $chapter;
     }
 
@@ -69,7 +81,7 @@ class ChapterRepo extends EntityRepo
     {
         $pages = $this->permissionService->enforcePageRestrictions($chapter->pages())->get();
         // Sort items with drafts first then by priority.
-        return $pages->sortBy(function($child, $key) {
+        return $pages->sortBy(function ($child, $key) {
             $score = $child->priority;
             if ($child->draft) $score -= 100;
             return $score;
@@ -86,8 +98,8 @@ class ChapterRepo extends EntityRepo
     {
         $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->created_by = user()->id;
+        $chapter->updated_by = user()->id;
         $chapter = $book->chapters()->save($chapter);
         $this->permissionService->buildJointPermissionsForEntity($chapter);
         return $chapter;
@@ -138,7 +150,7 @@ class ChapterRepo extends EntityRepo
      */
     public function findSuitableSlug($name, $bookId, $currentId = false)
     {
-        $slug = Str::slug($name);
+        $slug = $this->nameToSlug($name);
         while ($this->doesSlugExist($slug, $bookId, $currentId)) {
             $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
         }
@@ -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
@@ -181,19 +194,32 @@ class ChapterRepo extends EntityRepo
 
     /**
      * Changes the book relation of this chapter.
-     * @param         $bookId
+     * @param $bookId
      * @param Chapter $chapter
+     * @param bool $rebuildPermissions
      * @return Chapter
      */
-    public function changeBook($bookId, Chapter $chapter)
+    public function changeBook($bookId, Chapter $chapter, $rebuildPermissions = false)
     {
         $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 if applicable
+        if ($rebuildPermissions) {
+            $chapter->load('book');
+            $this->permissionService->buildJointPermissionsForEntity($chapter->book);
+        }
+
         return $chapter;
     }