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 return $this->book->where('slug', '=', $slug)->first();
102 * Checks if a book exists.
106 public function exists($id)
108 return $this->book->where('id', '=', $id)->exists();
112 * Get a new book instance from request input.
116 public function newFromInput($input)
118 return $this->book->fill($input);
122 * Count the amount of books that have a specific slug.
126 public function countBySlug($slug)
128 return $this->book->where('slug', '=', $slug)->count();
132 * Destroy a book identified by the given slug.
135 public function destroyBySlug($bookSlug)
137 $book = $this->getBySlug($bookSlug);
138 foreach ($book->pages as $page) {
139 $this->pageRepo->destroy($page);
141 foreach ($book->chapters as $chapter) {
142 $this->chapterRepo->destroy($chapter);
144 $book->views()->delete();
149 * Get the next child element priority.
153 public function getNewPriority($book)
155 $lastElem = $this->getChildren($book)->pop();
156 return $lastElem ? $lastElem->priority + 1 : 0;
160 * @param string $slug
161 * @param bool|false $currentId
164 public function doesSlugExist($slug, $currentId = false)
166 $query = $this->book->where('slug', '=', $slug);
168 $query = $query->where('id', '!=', $currentId);
170 return $query->count() > 0;
174 * Provides a suitable slug for the given book name.
175 * Ensures the returned slug is unique in the system.
176 * @param string $name
177 * @param bool|false $currentId
180 public function findSuitableSlug($name, $currentId = false)
182 $originalSlug = Str::slug($name);
183 $slug = $originalSlug;
185 while ($this->doesSlugExist($slug, $currentId)) {
186 $slug = $originalSlug . '-' . $count;
193 * Get all child objects of a book.
194 * Returns a sorted collection of Pages and Chapters.
195 * Loads the bookslug onto child elements to prevent access database access for getting the slug.
199 public function getChildren(Book $book)
201 $pages = $book->pages()->where('chapter_id', '=', 0)->get();
202 $chapters = $book->chapters()->with('pages')->get();
203 $children = $pages->merge($chapters);
204 $bookSlug = $book->slug;
205 $children->each(function ($child) use ($bookSlug) {
206 $child->setAttribute('bookSlug', $bookSlug);
207 if ($child->isA('chapter')) {
208 $child->pages->each(function ($page) use ($bookSlug) {
209 $page->setAttribute('bookSlug', $bookSlug);
213 return $children->sortBy('priority');
217 * Get books by search term.
221 public function getBySearch($term)
223 $terms = explode(' ', preg_quote(trim($term)));
224 $books = $this->book->fullTextSearch(['name', 'description'], $terms);
225 $words = join('|', $terms);
226 foreach ($books as $book) {
228 $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $book->getExcerpt(100));
229 $book->searchSnippet = $result;