]> BookStack Code Mirror - bookstack/blob - app/Entities/Repos/BookRepo.php
Perms: Fixed some issues made when adding transactions
[bookstack] / app / Entities / Repos / BookRepo.php
1 <?php
2
3 namespace BookStack\Entities\Repos;
4
5 use BookStack\Activity\ActivityType;
6 use BookStack\Activity\TagRepo;
7 use BookStack\Entities\Models\Book;
8 use BookStack\Entities\Tools\TrashCan;
9 use BookStack\Exceptions\ImageUploadException;
10 use BookStack\Facades\Activity;
11 use BookStack\Sorting\SortRule;
12 use BookStack\Uploads\ImageRepo;
13 use BookStack\Util\DatabaseTransaction;
14 use Exception;
15 use Illuminate\Http\UploadedFile;
16
17 class BookRepo
18 {
19     public function __construct(
20         protected BaseRepo $baseRepo,
21         protected TagRepo $tagRepo,
22         protected ImageRepo $imageRepo,
23         protected TrashCan $trashCan,
24     ) {
25     }
26
27     /**
28      * Create a new book in the system.
29      */
30     public function create(array $input): Book
31     {
32         return (new DatabaseTransaction(function () use ($input) {
33             $book = new Book();
34
35             $this->baseRepo->create($book, $input);
36             $this->baseRepo->updateCoverImage($book, $input['image'] ?? null);
37             $this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id'] ?? null));
38             Activity::add(ActivityType::BOOK_CREATE, $book);
39
40             $defaultBookSortSetting = intval(setting('sorting-book-default', '0'));
41             if ($defaultBookSortSetting && SortRule::query()->find($defaultBookSortSetting)) {
42                 $book->sort_rule_id = $defaultBookSortSetting;
43                 $book->save();
44             }
45
46             return $book;
47         }))->run();
48     }
49
50     /**
51      * Update the given book.
52      */
53     public function update(Book $book, array $input): Book
54     {
55         $this->baseRepo->update($book, $input);
56
57         if (array_key_exists('default_template_id', $input)) {
58             $this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id']));
59         }
60
61         if (array_key_exists('image', $input)) {
62             $this->baseRepo->updateCoverImage($book, $input['image'], $input['image'] === null);
63         }
64
65         Activity::add(ActivityType::BOOK_UPDATE, $book);
66
67         return $book;
68     }
69
70     /**
71      * Update the given book's cover image, or clear it.
72      *
73      * @throws ImageUploadException
74      * @throws Exception
75      */
76     public function updateCoverImage(Book $book, ?UploadedFile $coverImage, bool $removeImage = false)
77     {
78         $this->baseRepo->updateCoverImage($book, $coverImage, $removeImage);
79     }
80
81     /**
82      * Remove a book from the system.
83      *
84      * @throws Exception
85      */
86     public function destroy(Book $book)
87     {
88         $this->trashCan->softDestroyBook($book);
89         Activity::add(ActivityType::BOOK_DELETE, $book);
90
91         $this->trashCan->autoClearOld();
92     }
93 }