1 <?php namespace BookStack\Repos;
4 use Illuminate\Support\Str;
13 * ChapterRepo constructor.
16 public function __construct(Chapter $chapter)
18 $this->chapter = $chapter;
21 public function idExists($id)
23 return $this->chapter->where('id', '=', $id)->count() > 0;
26 public function getById($id)
28 return $this->chapter->findOrFail($id);
31 public function getAll()
33 return $this->chapter->all();
36 public function getBySlug($slug, $bookId)
38 return $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
41 public function newFromInput($input)
43 return $this->chapter->fill($input);
46 public function destroyById($id)
48 $page = $this->getById($id);
52 public function doesSlugExist($slug, $bookId, $currentId = false)
54 $query = $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId);
56 $query = $query->where('id', '!=', $currentId);
58 return $query->count() > 0;
61 public function findSuitableSlug($name, $bookId, $currentId = false)
63 $slug = Str::slug($name);
64 while($this->doesSlugExist($slug, $bookId, $currentId)) {
65 $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
70 public function getBySearch($term, $whereTerms = [])
72 $terms = explode(' ', preg_quote(trim($term)));
73 $chapters = $this->chapter->fullTextSearch(['name', 'description'], $terms, $whereTerms);
74 $words = join('|', $terms);
75 foreach ($chapters as $chapter) {
77 $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $chapter->getExcerpt(100));
78 $chapter->searchSnippet = $result;
83 public function setBookId($bookId, Chapter $chapter)
85 $chapter->book_id = $bookId;
86 foreach($chapter->activity as $activity) {
87 $activity->book_id = $bookId;