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