]> BookStack Code Mirror - bookstack/blob - app/Repos/BookRepo.php
3043d2916576398028f20e6dff1ce3519e534acd
[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', $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('book', $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      * 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.
90      * @param Book $book
91      * @param bool $filterDrafts
92      * @return mixed
93      */
94     public function getChildren(Book $book, $filterDrafts = false)
95     {
96         $pageQuery = $book->pages()->where('chapter_id', '=', 0);
97         $pageQuery = $this->permissionService->enforcePageRestrictions($pageQuery, 'view');
98
99         if ($filterDrafts) {
100             $pageQuery = $pageQuery->where('draft', '=', false);
101         }
102
103         $pages = $pageQuery->get();
104
105         $chapterQuery = $book->chapters()->with(['pages' => function ($query) use ($filterDrafts) {
106             $this->permissionService->enforcePageRestrictions($query, 'view');
107             if ($filterDrafts) $query->where('draft', '=', false);
108         }]);
109         $chapterQuery = $this->permissionService->enforceChapterRestrictions($chapterQuery, 'view');
110         $chapters = $chapterQuery->get();
111         $children = $pages->values();
112         foreach ($chapters as $chapter) {
113             $children->push($chapter);
114         }
115         $bookSlug = $book->slug;
116
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);
122                 });
123                 $child->pages = $child->pages->sortBy(function ($child, $key) {
124                     $score = $child->priority;
125                     if ($child->draft) $score -= 100;
126                     return $score;
127                 });
128             }
129         });
130
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;
135             return $score;
136         });
137     }
138
139 }