1 <?php namespace BookStack\Repos;
3 use BookStack\Activity;
4 use Illuminate\Support\Str;
15 * BookRepo constructor.
17 * @param PageRepo $pageRepo
19 public function __construct(Book $book, PageRepo $pageRepo)
22 $this->pageRepo = $pageRepo;
26 * Get the book that has the given id.
30 public function getById($id)
32 return $this->book->findOrFail($id);
36 * Get all books, Limited by count.
40 public function getAll($count = 10)
42 return $this->book->orderBy('name', 'asc')->take($count)->get();
46 * Get all books paginated.
50 public function getAllPaginated($count = 10)
52 return $this->book->orderBy('name', 'asc')->paginate($count);
55 public function getRecentlyViewed($count = 10, $page = 0)
57 return Views::getUserRecentlyViewed($count, $page, $this->book);
65 public function getBySlug($slug)
67 return $this->book->where('slug', '=', $slug)->first();
71 * Checks if a book exists.
75 public function exists($id)
77 return $this->book->where('id', '=', $id)->exists();
81 * Get a new book instance from request input.
85 public function newFromInput($input)
87 return $this->book->fill($input);
91 * Count the amount of books that have a specific slug.
95 public function countBySlug($slug)
97 return $this->book->where('slug', '=', $slug)->count();
101 * Destroy a book identified by the given slug.
104 public function destroyBySlug($bookSlug)
106 $book = $this->getBySlug($bookSlug);
107 foreach ($book->pages as $page) {
108 \Activity::removeEntity($page);
111 foreach ($book->chapters as $chapter) {
112 \Activity::removeEntity($chapter);
119 * Get the next child element priority.
123 public function getNewPriority($book)
125 $lastElem = $book->children()->pop();
126 return $lastElem ? $lastElem->priority + 1 : 0;
130 * @param string $slug
131 * @param bool|false $currentId
134 public function doesSlugExist($slug, $currentId = false)
136 $query = $this->book->where('slug', '=', $slug);
138 $query = $query->where('id', '!=', $currentId);
140 return $query->count() > 0;
144 * Provides a suitable slug for the given book name.
145 * Ensures the returned slug is unique in the system.
146 * @param string $name
147 * @param bool|false $currentId
150 public function findSuitableSlug($name, $currentId = false)
152 $originalSlug = Str::slug($name);
153 $slug = $originalSlug;
155 while ($this->doesSlugExist($slug, $currentId)) {
156 $slug = $originalSlug . '-' . $count;
163 * Get books by search term.
167 public function getBySearch($term)
169 $terms = explode(' ', preg_quote(trim($term)));
170 $books = $this->book->fullTextSearch(['name', 'description'], $terms);
171 $words = join('|', $terms);
172 foreach ($books as $book) {
174 $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $book->getExcerpt(100));
175 $book->searchSnippet = $result;