]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookController.php
Add Carbon localization support
[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 Views;
11
12 class BookController extends Controller
13 {
14
15     protected $bookRepo;
16     protected $pageRepo;
17     protected $chapterRepo;
18     protected $userRepo;
19
20     /**
21      * BookController constructor.
22      * @param BookRepo $bookRepo
23      * @param PageRepo $pageRepo
24      * @param ChapterRepo $chapterRepo
25      * @param UserRepo $userRepo
26      */
27     public function __construct(BookRepo $bookRepo, PageRepo $pageRepo, ChapterRepo $chapterRepo, UserRepo $userRepo)
28     {
29         $this->bookRepo = $bookRepo;
30         $this->pageRepo = $pageRepo;
31         $this->chapterRepo = $chapterRepo;
32         $this->userRepo = $userRepo;
33         parent::__construct();
34     }
35
36     /**
37      * Display a listing of the book.
38      * @return Response
39      */
40     public function index()
41     {
42         $books = $this->bookRepo->getAllPaginated(10);
43         $recents = $this->signedIn ? $this->bookRepo->getRecentlyViewed(4, 0) : false;
44         $popular = $this->bookRepo->getPopular(4, 0);
45         $this->setPageTitle('Books');
46         return view('books/index', ['books' => $books, 'recents' => $recents, 'popular' => $popular]);
47     }
48
49     /**
50      * Show the form for creating a new book.
51      * @return Response
52      */
53     public function create()
54     {
55         $this->checkPermission('book-create-all');
56         $this->setPageTitle('Create New Book');
57         return view('books/create');
58     }
59
60     /**
61      * Store a newly created book in storage.
62      *
63      * @param  Request $request
64      * @return Response
65      */
66     public function store(Request $request)
67     {
68         $this->checkPermission('book-create-all');
69         $this->validate($request, [
70             'name' => 'required|string|max:255',
71             'description' => 'string|max:1000'
72         ]);
73         $book = $this->bookRepo->createFromInput($request->all());
74         Activity::add($book, 'book_create', $book->id);
75         return redirect($book->getUrl());
76     }
77
78     /**
79      * Display the specified book.
80      * @param $slug
81      * @return Response
82      */
83     public function show($slug)
84     {
85         $book = $this->bookRepo->getBySlug($slug);
86         $this->checkOwnablePermission('book-view', $book);
87         $bookChildren = $this->bookRepo->getChildren($book);
88         Views::add($book);
89         $this->setPageTitle($book->getShortName());
90         return view('books/show', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
91     }
92
93     /**
94      * Show the form for editing the specified book.
95      * @param $slug
96      * @return Response
97      */
98     public function edit($slug)
99     {
100         $book = $this->bookRepo->getBySlug($slug);
101         $this->checkOwnablePermission('book-update', $book);
102         $this->setPageTitle('Edit Book ' . $book->getShortName());
103         return view('books/edit', ['book' => $book, 'current' => $book]);
104     }
105
106     /**
107      * Update the specified book in storage.
108      * @param  Request $request
109      * @param          $slug
110      * @return Response
111      */
112     public function update(Request $request, $slug)
113     {
114         $book = $this->bookRepo->getBySlug($slug);
115         $this->checkOwnablePermission('book-update', $book);
116         $this->validate($request, [
117             'name' => 'required|string|max:255',
118             'description' => 'string|max:1000'
119         ]);
120         $book = $this->bookRepo->updateFromInput($book, $request->all());
121         Activity::add($book, 'book_update', $book->id);
122         return redirect($book->getUrl());
123     }
124
125     /**
126      * Shows the page to confirm deletion
127      * @param $bookSlug
128      * @return \Illuminate\View\View
129      */
130     public function showDelete($bookSlug)
131     {
132         $book = $this->bookRepo->getBySlug($bookSlug);
133         $this->checkOwnablePermission('book-delete', $book);
134         $this->setPageTitle('Delete Book ' . $book->getShortName());
135         return view('books/delete', ['book' => $book, 'current' => $book]);
136     }
137
138     /**
139      * Shows the view which allows pages to be re-ordered and sorted.
140      * @param string $bookSlug
141      * @return \Illuminate\View\View
142      */
143     public function sort($bookSlug)
144     {
145         $book = $this->bookRepo->getBySlug($bookSlug);
146         $this->checkOwnablePermission('book-update', $book);
147         $bookChildren = $this->bookRepo->getChildren($book, true);
148         $books = $this->bookRepo->getAll(false);
149         $this->setPageTitle('Sort Book ' . $book->getShortName());
150         return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
151     }
152
153     /**
154      * Shows the sort box for a single book.
155      * Used via AJAX when loading in extra books to a sort.
156      * @param $bookSlug
157      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
158      */
159     public function getSortItem($bookSlug)
160     {
161         $book = $this->bookRepo->getBySlug($bookSlug);
162         $bookChildren = $this->bookRepo->getChildren($book);
163         return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
164     }
165
166     /**
167      * Saves an array of sort mapping to pages and chapters.
168      * @param  string $bookSlug
169      * @param Request $request
170      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
171      */
172     public function saveSort($bookSlug, Request $request)
173     {
174         $book = $this->bookRepo->getBySlug($bookSlug);
175         $this->checkOwnablePermission('book-update', $book);
176
177         // Return if no map sent
178         if (!$request->has('sort-tree')) {
179             return redirect($book->getUrl());
180         }
181
182         // Sort pages and chapters
183         $sortedBooks = [];
184         $updatedModels = collect();
185         $sortMap = json_decode($request->get('sort-tree'));
186         $defaultBookId = $book->id;
187
188         // Loop through contents of provided map and update entities accordingly
189         foreach ($sortMap as $bookChild) {
190             $priority = $bookChild->sort;
191             $id = intval($bookChild->id);
192             $isPage = $bookChild->type == 'page';
193             $bookId = $this->bookRepo->exists($bookChild->book) ? intval($bookChild->book) : $defaultBookId;
194             $chapterId = ($isPage && $bookChild->parentChapter === false) ? 0 : intval($bookChild->parentChapter);
195             $model = $isPage ? $this->pageRepo->getById($id) : $this->chapterRepo->getById($id);
196
197             // Update models only if there's a change in parent chain or ordering.
198             if ($model->priority !== $priority || $model->book_id !== $bookId || ($isPage && $model->chapter_id !== $chapterId)) {
199                 $isPage ? $this->pageRepo->changeBook($bookId, $model) : $this->chapterRepo->changeBook($bookId, $model);
200                 $model->priority = $priority;
201                 if ($isPage) $model->chapter_id = $chapterId;
202                 $model->save();
203                 $updatedModels->push($model);
204             }
205
206             // Store involved books to be sorted later
207             if (!in_array($bookId, $sortedBooks)) {
208                 $sortedBooks[] = $bookId;
209             }
210         }
211
212         // Add activity for books
213         foreach ($sortedBooks as $bookId) {
214             $updatedBook = $this->bookRepo->getById($bookId);
215             Activity::add($updatedBook, 'book_sort', $updatedBook->id);
216         }
217
218         // Update permissions on changed models
219         $this->bookRepo->buildJointPermissions($updatedModels);
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->destroy($book);
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->updateEntityPermissionsFromRequest($request, $book);
267         session()->flash('success', 'Book Restrictions Updated');
268         return redirect($book->getUrl());
269     }
270 }