1 <?php namespace BookStack\Repos;
4 use BookStack\Exceptions\NotFoundException;
5 use BookStack\Services\RestrictionService;
6 use Illuminate\Support\Str;
15 protected $chapterRepo;
16 protected $restrictionService;
19 * BookRepo constructor.
21 * @param PageRepo $pageRepo
22 * @param ChapterRepo $chapterRepo
23 * @param RestrictionService $restrictionService
25 public function __construct(Book $book, PageRepo $pageRepo, ChapterRepo $chapterRepo, RestrictionService $restrictionService)
28 $this->pageRepo = $pageRepo;
29 $this->chapterRepo = $chapterRepo;
30 $this->restrictionService = $restrictionService;
34 * Base query for getting books.
35 * Takes into account any restrictions.
38 private function bookQuery()
40 return $this->restrictionService->enforceBookRestrictions($this->book, 'view');
44 * Get the book that has the given id.
48 public function getById($id)
50 return $this->bookQuery()->findOrFail($id);
54 * Get all books, Limited by count.
58 public function getAll($count = 10)
60 $bookQuery = $this->bookQuery()->orderBy('name', 'asc');
61 if (!$count) return $bookQuery->get();
62 return $bookQuery->take($count)->get();
66 * Get all books paginated.
70 public function getAllPaginated($count = 10)
72 return $this->bookQuery()
73 ->orderBy('name', 'asc')->paginate($count);
78 * Get the latest books.
82 public function getLatest($count = 10)
84 return $this->bookQuery()->orderBy('created_at', 'desc')->take($count)->get();
88 * Gets the most recently viewed for a user.
93 public function getRecentlyViewed($count = 10, $page = 0)
96 return Views::getUserRecentlyViewed($count, $page, $this->book);
100 * Gets the most viewed books.
105 public function getPopular($count = 10, $page = 0)
108 return Views::getPopular($count, $page, $this->book);
115 * @throws NotFoundException
117 public function getBySlug($slug)
119 $book = $this->bookQuery()->where('slug', '=', $slug)->first();
120 if ($book === null) throw new NotFoundException('Book not found');
125 * Checks if a book exists.
129 public function exists($id)
131 return $this->bookQuery()->where('id', '=', $id)->exists();
135 * Get a new book instance from request input.
139 public function newFromInput($input)
141 return $this->book->newInstance($input);
145 * Destroy a book identified by the given slug.
148 public function destroyBySlug($bookSlug)
150 $book = $this->getBySlug($bookSlug);
151 foreach ($book->pages as $page) {
152 $this->pageRepo->destroy($page);
154 foreach ($book->chapters as $chapter) {
155 $this->chapterRepo->destroy($chapter);
157 $book->views()->delete();
158 $book->restrictions()->delete();
163 * Get the next child element priority.
167 public function getNewPriority($book)
169 $lastElem = $this->getChildren($book)->pop();
170 return $lastElem ? $lastElem->priority + 1 : 0;
174 * @param string $slug
175 * @param bool|false $currentId
178 public function doesSlugExist($slug, $currentId = false)
180 $query = $this->book->where('slug', '=', $slug);
182 $query = $query->where('id', '!=', $currentId);
184 return $query->count() > 0;
188 * Provides a suitable slug for the given book name.
189 * Ensures the returned slug is unique in the system.
190 * @param string $name
191 * @param bool|false $currentId
194 public function findSuitableSlug($name, $currentId = false)
196 $originalSlug = Str::slug($name);
197 $slug = $originalSlug;
199 while ($this->doesSlugExist($slug, $currentId)) {
200 $slug = $originalSlug . '-' . $count;
207 * Get all child objects of a book.
208 * Returns a sorted collection of Pages and Chapters.
209 * Loads the bookslug onto child elements to prevent access database access for getting the slug.
213 public function getChildren(Book $book)
215 $pageQuery = $book->pages()->where('chapter_id', '=', 0);
216 $pageQuery = $this->restrictionService->enforcePageRestrictions($pageQuery, 'view');
217 $pages = $pageQuery->get();
219 $chapterQuery = $book->chapters()->with(['pages' => function($query) {
220 $this->restrictionService->enforcePageRestrictions($query, 'view');
222 $chapterQuery = $this->restrictionService->enforceChapterRestrictions($chapterQuery, 'view');
223 $chapters = $chapterQuery->get();
224 $children = $pages->merge($chapters);
225 $bookSlug = $book->slug;
226 $children->each(function ($child) use ($bookSlug) {
227 $child->setAttribute('bookSlug', $bookSlug);
228 if ($child->isA('chapter')) {
229 $child->pages->each(function ($page) use ($bookSlug) {
230 $page->setAttribute('bookSlug', $bookSlug);
234 return $children->sortBy('priority');
238 * Get books by search term.
241 * @param array $paginationAppends
244 public function getBySearch($term, $count = 20, $paginationAppends = [])
247 preg_match_all('/"(.*?)"/', $term, $matches);
248 if (count($matches[1]) > 0) {
249 $terms = $matches[1];
250 $term = trim(preg_replace('/"(.*?)"/', '', $term));
255 $terms = array_merge($terms, explode(' ', $term));
257 $books = $this->book->fullTextSearchQuery(['name', 'description'], $terms)
259 $terms = explode(' ', $term);
260 $books = $this->restrictionService->enforceBookRestrictions($this->book->fullTextSearchQuery(['name', 'description'], $terms))
261 >>>>>>> custom_role_system
262 ->paginate($count)->appends($paginationAppends);
263 $words = join('|', explode(' ', preg_quote(trim($term), '/')));
264 foreach ($books as $book) {
266 $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $book->getExcerpt(100));
267 $book->searchSnippet = $result;
273 * Updates books restrictions from a request
277 public function updateRestrictionsFromRequest($request, $book)
279 // TODO - extract into shared repo
280 $book->restricted = $request->has('restricted') && $request->get('restricted') === 'true';
281 $book->restrictions()->delete();
282 if ($request->has('restrictions')) {
283 foreach ($request->get('restrictions') as $roleId => $restrictions) {
284 foreach ($restrictions as $action => $value) {
285 $book->restrictions()->create([
286 'role_id' => $roleId,
287 'action' => strtolower($action)