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