]> BookStack Code Mirror - bookstack/blob - app/Entities/Repos/ChapterRepo.php
Update PWA manifest orientation to any
[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\Page;
8 use BookStack\Entities\Models\Chapter;
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         $this->baseRepo->updateDefaultTemplate($chapter, intval($input['default_template_id'] ?? null));
50         Activity::add(ActivityType::CHAPTER_CREATE, $chapter);
51
52         return $chapter;
53     }
54
55     /**
56      * Update the given chapter.
57      */
58     public function update(Chapter $chapter, array $input): Chapter
59     {
60         $this->baseRepo->update($chapter, $input);
61
62         if (array_key_exists('default_template_id', $input)) {
63             $this->baseRepo->updateDefaultTemplate($chapter, intval($input['default_template_id']));
64         }
65
66         Activity::add(ActivityType::CHAPTER_UPDATE, $chapter);
67
68         return $chapter;
69     }
70
71     /**
72      * Remove a chapter from the system.
73      *
74      * @throws Exception
75      */
76     public function destroy(Chapter $chapter)
77     {
78         $trashCan = new TrashCan();
79         $trashCan->softDestroyChapter($chapter);
80         Activity::add(ActivityType::CHAPTER_DELETE, $chapter);
81         $trashCan->autoClearOld();
82     }
83
84     /**
85      * Move the given chapter into a new parent book.
86      * The $parentIdentifier must be a string of the following format:
87      * 'book:<id>' (book:5).
88      *
89      * @throws MoveOperationException
90      * @throws PermissionsException
91      */
92     public function move(Chapter $chapter, string $parentIdentifier): Book
93     {
94         $parent = $this->findParentByIdentifier($parentIdentifier);
95         if (is_null($parent)) {
96             throw new MoveOperationException('Book to move chapter into not found');
97         }
98
99         if (!userCan('chapter-create', $parent)) {
100             throw new PermissionsException('User does not have permission to create a chapter within the chosen book');
101         }
102
103         $chapter->changeBook($parent->id);
104         $chapter->rebuildPermissions();
105         Activity::add(ActivityType::CHAPTER_MOVE, $chapter);
106
107         return $parent;
108     }
109
110     /**
111      * Find a page parent entity via an identifier string in the format:
112      * {type}:{id}
113      * Example: (book:5).
114      *
115      * @throws MoveOperationException
116      */
117     public function findParentByIdentifier(string $identifier): ?Book
118     {
119         $stringExploded = explode(':', $identifier);
120         $entityType = $stringExploded[0];
121         $entityId = intval($stringExploded[1]);
122
123         if ($entityType !== 'book') {
124             throw new MoveOperationException('Chapters can only be in books');
125         }
126
127         return Book::visible()->where('id', '=', $entityId)->first();
128     }
129 }