]> BookStack Code Mirror - bookstack/blob - app/Repos/BookRepo.php
Started working on chapters
[bookstack] / app / Repos / BookRepo.php
1 <?php namespace Oxbow\Repos;
2
3 use Oxbow\Book;
4
5 class BookRepo
6 {
7
8     protected $book;
9     protected $pageRepo;
10
11     /**
12      * BookRepo constructor.
13      * @param Book $book
14      * @param PageRepo $pageRepo
15      */
16     public function __construct(Book $book, PageRepo $pageRepo)
17     {
18         $this->book = $book;
19         $this->pageRepo = $pageRepo;
20     }
21
22     public function getById($id)
23     {
24         return $this->book->findOrFail($id);
25     }
26
27     public function getAll()
28     {
29         return $this->book->all();
30     }
31
32     public function getBySlug($slug)
33     {
34         return $this->book->where('slug', '=', $slug)->first();
35     }
36
37     public function newFromInput($input)
38     {
39         return $this->book->fill($input);
40     }
41
42     public function countBySlug($slug)
43     {
44         return $this->book->where('slug', '=', $slug)->count();
45     }
46
47     public function destroyById($id)
48     {
49         $book = $this->getById($id);
50         foreach($book->pages as $page) {
51             $this->pageRepo->destroyById($page->id);
52         }
53         $book->delete();
54     }
55
56     public function getTree($book, $currentPageId = false)
57     {
58         $tree = $book->toArray();
59         $tree['pages'] = $this->pageRepo->getTreeByBookId($book->id, $currentPageId);
60         $tree['hasChildren'] = count($tree['pages']) > 0;
61         return $tree;
62     }
63
64 }