]> BookStack Code Mirror - bookstack/blob - app/Entities/Repos/ChapterRepo.php
WYSIWYG: Updated TinyMCE from 6.5.1 to 6.7.2
[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\Models\Entity;
9 use BookStack\Entities\Tools\BookContents;
10 use BookStack\Entities\Tools\TrashCan;
11 use BookStack\Exceptions\MoveOperationException;
12 use BookStack\Exceptions\NotFoundException;
13 use BookStack\Exceptions\PermissionsException;
14 use BookStack\Facades\Activity;
15 use Exception;
16
17 class ChapterRepo
18 {
19     public function __construct(
20         protected BaseRepo $baseRepo
21     ) {
22     }
23
24     /**
25      * Get a chapter via the slug.
26      *
27      * @throws NotFoundException
28      */
29     public function getBySlug(string $bookSlug, string $chapterSlug): Chapter
30     {
31         $chapter = Chapter::visible()->whereSlugs($bookSlug, $chapterSlug)->first();
32
33         if ($chapter === null) {
34             throw new NotFoundException(trans('errors.chapter_not_found'));
35         }
36
37         return $chapter;
38     }
39
40     /**
41      * Create a new chapter in the system.
42      */
43     public function create(array $input, Book $parentBook): Chapter
44     {
45         $chapter = new Chapter();
46         $chapter->book_id = $parentBook->id;
47         $chapter->priority = (new BookContents($parentBook))->getLastPriority() + 1;
48         $this->baseRepo->create($chapter, $input);
49         Activity::add(ActivityType::CHAPTER_CREATE, $chapter);
50
51         return $chapter;
52     }
53
54     /**
55      * Update the given chapter.
56      */
57     public function update(Chapter $chapter, array $input): Chapter
58     {
59         $this->baseRepo->update($chapter, $input);
60         Activity::add(ActivityType::CHAPTER_UPDATE, $chapter);
61
62         return $chapter;
63     }
64
65     /**
66      * Remove a chapter from the system.
67      *
68      * @throws Exception
69      */
70     public function destroy(Chapter $chapter)
71     {
72         $trashCan = new TrashCan();
73         $trashCan->softDestroyChapter($chapter);
74         Activity::add(ActivityType::CHAPTER_DELETE, $chapter);
75         $trashCan->autoClearOld();
76     }
77
78     /**
79      * Move the given chapter into a new parent book.
80      * The $parentIdentifier must be a string of the following format:
81      * 'book:<id>' (book:5).
82      *
83      * @throws MoveOperationException
84      * @throws PermissionsException
85      */
86     public function move(Chapter $chapter, string $parentIdentifier): Book
87     {
88         $parent = $this->findParentByIdentifier($parentIdentifier);
89         if (is_null($parent)) {
90             throw new MoveOperationException('Book to move chapter into not found');
91         }
92
93         if (!userCan('chapter-create', $parent)) {
94             throw new PermissionsException('User does not have permission to create a chapter within the chosen book');
95         }
96
97         $chapter->changeBook($parent->id);
98         $chapter->rebuildPermissions();
99         Activity::add(ActivityType::CHAPTER_MOVE, $chapter);
100
101         return $parent;
102     }
103
104     /**
105      * Find a page parent entity via an identifier string in the format:
106      * {type}:{id}
107      * Example: (book:5).
108      *
109      * @throws MoveOperationException
110      */
111     public function findParentByIdentifier(string $identifier): ?Book
112     {
113         $stringExploded = explode(':', $identifier);
114         $entityType = $stringExploded[0];
115         $entityId = intval($stringExploded[1]);
116
117         if ($entityType !== 'book') {
118             throw new MoveOperationException('Chapters can only be in books');
119         }
120
121         return Book::visible()->where('id', '=', $entityId)->first();
122     }
123 }