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