1 <?php namespace BookStack\Repos;
3 use Illuminate\Support\Str;
13 * BookRepo constructor.
15 * @param PageRepo $pageRepo
17 public function __construct(Book $book, PageRepo $pageRepo)
20 $this->pageRepo = $pageRepo;
23 public function getById($id)
25 return $this->book->findOrFail($id);
28 public function getAll($count = 10)
30 return $this->book->orderBy('name', 'asc')->take($count)->get();
38 public function getAllPaginated($count = 10)
40 return $this->book->orderBy('name', 'asc')->paginate($count);
43 public function getBySlug($slug)
45 return $this->book->where('slug', '=', $slug)->first();
49 * Checks if a book exists.
53 public function exists($id)
55 return $this->book->where('id', '=', $id)->exists();
59 * Get a new book instance from request input.
63 public function newFromInput($input)
65 return $this->book->fill($input);
68 public function countBySlug($slug)
70 return $this->book->where('slug', '=', $slug)->count();
73 public function destroyBySlug($bookSlug)
75 $book = $this->getBySlug($bookSlug);
76 foreach ($book->pages as $page) {
77 \Activity::removeEntity($page);
80 foreach ($book->chapters as $chapter) {
81 \Activity::removeEntity($chapter);
87 public function getNewPriority($book)
89 $lastElem = $book->children()->pop();
90 return $lastElem ? $lastElem->priority + 1 : 0;
93 public function doesSlugExist($slug, $currentId = false)
95 $query = $this->book->where('slug', '=', $slug);
97 $query = $query->where('id', '!=', $currentId);
99 return $query->count() > 0;
102 public function findSuitableSlug($name, $currentId = false)
104 $originalSlug = Str::slug($name);
105 $slug = $originalSlug;
107 while ($this->doesSlugExist($slug, $currentId)) {
108 $slug = $originalSlug . '-' . $count;
114 public function getBySearch($term)
116 $terms = explode(' ', preg_quote(trim($term)));
117 $books = $this->book->fullTextSearch(['name', 'description'], $terms);
118 $words = join('|', $terms);
119 foreach ($books as $book) {
121 $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $book->getExcerpt(100));
122 $book->searchSnippet = $result;