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