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 $bookQuery = $this->book->orderBy('name', 'asc');
46 if (!$count) return $bookQuery->get();
47 return $bookQuery->take($count)->get();
51 * Get all books paginated.
55 public function getAllPaginated($count = 10)
57 return $this->book->orderBy('name', 'asc')->paginate($count);
62 * Get the latest books.
66 public function getLatest($count = 10)
68 return $this->book->orderBy('created_at', 'desc')->take($count)->get();
72 * Gets the most recently viewed for a user.
77 public function getRecentlyViewed($count = 10, $page = 0)
79 return Views::getUserRecentlyViewed($count, $page, $this->book);
83 * Gets the most viewed books.
88 public function getPopular($count = 10, $page = 0)
90 return Views::getPopular($count, $page, $this->book);
98 public function getBySlug($slug)
100 $book = $this->book->where('slug', '=', $slug)->first();
101 if ($book === null) abort(404);
106 * Checks if a book exists.
110 public function exists($id)
112 return $this->book->where('id', '=', $id)->exists();
116 * Get a new book instance from request input.
120 public function newFromInput($input)
122 return $this->book->fill($input);
126 * Count the amount of books that have a specific slug.
130 public function countBySlug($slug)
132 return $this->book->where('slug', '=', $slug)->count();
136 * Destroy a book identified by the given slug.
139 public function destroyBySlug($bookSlug)
141 $book = $this->getBySlug($bookSlug);
142 foreach ($book->pages as $page) {
143 $this->pageRepo->destroy($page);
145 foreach ($book->chapters as $chapter) {
146 $this->chapterRepo->destroy($chapter);
148 $book->views()->delete();
153 * Get the next child element priority.
157 public function getNewPriority($book)
159 $lastElem = $this->getChildren($book)->pop();
160 return $lastElem ? $lastElem->priority + 1 : 0;
164 * @param string $slug
165 * @param bool|false $currentId
168 public function doesSlugExist($slug, $currentId = false)
170 $query = $this->book->where('slug', '=', $slug);
172 $query = $query->where('id', '!=', $currentId);
174 return $query->count() > 0;
178 * Provides a suitable slug for the given book name.
179 * Ensures the returned slug is unique in the system.
180 * @param string $name
181 * @param bool|false $currentId
184 public function findSuitableSlug($name, $currentId = false)
186 $originalSlug = Str::slug($name);
187 $slug = $originalSlug;
189 while ($this->doesSlugExist($slug, $currentId)) {
190 $slug = $originalSlug . '-' . $count;
197 * Get all child objects of a book.
198 * Returns a sorted collection of Pages and Chapters.
199 * Loads the bookslug onto child elements to prevent access database access for getting the slug.
203 public function getChildren(Book $book)
205 $pages = $book->pages()->where('chapter_id', '=', 0)->get();
206 $chapters = $book->chapters()->with('pages')->get();
207 $children = $pages->merge($chapters);
208 $bookSlug = $book->slug;
209 $children->each(function ($child) use ($bookSlug) {
210 $child->setAttribute('bookSlug', $bookSlug);
211 if ($child->isA('chapter')) {
212 $child->pages->each(function ($page) use ($bookSlug) {
213 $page->setAttribute('bookSlug', $bookSlug);
217 return $children->sortBy('priority');
221 * Get books by search term.
224 * @param array $paginationAppends
227 public function getBySearch($term, $count = 20, $paginationAppends = [])
229 preg_match_all('/"(.*?)"/', $term, $matches);
230 if (count($matches[1]) > 0) {
231 $terms = $matches[1];
232 $term = trim(preg_replace('/"(.*?)"/', '', $term));
237 $terms = array_merge($terms, explode(' ', $term));
239 $books = $this->book->fullTextSearchQuery(['name', 'description'], $terms)
240 ->paginate($count)->appends($paginationAppends);
241 $words = join('|', explode(' ', preg_quote(trim($term), '/')));
242 foreach ($books as $book) {
244 $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $book->getExcerpt(100));
245 $book->searchSnippet = $result;