1 <?php namespace BookStack\Entities\Managers;
3 use BookStack\Entities\Book;
4 use BookStack\Entities\Bookshelf;
5 use BookStack\Entities\Chapter;
6 use BookStack\Entities\DeleteRecord;
7 use BookStack\Entities\Entity;
8 use BookStack\Entities\HasCoverImage;
9 use BookStack\Entities\Page;
10 use BookStack\Exceptions\NotifyException;
11 use BookStack\Facades\Activity;
12 use BookStack\Uploads\AttachmentService;
13 use BookStack\Uploads\ImageService;
20 * Send a shelf to the recycle bin.
22 public function softDestroyShelf(Bookshelf $shelf)
24 DeleteRecord::createForEntity($shelf);
29 * Send a book to the recycle bin.
32 public function softDestroyBook(Book $book)
34 DeleteRecord::createForEntity($book);
36 foreach ($book->pages as $page) {
37 $this->softDestroyPage($page, false);
40 foreach ($book->chapters as $chapter) {
41 $this->softDestroyChapter($chapter, false);
48 * Send a chapter to the recycle bin.
51 public function softDestroyChapter(Chapter $chapter, bool $recordDelete = true)
54 DeleteRecord::createForEntity($chapter);
57 if (count($chapter->pages) > 0) {
58 foreach ($chapter->pages as $page) {
59 $this->softDestroyPage($page, false);
67 * Send a page to the recycle bin.
70 public function softDestroyPage(Page $page, bool $recordDelete = true)
73 DeleteRecord::createForEntity($page);
76 // Check if set as custom homepage & remove setting if not used or throw error if active
77 $customHome = setting('app-homepage', '0:');
78 if (intval($page->id) === intval(explode(':', $customHome)[0])) {
79 if (setting('app-homepage-type') === 'page') {
80 throw new NotifyException(trans('errors.page_custom_home_deletion'), $page->getUrl());
82 setting()->remove('app-homepage');
89 * Remove a bookshelf from the system.
92 public function destroyShelf(Bookshelf $shelf)
94 $this->destroyCommonRelations($shelf);
95 $shelf->forceDelete();
99 * Remove a book from the system.
100 * Destroys any child chapters and pages.
103 public function destroyBook(Book $book)
105 $pages = $book->pages()->withTrashed()->get();
106 foreach ($pages as $page) {
107 $this->destroyPage($page);
110 $chapters = $book->chapters()->withTrashed()->get();
111 foreach ($chapters as $chapter) {
112 $this->destroyChapter($chapter);
115 $this->destroyCommonRelations($book);
116 $book->forceDelete();
120 * Remove a chapter from the system.
121 * Destroys all pages within.
124 public function destroyChapter(Chapter $chapter)
126 $pages = $chapter->pages()->withTrashed()->get();
128 foreach ($pages as $page) {
129 $this->destroyPage($page);
133 $this->destroyCommonRelations($chapter);
134 $chapter->forceDelete();
138 * Remove a page from the system.
141 public function destroyPage(Page $page)
143 $this->destroyCommonRelations($page);
145 // Delete Attached Files
146 $attachmentService = app(AttachmentService::class);
147 foreach ($page->attachments as $attachment) {
148 $attachmentService->deleteFile($attachment);
151 $page->forceDelete();
155 * Update entity relations to remove or update outstanding connections.
157 protected function destroyCommonRelations(Entity $entity)
159 Activity::removeEntity($entity);
160 $entity->views()->delete();
161 $entity->permissions()->delete();
162 $entity->tags()->delete();
163 $entity->comments()->delete();
164 $entity->jointPermissions()->delete();
165 $entity->searchTerms()->delete();
166 $entity->deleteRecords()->delete();
168 if ($entity instanceof HasCoverImage && $entity->cover) {
169 $imageService = app()->make(ImageService::class);
170 $imageService->destroy($entity->cover);