]> BookStack Code Mirror - bookstack/blob - app/Entities/Tools/HierarchyTransformer.php
93c5bb9bb7103fb12ab3a60f2d76ebc0d75b22fc
[bookstack] / app / Entities / Tools / HierarchyTransformer.php
1 <?php
2
3 namespace BookStack\Entities\Tools;
4
5 use BookStack\Actions\ActivityType;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Bookshelf;
8 use BookStack\Entities\Models\Chapter;
9 use BookStack\Entities\Models\Page;
10 use BookStack\Entities\Repos\BookRepo;
11 use BookStack\Entities\Repos\BookshelfRepo;
12 use BookStack\Facades\Activity;
13
14 class HierarchyTransformer
15 {
16     protected BookRepo $bookRepo;
17     protected BookshelfRepo $shelfRepo;
18     protected Cloner $cloner;
19     protected TrashCan $trashCan;
20
21     public function __construct(BookRepo $bookRepo, BookshelfRepo $shelfRepo, Cloner $cloner, TrashCan $trashCan)
22     {
23         $this->bookRepo = $bookRepo;
24         $this->shelfRepo = $shelfRepo;
25         $this->cloner = $cloner;
26         $this->trashCan = $trashCan;
27     }
28
29     /**
30      * Transform a chapter into a book.
31      * Does not check permissions, check before calling.
32      */
33     public function transformChapterToBook(Chapter $chapter): Book
34     {
35         $inputData = $this->cloner->entityToInputData($chapter);
36         $book = $this->bookRepo->create($inputData);
37         $this->cloner->copyEntityPermissions($chapter, $book);
38
39         /** @var Page $page */
40         foreach ($chapter->pages as $page) {
41             $page->chapter_id = 0;
42             $page->changeBook($book->id);
43         }
44
45         $this->trashCan->destroyEntity($chapter);
46
47         Activity::add(ActivityType::BOOK_CREATE_FROM_CHAPTER, $book);
48         return $book;
49     }
50
51     /**
52      * Transform a book into a shelf.
53      * Does not check permissions, check before calling.
54      */
55     public function transformBookToShelf(Book $book): Bookshelf
56     {
57         $inputData = $this->cloner->entityToInputData($book);
58         $shelf = $this->shelfRepo->create($inputData, []);
59         $this->cloner->copyEntityPermissions($book, $shelf);
60
61         $shelfBookSyncData = [];
62
63         /** @var Chapter $chapter */
64         foreach ($book->chapters as $index => $chapter) {
65             $newBook = $this->transformChapterToBook($chapter);
66             $shelfBookSyncData[$newBook->id] = ['order' => $index];
67             if (!$newBook->restricted) {
68                 $this->cloner->copyEntityPermissions($shelf, $newBook);
69             }
70         }
71
72         if ($book->directPages->count() > 0) {
73             $book->name .= ' ' . trans('entities.pages');
74             $shelfBookSyncData[$book->id] = ['order' => count($shelfBookSyncData) + 1];
75             $book->save();
76         } else {
77             $this->trashCan->destroyEntity($book);
78         }
79
80         $shelf->books()->sync($shelfBookSyncData);
81
82         Activity::add(ActivityType::BOOKSHELF_CREATE_FROM_BOOK, $shelf);
83         return $shelf;
84     }
85 }