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', $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('book', $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;
87 * Get all child objects of a book.
88 * Returns a sorted collection of Pages and Chapters.
89 * Loads the book slug onto child elements to prevent access database access for getting the slug.
91 * @param bool $filterDrafts
94 public function getChildren(Book $book, $filterDrafts = false)
96 $pageQuery = $book->pages()->where('chapter_id', '=', 0);
97 $pageQuery = $this->permissionService->enforcePageRestrictions($pageQuery, 'view');
100 $pageQuery = $pageQuery->where('draft', '=', false);
103 $pages = $pageQuery->get();
105 $chapterQuery = $book->chapters()->with(['pages' => function ($query) use ($filterDrafts) {
106 $this->permissionService->enforcePageRestrictions($query, 'view');
107 if ($filterDrafts) $query->where('draft', '=', false);
109 $chapterQuery = $this->permissionService->enforceChapterRestrictions($chapterQuery, 'view');
110 $chapters = $chapterQuery->get();
111 $children = $pages->values();
112 foreach ($chapters as $chapter) {
113 $children->push($chapter);
115 $bookSlug = $book->slug;
117 $children->each(function ($child) use ($bookSlug) {
118 $child->setAttribute('bookSlug', $bookSlug);
119 if ($child->isA('chapter')) {
120 $child->pages->each(function ($page) use ($bookSlug) {
121 $page->setAttribute('bookSlug', $bookSlug);
123 $child->pages = $child->pages->sortBy(function ($child, $key) {
124 $score = $child->priority;
125 if ($child->draft) $score -= 100;
131 // Sort items with drafts first then by priority.
132 return $children->sortBy(function ($child, $key) {
133 $score = $child->priority;
134 if ($child->isA('page') && $child->draft) $score -= 100;