]> BookStack Code Mirror - bookstack/blob - app/Entities/Managers/TrashCan.php
aedf4d7afc3c32b487adae6264d3c292f81e4687
[bookstack] / app / Entities / Managers / TrashCan.php
1 <?php namespace BookStack\Entities\Managers;
2
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;
15 use Exception;
16
17 class TrashCan
18 {
19
20     /**
21      * Send a shelf to the recycle bin.
22      */
23     public function softDestroyShelf(Bookshelf $shelf)
24     {
25         Deletion::createForEntity($shelf);
26         $shelf->delete();
27     }
28
29     /**
30      * Send a book to the recycle bin.
31      * @throws Exception
32      */
33     public function softDestroyBook(Book $book)
34     {
35         Deletion::createForEntity($book);
36
37         foreach ($book->pages as $page) {
38             $this->softDestroyPage($page, false);
39         }
40
41         foreach ($book->chapters as $chapter) {
42             $this->softDestroyChapter($chapter, false);
43         }
44
45         $book->delete();
46     }
47
48     /**
49      * Send a chapter to the recycle bin.
50      * @throws Exception
51      */
52     public function softDestroyChapter(Chapter $chapter, bool $recordDelete = true)
53     {
54         if ($recordDelete) {
55             Deletion::createForEntity($chapter);
56         }
57
58         if (count($chapter->pages) > 0) {
59             foreach ($chapter->pages as $page) {
60                 $this->softDestroyPage($page, false);
61             }
62         }
63
64         $chapter->delete();
65     }
66
67     /**
68      * Send a page to the recycle bin.
69      * @throws Exception
70      */
71     public function softDestroyPage(Page $page, bool $recordDelete = true)
72     {
73         if ($recordDelete) {
74             Deletion::createForEntity($page);
75         }
76
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());
82             }
83             setting()->remove('app-homepage');
84         }
85
86         $page->delete();
87     }
88
89     /**
90      * Remove a bookshelf from the system.
91      * @throws Exception
92      */
93     public function destroyShelf(Bookshelf $shelf): int
94     {
95         $this->destroyCommonRelations($shelf);
96         $shelf->forceDelete();
97         return 1;
98     }
99
100     /**
101      * Remove a book from the system.
102      * Destroys any child chapters and pages.
103      * @throws Exception
104      */
105     public function destroyBook(Book $book): int
106     {
107         $count = 0;
108         $pages = $book->pages()->withTrashed()->get();
109         foreach ($pages as $page) {
110             $this->destroyPage($page);
111             $count++;
112         }
113
114         $chapters = $book->chapters()->withTrashed()->get();
115         foreach ($chapters as $chapter) {
116             $this->destroyChapter($chapter);
117             $count++;
118         }
119
120         $this->destroyCommonRelations($book);
121         $book->forceDelete();
122         return $count + 1;
123     }
124
125     /**
126      * Remove a chapter from the system.
127      * Destroys all pages within.
128      * @throws Exception
129      */
130     public function destroyChapter(Chapter $chapter): int
131     {
132         $count = 0;
133         $pages = $chapter->pages()->withTrashed()->get();
134         if (count($pages)) {
135             foreach ($pages as $page) {
136                 $this->destroyPage($page);
137                 $count++;
138             }
139         }
140
141         $this->destroyCommonRelations($chapter);
142         $chapter->forceDelete();
143         return $count + 1;
144     }
145
146     /**
147      * Remove a page from the system.
148      * @throws Exception
149      */
150     public function destroyPage(Page $page): int
151     {
152         $this->destroyCommonRelations($page);
153
154         // Delete Attached Files
155         $attachmentService = app(AttachmentService::class);
156         foreach ($page->attachments as $attachment) {
157             $attachmentService->deleteFile($attachment);
158         }
159
160         $page->forceDelete();
161         return 1;
162     }
163
164     /**
165      * Get the total counts of those that have been trashed
166      * but not yet fully deleted (In recycle bin).
167      */
168     public function getTrashedCounts(): array
169     {
170         $provider = app(EntityProvider::class);
171         $counts = [];
172
173         /** @var Entity $instance */
174         foreach ($provider->all() as $key => $instance) {
175             $counts[$key] = $instance->newQuery()->onlyTrashed()->count();
176         }
177
178         return $counts;
179     }
180
181     /**
182      * Destroy all items that have pending deletions.
183      */
184     public function destroyFromAllDeletions(): int
185     {
186         $deletions = Deletion::all();
187         $deleteCount = 0;
188         foreach ($deletions as $deletion) {
189             // For each one we load in the relation since it may have already
190             // been deleted as part of another deletion in this loop.
191             $entity = $deletion->deletable()->first();
192             if ($entity) {
193                 $count = $this->destroyEntity($deletion->deletable);
194                 $deleteCount += $count;
195             }
196             $deletion->delete();
197         }
198         return $deleteCount;
199     }
200
201     /**
202      * Destroy the given entity.
203      */
204     protected function destroyEntity(Entity $entity): int
205     {
206         if ($entity->isA('page')) {
207             return $this->destroyPage($entity);
208         }
209         if ($entity->isA('chapter')) {
210             return $this->destroyChapter($entity);
211         }
212         if ($entity->isA('book')) {
213             return $this->destroyBook($entity);
214         }
215         if ($entity->isA('shelf')) {
216             return $this->destroyShelf($entity);
217         }
218     }
219
220     /**
221      * Update entity relations to remove or update outstanding connections.
222      */
223     protected function destroyCommonRelations(Entity $entity)
224     {
225         Activity::removeEntity($entity);
226         $entity->views()->delete();
227         $entity->permissions()->delete();
228         $entity->tags()->delete();
229         $entity->comments()->delete();
230         $entity->jointPermissions()->delete();
231         $entity->searchTerms()->delete();
232         $entity->deletions()->delete();
233
234         if ($entity instanceof HasCoverImage && $entity->cover) {
235             $imageService = app()->make(ImageService::class);
236             $imageService->destroy($entity->cover);
237         }
238     }
239 }