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\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;
14 use Illuminate\Contracts\Container\BindingResolutionException;
20 * Remove a bookshelf from the system.
23 public function destroyShelf(Bookshelf $shelf)
25 $this->destroyCommonRelations($shelf);
30 * Remove a book from the system.
31 * @throws NotifyException
32 * @throws BindingResolutionException
34 public function destroyBook(Book $book)
36 foreach ($book->pages as $page) {
37 $this->destroyPage($page);
40 foreach ($book->chapters as $chapter) {
41 $this->destroyChapter($chapter);
44 $this->destroyCommonRelations($book);
49 * Remove a page from the system.
50 * @throws NotifyException
52 public function destroyPage(Page $page)
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());
60 setting()->remove('app-homepage');
63 $this->destroyCommonRelations($page);
65 // Delete Attached Files
66 $attachmentService = app(AttachmentService::class);
67 foreach ($page->attachments as $attachment) {
68 $attachmentService->deleteFile($attachment);
75 * Remove a chapter from the system.
78 public function destroyChapter(Chapter $chapter)
80 if (count($chapter->pages) > 0) {
81 foreach ($chapter->pages as $page) {
82 $page->chapter_id = 0;
87 $this->destroyCommonRelations($chapter);
92 * Update entity relations to remove or update outstanding connections.
94 protected function destroyCommonRelations(Entity $entity)
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();
104 if ($entity instanceof HasCoverImage && $entity->cover) {
105 $imageService = app()->make(ImageService::class);
106 $imageService->destroy($entity->cover);