| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack\Entities\Tools; | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 08:17:45 +08:00
										 |  |  | use BookStack\Entities\Models\Book; | 
					
						
							|  |  |  | use BookStack\Entities\Models\BookChild; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Chapter; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Entity; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Page; | 
					
						
							| 
									
										
										
										
											2024-02-08 00:37:36 +08:00
										 |  |  | use BookStack\Entities\Queries\EntityQueries; | 
					
						
							| 
									
										
										
										
											2025-01-30 00:40:11 +08:00
										 |  |  | use BookStack\Sorting\BookSortMap; | 
					
						
							|  |  |  | use BookStack\Sorting\BookSortMapItem; | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class BookContents | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-02-08 00:37:36 +08:00
										 |  |  |     protected EntityQueries $queries; | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-08 00:37:36 +08:00
										 |  |  |     public function __construct( | 
					
						
							|  |  |  |         protected Book $book, | 
					
						
							|  |  |  |     ) { | 
					
						
							|  |  |  |         $this->queries = app()->make(EntityQueries::class); | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-09-29 22:05:57 +08:00
										 |  |  |      * Get the current priority of the last item at the top-level of the book. | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function getLastPriority(): int | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-02-08 00:37:36 +08:00
										 |  |  |         $maxPage = $this->book->pages() | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |             ->where('draft', '=', false) | 
					
						
							| 
									
										
										
										
											2024-02-08 00:37:36 +08:00
										 |  |  |             ->where('chapter_id', '=', 0) | 
					
						
							|  |  |  |             ->max('priority'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $maxChapter = $this->book->chapters() | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |             ->max('priority'); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         return max($maxChapter, $maxPage, 1); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get the contents as a sorted collection tree. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getTree(bool $showDrafts = false, bool $renderPages = false): Collection | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-08-22 20:15:58 +08:00
										 |  |  |         $pages = $this->getPages($showDrafts, $renderPages); | 
					
						
							| 
									
										
										
										
											2024-02-08 00:37:36 +08:00
										 |  |  |         $chapters = $this->book->chapters()->scopes('visible')->get(); | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         $all = collect()->concat($pages)->concat($chapters); | 
					
						
							|  |  |  |         $chapterMap = $chapters->keyBy('id'); | 
					
						
							|  |  |  |         $lonePages = collect(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $pages->groupBy('chapter_id')->each(function ($pages, $chapter_id) use ($chapterMap, &$lonePages) { | 
					
						
							|  |  |  |             $chapter = $chapterMap->get($chapter_id); | 
					
						
							|  |  |  |             if ($chapter) { | 
					
						
							| 
									
										
										
										
											2020-12-18 01:31:18 +08:00
										 |  |  |                 $chapter->setAttribute('visible_pages', collect($pages)->sortBy($this->bookChildSortFunc())); | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |             } else { | 
					
						
							|  |  |  |                 $lonePages = $lonePages->concat($pages); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-18 01:31:18 +08:00
										 |  |  |         $chapters->whereNull('visible_pages')->each(function (Chapter $chapter) { | 
					
						
							|  |  |  |             $chapter->setAttribute('visible_pages', collect([])); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-15 05:13:52 +08:00
										 |  |  |         $all->each(function (Entity $entity) use ($renderPages) { | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |             $entity->setRelation('book', $this->book); | 
					
						
							| 
									
										
										
										
											2020-08-15 05:13:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-20 22:03:56 +08:00
										 |  |  |             if ($renderPages && $entity instanceof Page) { | 
					
						
							| 
									
										
										
										
											2020-08-15 05:13:52 +08:00
										 |  |  |                 $entity->html = (new PageContent($entity))->render(); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return collect($chapters)->concat($lonePages)->sortBy($this->bookChildSortFunc()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Function for providing a sorting score for an entity in relation to the | 
					
						
							|  |  |  |      * other items within the book. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function bookChildSortFunc(): callable | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return function (Entity $entity) { | 
					
						
							|  |  |  |             if (isset($entity['draft']) && $entity['draft']) { | 
					
						
							|  |  |  |                 return -100; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |             return $entity['priority'] ?? 0; | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get the visible pages within this book. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-08-22 02:58:19 +08:00
										 |  |  |     protected function getPages(bool $showDrafts = false, bool $getPageContent = false): Collection | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-02-08 00:37:36 +08:00
										 |  |  |         if ($getPageContent) { | 
					
						
							|  |  |  |             $query = $this->queries->pages->visibleWithContents(); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $query = $this->queries->pages->visibleForList(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (!$showDrafts) { | 
					
						
							|  |  |  |             $query->where('draft', '=', false); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-08 00:37:36 +08:00
										 |  |  |         return $query->where('book_id', '=', $this->book->id)->get(); | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | } |