]> BookStack Code Mirror - bookstack/blob - app/Repos/BookRepo.php
Update PageRepo.php
[bookstack] / app / Repos / BookRepo.php
1 <?php namespace BookStack\Repos;
2
3 use Activity;
4 use Illuminate\Support\Str;
5 use BookStack\Book;
6 use Views;
7
8 class BookRepo
9 {
10
11     protected $book;
12     protected $pageRepo;
13     protected $chapterRepo;
14
15     /**
16      * BookRepo constructor.
17      * @param Book        $book
18      * @param PageRepo    $pageRepo
19      * @param ChapterRepo $chapterRepo
20      */
21     public function __construct(Book $book, PageRepo $pageRepo, ChapterRepo $chapterRepo)
22     {
23         $this->book = $book;
24         $this->pageRepo = $pageRepo;
25         $this->chapterRepo = $chapterRepo;
26     }
27
28     /**
29      * Get the book that has the given id.
30      * @param $id
31      * @return mixed
32      */
33     public function getById($id)
34     {
35         return $this->book->findOrFail($id);
36     }
37
38     /**
39      * Get all books, Limited by count.
40      * @param int $count
41      * @return mixed
42      */
43     public function getAll($count = 10)
44     {
45         return $this->book->orderBy('name', 'asc')->take($count)->get();
46     }
47
48     /**
49      * Get all books paginated.
50      * @param int $count
51      * @return mixed
52      */
53     public function getAllPaginated($count = 10)
54     {
55         return $this->book->orderBy('name', 'asc')->paginate($count);
56     }
57
58
59     /**
60      * Get the latest books.
61      * @param int $count
62      * @return mixed
63      */
64     public function getLatest($count = 10)
65     {
66         return $this->book->orderBy('created_at', 'desc')->take($count)->get();
67     }
68
69     /**
70      * Gets the most recently viewed for a user.
71      * @param int $count
72      * @param int $page
73      * @return mixed
74      */
75     public function getRecentlyViewed($count = 10, $page = 0)
76     {
77         return Views::getUserRecentlyViewed($count, $page, $this->book);
78     }
79
80     /**
81      * Gets the most viewed books.
82      * @param int $count
83      * @param int $page
84      * @return mixed
85      */
86     public function getPopular($count = 10, $page = 0)
87     {
88         return Views::getPopular($count, $page, $this->book);
89     }
90
91     /**
92      * Get a book by slug
93      * @param $slug
94      * @return mixed
95      */
96     public function getBySlug($slug)
97     {
98         $book = $this->book->where('slug', '=', $slug)->first();
99         if ($book === null) abort(404);
100         return $book;
101     }
102
103     /**
104      * Checks if a book exists.
105      * @param $id
106      * @return bool
107      */
108     public function exists($id)
109     {
110         return $this->book->where('id', '=', $id)->exists();
111     }
112
113     /**
114      * Get a new book instance from request input.
115      * @param $input
116      * @return Book
117      */
118     public function newFromInput($input)
119     {
120         return $this->book->fill($input);
121     }
122
123     /**
124      * Count the amount of books that have a specific slug.
125      * @param $slug
126      * @return mixed
127      */
128     public function countBySlug($slug)
129     {
130         return $this->book->where('slug', '=', $slug)->count();
131     }
132
133     /**
134      * Destroy a book identified by the given slug.
135      * @param $bookSlug
136      */
137     public function destroyBySlug($bookSlug)
138     {
139         $book = $this->getBySlug($bookSlug);
140         foreach ($book->pages as $page) {
141             $this->pageRepo->destroy($page);
142         }
143         foreach ($book->chapters as $chapter) {
144             $this->chapterRepo->destroy($chapter);
145         }
146         $book->views()->delete();
147         $book->delete();
148     }
149
150     /**
151      * Get the next child element priority.
152      * @param Book $book
153      * @return int
154      */
155     public function getNewPriority($book)
156     {
157         $lastElem = $this->getChildren($book)->pop();
158         return $lastElem ? $lastElem->priority + 1 : 0;
159     }
160
161     /**
162      * @param string     $slug
163      * @param bool|false $currentId
164      * @return bool
165      */
166     public function doesSlugExist($slug, $currentId = false)
167     {
168         $query = $this->book->where('slug', '=', $slug);
169         if ($currentId) {
170             $query = $query->where('id', '!=', $currentId);
171         }
172         return $query->count() > 0;
173     }
174
175     /**
176      * Provides a suitable slug for the given book name.
177      * Ensures the returned slug is unique in the system.
178      * @param string     $name
179      * @param bool|false $currentId
180      * @return string
181      */
182     public function findSuitableSlug($name, $currentId = false)
183     {
184         $originalSlug = Str::slug($name);
185         $slug = $originalSlug;
186         $count = 2;
187         while ($this->doesSlugExist($slug, $currentId)) {
188             $slug = $originalSlug . '-' . $count;
189             $count++;
190         }
191         return $slug;
192     }
193
194     /**
195      * Get all child objects of a book.
196      * Returns a sorted collection of Pages and Chapters.
197      * Loads the bookslug onto child elements to prevent access database access for getting the slug.
198      * @param Book $book
199      * @return mixed
200      */
201     public function getChildren(Book $book)
202     {
203         $pages = $book->pages()->where('chapter_id', '=', 0)->get();
204         $chapters = $book->chapters()->with('pages')->get();
205         $children = $pages->merge($chapters);
206         $bookSlug = $book->slug;
207         $children->each(function ($child) use ($bookSlug) {
208             $child->setAttribute('bookSlug', $bookSlug);
209             if ($child->isA('chapter')) {
210                 $child->pages->each(function ($page) use ($bookSlug) {
211                     $page->setAttribute('bookSlug', $bookSlug);
212                 });
213             }
214         });
215         return $children->sortBy('priority');
216     }
217
218     /**
219      * Get books by search term.
220      * @param $term
221      * @return mixed
222      */
223     public function getBySearch($term)
224     {
225         $terms = explode(' ', $term);
226         $books = $this->book->fullTextSearch(['name', 'description'], $terms);
227         $words = join('|', explode(' ', preg_quote(trim($term), '/')));
228         foreach ($books as $book) {
229             //highlight
230             $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $book->getExcerpt(100));
231             $book->searchSnippet = $result;
232         }
233         return $books;
234     }
235
236 }