1 <?php namespace BookStack\Repos;
4 use BookStack\Exceptions\NotFoundException;
5 use Illuminate\Database\Eloquent\Collection;
6 use Illuminate\Support\Str;
10 class BookRepo extends EntityRepo
13 protected $chapterRepo;
16 * BookRepo constructor.
17 * @param PageRepo $pageRepo
18 * @param ChapterRepo $chapterRepo
20 public function __construct(PageRepo $pageRepo, ChapterRepo $chapterRepo)
22 $this->pageRepo = $pageRepo;
23 $this->chapterRepo = $chapterRepo;
24 parent::__construct();
28 * Base query for getting books.
29 * Takes into account any restrictions.
32 private function bookQuery()
34 return $this->permissionService->enforceBookRestrictions($this->book, 'view');
38 * Get the book that has the given id.
42 public function getById($id)
44 return $this->bookQuery()->findOrFail($id);
48 * Get all books, Limited by count.
52 public function getAll($count = 10)
54 $bookQuery = $this->bookQuery()->orderBy('name', 'asc');
55 if (!$count) return $bookQuery->get();
56 return $bookQuery->take($count)->get();
60 * Get all books paginated.
64 public function getAllPaginated($count = 10)
66 return $this->bookQuery()
67 ->orderBy('name', 'asc')->paginate($count);
72 * Get the latest books.
76 public function getLatest($count = 10)
78 return $this->bookQuery()->orderBy('created_at', 'desc')->take($count)->get();
82 * Gets the most recently viewed for a user.
87 public function getRecentlyViewed($count = 10, $page = 0)
89 return Views::getUserRecentlyViewed($count, $page, $this->book);
93 * Gets the most viewed books.
98 public function getPopular($count = 10, $page = 0)
100 return Views::getPopular($count, $page, $this->book);
107 * @throws NotFoundException
109 public function getBySlug($slug)
111 $book = $this->bookQuery()->where('slug', '=', $slug)->first();
112 if ($book === null) throw new NotFoundException(trans('errors.book_not_found'));
117 * Checks if a book exists.
121 public function exists($id)
123 return $this->bookQuery()->where('id', '=', $id)->exists();
127 * Get a new book instance from request input.
128 * @param array $input
131 public function createFromInput($input)
133 $book = $this->book->newInstance($input);
134 $book->slug = $this->findSuitableSlug($book->name);
135 $book->created_by = user()->id;
136 $book->updated_by = user()->id;
138 $this->permissionService->buildJointPermissionsForEntity($book);
143 * Update the given book from user input.
148 public function updateFromInput(Book $book, $input)
150 if ($book->name !== $input['name']) {
151 $book->slug = $this->findSuitableSlug($input['name'], $book->id);
154 $book->updated_by = user()->id;
156 $this->permissionService->buildJointPermissionsForEntity($book);
161 * Destroy the given book.
165 public function destroy(Book $book)
167 foreach ($book->pages as $page) {
168 $this->pageRepo->destroy($page);
170 foreach ($book->chapters as $chapter) {
171 $this->chapterRepo->destroy($chapter);
173 $book->views()->delete();
174 $book->permissions()->delete();
175 $this->permissionService->deleteJointPermissionsForEntity($book);
180 * Get the next child element priority.
184 public function getNewPriority($book)
186 $lastElem = $this->getChildren($book)->pop();
187 return $lastElem ? $lastElem->priority + 1 : 0;
191 * @param string $slug
192 * @param bool|false $currentId
195 public function doesSlugExist($slug, $currentId = false)
197 $query = $this->book->where('slug', '=', $slug);
199 $query = $query->where('id', '!=', $currentId);
201 return $query->count() > 0;
205 * Provides a suitable slug for the given book name.
206 * Ensures the returned slug is unique in the system.
207 * @param string $name
208 * @param bool|false $currentId
211 public function findSuitableSlug($name, $currentId = false)
213 $slug = $this->nameToSlug($name);
214 while ($this->doesSlugExist($slug, $currentId)) {
215 $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
221 * Get all child objects of a book.
222 * Returns a sorted collection of Pages and Chapters.
223 * Loads the book slug onto child elements to prevent access database access for getting the slug.
225 * @param bool $filterDrafts
228 public function getChildren(Book $book, $filterDrafts = false)
230 $pageQuery = $book->pages()->where('chapter_id', '=', 0);
231 $pageQuery = $this->permissionService->enforcePageRestrictions($pageQuery, 'view');
234 $pageQuery = $pageQuery->where('draft', '=', false);
237 $pages = $pageQuery->get();
239 $chapterQuery = $book->chapters()->with(['pages' => function ($query) use ($filterDrafts) {
240 $this->permissionService->enforcePageRestrictions($query, 'view');
241 if ($filterDrafts) $query->where('draft', '=', false);
243 $chapterQuery = $this->permissionService->enforceChapterRestrictions($chapterQuery, 'view');
244 $chapters = $chapterQuery->get();
245 $children = $pages->values();
246 foreach ($chapters as $chapter) {
247 $children->push($chapter);
249 $bookSlug = $book->slug;
251 $children->each(function ($child) use ($bookSlug) {
252 $child->setAttribute('bookSlug', $bookSlug);
253 if ($child->isA('chapter')) {
254 $child->pages->each(function ($page) use ($bookSlug) {
255 $page->setAttribute('bookSlug', $bookSlug);
257 $child->pages = $child->pages->sortBy(function ($child, $key) {
258 $score = $child->priority;
259 if ($child->draft) $score -= 100;
265 // Sort items with drafts first then by priority.
266 return $children->sortBy(function ($child, $key) {
267 $score = $child->priority;
268 if ($child->isA('page') && $child->draft) $score -= 100;
274 * Get books by search term.
277 * @param array $paginationAppends
280 public function getBySearch($term, $count = 20, $paginationAppends = [])
282 $terms = $this->prepareSearchTerms($term);
283 $bookQuery = $this->permissionService->enforceBookRestrictions($this->book->fullTextSearchQuery(['name', 'description'], $terms));
284 $bookQuery = $this->addAdvancedSearchQueries($bookQuery, $term);
285 $books = $bookQuery->paginate($count)->appends($paginationAppends);
286 $words = join('|', explode(' ', preg_quote(trim($term), '/')));
287 foreach ($books as $book) {
289 $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $book->getExcerpt(100));
290 $book->searchSnippet = $result;