]> BookStack Code Mirror - bookstack/blob - app/Repos/BookRepo.php
f5d19bc3731f60fe5f5ec179169a35f1066c642a
[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         $q = $this->permissionService->bookChildrenQuery($book->id, $filterDrafts);
97         $entities = [];
98         $parents = [];
99         $tree = [];
100
101         foreach ($q as $index => $rawEntity) {
102             if ($rawEntity->entity_type === 'Bookstack\\Page') {
103                 $entities[$index] = $this->page->newFromBuilder($rawEntity);
104             } else if ($rawEntity->entity_type === 'Bookstack\\Chapter') {
105                 $entities[$index] = $this->chapter->newFromBuilder($rawEntity);
106                 $key = $entities[$index]->entity_type . ':' . $entities[$index]->id;
107                 $parents[$key] = $entities[$index];
108                 $parents[$key]->setAttribute('pages', collect());
109             }
110             if ($entities[$index]->chapter_id === 0) $tree[] = $entities[$index];
111             $entities[$index]->book = $book;
112         }
113
114         foreach ($entities as $entity) {
115             if ($entity->chapter_id === 0) continue;
116             $parentKey = 'Bookstack\\Chapter:' . $entity->chapter_id;
117             $chapter = $parents[$parentKey];
118             $chapter->pages->push($entity);
119         }
120
121         return collect($tree);
122     }
123
124 }