3 namespace BookStack\Entities\Tools;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\BookChild;
7 use BookStack\Entities\Models\Chapter;
8 use BookStack\Entities\Models\Entity;
9 use BookStack\Entities\Models\Page;
10 use BookStack\Exceptions\SortOperationException;
11 use Illuminate\Support\Collection;
21 * BookContents constructor.
23 public function __construct(Book $book)
29 * Get the current priority of the last item
30 * at the top-level of the book.
32 public function getLastPriority(): int
34 $maxPage = Page::visible()->where('book_id', '=', $this->book->id)
35 ->where('draft', '=', false)
36 ->where('chapter_id', '=', 0)->max('priority');
37 $maxChapter = Chapter::visible()->where('book_id', '=', $this->book->id)
40 return max($maxChapter, $maxPage, 1);
44 * Get the contents as a sorted collection tree.
46 public function getTree(bool $showDrafts = false, bool $renderPages = false): Collection
48 $pages = $this->getPages($showDrafts, $renderPages);
49 $chapters = Chapter::visible()->where('book_id', '=', $this->book->id)->get();
50 $all = collect()->concat($pages)->concat($chapters);
51 $chapterMap = $chapters->keyBy('id');
52 $lonePages = collect();
54 $pages->groupBy('chapter_id')->each(function ($pages, $chapter_id) use ($chapterMap, &$lonePages) {
55 $chapter = $chapterMap->get($chapter_id);
57 $chapter->setAttribute('visible_pages', collect($pages)->sortBy($this->bookChildSortFunc()));
59 $lonePages = $lonePages->concat($pages);
63 $chapters->whereNull('visible_pages')->each(function (Chapter $chapter) {
64 $chapter->setAttribute('visible_pages', collect([]));
67 $all->each(function (Entity $entity) use ($renderPages) {
68 $entity->setRelation('book', $this->book);
70 if ($renderPages && $entity instanceof Page) {
71 $entity->html = (new PageContent($entity))->render();
75 return collect($chapters)->concat($lonePages)->sortBy($this->bookChildSortFunc());
79 * Function for providing a sorting score for an entity in relation to the
80 * other items within the book.
82 protected function bookChildSortFunc(): callable
84 return function (Entity $entity) {
85 if (isset($entity['draft']) && $entity['draft']) {
89 return $entity['priority'] ?? 0;
94 * Get the visible pages within this book.
96 protected function getPages(bool $showDrafts = false, bool $getPageContent = false): Collection
98 $query = Page::visible()
99 ->select($getPageContent ? Page::$contentAttributes : Page::$listAttributes)
100 ->where('book_id', '=', $this->book->id);
103 $query->where('draft', '=', false);
106 return $query->get();
110 * Sort the books content using the given map.
111 * The map is a single-dimension collection of objects in the following format:
113 * +"id": "294" (ID of item)
114 * +"sort": 1 (Sort order index)
115 * +"parentChapter": false (ID of parent chapter, as string, or false)
116 * +"type": "page" (Entity type of item)
117 * +"book": "1" (Id of book to place item in)
120 * Returns a list of books that were involved in the operation.
122 * @throws SortOperationException
124 public function sortUsingMap(Collection $sortMap): Collection
126 // Load models into map
127 $this->loadModelsIntoSortMap($sortMap);
128 $booksInvolved = $this->getBooksInvolvedInSort($sortMap);
131 $sortMap->each(function ($mapItem) {
132 $this->applySortUpdates($mapItem);
135 // Update permissions and activity.
136 $booksInvolved->each(function (Book $book) {
137 $book->rebuildPermissions();
140 return $booksInvolved;
144 * Using the given sort map item, detect changes for the related model
145 * and update it if required.
147 protected function applySortUpdates(\stdClass $sortMapItem)
149 /** @var BookChild $model */
150 $model = $sortMapItem->model;
152 $priorityChanged = intval($model->priority) !== intval($sortMapItem->sort);
153 $bookChanged = intval($model->book_id) !== intval($sortMapItem->book);
154 $chapterChanged = ($model instanceof Page) && intval($model->chapter_id) !== $sortMapItem->parentChapter;
157 $model->changeBook($sortMapItem->book);
160 if ($chapterChanged) {
161 $model->chapter_id = intval($sortMapItem->parentChapter);
165 if ($priorityChanged) {
166 $model->priority = intval($sortMapItem->sort);
172 * Load models from the database into the given sort map.
174 protected function loadModelsIntoSortMap(Collection $sortMap): void
176 $keyMap = $sortMap->keyBy(function (\stdClass $sortMapItem) {
177 return $sortMapItem->type . ':' . $sortMapItem->id;
179 $pageIds = $sortMap->where('type', '=', 'page')->pluck('id');
180 $chapterIds = $sortMap->where('type', '=', 'chapter')->pluck('id');
182 $pages = Page::visible()->whereIn('id', $pageIds)->get();
183 $chapters = Chapter::visible()->whereIn('id', $chapterIds)->get();
185 foreach ($pages as $page) {
186 $sortItem = $keyMap->get('page:' . $page->id);
187 $sortItem->model = $page;
190 foreach ($chapters as $chapter) {
191 $sortItem = $keyMap->get('chapter:' . $chapter->id);
192 $sortItem->model = $chapter;
197 * Get the books involved in a sort.
198 * The given sort map should have its models loaded first.
200 * @throws SortOperationException
202 protected function getBooksInvolvedInSort(Collection $sortMap): Collection
204 $bookIdsInvolved = collect([$this->book->id]);
205 $bookIdsInvolved = $bookIdsInvolved->concat($sortMap->pluck('book'));
206 $bookIdsInvolved = $bookIdsInvolved->concat($sortMap->pluck('model.book_id'));
207 $bookIdsInvolved = $bookIdsInvolved->unique()->toArray();
209 $books = Book::hasPermission('update')->whereIn('id', $bookIdsInvolved)->get();
211 if (count($books) !== count($bookIdsInvolved)) {
212 throw new SortOperationException('Could not find all books requested in sort operation');