]> BookStack Code Mirror - bookstack/blob - app/Repos/BookRepo.php
Change application namespace to BookStack
[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()
29     {
30         return $this->book->all();
31     }
32
33     public function getBySlug($slug)
34     {
35         return $this->book->where('slug', '=', $slug)->first();
36     }
37
38     /**
39      * Checks if a book exists.
40      * @param $id
41      * @return bool
42      */
43     public function exists($id)
44     {
45         return $this->book->where('id', '=', $id)->exists();
46     }
47
48     /**
49      * Get a new book instance from request input.
50      * @param $input
51      * @return Book
52      */
53     public function newFromInput($input)
54     {
55         return $this->book->fill($input);
56     }
57
58     public function countBySlug($slug)
59     {
60         return $this->book->where('slug', '=', $slug)->count();
61     }
62
63     public function destroyBySlug($bookSlug)
64     {
65         $book = $this->getBySlug($bookSlug);
66         foreach($book->pages as $page) {
67             \Activity::removeEntity($page);
68             $page->delete();
69         }
70         foreach($book->chapters as $chapter) {
71             \Activity::removeEntity($chapter);
72             $chapter->delete();
73         }
74         $book->delete();
75     }
76
77     public function getNewPriority($book)
78     {
79         $lastElem = $book->children()->pop();
80         return $lastElem ? $lastElem->priority + 1 : 0;
81     }
82
83     public function doesSlugExist($slug, $currentId = false)
84     {
85         $query = $this->book->where('slug', '=', $slug);
86         if($currentId) {
87             $query = $query->where('id', '!=', $currentId);
88         }
89         return $query->count() > 0;
90     }
91
92     public function findSuitableSlug($name, $currentId = false)
93     {
94         $slug = Str::slug($name);
95         while($this->doesSlugExist($slug, $currentId)) {
96             $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
97         }
98         return $slug;
99     }
100
101     public function getBySearch($term)
102     {
103         $terms = explode(' ', preg_quote(trim($term)));
104         $books = $this->book->fullTextSearch(['name', 'description'], $terms);
105         $words = join('|', $terms);
106         foreach ($books as $book) {
107             //highlight
108             $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $book->getExcerpt(100));
109             $book->searchSnippet = $result;
110         }
111         return $books;
112     }
113
114 }