]> BookStack Code Mirror - bookstack/blob - app/Repos/BookRepo.php
ebfda3fa44dd0829455c34d66ab31cb60e2434f6
[bookstack] / app / Repos / BookRepo.php
1 <?php namespace BookStack\Repos;
2
3 use BookStack\Book;
4
5 class BookRepo extends EntityRepo
6 {
7     protected $pageRepo;
8     protected $chapterRepo;
9
10     /**
11      * BookRepo constructor.
12      * @param PageRepo $pageRepo
13      * @param ChapterRepo $chapterRepo
14      */
15     public function __construct(PageRepo $pageRepo, ChapterRepo $chapterRepo)
16     {
17         $this->pageRepo = $pageRepo;
18         $this->chapterRepo = $chapterRepo;
19         parent::__construct();
20     }
21
22     /**
23      * Get a new book instance from request input.
24      * @param array $input
25      * @return Book
26      */
27     public function createFromInput($input)
28     {
29         $book = $this->book->newInstance($input);
30         $book->slug = $this->findSuitableSlug($book->name);
31         $book->created_by = user()->id;
32         $book->updated_by = user()->id;
33         $book->save();
34         $this->permissionService->buildJointPermissionsForEntity($book);
35         return $book;
36     }
37
38     /**
39      * Update the given book from user input.
40      * @param Book $book
41      * @param $input
42      * @return Book
43      */
44     public function updateFromInput(Book $book, $input)
45     {
46         if ($book->name !== $input['name']) {
47             $book->slug = $this->findSuitableSlug($input['name'], $book->id);
48         }
49         $book->fill($input);
50         $book->updated_by = user()->id;
51         $book->save();
52         $this->permissionService->buildJointPermissionsForEntity($book);
53         return $book;
54     }
55
56     /**
57      * Destroy the given book.
58      * @param Book $book
59      * @throws \Exception
60      */
61     public function destroy(Book $book)
62     {
63         foreach ($book->pages as $page) {
64             $this->pageRepo->destroy($page);
65         }
66         foreach ($book->chapters as $chapter) {
67             $this->chapterRepo->destroy($chapter);
68         }
69         $book->views()->delete();
70         $book->permissions()->delete();
71         $this->permissionService->deleteJointPermissionsForEntity($book);
72         $book->delete();
73     }
74
75     /**
76      * Get the next child element priority.
77      * @param Book $book
78      * @return int
79      */
80     public function getNewPriority($book)
81     {
82         $lastElem = $this->getChildren($book)->pop();
83         return $lastElem ? $lastElem->priority + 1 : 0;
84     }
85
86     /**
87      * @param string $slug
88      * @param bool|false $currentId
89      * @return bool
90      */
91     public function doesSlugExist($slug, $currentId = false)
92     {
93         $query = $this->book->where('slug', '=', $slug);
94         if ($currentId) {
95             $query = $query->where('id', '!=', $currentId);
96         }
97         return $query->count() > 0;
98     }
99
100     /**
101      * Provides a suitable slug for the given book name.
102      * Ensures the returned slug is unique in the system.
103      * @param string $name
104      * @param bool|false $currentId
105      * @return string
106      */
107     public function findSuitableSlug($name, $currentId = false)
108     {
109         $slug = $this->nameToSlug($name);
110         while ($this->doesSlugExist($slug, $currentId)) {
111             $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
112         }
113         return $slug;
114     }
115
116     /**
117      * Get all child objects of a book.
118      * Returns a sorted collection of Pages and Chapters.
119      * Loads the book slug onto child elements to prevent access database access for getting the slug.
120      * @param Book $book
121      * @param bool $filterDrafts
122      * @return mixed
123      */
124     public function getChildren(Book $book, $filterDrafts = false)
125     {
126         $pageQuery = $book->pages()->where('chapter_id', '=', 0);
127         $pageQuery = $this->permissionService->enforcePageRestrictions($pageQuery, 'view');
128
129         if ($filterDrafts) {
130             $pageQuery = $pageQuery->where('draft', '=', false);
131         }
132
133         $pages = $pageQuery->get();
134
135         $chapterQuery = $book->chapters()->with(['pages' => function ($query) use ($filterDrafts) {
136             $this->permissionService->enforcePageRestrictions($query, 'view');
137             if ($filterDrafts) $query->where('draft', '=', false);
138         }]);
139         $chapterQuery = $this->permissionService->enforceChapterRestrictions($chapterQuery, 'view');
140         $chapters = $chapterQuery->get();
141         $children = $pages->values();
142         foreach ($chapters as $chapter) {
143             $children->push($chapter);
144         }
145         $bookSlug = $book->slug;
146
147         $children->each(function ($child) use ($bookSlug) {
148             $child->setAttribute('bookSlug', $bookSlug);
149             if ($child->isA('chapter')) {
150                 $child->pages->each(function ($page) use ($bookSlug) {
151                     $page->setAttribute('bookSlug', $bookSlug);
152                 });
153                 $child->pages = $child->pages->sortBy(function ($child, $key) {
154                     $score = $child->priority;
155                     if ($child->draft) $score -= 100;
156                     return $score;
157                 });
158             }
159         });
160
161         // Sort items with drafts first then by priority.
162         return $children->sortBy(function ($child, $key) {
163             $score = $child->priority;
164             if ($child->isA('page') && $child->draft) $score -= 100;
165             return $score;
166         });
167     }
168
169     /**
170      * Get books by search term.
171      * @param $term
172      * @param int $count
173      * @param array $paginationAppends
174      * @return mixed
175      */
176     public function getBySearch($term, $count = 20, $paginationAppends = [])
177     {
178         $terms = $this->prepareSearchTerms($term);
179         $bookQuery = $this->permissionService->enforceBookRestrictions($this->book->fullTextSearchQuery(['name', 'description'], $terms));
180         $bookQuery = $this->addAdvancedSearchQueries($bookQuery, $term);
181         $books = $bookQuery->paginate($count)->appends($paginationAppends);
182         $words = join('|', explode(' ', preg_quote(trim($term), '/')));
183         foreach ($books as $book) {
184             //highlight
185             $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $book->getExcerpt(100));
186             $book->searchSnippet = $result;
187         }
188         return $books;
189     }
190
191 }