1 <?php namespace BookStack\Repos;
4 use Illuminate\Support\Str;
13 protected $chapterRepo;
14 protected $restrictionService;
17 * BookRepo constructor.
19 * @param PageRepo $pageRepo
20 * @param ChapterRepo $chapterRepo
21 * @param RestrictionService $restrictionService
23 public function __construct(Book $book, PageRepo $pageRepo, ChapterRepo $chapterRepo, RestrictionService $restrictionService)
26 $this->pageRepo = $pageRepo;
27 $this->chapterRepo = $chapterRepo;
28 $this->restrictionService = $restrictionService;
32 * Base query for getting books.
33 * Takes into account any restrictions.
36 private function bookQuery()
38 return $this->restrictionService->enforceBookRestrictions($this->book, 'view');
42 * Get the book that has the given id.
46 public function getById($id)
48 return $this->bookQuery()->findOrFail($id);
52 * Get all books, Limited by count.
56 public function getAll($count = 10)
58 $bookQuery = $this->bookQuery()->orderBy('name', 'asc');
59 if (!$count) return $bookQuery->get();
60 return $bookQuery->take($count)->get();
64 * Get all books paginated.
68 public function getAllPaginated($count = 10)
70 return $this->bookQuery()
71 ->orderBy('name', 'asc')->paginate($count);
76 * Get the latest books.
80 public function getLatest($count = 10)
82 return $this->bookQuery()->orderBy('created_at', 'desc')->take($count)->get();
86 * Gets the most recently viewed for a user.
91 public function getRecentlyViewed($count = 10, $page = 0)
94 return Views::getUserRecentlyViewed($count, $page, $this->book);
98 * Gets the most viewed books.
103 public function getPopular($count = 10, $page = 0)
106 return Views::getPopular($count, $page, $this->book);
113 * @throws NotFoundException
115 public function getBySlug($slug)
117 $book = $this->bookQuery()->where('slug', '=', $slug)->first();
118 if ($book === null) throw new NotFoundException('Book not found');
123 * Checks if a book exists.
127 public function exists($id)
129 return $this->bookQuery()->where('id', '=', $id)->exists();
133 * Get a new book instance from request input.
137 public function newFromInput($input)
139 return $this->book->newInstance($input);
143 * Destroy a book identified by the given slug.
146 public function destroyBySlug($bookSlug)
148 $book = $this->getBySlug($bookSlug);
149 foreach ($book->pages as $page) {
150 $this->pageRepo->destroy($page);
152 foreach ($book->chapters as $chapter) {
153 $this->chapterRepo->destroy($chapter);
155 $book->views()->delete();
156 $book->restrictions()->delete();
161 * Get the next child element priority.
165 public function getNewPriority($book)
167 $lastElem = $this->getChildren($book)->pop();
168 return $lastElem ? $lastElem->priority + 1 : 0;
172 * @param string $slug
173 * @param bool|false $currentId
176 public function doesSlugExist($slug, $currentId = false)
178 $query = $this->book->where('slug', '=', $slug);
180 $query = $query->where('id', '!=', $currentId);
182 return $query->count() > 0;
186 * Provides a suitable slug for the given book name.
187 * Ensures the returned slug is unique in the system.
188 * @param string $name
189 * @param bool|false $currentId
192 public function findSuitableSlug($name, $currentId = false)
194 $originalSlug = Str::slug($name);
195 $slug = $originalSlug;
197 while ($this->doesSlugExist($slug, $currentId)) {
198 $slug = $originalSlug . '-' . $count;
205 * Get all child objects of a book.
206 * Returns a sorted collection of Pages and Chapters.
207 * Loads the bookslug onto child elements to prevent access database access for getting the slug.
211 public function getChildren(Book $book)
213 $pageQuery = $book->pages()->where('chapter_id', '=', 0);
214 $pageQuery = $this->restrictionService->enforcePageRestrictions($pageQuery, 'view');
215 $pages = $pageQuery->get();
217 $chapterQuery = $book->chapters()->with(['pages' => function($query) {
218 $this->restrictionService->enforcePageRestrictions($query, 'view');
220 $chapterQuery = $this->restrictionService->enforceChapterRestrictions($chapterQuery, 'view');
221 $chapters = $chapterQuery->get();
222 $children = $pages->merge($chapters);
223 $bookSlug = $book->slug;
224 $children->each(function ($child) use ($bookSlug) {
225 $child->setAttribute('bookSlug', $bookSlug);
226 if ($child->isA('chapter')) {
227 $child->pages->each(function ($page) use ($bookSlug) {
228 $page->setAttribute('bookSlug', $bookSlug);
232 return $children->sortBy('priority');
236 * Get books by search term.
239 * @param array $paginationAppends
242 public function getBySearch($term, $count = 20, $paginationAppends = [])
244 preg_match_all('/"(.*?)"/', $term, $matches);
245 if (count($matches[1]) > 0) {
246 $terms = $matches[1];
247 $term = trim(preg_replace('/"(.*?)"/', '', $term));
252 $terms = array_merge($terms, explode(' ', $term));
254 $books = $this->restrictionService->enforceBookRestrictions($this->book->fullTextSearchQuery(['name', 'description'], $terms))
255 ->paginate($count)->appends($paginationAppends);
256 $words = join('|', explode(' ', preg_quote(trim($term), '/')));
257 foreach ($books as $book) {
259 $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $book->getExcerpt(100));
260 $book->searchSnippet = $result;
266 * Updates books restrictions from a request
270 public function updateRestrictionsFromRequest($request, $book)
272 // TODO - extract into shared repo
273 $book->restricted = $request->has('restricted') && $request->get('restricted') === 'true';
274 $book->restrictions()->delete();
275 if ($request->has('restrictions')) {
276 foreach ($request->get('restrictions') as $roleId => $restrictions) {
277 foreach ($restrictions as $action => $value) {
278 $book->restrictions()->create([
279 'role_id' => $roleId,
280 'action' => strtolower($action)