]> BookStack Code Mirror - bookstack/blob - app/Entities/Tools/HierarchyTransformer.php
Played around with a new app structure
[bookstack] / app / Entities / Tools / HierarchyTransformer.php
1 <?php
2
3 namespace BookStack\Entities\Tools;
4
5 use BookStack\Activity\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
49         return $book;
50     }
51
52     /**
53      * Transform a book into a shelf.
54      * Does not check permissions, check before calling.
55      */
56     public function transformBookToShelf(Book $book): Bookshelf
57     {
58         $inputData = $this->cloner->entityToInputData($book);
59         $shelf = $this->shelfRepo->create($inputData, []);
60         $this->cloner->copyEntityPermissions($book, $shelf);
61
62         $shelfBookSyncData = [];
63
64         /** @var Chapter $chapter */
65         foreach ($book->chapters as $index => $chapter) {
66             $newBook = $this->transformChapterToBook($chapter);
67             $shelfBookSyncData[$newBook->id] = ['order' => $index];
68             if (!$newBook->hasPermissions()) {
69                 $this->cloner->copyEntityPermissions($shelf, $newBook);
70             }
71         }
72
73         if ($book->directPages->count() > 0) {
74             $book->name .= ' ' . trans('entities.pages');
75             $shelfBookSyncData[$book->id] = ['order' => count($shelfBookSyncData) + 1];
76             $book->save();
77         } else {
78             $this->trashCan->destroyEntity($book);
79         }
80
81         $shelf->books()->sync($shelfBookSyncData);
82
83         Activity::add(ActivityType::BOOKSHELF_CREATE_FROM_BOOK, $shelf);
84
85         return $shelf;
86     }
87 }