1 <?php namespace BookStack\Repos;
3 use BookStack\Exceptions\NotFoundException;
4 use Illuminate\Support\Str;
8 class BookRepo extends EntityRepo
11 protected $chapterRepo;
14 * BookRepo constructor.
15 * @param PageRepo $pageRepo
16 * @param ChapterRepo $chapterRepo
18 public function __construct(PageRepo $pageRepo, ChapterRepo $chapterRepo)
20 $this->pageRepo = $pageRepo;
21 $this->chapterRepo = $chapterRepo;
22 parent::__construct();
26 * Base query for getting books.
27 * Takes into account any restrictions.
30 private function bookQuery()
32 return $this->restrictionService->enforceBookRestrictions($this->book, 'view');
36 * Get the book that has the given id.
40 public function getById($id)
42 return $this->bookQuery()->findOrFail($id);
46 * Get all books, Limited by count.
50 public function getAll($count = 10)
52 $bookQuery = $this->bookQuery()->orderBy('name', 'asc');
53 if (!$count) return $bookQuery->get();
54 return $bookQuery->take($count)->get();
58 * Get all books paginated.
62 public function getAllPaginated($count = 10)
64 return $this->bookQuery()
65 ->orderBy('name', 'asc')->paginate($count);
70 * Get the latest books.
74 public function getLatest($count = 10)
76 return $this->bookQuery()->orderBy('created_at', 'desc')->take($count)->get();
80 * Gets the most recently viewed for a user.
85 public function getRecentlyViewed($count = 10, $page = 0)
87 return Views::getUserRecentlyViewed($count, $page, $this->book);
91 * Gets the most viewed books.
96 public function getPopular($count = 10, $page = 0)
98 return Views::getPopular($count, $page, $this->book);
105 * @throws NotFoundException
107 public function getBySlug($slug)
109 $book = $this->bookQuery()->where('slug', '=', $slug)->first();
110 if ($book === null) throw new NotFoundException('Book not found');
115 * Checks if a book exists.
119 public function exists($id)
121 return $this->bookQuery()->where('id', '=', $id)->exists();
125 * Get a new book instance from request input.
129 public function newFromInput($input)
131 return $this->book->newInstance($input);
135 * Destroy a book identified by the given slug.
138 public function destroyBySlug($bookSlug)
140 $book = $this->getBySlug($bookSlug);
141 foreach ($book->pages as $page) {
142 $this->pageRepo->destroy($page);
144 foreach ($book->chapters as $chapter) {
145 $this->chapterRepo->destroy($chapter);
147 $book->views()->delete();
148 $book->restrictions()->delete();
153 * Get the next child element priority.
157 public function getNewPriority($book)
159 $lastElem = $this->getChildren($book)->pop();
160 return $lastElem ? $lastElem->priority + 1 : 0;
164 * @param string $slug
165 * @param bool|false $currentId
168 public function doesSlugExist($slug, $currentId = false)
170 $query = $this->book->where('slug', '=', $slug);
172 $query = $query->where('id', '!=', $currentId);
174 return $query->count() > 0;
178 * Provides a suitable slug for the given book name.
179 * Ensures the returned slug is unique in the system.
180 * @param string $name
181 * @param bool|false $currentId
184 public function findSuitableSlug($name, $currentId = false)
186 $originalSlug = Str::slug($name);
187 $slug = $originalSlug;
189 while ($this->doesSlugExist($slug, $currentId)) {
190 $slug = $originalSlug . '-' . $count;
197 * Get all child objects of a book.
198 * Returns a sorted collection of Pages and Chapters.
199 * Loads the bookslug onto child elements to prevent access database access for getting the slug.
203 public function getChildren(Book $book)
205 $pageQuery = $book->pages()->where('chapter_id', '=', 0);
206 $pageQuery = $this->restrictionService->enforcePageRestrictions($pageQuery, 'view');
207 $pages = $pageQuery->get();
209 $chapterQuery = $book->chapters()->with(['pages' => function($query) {
210 $this->restrictionService->enforcePageRestrictions($query, 'view');
212 $chapterQuery = $this->restrictionService->enforceChapterRestrictions($chapterQuery, 'view');
213 $chapters = $chapterQuery->get();
214 $children = $pages->merge($chapters);
215 $bookSlug = $book->slug;
217 $children->each(function ($child) use ($bookSlug) {
218 $child->setAttribute('bookSlug', $bookSlug);
219 if ($child->isA('chapter')) {
220 $child->pages->each(function ($page) use ($bookSlug) {
221 $page->setAttribute('bookSlug', $bookSlug);
223 $child->pages = $child->pages->sortBy(function($child, $key) {
224 $score = $child->priority;
225 if ($child->draft) $score -= 100;
231 // Sort items with drafts first then by priority.
232 return $children->sortBy(function($child, $key) {
233 $score = $child->priority;
234 if ($child->isA('page') && $child->draft) $score -= 100;
240 * Get books by search term.
243 * @param array $paginationAppends
246 public function getBySearch($term, $count = 20, $paginationAppends = [])
248 $terms = $this->prepareSearchTerms($term);
249 $books = $this->restrictionService->enforceBookRestrictions($this->book->fullTextSearchQuery(['name', 'description'], $terms))
250 ->paginate($count)->appends($paginationAppends);
251 $words = join('|', explode(' ', preg_quote(trim($term), '/')));
252 foreach ($books as $book) {
254 $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $book->getExcerpt(100));
255 $book->searchSnippet = $result;