]> BookStack Code Mirror - bookstack/blob - app/Entities/Managers/TrashCan.php
Updated styles to use logical properties/values
[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\Entity;
7 use BookStack\Entities\HasCoverImage;
8 use BookStack\Entities\Page;
9 use BookStack\Exceptions\NotifyException;
10 use BookStack\Facades\Activity;
11 use BookStack\Uploads\AttachmentService;
12 use BookStack\Uploads\ImageService;
13 use Exception;
14 use Illuminate\Contracts\Container\BindingResolutionException;
15
16 class TrashCan
17 {
18
19     /**
20      * Remove a bookshelf from the system.
21      * @throws Exception
22      */
23     public function destroyShelf(Bookshelf $shelf)
24     {
25         $this->destroyCommonRelations($shelf);
26         $shelf->delete();
27     }
28
29     /**
30      * Remove a book from the system.
31      * @throws NotifyException
32      * @throws BindingResolutionException
33      */
34     public function destroyBook(Book $book)
35     {
36         foreach ($book->pages as $page) {
37             $this->destroyPage($page);
38         }
39
40         foreach ($book->chapters as $chapter) {
41             $this->destroyChapter($chapter);
42         }
43
44         $this->destroyCommonRelations($book);
45         $book->delete();
46     }
47
48     /**
49      * Remove a page from the system.
50      * @throws NotifyException
51      */
52     public function destroyPage(Page $page)
53     {
54         // Check if set as custom homepage & remove setting if not used or throw error if active
55         $customHome = setting('app-homepage', '0:');
56         if (intval($page->id) === intval(explode(':', $customHome)[0])) {
57             if (setting('app-homepage-type') === 'page') {
58                 throw new NotifyException(trans('errors.page_custom_home_deletion'), $page->getUrl());
59             }
60             setting()->remove('app-homepage');
61         }
62
63         $this->destroyCommonRelations($page);
64
65         // Delete Attached Files
66         $attachmentService = app(AttachmentService::class);
67         foreach ($page->attachments as $attachment) {
68             $attachmentService->deleteFile($attachment);
69         }
70
71         $page->delete();
72     }
73
74     /**
75      * Remove a chapter from the system.
76      * @throws Exception
77      */
78     public function destroyChapter(Chapter $chapter)
79     {
80         if (count($chapter->pages) > 0) {
81             foreach ($chapter->pages as $page) {
82                 $page->chapter_id = 0;
83                 $page->save();
84             }
85         }
86
87         $this->destroyCommonRelations($chapter);
88         $chapter->delete();
89     }
90
91     /**
92      * Update entity relations to remove or update outstanding connections.
93      */
94     protected function destroyCommonRelations(Entity $entity)
95     {
96         Activity::removeEntity($entity);
97         $entity->views()->delete();
98         $entity->permissions()->delete();
99         $entity->tags()->delete();
100         $entity->comments()->delete();
101         $entity->jointPermissions()->delete();
102         $entity->searchTerms()->delete();
103
104         if ($entity instanceof HasCoverImage && $entity->cover) {
105             $imageService = app()->make(ImageService::class);
106             $imageService->destroy($entity->cover);
107         }
108     }
109 }