]> BookStack Code Mirror - bookstack/blob - app/Repos/BookRepo.php
Added limit to books shown on homepage and make alphabetical
[bookstack] / app / Repos / BookRepo.php
1 <?php namespace BookStack\Repos;
2
3 use Illuminate\Support\Str;
4 use BookStack\Book;
5
6 class BookRepo
7 {
8
9     protected $book;
10     protected $pageRepo;
11
12     /**
13      * BookRepo constructor.
14      * @param Book     $book
15      * @param PageRepo $pageRepo
16      */
17     public function __construct(Book $book, PageRepo $pageRepo)
18     {
19         $this->book = $book;
20         $this->pageRepo = $pageRepo;
21     }
22
23     public function getById($id)
24     {
25         return $this->book->findOrFail($id);
26     }
27
28     public function getAll($count = 10)
29     {
30         return $this->book->orderBy('name', 'asc')->take($count)->get();
31     }
32
33     /**
34      * Getas
35      * @param int $count
36      * @return mixed
37      */
38     public function getAllPaginated($count = 10)
39     {
40         return $this->book->orderBy('name', 'asc')->paginate($count);
41     }
42
43     public function getBySlug($slug)
44     {
45         return $this->book->where('slug', '=', $slug)->first();
46     }
47
48     /**
49      * Checks if a book exists.
50      * @param $id
51      * @return bool
52      */
53     public function exists($id)
54     {
55         return $this->book->where('id', '=', $id)->exists();
56     }
57
58     /**
59      * Get a new book instance from request input.
60      * @param $input
61      * @return Book
62      */
63     public function newFromInput($input)
64     {
65         return $this->book->fill($input);
66     }
67
68     public function countBySlug($slug)
69     {
70         return $this->book->where('slug', '=', $slug)->count();
71     }
72
73     public function destroyBySlug($bookSlug)
74     {
75         $book = $this->getBySlug($bookSlug);
76         foreach ($book->pages as $page) {
77             \Activity::removeEntity($page);
78             $page->delete();
79         }
80         foreach ($book->chapters as $chapter) {
81             \Activity::removeEntity($chapter);
82             $chapter->delete();
83         }
84         $book->delete();
85     }
86
87     public function getNewPriority($book)
88     {
89         $lastElem = $book->children()->pop();
90         return $lastElem ? $lastElem->priority + 1 : 0;
91     }
92
93     public function doesSlugExist($slug, $currentId = false)
94     {
95         $query = $this->book->where('slug', '=', $slug);
96         if ($currentId) {
97             $query = $query->where('id', '!=', $currentId);
98         }
99         return $query->count() > 0;
100     }
101
102     public function findSuitableSlug($name, $currentId = false)
103     {
104         $originalSlug = Str::slug($name);
105         $slug = $originalSlug;
106         $count = 2;
107         while ($this->doesSlugExist($slug, $currentId)) {
108             $slug = $originalSlug . '-' . $count;
109             $count++;
110         }
111         return $slug;
112     }
113
114     public function getBySearch($term)
115     {
116         $terms = explode(' ', preg_quote(trim($term)));
117         $books = $this->book->fullTextSearch(['name', 'description'], $terms);
118         $words = join('|', $terms);
119         foreach ($books as $book) {
120             //highlight
121             $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $book->getExcerpt(100));
122             $book->searchSnippet = $result;
123         }
124         return $books;
125     }
126
127 }