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 $slug = Str::slug($name);
220 if ($slug === "") $slug = substr(md5(rand(1, 500)), 0, 5);
221 while ($this->doesSlugExist($slug, $currentId)) {
222 $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
228 * Get all child objects of a book.
229 * Returns a sorted collection of Pages and Chapters.
230 * Loads the book slug onto child elements to prevent access database access for getting the slug.
232 * @param bool $filterDrafts
235 public function getChildren(Book $book, $filterDrafts = false)
237 $pageQuery = $book->pages()->where('chapter_id', '=', 0);
238 $pageQuery = $this->permissionService->enforcePageRestrictions($pageQuery, 'view');
241 $pageQuery = $pageQuery->where('draft', '=', false);
244 $pages = $pageQuery->get();
246 $chapterQuery = $book->chapters()->with(['pages' => function ($query) use ($filterDrafts) {
247 $this->permissionService->enforcePageRestrictions($query, 'view');
248 if ($filterDrafts) $query->where('draft', '=', false);
250 $chapterQuery = $this->permissionService->enforceChapterRestrictions($chapterQuery, 'view');
251 $chapters = $chapterQuery->get();
252 $children = $pages->values();
253 foreach ($chapters as $chapter) {
254 $children->push($chapter);
256 $bookSlug = $book->slug;
258 $children->each(function ($child) use ($bookSlug) {
259 $child->setAttribute('bookSlug', $bookSlug);
260 if ($child->isA('chapter')) {
261 $child->pages->each(function ($page) use ($bookSlug) {
262 $page->setAttribute('bookSlug', $bookSlug);
264 $child->pages = $child->pages->sortBy(function ($child, $key) {
265 $score = $child->priority;
266 if ($child->draft) $score -= 100;
272 // Sort items with drafts first then by priority.
273 return $children->sortBy(function ($child, $key) {
274 $score = $child->priority;
275 if ($child->isA('page') && $child->draft) $score -= 100;
281 * Get books by search term.
284 * @param array $paginationAppends
287 public function getBySearch($term, $count = 20, $paginationAppends = [])
289 $terms = $this->prepareSearchTerms($term);
290 $bookQuery = $this->permissionService->enforceBookRestrictions($this->book->fullTextSearchQuery(['name', 'description'], $terms));
291 $bookQuery = $this->addAdvancedSearchQueries($bookQuery, $term);
292 $books = $bookQuery->paginate($count)->appends($paginationAppends);
293 $words = join('|', explode(' ', preg_quote(trim($term), '/')));
294 foreach ($books as $book) {
296 $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $book->getExcerpt(100));
297 $book->searchSnippet = $result;