]> BookStack Code Mirror - bookstack/blob - app/Entities/Repos/ChapterRepo.php
Checked over recycle bin parent/child flows
[bookstack] / app / Entities / Repos / ChapterRepo.php
1 <?php namespace BookStack\Entities\Repos;
2
3 use BookStack\Entities\Book;
4 use BookStack\Entities\Chapter;
5 use BookStack\Entities\Managers\BookContents;
6 use BookStack\Entities\Managers\TrashCan;
7 use BookStack\Exceptions\MoveOperationException;
8 use BookStack\Exceptions\NotFoundException;
9 use Exception;
10 use Illuminate\Support\Collection;
11
12 class ChapterRepo
13 {
14
15     protected $baseRepo;
16
17     /**
18      * ChapterRepo constructor.
19      */
20     public function __construct(BaseRepo $baseRepo)
21     {
22         $this->baseRepo = $baseRepo;
23     }
24
25     /**
26      * Get a chapter via the slug.
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         return $chapter;
50     }
51
52     /**
53      * Update the given chapter.
54      */
55     public function update(Chapter $chapter, array $input): Chapter
56     {
57         $this->baseRepo->update($chapter, $input);
58         return $chapter;
59     }
60
61     /**
62      * Update the permissions of a chapter.
63      */
64     public function updatePermissions(Chapter $chapter, bool $restricted, Collection $permissions = null)
65     {
66         $this->baseRepo->updatePermissions($chapter, $restricted, $permissions);
67     }
68
69     /**
70      * Remove a chapter from the system.
71      * @throws Exception
72      */
73     public function destroy(Chapter $chapter)
74     {
75         $trashCan = new TrashCan();
76         $trashCan->softDestroyChapter($chapter);
77         $trashCan->autoClearOld();
78     }
79
80     /**
81      * Move the given chapter into a new parent book.
82      * The $parentIdentifier must be a string of the following format:
83      * 'book:<id>' (book:5)
84      * @throws MoveOperationException
85      */
86     public function move(Chapter $chapter, string $parentIdentifier): Book
87     {
88         $stringExploded = explode(':', $parentIdentifier);
89         $entityType = $stringExploded[0];
90         $entityId = intval($stringExploded[1]);
91
92         if ($entityType !== 'book') {
93             throw new MoveOperationException('Chapters can only be moved into books');
94         }
95
96         $parent = Book::visible()->where('id', '=', $entityId)->first();
97         if ($parent === null) {
98             throw new MoveOperationException('Book to move chapter into not found');
99         }
100
101         $chapter->changeBook($parent->id);
102         $chapter->rebuildPermissions();
103         return $parent;
104     }
105 }