1 <?php namespace BookStack\Repos;
5 use BookStack\Services\RestrictionService;
6 use Illuminate\Support\Str;
13 protected $restrictionService;
16 * ChapterRepo constructor.
17 * @param Chapter $chapter
18 * @param RestrictionService $restrictionService
20 public function __construct(Chapter $chapter, RestrictionService $restrictionService)
22 $this->chapter = $chapter;
23 $this->restrictionService = $restrictionService;
27 * Base query for getting chapters, Takes restrictions into account.
30 private function chapterQuery()
32 return $this->restrictionService->enforceChapterRestrictions($this->chapter, 'view');
36 * Check if an id exists.
40 public function idExists($id)
42 return $this->chapterQuery()->where('id', '=', $id)->count() > 0;
46 * Get a chapter by a specific id.
50 public function getById($id)
52 return $this->chapterQuery()->findOrFail($id);
57 * @return \Illuminate\Database\Eloquent\Collection|static[]
59 public function getAll()
61 return $this->chapterQuery()->all();
65 * Get a chapter that has the given slug within the given book.
70 public function getBySlug($slug, $bookId)
72 $chapter = $this->chapterQuery()->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
73 if ($chapter === null) abort(404);
78 * Create a new chapter from request input.
82 public function newFromInput($input)
84 return $this->chapter->fill($input);
88 * Destroy a chapter and its relations by providing its slug.
89 * @param Chapter $chapter
91 public function destroy(Chapter $chapter)
93 if (count($chapter->pages) > 0) {
94 foreach ($chapter->pages as $page) {
95 $page->chapter_id = 0;
99 Activity::removeEntity($chapter);
100 $chapter->views()->delete();
105 * Check if a chapter's slug exists.
108 * @param bool|false $currentId
111 public function doesSlugExist($slug, $bookId, $currentId = false)
113 $query = $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId);
115 $query = $query->where('id', '!=', $currentId);
117 return $query->count() > 0;
121 * Finds a suitable slug for the provided name.
122 * Checks database to prevent duplicate slugs.
125 * @param bool|false $currentId
128 public function findSuitableSlug($name, $bookId, $currentId = false)
130 $slug = Str::slug($name);
131 while ($this->doesSlugExist($slug, $bookId, $currentId)) {
132 $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
138 * Get chapters by the given search term.
140 * @param array $whereTerms
142 * @param array $paginationAppends
145 public function getBySearch($term, $whereTerms = [], $count = 20, $paginationAppends = [])
147 $terms = explode(' ', $term);
148 $chapters = $this->restrictionService->enforceChapterRestrictions($this->chapter->fullTextSearchQuery(['name', 'description'], $terms, $whereTerms))
149 ->paginate($count)->appends($paginationAppends);
150 $words = join('|', explode(' ', preg_quote(trim($term), '/')));
151 foreach ($chapters as $chapter) {
153 $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $chapter->getExcerpt(100));
154 $chapter->searchSnippet = $result;
160 * Changes the book relation of this chapter.
162 * @param Chapter $chapter
165 public function changeBook($bookId, Chapter $chapter)
167 $chapter->book_id = $bookId;
168 foreach ($chapter->activity as $activity) {
169 $activity->book_id = $bookId;
172 $chapter->slug = $this->findSuitableSlug($chapter->name, $bookId, $chapter->id);
178 * Updates pages restrictions from a request
182 public function updateRestrictionsFromRequest($request, $chapter)
184 // TODO - extract into shared repo
185 $chapter->restricted = $request->has('restricted') && $request->get('restricted') === 'true';
186 $chapter->restrictions()->delete();
187 if ($request->has('restrictions')) {
188 foreach($request->get('restrictions') as $roleId => $restrictions) {
189 foreach ($restrictions as $action => $value) {
190 $chapter->restrictions()->create([
191 'role_id' => $roleId,
192 'action' => strtolower($action)