]> BookStack Code Mirror - bookstack/blob - app/Repos/BookRepo.php
Added 404 page and extra tests
[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         $originalSlug = Str::slug($name);
95         $slug = $originalSlug;
96         $count = 2;
97         while($this->doesSlugExist($slug, $currentId)) {
98             $slug = $originalSlug . '-' . $count;
99             $count++;
100         }
101         return $slug;
102     }
103
104     public function getBySearch($term)
105     {
106         $terms = explode(' ', preg_quote(trim($term)));
107         $books = $this->book->fullTextSearch(['name', 'description'], $terms);
108         $words = join('|', $terms);
109         foreach ($books as $book) {
110             //highlight
111             $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $book->getExcerpt(100));
112             $book->searchSnippet = $result;
113         }
114         return $books;
115     }
116
117 }