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