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