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()
30 return $this->book->all();
33 public function getBySlug($slug)
35 return $this->book->where('slug', '=', $slug)->first();
39 * Checks if a book exists.
43 public function exists($id)
45 return $this->book->where('id', '=', $id)->exists();
49 * Get a new book instance from request input.
53 public function newFromInput($input)
55 return $this->book->fill($input);
58 public function countBySlug($slug)
60 return $this->book->where('slug', '=', $slug)->count();
63 public function destroyBySlug($bookSlug)
65 $book = $this->getBySlug($bookSlug);
66 foreach($book->pages as $page) {
67 \Activity::removeEntity($page);
70 foreach($book->chapters as $chapter) {
71 \Activity::removeEntity($chapter);
77 public function getNewPriority($book)
79 $lastElem = $book->children()->pop();
80 return $lastElem ? $lastElem->priority + 1 : 0;
83 public function doesSlugExist($slug, $currentId = false)
85 $query = $this->book->where('slug', '=', $slug);
87 $query = $query->where('id', '!=', $currentId);
89 return $query->count() > 0;
92 public function findSuitableSlug($name, $currentId = false)
94 $slug = Str::slug($name);
95 while($this->doesSlugExist($slug, $currentId)) {
96 $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
101 public function getBySearch($term)
103 $terms = explode(' ', preg_quote(trim($term)));
104 $books = $this->book->fullTextSearch(['name', 'description'], $terms);
105 $words = join('|', $terms);
106 foreach ($books as $book) {
108 $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $book->getExcerpt(100));
109 $book->searchSnippet = $result;