X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/8e6248f57f92d943a011c3219120d60ee4f2f00b..refs/pull/234/head:/app/Repos/ChapterRepo.php diff --git a/app/Repos/ChapterRepo.php b/app/Repos/ChapterRepo.php index 095596a60..4106f93ee 100644 --- a/app/Repos/ChapterRepo.php +++ b/app/Repos/ChapterRepo.php @@ -2,35 +2,32 @@ use Activity; +use BookStack\Book; use BookStack\Exceptions\NotFoundException; -use BookStack\Services\RestrictionService; use Illuminate\Support\Str; use BookStack\Chapter; -class ChapterRepo +class ChapterRepo extends EntityRepo { - - protected $chapter; - protected $restrictionService; + protected $pageRepo; /** * ChapterRepo constructor. - * @param Chapter $chapter - * @param RestrictionService $restrictionService + * @param $pageRepo */ - public function __construct(Chapter $chapter, RestrictionService $restrictionService) + public function __construct(PageRepo $pageRepo) { - $this->chapter = $chapter; - $this->restrictionService = $restrictionService; + $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'); } /** @@ -72,7 +69,7 @@ class ChapterRepo 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; } @@ -82,17 +79,30 @@ class ChapterRepo */ public function getChildren(Chapter $chapter) { - return $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; + 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; } /** @@ -109,7 +119,8 @@ class ChapterRepo } Activity::removeEntity($chapter); $chapter->views()->delete(); - $chapter->restrictions()->delete(); + $chapter->permissions()->delete(); + $this->permissionService->deleteJointPermissionsForEntity($chapter); $chapter->delete(); } @@ -139,16 +150,28 @@ class ChapterRepo */ 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); } 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 @@ -156,9 +179,10 @@ class ChapterRepo */ public function getBySearch($term, $whereTerms = [], $count = 20, $paginationAppends = []) { - $terms = explode(' ', $term); - $chapters = $this->restrictionService->enforceChapterRestrictions($this->chapter->fullTextSearchQuery(['name', 'description'], $terms, $whereTerms)) - ->paginate($count)->appends($paginationAppends); + $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 @@ -170,43 +194,33 @@ class ChapterRepo /** * 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(); - return $chapter; - } + // Update all child pages + foreach ($chapter->pages as $page) { + $this->pageRepo->changeBook($bookId, $page); + } - /** - * Updates pages restrictions from a request - * @param $request - * @param $chapter - */ - public function updateRestrictionsFromRequest($request, $chapter) - { - // TODO - extract into shared repo - $chapter->restricted = $request->has('restricted') && $request->get('restricted') === 'true'; - $chapter->restrictions()->delete(); - if ($request->has('restrictions')) { - foreach($request->get('restrictions') as $roleId => $restrictions) { - foreach ($restrictions as $action => $value) { - $chapter->restrictions()->create([ - 'role_id' => $roleId, - 'action' => strtolower($action) - ]); - } - } + // Update permissions if applicable + if ($rebuildPermissions) { + $chapter->load('book'); + $this->permissionService->buildJointPermissionsForEntity($chapter->book); } - $chapter->save(); + + return $chapter; } } \ No newline at end of file