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