1 <?php namespace BookStack\Repos;
5 class BookRepo extends EntityRepo
8 protected $chapterRepo;
11 * BookRepo constructor.
12 * @param PageRepo $pageRepo
13 * @param ChapterRepo $chapterRepo
15 public function __construct(PageRepo $pageRepo, ChapterRepo $chapterRepo)
17 $this->pageRepo = $pageRepo;
18 $this->chapterRepo = $chapterRepo;
19 parent::__construct();
23 * Get a new book instance from request input.
27 public function createFromInput($input)
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;
34 $this->permissionService->buildJointPermissionsForEntity($book);
39 * Update the given book from user input.
44 public function updateFromInput(Book $book, $input)
46 if ($book->name !== $input['name']) {
47 $book->slug = $this->findSuitableSlug($input['name'], $book->id);
50 $book->updated_by = user()->id;
52 $this->permissionService->buildJointPermissionsForEntity($book);
57 * Destroy the given book.
61 public function destroy(Book $book)
63 foreach ($book->pages as $page) {
64 $this->pageRepo->destroy($page);
66 foreach ($book->chapters as $chapter) {
67 $this->chapterRepo->destroy($chapter);
69 $book->views()->delete();
70 $book->permissions()->delete();
71 $this->permissionService->deleteJointPermissionsForEntity($book);
76 * Get the next child element priority.
80 public function getNewPriority($book)
82 $lastElem = $this->getChildren($book)->pop();
83 return $lastElem ? $lastElem->priority + 1 : 0;
88 * @param bool|false $currentId
91 public function doesSlugExist($slug, $currentId = false)
93 $query = $this->book->where('slug', '=', $slug);
95 $query = $query->where('id', '!=', $currentId);
97 return $query->count() > 0;
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
107 public function findSuitableSlug($name, $currentId = false)
109 $slug = $this->nameToSlug($name);
110 while ($this->doesSlugExist($slug, $currentId)) {
111 $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
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.
121 * @param bool $filterDrafts
124 public function getChildren(Book $book, $filterDrafts = false)
126 $pageQuery = $book->pages()->where('chapter_id', '=', 0);
127 $pageQuery = $this->permissionService->enforcePageRestrictions($pageQuery, 'view');
130 $pageQuery = $pageQuery->where('draft', '=', false);
133 $pages = $pageQuery->get();
135 $chapterQuery = $book->chapters()->with(['pages' => function ($query) use ($filterDrafts) {
136 $this->permissionService->enforcePageRestrictions($query, 'view');
137 if ($filterDrafts) $query->where('draft', '=', false);
139 $chapterQuery = $this->permissionService->enforceChapterRestrictions($chapterQuery, 'view');
140 $chapters = $chapterQuery->get();
141 $children = $pages->values();
142 foreach ($chapters as $chapter) {
143 $children->push($chapter);
145 $bookSlug = $book->slug;
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);
153 $child->pages = $child->pages->sortBy(function ($child, $key) {
154 $score = $child->priority;
155 if ($child->draft) $score -= 100;
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;
170 * Get books by search term.
173 * @param array $paginationAppends
176 public function getBySearch($term, $count = 20, $paginationAppends = [])
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) {
185 $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $book->getExcerpt(100));
186 $book->searchSnippet = $result;