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