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