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);
85 public function getBySlug($slug)
87 return $this->book->where('slug', '=', $slug)->first();
91 * Checks if a book exists.
95 public function exists($id)
97 return $this->book->where('id', '=', $id)->exists();
101 * Get a new book instance from request input.
105 public function newFromInput($input)
107 return $this->book->fill($input);
111 * Count the amount of books that have a specific slug.
115 public function countBySlug($slug)
117 return $this->book->where('slug', '=', $slug)->count();
121 * Destroy a book identified by the given slug.
124 public function destroyBySlug($bookSlug)
126 $book = $this->getBySlug($bookSlug);
127 foreach ($book->pages as $page) {
128 $this->pageRepo->destroy($page);
130 foreach ($book->chapters as $chapter) {
131 $this->chapterRepo->destroy($chapter);
133 $book->views()->delete();
138 * Get the next child element priority.
142 public function getNewPriority($book)
144 $lastElem = $this->getChildren($book)->pop();
145 return $lastElem ? $lastElem->priority + 1 : 0;
149 * @param string $slug
150 * @param bool|false $currentId
153 public function doesSlugExist($slug, $currentId = false)
155 $query = $this->book->where('slug', '=', $slug);
157 $query = $query->where('id', '!=', $currentId);
159 return $query->count() > 0;
163 * Provides a suitable slug for the given book name.
164 * Ensures the returned slug is unique in the system.
165 * @param string $name
166 * @param bool|false $currentId
169 public function findSuitableSlug($name, $currentId = false)
171 $originalSlug = Str::slug($name);
172 $slug = $originalSlug;
174 while ($this->doesSlugExist($slug, $currentId)) {
175 $slug = $originalSlug . '-' . $count;
182 * Get all child objects of a book.
183 * Returns a sorted collection of Pages and Chapters.
184 * Loads the bookslug onto child elements to prevent access database access for getting the slug.
188 public function getChildren(Book $book)
190 $pages = $book->pages()->where('chapter_id', '=', 0)->get();
191 $chapters = $book->chapters()->with('pages')->get();
192 $children = $pages->merge($chapters);
193 $bookSlug = $book->slug;
194 $children->each(function ($child) use ($bookSlug) {
195 $child->setAttribute('bookSlug', $bookSlug);
196 if ($child->isA('chapter')) {
197 $child->pages->each(function ($page) use ($bookSlug) {
198 $page->setAttribute('bookSlug', $bookSlug);
202 return $children->sortBy('priority');
206 * Get books by search term.
210 public function getBySearch($term)
212 $terms = explode(' ', preg_quote(trim($term)));
213 $books = $this->book->fullTextSearch(['name', 'description'], $terms);
214 $words = join('|', $terms);
215 foreach ($books as $book) {
217 $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $book->getExcerpt(100));
218 $book->searchSnippet = $result;