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\Deletion;
7 use BookStack\Entities\Entity;
8 use BookStack\Entities\EntityProvider;
9 use BookStack\Entities\HasCoverImage;
10 use BookStack\Entities\Page;
11 use BookStack\Exceptions\NotifyException;
12 use BookStack\Facades\Activity;
13 use BookStack\Uploads\AttachmentService;
14 use BookStack\Uploads\ImageService;
21 * Send a shelf to the recycle bin.
23 public function softDestroyShelf(Bookshelf $shelf)
25 Deletion::createForEntity($shelf);
30 * Send a book to the recycle bin.
33 public function softDestroyBook(Book $book)
35 Deletion::createForEntity($book);
37 foreach ($book->pages as $page) {
38 $this->softDestroyPage($page, false);
41 foreach ($book->chapters as $chapter) {
42 $this->softDestroyChapter($chapter, false);
49 * Send a chapter to the recycle bin.
52 public function softDestroyChapter(Chapter $chapter, bool $recordDelete = true)
55 Deletion::createForEntity($chapter);
58 if (count($chapter->pages) > 0) {
59 foreach ($chapter->pages as $page) {
60 $this->softDestroyPage($page, false);
68 * Send a page to the recycle bin.
71 public function softDestroyPage(Page $page, bool $recordDelete = true)
74 Deletion::createForEntity($page);
77 // Check if set as custom homepage & remove setting if not used or throw error if active
78 $customHome = setting('app-homepage', '0:');
79 if (intval($page->id) === intval(explode(':', $customHome)[0])) {
80 if (setting('app-homepage-type') === 'page') {
81 throw new NotifyException(trans('errors.page_custom_home_deletion'), $page->getUrl());
83 setting()->remove('app-homepage');
90 * Remove a bookshelf from the system.
93 public function destroyShelf(Bookshelf $shelf): int
95 $this->destroyCommonRelations($shelf);
96 $shelf->forceDelete();
101 * Remove a book from the system.
102 * Destroys any child chapters and pages.
105 public function destroyBook(Book $book): int
108 $pages = $book->pages()->withTrashed()->get();
109 foreach ($pages as $page) {
110 $this->destroyPage($page);
114 $chapters = $book->chapters()->withTrashed()->get();
115 foreach ($chapters as $chapter) {
116 $this->destroyChapter($chapter);
120 $this->destroyCommonRelations($book);
121 $book->forceDelete();
126 * Remove a chapter from the system.
127 * Destroys all pages within.
130 public function destroyChapter(Chapter $chapter): int
133 $pages = $chapter->pages()->withTrashed()->get();
135 foreach ($pages as $page) {
136 $this->destroyPage($page);
141 $this->destroyCommonRelations($chapter);
142 $chapter->forceDelete();
147 * Remove a page from the system.
150 public function destroyPage(Page $page): int
152 $this->destroyCommonRelations($page);
154 // Delete Attached Files
155 $attachmentService = app(AttachmentService::class);
156 foreach ($page->attachments as $attachment) {
157 $attachmentService->deleteFile($attachment);
160 $page->forceDelete();
165 * Get the total counts of those that have been trashed
166 * but not yet fully deleted (In recycle bin).
168 public function getTrashedCounts(): array
170 $provider = app(EntityProvider::class);
173 /** @var Entity $instance */
174 foreach ($provider->all() as $key => $instance) {
175 $counts[$key] = $instance->newQuery()->onlyTrashed()->count();
182 * Destroy all items that have pending deletions.
185 public function destroyFromAllDeletions(): int
187 $deletions = Deletion::all();
189 foreach ($deletions as $deletion) {
190 $deleteCount += $this->destroyFromDeletion($deletion);
196 * Destroy an element from the given deletion model.
199 public function destroyFromDeletion(Deletion $deletion): int
201 // We directly load the deletable element here just to ensure it still
202 // exists in the event it has already been destroyed during this request.
203 $entity = $deletion->deletable()->first();
206 $count = $this->destroyEntity($deletion->deletable);
213 * Restore the content within the given deletion.
216 public function restoreFromDeletion(Deletion $deletion): int
218 $shouldRestore = true;
220 $parent = $deletion->deletable->getParent();
222 if ($parent && $parent->trashed()) {
223 $shouldRestore = false;
226 if ($shouldRestore) {
227 $restoreCount = $this->restoreEntity($deletion->deletable);
231 return $restoreCount;
235 * Restore an entity so it is essentially un-deleted.
236 * Deletions on restored child elements will be removed during this restoration.
238 protected function restoreEntity(Entity $entity): int
243 if ($entity->isA('chapter') || $entity->isA('book')) {
244 foreach ($entity->pages()->withTrashed()->withCount('deletions')->get() as $page) {
245 if ($page->deletions_count > 0) {
246 $page->deletions()->delete();
254 if ($entity->isA('book')) {
255 foreach ($entity->chapters()->withTrashed()->withCount('deletions')->get() as $chapter) {
256 if ($chapter->deletions_count === 0) {
257 $chapter->deletions()->delete();
269 * Destroy the given entity.
271 protected function destroyEntity(Entity $entity): int
273 if ($entity->isA('page')) {
274 return $this->destroyPage($entity);
276 if ($entity->isA('chapter')) {
277 return $this->destroyChapter($entity);
279 if ($entity->isA('book')) {
280 return $this->destroyBook($entity);
282 if ($entity->isA('shelf')) {
283 return $this->destroyShelf($entity);
288 * Update entity relations to remove or update outstanding connections.
290 protected function destroyCommonRelations(Entity $entity)
292 Activity::removeEntity($entity);
293 $entity->views()->delete();
294 $entity->permissions()->delete();
295 $entity->tags()->delete();
296 $entity->comments()->delete();
297 $entity->jointPermissions()->delete();
298 $entity->searchTerms()->delete();
299 $entity->deletions()->delete();
301 if ($entity instanceof HasCoverImage && $entity->cover) {
302 $imageService = app()->make(ImageService::class);
303 $imageService->destroy($entity->cover);