]> BookStack Code Mirror - bookstack/blob - app/Entities/Repos/ChapterRepo.php
2ab6e671f6d5679b3d293dbc72804c113d2b5c38
[bookstack] / app / Entities / Repos / ChapterRepo.php
1 <?php
2
3 namespace BookStack\Entities\Repos;
4
5 use BookStack\Activity\ActivityType;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Chapter;
8 use BookStack\Entities\Queries\EntityQueries;
9 use BookStack\Entities\Tools\BookContents;
10 use BookStack\Entities\Tools\TrashCan;
11 use BookStack\Exceptions\MoveOperationException;
12 use BookStack\Exceptions\PermissionsException;
13 use BookStack\Facades\Activity;
14 use BookStack\Util\DatabaseTransaction;
15 use Exception;
16
17 class ChapterRepo
18 {
19     public function __construct(
20         protected BaseRepo $baseRepo,
21         protected EntityQueries $entityQueries,
22         protected TrashCan $trashCan,
23     ) {
24     }
25
26     /**
27      * Create a new chapter in the system.
28      */
29     public function create(array $input, Book $parentBook): Chapter
30     {
31         return (new DatabaseTransaction(function () use ($input, $parentBook) {
32             $chapter = new Chapter();
33             $chapter->book_id = $parentBook->id;
34             $chapter->priority = (new BookContents($parentBook))->getLastPriority() + 1;
35             $this->baseRepo->create($chapter, $input);
36             $this->baseRepo->updateDefaultTemplate($chapter, intval($input['default_template_id'] ?? null));
37             Activity::add(ActivityType::CHAPTER_CREATE, $chapter);
38
39             $this->baseRepo->sortParent($chapter);
40         }))->run();
41     }
42
43     /**
44      * Update the given chapter.
45      */
46     public function update(Chapter $chapter, array $input): Chapter
47     {
48         $this->baseRepo->update($chapter, $input);
49
50         if (array_key_exists('default_template_id', $input)) {
51             $this->baseRepo->updateDefaultTemplate($chapter, intval($input['default_template_id']));
52         }
53
54         Activity::add(ActivityType::CHAPTER_UPDATE, $chapter);
55
56         $this->baseRepo->sortParent($chapter);
57
58         return $chapter;
59     }
60
61     /**
62      * Remove a chapter from the system.
63      *
64      * @throws Exception
65      */
66     public function destroy(Chapter $chapter)
67     {
68         $this->trashCan->softDestroyChapter($chapter);
69         Activity::add(ActivityType::CHAPTER_DELETE, $chapter);
70         $this->trashCan->autoClearOld();
71     }
72
73     /**
74      * Move the given chapter into a new parent book.
75      * The $parentIdentifier must be a string of the following format:
76      * 'book:<id>' (book:5).
77      *
78      * @throws MoveOperationException
79      * @throws PermissionsException
80      */
81     public function move(Chapter $chapter, string $parentIdentifier): Book
82     {
83         $parent = $this->entityQueries->findVisibleByStringIdentifier($parentIdentifier);
84         if (!$parent instanceof Book) {
85             throw new MoveOperationException('Book to move chapter into not found');
86         }
87
88         if (!userCan('chapter-create', $parent)) {
89             throw new PermissionsException('User does not have permission to create a chapter within the chosen book');
90         }
91
92         return (new DatabaseTransaction(function () use ($chapter, $parent) {
93             $chapter->changeBook($parent->id);
94             $chapter->rebuildPermissions();
95             Activity::add(ActivityType::CHAPTER_MOVE, $chapter);
96
97             $this->baseRepo->sortParent($chapter);
98
99             return $parent;
100         }))->run();
101     }
102 }