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