X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/61ae96c5f84acfad45aea8077783de6a88c08bc5..refs/pull/234/head:/app/Repos/ChapterRepo.php diff --git a/app/Repos/ChapterRepo.php b/app/Repos/ChapterRepo.php index db2a72d96..4106f93ee 100644 --- a/app/Repos/ChapterRepo.php +++ b/app/Repos/ChapterRepo.php @@ -2,21 +2,32 @@ use Activity; +use BookStack\Book; +use BookStack\Exceptions\NotFoundException; use Illuminate\Support\Str; use BookStack\Chapter; -class ChapterRepo +class ChapterRepo extends EntityRepo { - - protected $chapter; + protected $pageRepo; /** * ChapterRepo constructor. - * @param $chapter + * @param $pageRepo + */ + public function __construct(PageRepo $pageRepo) + { + $this->pageRepo = $pageRepo; + parent::__construct(); + } + + /** + * Base query for getting chapters, Takes permissions into account. + * @return mixed */ - public function __construct(Chapter $chapter) + private function chapterQuery() { - $this->chapter = $chapter; + return $this->permissionService->enforceChapterRestrictions($this->chapter, 'view'); } /** @@ -26,7 +37,7 @@ class ChapterRepo */ public function idExists($id) { - return $this->chapter->where('id', '=', $id)->count() > 0; + return $this->chapterQuery()->where('id', '=', $id)->count() > 0; } /** @@ -36,7 +47,7 @@ class ChapterRepo */ public function getById($id) { - return $this->chapter->findOrFail($id); + return $this->chapterQuery()->findOrFail($id); } /** @@ -45,7 +56,7 @@ class ChapterRepo */ public function getAll() { - return $this->chapter->all(); + return $this->chapterQuery()->all(); } /** @@ -53,20 +64,45 @@ class ChapterRepo * @param $slug * @param $bookId * @return mixed + * @throws NotFoundException */ public function getBySlug($slug, $bookId) { - return $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first(); + $chapter = $this->chapterQuery()->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first(); + if ($chapter === null) throw new NotFoundException(trans('errors.chapter_not_found')); + return $chapter; + } + + /** + * Get the child items for a chapter + * @param Chapter $chapter + */ + public function getChildren(Chapter $chapter) + { + $pages = $this->permissionService->enforcePageRestrictions($chapter->pages())->get(); + // Sort items with drafts first then by priority. + return $pages->sortBy(function ($child, $key) { + $score = $child->priority; + if ($child->draft) $score -= 100; + return $score; + }); } /** * 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 = user()->id; + $chapter->updated_by = user()->id; + $chapter = $book->chapters()->save($chapter); + $this->permissionService->buildJointPermissionsForEntity($chapter); + return $chapter; } /** @@ -83,6 +119,8 @@ class ChapterRepo } Activity::removeEntity($chapter); $chapter->views()->delete(); + $chapter->permissions()->delete(); + $this->permissionService->deleteJointPermissionsForEntity($chapter); $chapter->delete(); } @@ -96,7 +134,7 @@ class ChapterRepo public function doesSlugExist($slug, $bookId, $currentId = false) { $query = $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId); - if($currentId) { + if ($currentId) { $query = $query->where('id', '!=', $currentId); } return $query->count() > 0; @@ -112,24 +150,40 @@ class ChapterRepo */ public function findSuitableSlug($name, $bookId, $currentId = false) { - $slug = Str::slug($name); - while($this->doesSlugExist($slug, $bookId, $currentId)) { + $slug = $this->nameToSlug($name); + while ($this->doesSlugExist($slug, $bookId, $currentId)) { $slug .= '-' . substr(md5(rand(1, 500)), 0, 3); } return $slug; } + /** + * Get a new priority value for a new page to be added + * to the given chapter. + * @param Chapter $chapter + * @return int + */ + public function getNewPriority(Chapter $chapter) + { + $lastPage = $chapter->pages->last(); + return $lastPage !== null ? $lastPage->priority + 1 : 0; + } + /** * Get chapters by the given search term. - * @param $term + * @param string $term * @param array $whereTerms + * @param int $count + * @param array $paginationAppends * @return mixed */ - public function getBySearch($term, $whereTerms = []) + public function getBySearch($term, $whereTerms = [], $count = 20, $paginationAppends = []) { - $terms = explode(' ', preg_quote(trim($term))); - $chapters = $this->chapter->fullTextSearch(['name', 'description'], $terms, $whereTerms); - $words = join('|', $terms); + $terms = $this->prepareSearchTerms($term); + $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 $result = preg_replace('#' . $words . '#iu', "\$0", $chapter->getExcerpt(100)); @@ -139,19 +193,33 @@ class ChapterRepo } /** - * Sets a chapters book id. - * @param $bookId + * Changes the book relation of this chapter. + * @param $bookId * @param Chapter $chapter + * @param bool $rebuildPermissions * @return Chapter */ - public function setBookId($bookId, Chapter $chapter) + public function changeBook($bookId, Chapter $chapter, $rebuildPermissions = false) { $chapter->book_id = $bookId; - foreach($chapter->activity as $activity) { + // 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; }