1 <?php namespace BookStack\Repos;
4 use BookStack\Services\RestrictionService;
5 use Illuminate\Support\Str;
14 protected $chapterRepo;
15 protected $restrictionService;
18 * BookRepo constructor.
20 * @param PageRepo $pageRepo
21 * @param ChapterRepo $chapterRepo
22 * @param RestrictionService $restrictionService
24 public function __construct(Book $book, PageRepo $pageRepo, ChapterRepo $chapterRepo, RestrictionService $restrictionService)
27 $this->pageRepo = $pageRepo;
28 $this->chapterRepo = $chapterRepo;
29 $this->restrictionService = $restrictionService;
33 * Base query for getting books.
34 * Takes into account any restrictions.
37 private function bookQuery()
39 return $this->restrictionService->enforceBookRestrictions($this->book, 'view');
43 * Get the book that has the given id.
47 public function getById($id)
49 return $this->bookQuery()->findOrFail($id);
53 * Get all books, Limited by count.
57 public function getAll($count = 10)
59 $bookQuery = $this->bookQuery()->orderBy('name', 'asc');
60 if (!$count) return $bookQuery->get();
61 return $bookQuery->take($count)->get();
65 * Get all books paginated.
69 public function getAllPaginated($count = 10)
71 return $this->bookQuery()
72 ->orderBy('name', 'asc')->paginate($count);
77 * Get the latest books.
81 public function getLatest($count = 10)
83 return $this->bookQuery()->orderBy('created_at', 'desc')->take($count)->get();
87 * Gets the most recently viewed for a user.
92 public function getRecentlyViewed($count = 10, $page = 0)
95 return Views::getUserRecentlyViewed($count, $page, $this->book);
99 * Gets the most viewed books.
104 public function getPopular($count = 10, $page = 0)
107 return Views::getPopular($count, $page, $this->book);
115 public function getBySlug($slug)
117 $book = $this->bookQuery()->where('slug', '=', $slug)->first();
118 if ($book === null) abort(404);
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();
160 * Get the next child element priority.
164 public function getNewPriority($book)
166 $lastElem = $this->getChildren($book)->pop();
167 return $lastElem ? $lastElem->priority + 1 : 0;
171 * @param string $slug
172 * @param bool|false $currentId
175 public function doesSlugExist($slug, $currentId = false)
177 $query = $this->book->where('slug', '=', $slug);
179 $query = $query->where('id', '!=', $currentId);
181 return $query->count() > 0;
185 * Provides a suitable slug for the given book name.
186 * Ensures the returned slug is unique in the system.
187 * @param string $name
188 * @param bool|false $currentId
191 public function findSuitableSlug($name, $currentId = false)
193 $originalSlug = Str::slug($name);
194 $slug = $originalSlug;
196 while ($this->doesSlugExist($slug, $currentId)) {
197 $slug = $originalSlug . '-' . $count;
204 * Get all child objects of a book.
205 * Returns a sorted collection of Pages and Chapters.
206 * Loads the bookslug onto child elements to prevent access database access for getting the slug.
210 public function getChildren(Book $book)
212 $pageQuery = $book->pages()->where('chapter_id', '=', 0);
213 $this->restrictionService->enforcePageRestrictions($pageQuery, 'view');
214 $pages = $pageQuery->get();
216 $chapterQuery = $book->chapters()->with('pages');
217 $this->restrictionService->enforceChapterRestrictions($chapterQuery, 'view');
218 $chapters = $chapterQuery->get();
219 $children = $pages->merge($chapters);
220 $bookSlug = $book->slug;
221 $children->each(function ($child) use ($bookSlug) {
222 $child->setAttribute('bookSlug', $bookSlug);
223 if ($child->isA('chapter')) {
224 $child->pages->each(function ($page) use ($bookSlug) {
225 $page->setAttribute('bookSlug', $bookSlug);
229 return $children->sortBy('priority');
233 * Get books by search term.
236 * @param array $paginationAppends
239 public function getBySearch($term, $count = 20, $paginationAppends = [])
241 $terms = explode(' ', $term);
242 $books = $this->restrictionService->enforceBookRestrictions($this->book->fullTextSearchQuery(['name', 'description'], $terms))
243 ->paginate($count)->appends($paginationAppends);
244 $words = join('|', explode(' ', preg_quote(trim($term), '/')));
245 foreach ($books as $book) {
247 $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $book->getExcerpt(100));
248 $book->searchSnippet = $result;
254 * Updates books restrictions from a request
258 public function updateRestrictionsFromRequest($request, $book)
260 // TODO - extract into shared repo
261 $book->restricted = $request->has('restricted') && $request->get('restricted') === 'true';
262 $book->restrictions()->delete();
263 if ($request->has('restrictions')) {
264 foreach ($request->get('restrictions') as $roleId => $restrictions) {
265 foreach ($restrictions as $action => $value) {
266 $book->restrictions()->create([
267 'role_id' => $roleId,
268 'action' => strtolower($action)