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