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