]> BookStack Code Mirror - bookstack/blob - app/Repos/BookRepo.php
Revamped image system to use driver-agnotstic storage and be more efficent
[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         return $this->book->where('slug', '=', $slug)->first();
99     }
100
101     /**
102      * Checks if a book exists.
103      * @param $id
104      * @return bool
105      */
106     public function exists($id)
107     {
108         return $this->book->where('id', '=', $id)->exists();
109     }
110
111     /**
112      * Get a new book instance from request input.
113      * @param $input
114      * @return Book
115      */
116     public function newFromInput($input)
117     {
118         return $this->book->fill($input);
119     }
120
121     /**
122      * Count the amount of books that have a specific slug.
123      * @param $slug
124      * @return mixed
125      */
126     public function countBySlug($slug)
127     {
128         return $this->book->where('slug', '=', $slug)->count();
129     }
130
131     /**
132      * Destroy a book identified by the given slug.
133      * @param $bookSlug
134      */
135     public function destroyBySlug($bookSlug)
136     {
137         $book = $this->getBySlug($bookSlug);
138         foreach ($book->pages as $page) {
139             $this->pageRepo->destroy($page);
140         }
141         foreach ($book->chapters as $chapter) {
142             $this->chapterRepo->destroy($chapter);
143         }
144         $book->views()->delete();
145         $book->delete();
146     }
147
148     /**
149      * Get the next child element priority.
150      * @param Book $book
151      * @return int
152      */
153     public function getNewPriority($book)
154     {
155         $lastElem = $this->getChildren($book)->pop();
156         return $lastElem ? $lastElem->priority + 1 : 0;
157     }
158
159     /**
160      * @param string     $slug
161      * @param bool|false $currentId
162      * @return bool
163      */
164     public function doesSlugExist($slug, $currentId = false)
165     {
166         $query = $this->book->where('slug', '=', $slug);
167         if ($currentId) {
168             $query = $query->where('id', '!=', $currentId);
169         }
170         return $query->count() > 0;
171     }
172
173     /**
174      * Provides a suitable slug for the given book name.
175      * Ensures the returned slug is unique in the system.
176      * @param string     $name
177      * @param bool|false $currentId
178      * @return string
179      */
180     public function findSuitableSlug($name, $currentId = false)
181     {
182         $originalSlug = Str::slug($name);
183         $slug = $originalSlug;
184         $count = 2;
185         while ($this->doesSlugExist($slug, $currentId)) {
186             $slug = $originalSlug . '-' . $count;
187             $count++;
188         }
189         return $slug;
190     }
191
192     /**
193      * Get all child objects of a book.
194      * Returns a sorted collection of Pages and Chapters.
195      * Loads the bookslug onto child elements to prevent access database access for getting the slug.
196      * @param Book $book
197      * @return mixed
198      */
199     public function getChildren(Book $book)
200     {
201         $pages = $book->pages()->where('chapter_id', '=', 0)->get();
202         $chapters = $book->chapters()->with('pages')->get();
203         $children = $pages->merge($chapters);
204         $bookSlug = $book->slug;
205         $children->each(function ($child) use ($bookSlug) {
206             $child->setAttribute('bookSlug', $bookSlug);
207             if ($child->isA('chapter')) {
208                 $child->pages->each(function ($page) use ($bookSlug) {
209                     $page->setAttribute('bookSlug', $bookSlug);
210                 });
211             }
212         });
213         return $children->sortBy('priority');
214     }
215
216     /**
217      * Get books by search term.
218      * @param $term
219      * @return mixed
220      */
221     public function getBySearch($term)
222     {
223         $terms = explode(' ', preg_quote(trim($term)));
224         $books = $this->book->fullTextSearch(['name', 'description'], $terms);
225         $words = join('|', $terms);
226         foreach ($books as $book) {
227             //highlight
228             $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $book->getExcerpt(100));
229             $book->searchSnippet = $result;
230         }
231         return $books;
232     }
233
234 }