]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/ChapterController.php
Merge branch 'prev-next-button' of https://github.com/shubhamosmosys/BookStack into...
[bookstack] / app / Http / Controllers / ChapterController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use BookStack\Actions\View;
4 use BookStack\Entities\Models\Book;
5 use BookStack\Entities\Tools\BookContents;
6 use BookStack\Entities\Repos\ChapterRepo;
7 use BookStack\Entities\Tools\PermissionsUpdater;
8 use BookStack\Exceptions\MoveOperationException;
9 use BookStack\Exceptions\NotFoundException;
10 use Illuminate\Http\Request;
11 use Illuminate\Validation\ValidationException;
12 use Throwable;
13 use Views;
14
15 class ChapterController extends Controller
16 {
17
18     protected $chapterRepo;
19
20     /**
21      * ChapterController constructor.
22      */
23     public function __construct(ChapterRepo $chapterRepo)
24     {
25         $this->chapterRepo = $chapterRepo;
26     }
27
28     /**
29      * Show the form for creating a new chapter.
30      */
31     public function create(string $bookSlug)
32     {
33         $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
34         $this->checkOwnablePermission('chapter-create', $book);
35
36         $this->setPageTitle(trans('entities.chapters_create'));
37         return view('chapters.create', ['book' => $book, 'current' => $book]);
38     }
39
40     /**
41      * Store a newly created chapter in storage.
42      * @throws ValidationException
43      */
44     public function store(Request $request, string $bookSlug)
45     {
46         $this->validate($request, [
47             'name' => 'required|string|max:255'
48         ]);
49
50         $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
51         $this->checkOwnablePermission('chapter-create', $book);
52
53         $chapter = $this->chapterRepo->create($request->all(), $book);
54
55         return redirect($chapter->getUrl());
56     }
57
58     /**
59      * Display the specified chapter.
60      */
61     public function show(string $bookSlug, string $chapterSlug)
62     {
63         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
64         $this->checkOwnablePermission('chapter-view', $chapter);
65
66         $sidebarTree = (new BookContents($chapter->book))->getTree();
67         $pages = $chapter->getVisiblePages();
68         View::incrementFor($chapter);
69
70         $this->setPageTitle($chapter->getShortName());
71         return view('chapters.show', [
72             'book' => $chapter->book,
73             'chapter' => $chapter,
74             'current' => $chapter,
75             'sidebarTree' => $sidebarTree,
76             'pages' => $pages
77         ]);
78     }
79
80     /**
81      * Show the form for editing the specified chapter.
82      */
83     public function edit(string $bookSlug, string $chapterSlug)
84     {
85         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
86         $this->checkOwnablePermission('chapter-update', $chapter);
87
88         $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
89         return view('chapters.edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
90     }
91
92     /**
93      * Update the specified chapter in storage.
94      * @throws NotFoundException
95      */
96     public function update(Request $request, string $bookSlug, string $chapterSlug)
97     {
98         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
99         $this->checkOwnablePermission('chapter-update', $chapter);
100
101         $this->chapterRepo->update($chapter, $request->all());
102
103         return redirect($chapter->getUrl());
104     }
105
106     /**
107      * Shows the page to confirm deletion of this chapter.
108      * @throws NotFoundException
109      */
110     public function showDelete(string $bookSlug, string $chapterSlug)
111     {
112         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
113         $this->checkOwnablePermission('chapter-delete', $chapter);
114
115         $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
116         return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
117     }
118
119     /**
120      * Remove the specified chapter from storage.
121      * @throws NotFoundException
122      * @throws Throwable
123      */
124     public function destroy(string $bookSlug, string $chapterSlug)
125     {
126         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
127         $this->checkOwnablePermission('chapter-delete', $chapter);
128
129         $this->chapterRepo->destroy($chapter);
130
131         return redirect($chapter->book->getUrl());
132     }
133
134     /**
135      * Show the page for moving a chapter.
136      * @throws NotFoundException
137      */
138     public function showMove(string $bookSlug, string $chapterSlug)
139     {
140         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
141         $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
142         $this->checkOwnablePermission('chapter-update', $chapter);
143         $this->checkOwnablePermission('chapter-delete', $chapter);
144
145         return view('chapters.move', [
146             'chapter' => $chapter,
147             'book' => $chapter->book
148         ]);
149     }
150
151     /**
152      * Perform the move action for a chapter.
153      * @throws NotFoundException
154      */
155     public function move(Request $request, string $bookSlug, string $chapterSlug)
156     {
157         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
158         $this->checkOwnablePermission('chapter-update', $chapter);
159         $this->checkOwnablePermission('chapter-delete', $chapter);
160
161         $entitySelection = $request->get('entity_selection', null);
162         if ($entitySelection === null || $entitySelection === '') {
163             return redirect($chapter->getUrl());
164         }
165
166         try {
167             $newBook = $this->chapterRepo->move($chapter, $entitySelection);
168         } catch (MoveOperationException $exception) {
169             $this->showErrorNotification(trans('errors.selected_book_not_found'));
170             return redirect()->back();
171         }
172
173         $this->showSuccessNotification(trans('entities.chapter_move_success', ['bookName' => $newBook->name]));
174         return redirect($chapter->getUrl());
175     }
176
177     /**
178      * Show the Restrictions view.
179      * @throws NotFoundException
180      */
181     public function showPermissions(string $bookSlug, string $chapterSlug)
182     {
183         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
184         $this->checkOwnablePermission('restrictions-manage', $chapter);
185
186         return view('chapters.permissions', [
187             'chapter' => $chapter,
188         ]);
189     }
190
191     /**
192      * Set the restrictions for this chapter.
193      * @throws NotFoundException
194      */
195     public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug, string $chapterSlug)
196     {
197         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
198         $this->checkOwnablePermission('restrictions-manage', $chapter);
199
200         $permissionsUpdater->updateFromPermissionsForm($chapter, $request);
201
202         $this->showSuccessNotification(trans('entities.chapters_permissions_success'));
203         return redirect($chapter->getUrl());
204     }
205 }