]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookController.php
Added "update-url" command to find/replace url in the database
[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
118         Views::add($book);
119         if ($request->has('shelf')) {
120             $this->entityContextManager->setShelfContext(intval($request->get('shelf')));
121         }
122
123         $this->setPageTitle($book->getShortName());
124         return view('books.show', [
125             'book' => $book,
126             'current' => $book,
127             'bookChildren' => $bookChildren,
128             'activity' => Activity::entityActivity($book, 20, 1)
129         ]);
130     }
131
132     /**
133      * Show the form for editing the specified book.
134      */
135     public function edit(string $slug)
136     {
137         $book = $this->bookRepo->getBySlug($slug);
138         $this->checkOwnablePermission('book-update', $book);
139         $this->setPageTitle(trans('entities.books_edit_named', ['bookName'=>$book->getShortName()]));
140         return view('books.edit', ['book' => $book, 'current' => $book]);
141     }
142
143     /**
144      * Update the specified book in storage.
145      * @throws ImageUploadException
146      * @throws ValidationException
147      * @throws Throwable
148      */
149     public function update(Request $request, string $slug)
150     {
151         $book = $this->bookRepo->getBySlug($slug);
152         $this->checkOwnablePermission('book-update', $book);
153         $this->validate($request, [
154             'name' => 'required|string|max:255',
155             'description' => 'string|max:1000',
156             'image' => 'nullable|' . $this->getImageValidationRules(),
157         ]);
158
159         $book = $this->bookRepo->update($book, $request->all());
160         $resetCover = $request->has('image_reset');
161         $this->bookRepo->updateCoverImage($book, $request->file('image', null), $resetCover);
162
163         Activity::add($book, 'book_update', $book->id);
164
165         return redirect($book->getUrl());
166     }
167
168     /**
169      * Shows the page to confirm deletion.
170      */
171     public function showDelete(string $bookSlug)
172     {
173         $book = $this->bookRepo->getBySlug($bookSlug);
174         $this->checkOwnablePermission('book-delete', $book);
175         $this->setPageTitle(trans('entities.books_delete_named', ['bookName' => $book->getShortName()]));
176         return view('books.delete', ['book' => $book, 'current' => $book]);
177     }
178
179     /**
180      * Remove the specified book from the system.
181      * @throws Throwable
182      * @throws NotifyException
183      */
184     public function destroy(string $bookSlug)
185     {
186         $book = $this->bookRepo->getBySlug($bookSlug);
187         $this->checkOwnablePermission('book-delete', $book);
188
189         Activity::addMessage('book_delete', $book->name);
190         $this->bookRepo->destroy($book);
191
192         return redirect('/books');
193     }
194
195     /**
196      * Show the permissions view.
197      */
198     public function showPermissions(string $bookSlug)
199     {
200         $book = $this->bookRepo->getBySlug($bookSlug);
201         $this->checkOwnablePermission('restrictions-manage', $book);
202
203         return view('books.permissions', [
204             'book' => $book,
205         ]);
206     }
207
208     /**
209      * Set the restrictions for this book.
210      * @throws Throwable
211      */
212     public function permissions(Request $request, string $bookSlug)
213     {
214         $book = $this->bookRepo->getBySlug($bookSlug);
215         $this->checkOwnablePermission('restrictions-manage', $book);
216
217         $restricted = $request->get('restricted') === 'true';
218         $permissions = $request->filled('restrictions') ? collect($request->get('restrictions')) : null;
219         $this->bookRepo->updatePermissions($book, $restricted, $permissions);
220
221         $this->showSuccessNotification(trans('entities.books_permissions_updated'));
222         return redirect($book->getUrl());
223     }
224 }