1 <?php namespace BookStack\Repos;
4 use Illuminate\Support\Str;
13 protected $chapterRepo;
16 * BookRepo constructor.
18 * @param PageRepo $pageRepo
19 * @param ChapterRepo $chapterRepo
21 public function __construct(Book $book, PageRepo $pageRepo, ChapterRepo $chapterRepo)
24 $this->pageRepo = $pageRepo;
25 $this->chapterRepo = $chapterRepo;
29 * Get the book that has the given id.
33 public function getById($id)
35 return $this->book->findOrFail($id);
39 * Get all books, Limited by count.
43 public function getAll($count = 10)
45 return $this->book->orderBy('name', 'asc')->take($count)->get();
49 * Get all books paginated.
53 public function getAllPaginated($count = 10)
55 return $this->book->orderBy('name', 'asc')->paginate($count);
60 * Get the latest books.
64 public function getLatest($count = 10)
66 return $this->book->orderBy('created_at', 'desc')->take($count)->get();
70 * Gets the most recently viewed for a user.
75 public function getRecentlyViewed($count = 10, $page = 0)
77 return Views::getUserRecentlyViewed($count, $page, $this->book);
81 * Gets the most viewed books.
86 public function getPopular($count = 10, $page = 0)
88 return Views::getPopular($count, $page, $this->book);
96 public function getBySlug($slug)
98 $book = $this->book->where('slug', '=', $slug)->first();
99 if ($book === null) abort(404);
104 * Checks if a book exists.
108 public function exists($id)
110 return $this->book->where('id', '=', $id)->exists();
114 * Get a new book instance from request input.
118 public function newFromInput($input)
120 return $this->book->fill($input);
124 * Count the amount of books that have a specific slug.
128 public function countBySlug($slug)
130 return $this->book->where('slug', '=', $slug)->count();
134 * Destroy a book identified by the given slug.
137 public function destroyBySlug($bookSlug)
139 $book = $this->getBySlug($bookSlug);
140 foreach ($book->pages as $page) {
141 $this->pageRepo->destroy($page);
143 foreach ($book->chapters as $chapter) {
144 $this->chapterRepo->destroy($chapter);
146 $book->views()->delete();
151 * Get the next child element priority.
155 public function getNewPriority($book)
157 $lastElem = $this->getChildren($book)->pop();
158 return $lastElem ? $lastElem->priority + 1 : 0;
162 * @param string $slug
163 * @param bool|false $currentId
166 public function doesSlugExist($slug, $currentId = false)
168 $query = $this->book->where('slug', '=', $slug);
170 $query = $query->where('id', '!=', $currentId);
172 return $query->count() > 0;
176 * Provides a suitable slug for the given book name.
177 * Ensures the returned slug is unique in the system.
178 * @param string $name
179 * @param bool|false $currentId
182 public function findSuitableSlug($name, $currentId = false)
184 $originalSlug = Str::slug($name);
185 $slug = $originalSlug;
187 while ($this->doesSlugExist($slug, $currentId)) {
188 $slug = $originalSlug . '-' . $count;
195 * Get all child objects of a book.
196 * Returns a sorted collection of Pages and Chapters.
197 * Loads the bookslug onto child elements to prevent access database access for getting the slug.
201 public function getChildren(Book $book)
203 $pages = $book->pages()->where('chapter_id', '=', 0)->get();
204 $chapters = $book->chapters()->with('pages')->get();
205 $children = $pages->merge($chapters);
206 $bookSlug = $book->slug;
207 $children->each(function ($child) use ($bookSlug) {
208 $child->setAttribute('bookSlug', $bookSlug);
209 if ($child->isA('chapter')) {
210 $child->pages->each(function ($page) use ($bookSlug) {
211 $page->setAttribute('bookSlug', $bookSlug);
215 return $children->sortBy('priority');
219 * Get books by search term.
223 public function getBySearch($term)
225 $terms = explode(' ', $term);
226 $books = $this->book->fullTextSearch(['name', 'description'], $terms);
227 $words = join('|', explode(' ', preg_quote(trim($term), '/')));
228 foreach ($books as $book) {
230 $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $book->getExcerpt(100));
231 $book->searchSnippet = $result;