]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookController.php
Updated attachment download responses to stream from filesystem
[bookstack] / app / Http / Controllers / BookController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Actions\ActivityQueries;
6 use BookStack\Actions\ActivityType;
7 use BookStack\Actions\View;
8 use BookStack\Entities\Models\Bookshelf;
9 use BookStack\Entities\Repos\BookRepo;
10 use BookStack\Entities\Tools\BookContents;
11 use BookStack\Entities\Tools\Cloner;
12 use BookStack\Entities\Tools\PermissionsUpdater;
13 use BookStack\Entities\Tools\ShelfContext;
14 use BookStack\Exceptions\ImageUploadException;
15 use BookStack\Exceptions\NotFoundException;
16 use BookStack\Facades\Activity;
17 use Illuminate\Http\Request;
18 use Illuminate\Validation\ValidationException;
19 use Throwable;
20
21 class BookController extends Controller
22 {
23     protected $bookRepo;
24     protected $entityContextManager;
25
26     public function __construct(ShelfContext $entityContextManager, BookRepo $bookRepo)
27     {
28         $this->bookRepo = $bookRepo;
29         $this->entityContextManager = $entityContextManager;
30     }
31
32     /**
33      * Display a listing of the book.
34      */
35     public function index()
36     {
37         $view = setting()->getForCurrentUser('books_view_type');
38         $sort = setting()->getForCurrentUser('books_sort', 'name');
39         $order = setting()->getForCurrentUser('books_sort_order', 'asc');
40
41         $books = $this->bookRepo->getAllPaginated(18, $sort, $order);
42         $recents = $this->isSignedIn() ? $this->bookRepo->getRecentlyViewed(4) : false;
43         $popular = $this->bookRepo->getPopular(4);
44         $new = $this->bookRepo->getRecentlyCreated(4);
45
46         $this->entityContextManager->clearShelfContext();
47
48         $this->setPageTitle(trans('entities.books'));
49
50         return view('books.index', [
51             'books'   => $books,
52             'recents' => $recents,
53             'popular' => $popular,
54             'new'     => $new,
55             'view'    => $view,
56             'sort'    => $sort,
57             'order'   => $order,
58         ]);
59     }
60
61     /**
62      * Show the form for creating a new book.
63      */
64     public function create(string $shelfSlug = null)
65     {
66         $this->checkPermission('book-create-all');
67
68         $bookshelf = null;
69         if ($shelfSlug !== null) {
70             $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
71             $this->checkOwnablePermission('bookshelf-update', $bookshelf);
72         }
73
74         $this->setPageTitle(trans('entities.books_create'));
75
76         return view('books.create', [
77             'bookshelf' => $bookshelf,
78         ]);
79     }
80
81     /**
82      * Store a newly created book in storage.
83      *
84      * @throws ImageUploadException
85      * @throws ValidationException
86      */
87     public function store(Request $request, string $shelfSlug = null)
88     {
89         $this->checkPermission('book-create-all');
90         $this->validate($request, [
91             'name'        => ['required', 'string', 'max:255'],
92             'description' => ['string', 'max:1000'],
93             'image'       => array_merge(['nullable'], $this->getImageValidationRules()),
94         ]);
95
96         $bookshelf = null;
97         if ($shelfSlug !== null) {
98             $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
99             $this->checkOwnablePermission('bookshelf-update', $bookshelf);
100         }
101
102         $book = $this->bookRepo->create($request->all());
103         $this->bookRepo->updateCoverImage($book, $request->file('image', null));
104
105         if ($bookshelf) {
106             $bookshelf->appendBook($book);
107             Activity::add(ActivityType::BOOKSHELF_UPDATE, $bookshelf);
108         }
109
110         return redirect($book->getUrl());
111     }
112
113     /**
114      * Display the specified book.
115      */
116     public function show(Request $request, ActivityQueries $activities, string $slug)
117     {
118         $book = $this->bookRepo->getBySlug($slug);
119         $bookChildren = (new BookContents($book))->getTree(true);
120         $bookParentShelves = $book->shelves()->scopes('visible')->get();
121
122         View::incrementFor($book);
123         if ($request->has('shelf')) {
124             $this->entityContextManager->setShelfContext(intval($request->get('shelf')));
125         }
126
127         $this->setPageTitle($book->getShortName());
128
129         return view('books.show', [
130             'book'              => $book,
131             'current'           => $book,
132             'bookChildren'      => $bookChildren,
133             'bookParentShelves' => $bookParentShelves,
134             'activity'          => $activities->entityActivity($book, 20, 1),
135         ]);
136     }
137
138     /**
139      * Show the form for editing the specified book.
140      */
141     public function edit(string $slug)
142     {
143         $book = $this->bookRepo->getBySlug($slug);
144         $this->checkOwnablePermission('book-update', $book);
145         $this->setPageTitle(trans('entities.books_edit_named', ['bookName'=>$book->getShortName()]));
146
147         return view('books.edit', ['book' => $book, 'current' => $book]);
148     }
149
150     /**
151      * Update the specified book in storage.
152      *
153      * @throws ImageUploadException
154      * @throws ValidationException
155      * @throws Throwable
156      */
157     public function update(Request $request, string $slug)
158     {
159         $book = $this->bookRepo->getBySlug($slug);
160         $this->checkOwnablePermission('book-update', $book);
161         $this->validate($request, [
162             'name'        => ['required', 'string', 'max:255'],
163             'description' => ['string', 'max:1000'],
164             'image'       => array_merge(['nullable'], $this->getImageValidationRules()),
165         ]);
166
167         $book = $this->bookRepo->update($book, $request->all());
168         $resetCover = $request->has('image_reset');
169         $this->bookRepo->updateCoverImage($book, $request->file('image', null), $resetCover);
170
171         return redirect($book->getUrl());
172     }
173
174     /**
175      * Shows the page to confirm deletion.
176      */
177     public function showDelete(string $bookSlug)
178     {
179         $book = $this->bookRepo->getBySlug($bookSlug);
180         $this->checkOwnablePermission('book-delete', $book);
181         $this->setPageTitle(trans('entities.books_delete_named', ['bookName' => $book->getShortName()]));
182
183         return view('books.delete', ['book' => $book, 'current' => $book]);
184     }
185
186     /**
187      * Remove the specified book from the system.
188      *
189      * @throws Throwable
190      */
191     public function destroy(string $bookSlug)
192     {
193         $book = $this->bookRepo->getBySlug($bookSlug);
194         $this->checkOwnablePermission('book-delete', $book);
195
196         $this->bookRepo->destroy($book);
197
198         return redirect('/books');
199     }
200
201     /**
202      * Show the permissions view.
203      */
204     public function showPermissions(string $bookSlug)
205     {
206         $book = $this->bookRepo->getBySlug($bookSlug);
207         $this->checkOwnablePermission('restrictions-manage', $book);
208
209         return view('books.permissions', [
210             'book' => $book,
211         ]);
212     }
213
214     /**
215      * Set the restrictions for this book.
216      *
217      * @throws Throwable
218      */
219     public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug)
220     {
221         $book = $this->bookRepo->getBySlug($bookSlug);
222         $this->checkOwnablePermission('restrictions-manage', $book);
223
224         $permissionsUpdater->updateFromPermissionsForm($book, $request);
225
226         $this->showSuccessNotification(trans('entities.books_permissions_updated'));
227
228         return redirect($book->getUrl());
229     }
230
231     /**
232      * Show the view to copy a book.
233      *
234      * @throws NotFoundException
235      */
236     public function showCopy(string $bookSlug)
237     {
238         $book = $this->bookRepo->getBySlug($bookSlug);
239         $this->checkOwnablePermission('book-view', $book);
240
241         session()->flashInput(['name' => $book->name]);
242
243         return view('books.copy', [
244             'book' => $book,
245         ]);
246     }
247
248     /**
249      * Create a copy of a book within the requested target destination.
250      *
251      * @throws NotFoundException
252      */
253     public function copy(Request $request, Cloner $cloner, string $bookSlug)
254     {
255         $book = $this->bookRepo->getBySlug($bookSlug);
256         $this->checkOwnablePermission('book-view', $book);
257         $this->checkPermission('book-create-all');
258
259         $newName = $request->get('name') ?: $book->name;
260         $bookCopy = $cloner->cloneBook($book, $newName);
261         $this->showSuccessNotification(trans('entities.books_copy_success'));
262
263         return redirect($bookCopy->getUrl());
264     }
265 }