]> BookStack Code Mirror - bookstack/blob - app/Entities/Repos/BookRepo.php
Updated translator & dependency attribution before release v25.05.1
[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 Exception;
14 use Illuminate\Http\UploadedFile;
15
16 class BookRepo
17 {
18     public function __construct(
19         protected BaseRepo $baseRepo,
20         protected TagRepo $tagRepo,
21         protected ImageRepo $imageRepo,
22         protected TrashCan $trashCan,
23     ) {
24     }
25
26     /**
27      * Create a new book in the system.
28      */
29     public function create(array $input): Book
30     {
31         $book = new Book();
32         $this->baseRepo->create($book, $input);
33         $this->baseRepo->updateCoverImage($book, $input['image'] ?? null);
34         $this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id'] ?? null));
35         Activity::add(ActivityType::BOOK_CREATE, $book);
36
37         $defaultBookSortSetting = intval(setting('sorting-book-default', '0'));
38         if ($defaultBookSortSetting && SortRule::query()->find($defaultBookSortSetting)) {
39             $book->sort_rule_id = $defaultBookSortSetting;
40             $book->save();
41         }
42
43         return $book;
44     }
45
46     /**
47      * Update the given book.
48      */
49     public function update(Book $book, array $input): Book
50     {
51         $this->baseRepo->update($book, $input);
52
53         if (array_key_exists('default_template_id', $input)) {
54             $this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id']));
55         }
56
57         if (array_key_exists('image', $input)) {
58             $this->baseRepo->updateCoverImage($book, $input['image'], $input['image'] === null);
59         }
60
61         Activity::add(ActivityType::BOOK_UPDATE, $book);
62
63         return $book;
64     }
65
66     /**
67      * Update the given book's cover image, or clear it.
68      *
69      * @throws ImageUploadException
70      * @throws Exception
71      */
72     public function updateCoverImage(Book $book, ?UploadedFile $coverImage, bool $removeImage = false)
73     {
74         $this->baseRepo->updateCoverImage($book, $coverImage, $removeImage);
75     }
76
77     /**
78      * Remove a book from the system.
79      *
80      * @throws Exception
81      */
82     public function destroy(Book $book)
83     {
84         $this->trashCan->softDestroyBook($book);
85         Activity::add(ActivityType::BOOK_DELETE, $book);
86
87         $this->trashCan->autoClearOld();
88     }
89 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.