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