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