1 <?php namespace BookStack\Repos;
5 use BookStack\Exceptions\NotFoundException;
6 use Illuminate\Support\Str;
9 class ChapterRepo extends EntityRepo
12 * Base query for getting chapters, Takes restrictions into account.
15 private function chapterQuery()
17 return $this->restrictionService->enforceChapterRestrictions($this->chapter, 'view');
21 * Check if an id exists.
25 public function idExists($id)
27 return $this->chapterQuery()->where('id', '=', $id)->count() > 0;
31 * Get a chapter by a specific id.
35 public function getById($id)
37 return $this->chapterQuery()->findOrFail($id);
42 * @return \Illuminate\Database\Eloquent\Collection|static[]
44 public function getAll()
46 return $this->chapterQuery()->all();
50 * Get a chapter that has the given slug within the given book.
54 * @throws NotFoundException
56 public function getBySlug($slug, $bookId)
58 $chapter = $this->chapterQuery()->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
59 if ($chapter === null) throw new NotFoundException('Chapter not found');
64 * Get the child items for a chapter
65 * @param Chapter $chapter
67 public function getChildren(Chapter $chapter)
69 $pages = $this->restrictionService->enforcePageRestrictions($chapter->pages())->get();
70 // Sort items with drafts first then by priority.
71 return $pages->sortBy(function($child, $key) {
72 $score = $child->priority;
73 if ($child->draft) $score -= 100;
79 * Create a new chapter from request input.
83 public function newFromInput($input)
85 return $this->chapter->fill($input);
89 * Destroy a chapter and its relations by providing its slug.
90 * @param Chapter $chapter
92 public function destroy(Chapter $chapter)
94 if (count($chapter->pages) > 0) {
95 foreach ($chapter->pages as $page) {
96 $page->chapter_id = 0;
100 Activity::removeEntity($chapter);
101 $chapter->views()->delete();
102 $chapter->restrictions()->delete();
107 * Check if a chapter's slug exists.
110 * @param bool|false $currentId
113 public function doesSlugExist($slug, $bookId, $currentId = false)
115 $query = $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId);
117 $query = $query->where('id', '!=', $currentId);
119 return $query->count() > 0;
123 * Finds a suitable slug for the provided name.
124 * Checks database to prevent duplicate slugs.
127 * @param bool|false $currentId
130 public function findSuitableSlug($name, $bookId, $currentId = false)
132 $slug = Str::slug($name);
133 while ($this->doesSlugExist($slug, $bookId, $currentId)) {
134 $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
140 * Get chapters by the given search term.
141 * @param string $term
142 * @param array $whereTerms
144 * @param array $paginationAppends
147 public function getBySearch($term, $whereTerms = [], $count = 20, $paginationAppends = [])
149 $terms = $this->prepareSearchTerms($term);
150 $chapters = $this->restrictionService->enforceChapterRestrictions($this->chapter->fullTextSearchQuery(['name', 'description'], $terms, $whereTerms))
151 ->paginate($count)->appends($paginationAppends);
152 $words = join('|', explode(' ', preg_quote(trim($term), '/')));
153 foreach ($chapters as $chapter) {
155 $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $chapter->getExcerpt(100));
156 $chapter->searchSnippet = $result;
162 * Changes the book relation of this chapter.
164 * @param Chapter $chapter
167 public function changeBook($bookId, Chapter $chapter)
169 $chapter->book_id = $bookId;
170 foreach ($chapter->activity as $activity) {
171 $activity->book_id = $bookId;
174 $chapter->slug = $this->findSuitableSlug($chapter->name, $bookId, $chapter->id);