]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookController.php
Merge pull request #234 from BookStackApp/translations
[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 BookStack\Http\Requests;
7 use BookStack\Repos\BookRepo;
8 use BookStack\Repos\ChapterRepo;
9 use BookStack\Repos\PageRepo;
10 use Illuminate\Http\Response;
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(trans('entities.books_create'));
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(trans('entities.books_edit_named',['bookName'=>$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(trans('entities.books_delete_named', ['bookName'=>$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(trans('entities.books_sort_named', ['bookName'=>$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         // Sort pages and chapters
184         $sortedBooks = [];
185         $updatedModels = collect();
186         $sortMap = json_decode($request->get('sort-tree'));
187         $defaultBookId = $book->id;
188
189         // Loop through contents of provided map and update entities accordingly
190         foreach ($sortMap as $bookChild) {
191             $priority = $bookChild->sort;
192             $id = intval($bookChild->id);
193             $isPage = $bookChild->type == 'page';
194             $bookId = $this->bookRepo->exists($bookChild->book) ? intval($bookChild->book) : $defaultBookId;
195             $chapterId = ($isPage && $bookChild->parentChapter === false) ? 0 : intval($bookChild->parentChapter);
196             $model = $isPage ? $this->pageRepo->getById($id) : $this->chapterRepo->getById($id);
197
198             // Update models only if there's a change in parent chain or ordering.
199             if ($model->priority !== $priority || $model->book_id !== $bookId || ($isPage && $model->chapter_id !== $chapterId)) {
200                 $isPage ? $this->pageRepo->changeBook($bookId, $model) : $this->chapterRepo->changeBook($bookId, $model);
201                 $model->priority = $priority;
202                 if ($isPage) $model->chapter_id = $chapterId;
203                 $model->save();
204                 $updatedModels->push($model);
205             }
206
207             // Store involved books to be sorted later
208             if (!in_array($bookId, $sortedBooks)) {
209                 $sortedBooks[] = $bookId;
210             }
211         }
212
213         // Add activity for books
214         foreach ($sortedBooks as $bookId) {
215             $updatedBook = $this->bookRepo->getById($bookId);
216             Activity::add($updatedBook, 'book_sort', $updatedBook->id);
217         }
218
219         // Update permissions on changed models
220         $this->bookRepo->buildJointPermissions($updatedModels);
221
222         return redirect($book->getUrl());
223     }
224
225     /**
226      * Remove the specified book from storage.
227      * @param $bookSlug
228      * @return Response
229      */
230     public function destroy($bookSlug)
231     {
232         $book = $this->bookRepo->getBySlug($bookSlug);
233         $this->checkOwnablePermission('book-delete', $book);
234         Activity::addMessage('book_delete', 0, $book->name);
235         Activity::removeEntity($book);
236         $this->bookRepo->destroy($book);
237         return redirect('/books');
238     }
239
240     /**
241      * Show the Restrictions view.
242      * @param $bookSlug
243      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
244      */
245     public function showRestrict($bookSlug)
246     {
247         $book = $this->bookRepo->getBySlug($bookSlug);
248         $this->checkOwnablePermission('restrictions-manage', $book);
249         $roles = $this->userRepo->getRestrictableRoles();
250         return view('books/restrictions', [
251             'book' => $book,
252             'roles' => $roles
253         ]);
254     }
255
256     /**
257      * Set the restrictions for this book.
258      * @param $bookSlug
259      * @param $bookSlug
260      * @param Request $request
261      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
262      */
263     public function restrict($bookSlug, Request $request)
264     {
265         $book = $this->bookRepo->getBySlug($bookSlug);
266         $this->checkOwnablePermission('restrictions-manage', $book);
267         $this->bookRepo->updateEntityPermissionsFromRequest($request, $book);
268         session()->flash('success', trans('entities.books_permissions_updated'));
269         return redirect($book->getUrl());
270     }
271 }