1 <?php namespace BookStack\Repos;
4 use BookStack\Exceptions\NotFoundException;
5 use Illuminate\Support\Str;
9 class BookRepo extends EntityRepo
12 protected $chapterRepo;
15 * BookRepo constructor.
16 * @param PageRepo $pageRepo
17 * @param ChapterRepo $chapterRepo
19 public function __construct(PageRepo $pageRepo, ChapterRepo $chapterRepo)
21 $this->pageRepo = $pageRepo;
22 $this->chapterRepo = $chapterRepo;
23 parent::__construct();
27 * Base query for getting books.
28 * Takes into account any restrictions.
31 private function bookQuery()
33 return $this->permissionService->enforceBookRestrictions($this->book, 'view');
37 * Get the book that has the given id.
41 public function getById($id)
43 return $this->bookQuery()->findOrFail($id);
47 * Get all books, Limited by count.
51 public function getAll($count = 10)
53 $bookQuery = $this->bookQuery()->orderBy('name', 'asc');
54 if (!$count) return $bookQuery->get();
55 return $bookQuery->take($count)->get();
59 * Get all books paginated.
63 public function getAllPaginated($count = 10)
65 return $this->bookQuery()
66 ->orderBy('name', 'asc')->paginate($count);
71 * Get the latest books.
75 public function getLatest($count = 10)
77 return $this->bookQuery()->orderBy('created_at', 'desc')->take($count)->get();
81 * Gets the most recently viewed for a user.
86 public function getRecentlyViewed($count = 10, $page = 0)
88 return Views::getUserRecentlyViewed($count, $page, $this->book);
92 * Gets the most viewed books.
97 public function getPopular($count = 10, $page = 0)
99 return Views::getPopular($count, $page, $this->book);
106 * @throws NotFoundException
108 public function getBySlug($slug)
110 $book = $this->bookQuery()->where('slug', '=', $slug)->first();
111 if ($book === null) throw new NotFoundException('Book not found');
116 * Checks if a book exists.
120 public function exists($id)
122 return $this->bookQuery()->where('id', '=', $id)->exists();
126 * Get a new book instance from request input.
127 * @param array $input
130 public function createFromInput($input)
132 $book = $this->book->newInstance($input);
133 $book->slug = $this->findSuitableSlug($book->name);
134 $book->created_by = auth()->user()->id;
135 $book->updated_by = auth()->user()->id;
137 $this->permissionService->buildJointPermissionsForEntity($book);
142 * Update the given book from user input.
147 public function updateFromInput(Book $book, $input)
150 $book->slug = $this->findSuitableSlug($book->name, $book->id);
151 $book->updated_by = auth()->user()->id;
153 $this->permissionService->buildJointPermissionsForEntity($book);
158 * Destroy the given book.
162 public function destroy(Book $book)
164 foreach ($book->pages as $page) {
165 $this->pageRepo->destroy($page);
167 foreach ($book->chapters as $chapter) {
168 $this->chapterRepo->destroy($chapter);
170 $book->views()->delete();
171 $book->permissions()->delete();
172 $this->permissionService->deleteJointPermissionsForEntity($book);
177 * Alias method to update the book jointPermissions in the PermissionService.
180 public function updateBookPermissions(Book $book)
182 $this->permissionService->buildJointPermissionsForEntity($book);
186 * Get the next child element priority.
190 public function getNewPriority($book)
192 $lastElem = $this->getChildren($book)->pop();
193 return $lastElem ? $lastElem->priority + 1 : 0;
197 * @param string $slug
198 * @param bool|false $currentId
201 public function doesSlugExist($slug, $currentId = false)
203 $query = $this->book->where('slug', '=', $slug);
205 $query = $query->where('id', '!=', $currentId);
207 return $query->count() > 0;
211 * Provides a suitable slug for the given book name.
212 * Ensures the returned slug is unique in the system.
213 * @param string $name
214 * @param bool|false $currentId
217 public function findSuitableSlug($name, $currentId = false)
219 $originalSlug = Str::slug($name);
220 $slug = $originalSlug;
222 while ($this->doesSlugExist($slug, $currentId)) {
223 $slug = $originalSlug . '-' . $count;
230 * Get all child objects of a book.
231 * Returns a sorted collection of Pages and Chapters.
232 * Loads the bookslug onto child elements to prevent access database access for getting the slug.
234 * @param bool $filterDrafts
237 public function getChildren(Book $book, $filterDrafts = false)
239 $pageQuery = $book->pages()->where('chapter_id', '=', 0);
240 $pageQuery = $this->permissionService->enforcePageRestrictions($pageQuery, 'view');
243 $pageQuery = $pageQuery->where('draft', '=', false);
246 $pages = $pageQuery->get();
248 $chapterQuery = $book->chapters()->with(['pages' => function($query) use ($filterDrafts) {
249 $this->permissionService->enforcePageRestrictions($query, 'view');
250 if ($filterDrafts) $query->where('draft', '=', false);
252 $chapterQuery = $this->permissionService->enforceChapterRestrictions($chapterQuery, 'view');
253 $chapters = $chapterQuery->get();
254 $children = $pages->merge($chapters);
255 $bookSlug = $book->slug;
257 $children->each(function ($child) use ($bookSlug) {
258 $child->setAttribute('bookSlug', $bookSlug);
259 if ($child->isA('chapter')) {
260 $child->pages->each(function ($page) use ($bookSlug) {
261 $page->setAttribute('bookSlug', $bookSlug);
263 $child->pages = $child->pages->sortBy(function($child, $key) {
264 $score = $child->priority;
265 if ($child->draft) $score -= 100;
271 // Sort items with drafts first then by priority.
272 return $children->sortBy(function($child, $key) {
273 $score = $child->priority;
274 if ($child->isA('page') && $child->draft) $score -= 100;
280 * Get books by search term.
283 * @param array $paginationAppends
286 public function getBySearch($term, $count = 20, $paginationAppends = [])
288 $terms = $this->prepareSearchTerms($term);
289 $books = $this->permissionService->enforceBookRestrictions($this->book->fullTextSearchQuery(['name', 'description'], $terms))
290 ->paginate($count)->appends($paginationAppends);
291 $words = join('|', explode(' ', preg_quote(trim($term), '/')));
292 foreach ($books as $book) {
294 $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $book->getExcerpt(100));
295 $book->searchSnippet = $result;