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 Illuminate\Support\Collection;
16 public function __construct(Book $book)
22 * Get the current priority of the last item at the top-level of the book.
24 public function getLastPriority(): int
26 $maxPage = Page::visible()->where('book_id', '=', $this->book->id)
27 ->where('draft', '=', false)
28 ->where('chapter_id', '=', 0)->max('priority');
29 $maxChapter = Chapter::visible()->where('book_id', '=', $this->book->id)
32 return max($maxChapter, $maxPage, 1);
36 * Get the contents as a sorted collection tree.
38 public function getTree(bool $showDrafts = false, bool $renderPages = false): Collection
40 $pages = $this->getPages($showDrafts, $renderPages);
41 $chapters = Chapter::visible()->where('book_id', '=', $this->book->id)->get();
42 $all = collect()->concat($pages)->concat($chapters);
43 $chapterMap = $chapters->keyBy('id');
44 $lonePages = collect();
46 $pages->groupBy('chapter_id')->each(function ($pages, $chapter_id) use ($chapterMap, &$lonePages) {
47 $chapter = $chapterMap->get($chapter_id);
49 $chapter->setAttribute('visible_pages', collect($pages)->sortBy($this->bookChildSortFunc()));
51 $lonePages = $lonePages->concat($pages);
55 $chapters->whereNull('visible_pages')->each(function (Chapter $chapter) {
56 $chapter->setAttribute('visible_pages', collect([]));
59 $all->each(function (Entity $entity) use ($renderPages) {
60 $entity->setRelation('book', $this->book);
62 if ($renderPages && $entity instanceof Page) {
63 $entity->html = (new PageContent($entity))->render();
67 return collect($chapters)->concat($lonePages)->sortBy($this->bookChildSortFunc());
71 * Function for providing a sorting score for an entity in relation to the
72 * other items within the book.
74 protected function bookChildSortFunc(): callable
76 return function (Entity $entity) {
77 if (isset($entity['draft']) && $entity['draft']) {
81 return $entity['priority'] ?? 0;
86 * Get the visible pages within this book.
88 protected function getPages(bool $showDrafts = false, bool $getPageContent = false): Collection
90 $query = Page::visible()
91 ->select($getPageContent ? Page::$contentAttributes : Page::$listAttributes)
92 ->where('book_id', '=', $this->book->id);
95 $query->where('draft', '=', false);
102 * Sort the books content using the given sort map.
103 * Returns a list of books that were involved in the operation.
107 public function sortUsingMap(BookSortMap $sortMap): array
109 // Load models into map
110 $modelMap = $this->loadModelsFromSortMap($sortMap);
112 // Sort our changes from our map to be chapters first
113 // Since they need to be process to ensure book alignment for child page changes.
114 $sortMapItems = $sortMap->all();
115 usort($sortMapItems, function (BookSortMapItem $itemA, BookSortMapItem $itemB) {
116 $aScore = $itemA->type === 'page' ? 2 : 1;
117 $bScore = $itemB->type === 'page' ? 2 : 1;
119 return $aScore - $bScore;
123 foreach ($sortMapItems as $item) {
124 $this->applySortUpdates($item, $modelMap);
127 /** @var Book[] $booksInvolved */
128 $booksInvolved = array_values(array_filter($modelMap, function (string $key) {
129 return strpos($key, 'book:') === 0;
130 }, ARRAY_FILTER_USE_KEY));
132 // Update permissions of books involved
133 foreach ($booksInvolved as $book) {
134 $book->rebuildPermissions();
137 return $booksInvolved;
141 * Using the given sort map item, detect changes for the related model
142 * and update it if required. Changes where permissions are lacking will
143 * be skipped and not throw an error.
145 * @param array<string, Entity> $modelMap
147 protected function applySortUpdates(BookSortMapItem $sortMapItem, array $modelMap): void
149 /** @var BookChild $model */
150 $model = $modelMap[$sortMapItem->type . ':' . $sortMapItem->id] ?? null;
155 $priorityChanged = $model->priority !== $sortMapItem->sort;
156 $bookChanged = $model->book_id !== $sortMapItem->parentBookId;
157 $chapterChanged = ($model instanceof Page) && $model->chapter_id !== $sortMapItem->parentChapterId;
159 // Stop if there's no change
160 if (!$priorityChanged && !$bookChanged && !$chapterChanged) {
164 $currentParentKey = 'book:' . $model->book_id;
165 if ($model instanceof Page && $model->chapter_id) {
166 $currentParentKey = 'chapter:' . $model->chapter_id;
169 $currentParent = $modelMap[$currentParentKey] ?? null;
170 /** @var Book $newBook */
171 $newBook = $modelMap['book:' . $sortMapItem->parentBookId] ?? null;
172 /** @var ?Chapter $newChapter */
173 $newChapter = $sortMapItem->parentChapterId ? ($modelMap['chapter:' . $sortMapItem->parentChapterId] ?? null) : null;
175 if (!$this->isSortChangePermissible($sortMapItem, $model, $currentParent, $newBook, $newChapter)) {
179 // Action the required changes
181 $model->changeBook($newBook->id);
184 if ($model instanceof Page && $chapterChanged) {
185 $model->chapter_id = $newChapter->id ?? 0;
188 if ($priorityChanged) {
189 $model->priority = $sortMapItem->sort;
192 if ($chapterChanged || $priorityChanged) {
198 * Check if the current user has permissions to apply the given sorting change.
199 * Is quite complex since items can gain a different parent change. Acts as a:
200 * - Update of old parent element (Change of content/order).
201 * - Update of sorted/moved element.
202 * - Deletion of element (Relative to parent upon move).
203 * - Creation of element within parent (Upon move to new parent).
205 protected function isSortChangePermissible(BookSortMapItem $sortMapItem, BookChild $model, ?Entity $currentParent, ?Entity $newBook, ?Entity $newChapter): bool
207 // Stop if we can't see the current parent or new book.
208 if (!$currentParent || !$newBook) {
212 $hasNewParent = $newBook->id !== $model->book_id || ($model instanceof Page && $model->chapter_id !== ($sortMapItem->parentChapterId ?? 0));
213 if ($model instanceof Chapter) {
214 $hasPermission = userCan('book-update', $currentParent)
215 && userCan('book-update', $newBook)
216 && userCan('chapter-update', $model)
217 && (!$hasNewParent || userCan('chapter-create', $newBook))
218 && (!$hasNewParent || userCan('chapter-delete', $model));
220 if (!$hasPermission) {
225 if ($model instanceof Page) {
226 $parentPermission = ($currentParent instanceof Chapter) ? 'chapter-update' : 'book-update';
227 $hasCurrentParentPermission = userCan($parentPermission, $currentParent);
229 // This needs to check if there was an intended chapter location in the original sort map
230 // rather than inferring from the $newChapter since that variable may be null
231 // due to other reasons (Visibility).
232 $newParent = $sortMapItem->parentChapterId ? $newChapter : $newBook;
237 $hasPageEditPermission = userCan('page-update', $model);
238 $newParentInRightLocation = ($newParent instanceof Book || ($newParent instanceof Chapter && $newParent->book_id === $newBook->id));
239 $newParentPermission = ($newParent instanceof Chapter) ? 'chapter-update' : 'book-update';
240 $hasNewParentPermission = userCan($newParentPermission, $newParent);
242 $hasDeletePermissionIfMoving = (!$hasNewParent || userCan('page-delete', $model));
243 $hasCreatePermissionIfMoving = (!$hasNewParent || userCan('page-create', $newParent));
245 $hasPermission = $hasCurrentParentPermission
246 && $newParentInRightLocation
247 && $hasNewParentPermission
248 && $hasPageEditPermission
249 && $hasDeletePermissionIfMoving
250 && $hasCreatePermissionIfMoving;
252 if (!$hasPermission) {
261 * Load models from the database into the given sort map.
263 * @return array<string, Entity>
265 protected function loadModelsFromSortMap(BookSortMap $sortMap): array
274 foreach ($sortMap->all() as $sortMapItem) {
275 $ids[$sortMapItem->type][] = $sortMapItem->id;
276 $ids['book'][] = $sortMapItem->parentBookId;
277 if ($sortMapItem->parentChapterId) {
278 $ids['chapter'][] = $sortMapItem->parentChapterId;
282 $pages = Page::visible()->whereIn('id', array_unique($ids['page']))->get(Page::$listAttributes);
283 /** @var Page $page */
284 foreach ($pages as $page) {
285 $modelMap['page:' . $page->id] = $page;
286 $ids['book'][] = $page->book_id;
287 if ($page->chapter_id) {
288 $ids['chapter'][] = $page->chapter_id;
292 $chapters = Chapter::visible()->whereIn('id', array_unique($ids['chapter']))->get();
293 /** @var Chapter $chapter */
294 foreach ($chapters as $chapter) {
295 $modelMap['chapter:' . $chapter->id] = $chapter;
296 $ids['book'][] = $chapter->book_id;
299 $books = Book::visible()->whereIn('id', array_unique($ids['book']))->get();
300 /** @var Book $book */
301 foreach ($books as $book) {
302 $modelMap['book:' . $book->id] = $book;