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);
80 public function getPopular($count = 10, $page = 0)
82 return Views::getPopular($count, $page, $this->book);
90 public function getBySlug($slug)
92 return $this->book->where('slug', '=', $slug)->first();
96 * Checks if a book exists.
100 public function exists($id)
102 return $this->book->where('id', '=', $id)->exists();
106 * Get a new book instance from request input.
110 public function newFromInput($input)
112 return $this->book->fill($input);
116 * Count the amount of books that have a specific slug.
120 public function countBySlug($slug)
122 return $this->book->where('slug', '=', $slug)->count();
126 * Destroy a book identified by the given slug.
129 public function destroyBySlug($bookSlug)
131 $book = $this->getBySlug($bookSlug);
132 foreach ($book->pages as $page) {
133 $this->pageRepo->destroy($page);
135 foreach ($book->chapters as $chapter) {
136 $this->chapterRepo->destroy($chapter);
138 $book->views()->delete();
143 * Get the next child element priority.
147 public function getNewPriority($book)
149 $lastElem = $this->getChildren($book)->pop();
150 return $lastElem ? $lastElem->priority + 1 : 0;
154 * @param string $slug
155 * @param bool|false $currentId
158 public function doesSlugExist($slug, $currentId = false)
160 $query = $this->book->where('slug', '=', $slug);
162 $query = $query->where('id', '!=', $currentId);
164 return $query->count() > 0;
168 * Provides a suitable slug for the given book name.
169 * Ensures the returned slug is unique in the system.
170 * @param string $name
171 * @param bool|false $currentId
174 public function findSuitableSlug($name, $currentId = false)
176 $originalSlug = Str::slug($name);
177 $slug = $originalSlug;
179 while ($this->doesSlugExist($slug, $currentId)) {
180 $slug = $originalSlug . '-' . $count;
187 * Get all child objects of a book.
188 * Returns a sorted collection of Pages and Chapters.
189 * Loads the bookslug onto child elements to prevent access database access for getting the slug.
193 public function getChildren(Book $book)
195 $pages = $book->pages()->where('chapter_id', '=', 0)->get();
196 $chapters = $book->chapters()->with('pages')->get();
197 $children = $pages->merge($chapters);
198 $bookSlug = $book->slug;
199 $children->each(function ($child) use ($bookSlug) {
200 $child->setAttribute('bookSlug', $bookSlug);
201 if ($child->isA('chapter')) {
202 $child->pages->each(function ($page) use ($bookSlug) {
203 $page->setAttribute('bookSlug', $bookSlug);
207 return $children->sortBy('priority');
211 * Get books by search term.
215 public function getBySearch($term)
217 $terms = explode(' ', preg_quote(trim($term)));
218 $books = $this->book->fullTextSearch(['name', 'description'], $terms);
219 $words = join('|', $terms);
220 foreach ($books as $book) {
222 $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $book->getExcerpt(100));
223 $book->searchSnippet = $result;