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