]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookController.php
Started work towards adding role view permissions
[bookstack] / app / Http / Controllers / BookController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use Activity;
4 use BookStack\Repos\UserRepo;
5 use Illuminate\Http\Request;
6 use Illuminate\Support\Facades\Auth;
7 use BookStack\Http\Requests;
8 use BookStack\Repos\BookRepo;
9 use BookStack\Repos\ChapterRepo;
10 use BookStack\Repos\PageRepo;
11 use Views;
12
13 class BookController extends Controller
14 {
15
16     protected $bookRepo;
17     protected $pageRepo;
18     protected $chapterRepo;
19     protected $userRepo;
20
21     /**
22      * BookController constructor.
23      * @param BookRepo $bookRepo
24      * @param PageRepo $pageRepo
25      * @param ChapterRepo $chapterRepo
26      * @param UserRepo $userRepo
27      */
28     public function __construct(BookRepo $bookRepo, PageRepo $pageRepo, ChapterRepo $chapterRepo, UserRepo $userRepo)
29     {
30         $this->bookRepo = $bookRepo;
31         $this->pageRepo = $pageRepo;
32         $this->chapterRepo = $chapterRepo;
33         $this->userRepo = $userRepo;
34         parent::__construct();
35     }
36
37     /**
38      * Display a listing of the book.
39      *
40      * @return Response
41      */
42     public function index()
43     {
44         $books = $this->bookRepo->getAllPaginated(10);
45         $recents = $this->signedIn ? $this->bookRepo->getRecentlyViewed(4, 0) : false;
46         $popular = $this->bookRepo->getPopular(4, 0);
47         $this->setPageTitle('Books');
48         return view('books/index', ['books' => $books, 'recents' => $recents, 'popular' => $popular]);
49     }
50
51     /**
52      * Show the form for creating a new book.
53      *
54      * @return Response
55      */
56     public function create()
57     {
58         $this->checkPermission('book-create-all');
59         $this->setPageTitle('Create New Book');
60         return view('books/create');
61     }
62
63     /**
64      * Store a newly created book in storage.
65      *
66      * @param  Request $request
67      * @return Response
68      */
69     public function store(Request $request)
70     {
71         $this->checkPermission('book-create-all');
72         $this->validate($request, [
73             'name' => 'required|string|max:255',
74             'description' => 'string|max:1000'
75         ]);
76         $book = $this->bookRepo->newFromInput($request->all());
77         $book->slug = $this->bookRepo->findSuitableSlug($book->name);
78         $book->created_by = Auth::user()->id;
79         $book->updated_by = Auth::user()->id;
80         $book->save();
81         Activity::add($book, 'book_create', $book->id);
82         return redirect($book->getUrl());
83     }
84
85     /**
86      * Display the specified book.
87      *
88      * @param $slug
89      * @return Response
90      */
91     public function show($slug)
92     {
93         $book = $this->bookRepo->getBySlug($slug);
94         $this->checkOwnablePermission('book-view', $book);
95         $bookChildren = $this->bookRepo->getChildren($book);
96         Views::add($book);
97         $this->setPageTitle($book->getShortName());
98         return view('books/show', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
99     }
100
101     /**
102      * Show the form for editing the specified book.
103      *
104      * @param $slug
105      * @return Response
106      */
107     public function edit($slug)
108     {
109         $book = $this->bookRepo->getBySlug($slug);
110         $this->checkOwnablePermission('book-update', $book);
111         $this->setPageTitle('Edit Book ' . $book->getShortName());
112         return view('books/edit', ['book' => $book, 'current' => $book]);
113     }
114
115     /**
116      * Update the specified book in storage.
117      *
118      * @param  Request $request
119      * @param          $slug
120      * @return Response
121      */
122     public function update(Request $request, $slug)
123     {
124         $book = $this->bookRepo->getBySlug($slug);
125         $this->checkOwnablePermission('book-update', $book);
126         $this->validate($request, [
127             'name' => 'required|string|max:255',
128             'description' => 'string|max:1000'
129         ]);
130         $book->fill($request->all());
131         $book->slug = $this->bookRepo->findSuitableSlug($book->name, $book->id);
132         $book->updated_by = Auth::user()->id;
133         $book->save();
134         Activity::add($book, 'book_update', $book->id);
135         return redirect($book->getUrl());
136     }
137
138     /**
139      * Shows the page to confirm deletion
140      * @param $bookSlug
141      * @return \Illuminate\View\View
142      */
143     public function showDelete($bookSlug)
144     {
145         $book = $this->bookRepo->getBySlug($bookSlug);
146         $this->checkOwnablePermission('book-delete', $book);
147         $this->setPageTitle('Delete Book ' . $book->getShortName());
148         return view('books/delete', ['book' => $book, 'current' => $book]);
149     }
150
151     /**
152      * Shows the view which allows pages to be re-ordered and sorted.
153      * @param string $bookSlug
154      * @return \Illuminate\View\View
155      */
156     public function sort($bookSlug)
157     {
158         $book = $this->bookRepo->getBySlug($bookSlug);
159         $this->checkOwnablePermission('book-update', $book);
160         $bookChildren = $this->bookRepo->getChildren($book);
161         $books = $this->bookRepo->getAll(false);
162         $this->setPageTitle('Sort Book ' . $book->getShortName());
163         return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
164     }
165
166     /**
167      * Shows the sort box for a single book.
168      * Used via AJAX when loading in extra books to a sort.
169      * @param $bookSlug
170      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
171      */
172     public function getSortItem($bookSlug)
173     {
174         $book = $this->bookRepo->getBySlug($bookSlug);
175         $bookChildren = $this->bookRepo->getChildren($book);
176         return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
177     }
178
179     /**
180      * Saves an array of sort mapping to pages and chapters.
181      * @param  string $bookSlug
182      * @param Request $request
183      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
184      */
185     public function saveSort($bookSlug, Request $request)
186     {
187         $book = $this->bookRepo->getBySlug($bookSlug);
188         $this->checkOwnablePermission('book-update', $book);
189
190         // Return if no map sent
191         if (!$request->has('sort-tree')) {
192             return redirect($book->getUrl());
193         }
194
195         $sortedBooks = [];
196         // Sort pages and chapters
197         $sortMap = json_decode($request->get('sort-tree'));
198         $defaultBookId = $book->id;
199         foreach ($sortMap as $index => $bookChild) {
200             $id = $bookChild->id;
201             $isPage = $bookChild->type == 'page';
202             $bookId = $this->bookRepo->exists($bookChild->book) ? $bookChild->book : $defaultBookId;
203             $model = $isPage ? $this->pageRepo->getById($id) : $this->chapterRepo->getById($id);
204             $isPage ? $this->pageRepo->changeBook($bookId, $model) : $this->chapterRepo->changeBook($bookId, $model);
205             $model->priority = $index;
206             if ($isPage) {
207                 $model->chapter_id = ($bookChild->parentChapter === false) ? 0 : $bookChild->parentChapter;
208             }
209             $model->save();
210             if (!in_array($bookId, $sortedBooks)) {
211                 $sortedBooks[] = $bookId;
212             }
213         }
214
215         // Add activity for books
216         foreach ($sortedBooks as $bookId) {
217             $updatedBook = $this->bookRepo->getById($bookId);
218             Activity::add($updatedBook, 'book_sort', $updatedBook->id);
219         }
220
221         return redirect($book->getUrl());
222     }
223
224     /**
225      * Remove the specified book from storage.
226      * @param $bookSlug
227      * @return Response
228      */
229     public function destroy($bookSlug)
230     {
231         $book = $this->bookRepo->getBySlug($bookSlug);
232         $this->checkOwnablePermission('book-delete', $book);
233         Activity::addMessage('book_delete', 0, $book->name);
234         Activity::removeEntity($book);
235         $this->bookRepo->destroyBySlug($bookSlug);
236         return redirect('/books');
237     }
238
239     /**
240      * Show the Restrictions view.
241      * @param $bookSlug
242      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
243      */
244     public function showRestrict($bookSlug)
245     {
246         $book = $this->bookRepo->getBySlug($bookSlug);
247         $this->checkOwnablePermission('restrictions-manage', $book);
248         $roles = $this->userRepo->getRestrictableRoles();
249         return view('books/restrictions', [
250             'book' => $book,
251             'roles' => $roles
252         ]);
253     }
254
255     /**
256      * Set the restrictions for this book.
257      * @param $bookSlug
258      * @param $bookSlug
259      * @param Request $request
260      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
261      */
262     public function restrict($bookSlug, Request $request)
263     {
264         $book = $this->bookRepo->getBySlug($bookSlug);
265         $this->checkOwnablePermission('restrictions-manage', $book);
266         $this->bookRepo->updateRestrictionsFromRequest($request, $book);
267         session()->flash('success', 'Book Restrictions Updated');
268         return redirect($book->getUrl());
269     }
270 }