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('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)
151 $book->slug = $this->findSuitableSlug($book->name, $book->id);
152 $book->updated_by = user()->id;
154 $this->permissionService->buildJointPermissionsForEntity($book);
159 * Destroy the given book.
163 public function destroy(Book $book)
165 foreach ($book->pages as $page) {
166 $this->pageRepo->destroy($page);
168 foreach ($book->chapters as $chapter) {
169 $this->chapterRepo->destroy($chapter);
171 $book->views()->delete();
172 $book->permissions()->delete();
173 $this->permissionService->deleteJointPermissionsForEntity($book);
178 * Get the next child element priority.
182 public function getNewPriority($book)
184 $lastElem = $this->getChildren($book)->pop();
185 return $lastElem ? $lastElem->priority + 1 : 0;
189 * @param string $slug
190 * @param bool|false $currentId
193 public function doesSlugExist($slug, $currentId = false)
195 $query = $this->book->where('slug', '=', $slug);
197 $query = $query->where('id', '!=', $currentId);
199 return $query->count() > 0;
203 * Provides a suitable slug for the given book name.
204 * Ensures the returned slug is unique in the system.
205 * @param string $name
206 * @param bool|false $currentId
209 public function findSuitableSlug($name, $currentId = false)
211 $slug = Str::slug($name);
212 if ($slug === "") $slug = substr(md5(rand(1, 500)), 0, 5);
213 while ($this->doesSlugExist($slug, $currentId)) {
214 $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
220 * Get all child objects of a book.
221 * Returns a sorted collection of Pages and Chapters.
222 * Loads the book slug onto child elements to prevent access database access for getting the slug.
224 * @param bool $filterDrafts
227 public function getChildren(Book $book, $filterDrafts = false)
229 $pageQuery = $book->pages()->where('chapter_id', '=', 0);
230 $pageQuery = $this->permissionService->enforcePageRestrictions($pageQuery, 'view');
233 $pageQuery = $pageQuery->where('draft', '=', false);
236 $pages = $pageQuery->get();
238 $chapterQuery = $book->chapters()->with(['pages' => function ($query) use ($filterDrafts) {
239 $this->permissionService->enforcePageRestrictions($query, 'view');
240 if ($filterDrafts) $query->where('draft', '=', false);
242 $chapterQuery = $this->permissionService->enforceChapterRestrictions($chapterQuery, 'view');
243 $chapters = $chapterQuery->get();
244 $children = $pages->values();
245 foreach ($chapters as $chapter) {
246 $children->push($chapter);
248 $bookSlug = $book->slug;
250 $children->each(function ($child) use ($bookSlug) {
251 $child->setAttribute('bookSlug', $bookSlug);
252 if ($child->isA('chapter')) {
253 $child->pages->each(function ($page) use ($bookSlug) {
254 $page->setAttribute('bookSlug', $bookSlug);
256 $child->pages = $child->pages->sortBy(function ($child, $key) {
257 $score = $child->priority;
258 if ($child->draft) $score -= 100;
264 // Sort items with drafts first then by priority.
265 return $children->sortBy(function ($child, $key) {
266 $score = $child->priority;
267 if ($child->isA('page') && $child->draft) $score -= 100;
273 * Get books by search term.
276 * @param array $paginationAppends
279 public function getBySearch($term, $count = 20, $paginationAppends = [])
281 $terms = $this->prepareSearchTerms($term);
282 $bookQuery = $this->permissionService->enforceBookRestrictions($this->book->fullTextSearchQuery(['name', 'description'], $terms));
283 $bookQuery = $this->addAdvancedSearchQueries($bookQuery, $term);
284 $books = $bookQuery->paginate($count)->appends($paginationAppends);
285 $words = join('|', explode(' ', preg_quote(trim($term), '/')));
286 foreach ($books as $book) {
288 $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $book->getExcerpt(100));
289 $book->searchSnippet = $result;