1 <?php namespace Oxbow\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 * Get a new book instance from request input.
43 public function newFromInput($input)
45 return $this->book->fill($input);
48 public function countBySlug($slug)
50 return $this->book->where('slug', '=', $slug)->count();
53 public function destroyBySlug($bookSlug)
55 $book = $this->getBySlug($bookSlug);
56 foreach($book->pages as $page) {
59 foreach($book->chapters as $chapter) {
65 public function getNewPriority($book)
67 $lastElem = $book->children()->pop();
68 return $lastElem ? $lastElem->priority + 1 : 0;
71 public function doesSlugExist($slug, $currentId = false)
73 $query = $this->book->where('slug', '=', $slug);
75 $query = $query->where('id', '!=', $currentId);
77 return $query->count() > 0;
80 public function findSuitableSlug($name, $currentId = false)
82 $slug = Str::slug($name);
83 while($this->doesSlugExist($slug, $currentId)) {
84 $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
89 public function getBySearch($term)
91 $terms = explode(' ', preg_quote(trim($term)));
92 $books = $this->book->fullTextSearch(['name', 'description'], $terms);
93 $words = join('|', $terms);
94 foreach ($books as $book) {
96 $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $book->getExcerpt(100));
97 $book->searchSnippet = $result;