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();
38 public function newFromInput($input)
40 return $this->book->fill($input);
43 public function countBySlug($slug)
45 return $this->book->where('slug', '=', $slug)->count();
48 public function destroyBySlug($bookSlug)
50 $book = $this->getBySlug($bookSlug);
51 foreach($book->pages as $page) {
54 foreach($book->chapters as $chapter) {
60 public function getNewPriority($book)
62 $lastElem = $book->children()->pop();
63 return $lastElem ? $lastElem->priority + 1 : 0;
66 public function doesSlugExist($slug, $currentId = false)
68 $query = $this->book->where('slug', '=', $slug);
70 $query = $query->where('id', '!=', $currentId);
72 return $query->count() > 0;
75 public function findSuitableSlug($name, $currentId = false)
77 $slug = Str::slug($name);
78 while($this->doesSlugExist($slug, $currentId)) {
79 $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);