]> BookStack Code Mirror - bookstack/blob - app/Repos/BookRepo.php
55dbb86dc711f22a079befdf5615299aba7fd204
[bookstack] / app / Repos / BookRepo.php
1 <?php namespace BookStack\Repos;
2
3 use BookStack\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
14     /**
15      * BookRepo constructor.
16      * @param Book     $book
17      * @param PageRepo $pageRepo
18      */
19     public function __construct(Book $book, PageRepo $pageRepo)
20     {
21         $this->book = $book;
22         $this->pageRepo = $pageRepo;
23     }
24
25     /**
26      * Get the book that has the given id.
27      * @param $id
28      * @return mixed
29      */
30     public function getById($id)
31     {
32         return $this->book->findOrFail($id);
33     }
34
35     /**
36      * Get all books, Limited by count.
37      * @param int $count
38      * @return mixed
39      */
40     public function getAll($count = 10)
41     {
42         return $this->book->orderBy('name', 'asc')->take($count)->get();
43     }
44
45     /**
46      * Get all books paginated.
47      * @param int $count
48      * @return mixed
49      */
50     public function getAllPaginated($count = 10)
51     {
52         return $this->book->orderBy('name', 'asc')->paginate($count);
53     }
54
55     public function getRecentlyViewed($count = 10, $page = 0)
56     {
57         return Views::getUserRecentlyViewed($count, $page, $this->book);
58     }
59
60     /**
61      * Get a book by slug
62      * @param $slug
63      * @return mixed
64      */
65     public function getBySlug($slug)
66     {
67         return $this->book->where('slug', '=', $slug)->first();
68     }
69
70     /**
71      * Checks if a book exists.
72      * @param $id
73      * @return bool
74      */
75     public function exists($id)
76     {
77         return $this->book->where('id', '=', $id)->exists();
78     }
79
80     /**
81      * Get a new book instance from request input.
82      * @param $input
83      * @return Book
84      */
85     public function newFromInput($input)
86     {
87         return $this->book->fill($input);
88     }
89
90     /**
91      * Count the amount of books that have a specific slug.
92      * @param $slug
93      * @return mixed
94      */
95     public function countBySlug($slug)
96     {
97         return $this->book->where('slug', '=', $slug)->count();
98     }
99
100     /**
101      * Destroy a book identified by the given slug.
102      * @param $bookSlug
103      */
104     public function destroyBySlug($bookSlug)
105     {
106         $book = $this->getBySlug($bookSlug);
107         foreach ($book->pages as $page) {
108             \Activity::removeEntity($page);
109             $page->delete();
110         }
111         foreach ($book->chapters as $chapter) {
112             \Activity::removeEntity($chapter);
113             $chapter->delete();
114         }
115         $book->delete();
116     }
117
118     /**
119      * Get the next child element priority.
120      * @param Book $book
121      * @return int
122      */
123     public function getNewPriority($book)
124     {
125         $lastElem = $book->children()->pop();
126         return $lastElem ? $lastElem->priority + 1 : 0;
127     }
128
129     /**
130      * @param string     $slug
131      * @param bool|false $currentId
132      * @return bool
133      */
134     public function doesSlugExist($slug, $currentId = false)
135     {
136         $query = $this->book->where('slug', '=', $slug);
137         if ($currentId) {
138             $query = $query->where('id', '!=', $currentId);
139         }
140         return $query->count() > 0;
141     }
142
143     /**
144      * Provides a suitable slug for the given book name.
145      * Ensures the returned slug is unique in the system.
146      * @param string     $name
147      * @param bool|false $currentId
148      * @return string
149      */
150     public function findSuitableSlug($name, $currentId = false)
151     {
152         $originalSlug = Str::slug($name);
153         $slug = $originalSlug;
154         $count = 2;
155         while ($this->doesSlugExist($slug, $currentId)) {
156             $slug = $originalSlug . '-' . $count;
157             $count++;
158         }
159         return $slug;
160     }
161
162     /**
163      * Get books by search term.
164      * @param $term
165      * @return mixed
166      */
167     public function getBySearch($term)
168     {
169         $terms = explode(' ', preg_quote(trim($term)));
170         $books = $this->book->fullTextSearch(['name', 'description'], $terms);
171         $words = join('|', $terms);
172         foreach ($books as $book) {
173             //highlight
174             $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $book->getExcerpt(100));
175             $book->searchSnippet = $result;
176         }
177         return $books;
178     }
179
180 }