1 <?php namespace BookStack\Http\Controllers;
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;
13 class BookController extends Controller
18 protected $chapterRepo;
22 * BookController constructor.
23 * @param BookRepo $bookRepo
24 * @param PageRepo $pageRepo
25 * @param ChapterRepo $chapterRepo
26 * @param UserRepo $userRepo
28 public function __construct(BookRepo $bookRepo, PageRepo $pageRepo, ChapterRepo $chapterRepo, UserRepo $userRepo)
30 $this->bookRepo = $bookRepo;
31 $this->pageRepo = $pageRepo;
32 $this->chapterRepo = $chapterRepo;
33 $this->userRepo = $userRepo;
34 parent::__construct();
38 * Display a listing of the book.
41 public function index()
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]);
51 * Show the form for creating a new book.
54 public function create()
56 $this->checkPermission('book-create-all');
57 $this->setPageTitle(trans('entities.books_create'));
58 return view('books/create');
62 * Store a newly created book in storage.
64 * @param Request $request
67 public function store(Request $request)
69 $this->checkPermission('book-create-all');
70 $this->validate($request, [
71 'name' => 'required|string|max:255',
72 'description' => 'string|max:1000'
74 $book = $this->bookRepo->createFromInput($request->all());
75 Activity::add($book, 'book_create', $book->id);
76 return redirect($book->getUrl());
80 * Display the specified book.
84 public function show($slug)
86 $book = $this->bookRepo->getBySlug($slug);
87 $this->checkOwnablePermission('book-view', $book);
88 $bookChildren = $this->bookRepo->getChildren($book);
90 $this->setPageTitle($book->getShortName());
91 return view('books/show', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
95 * Show the form for editing the specified book.
99 public function edit($slug)
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]);
108 * Update the specified book in storage.
109 * @param Request $request
113 public function update(Request $request, $slug)
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'
121 $book = $this->bookRepo->updateFromInput($book, $request->all());
122 Activity::add($book, 'book_update', $book->id);
123 return redirect($book->getUrl());
127 * Shows the page to confirm deletion
129 * @return \Illuminate\View\View
131 public function showDelete($bookSlug)
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]);
140 * Shows the view which allows pages to be re-ordered and sorted.
141 * @param string $bookSlug
142 * @return \Illuminate\View\View
144 public function sort($bookSlug)
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]);
155 * Shows the sort box for a single book.
156 * Used via AJAX when loading in extra books to a sort.
158 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
160 public function getSortItem($bookSlug)
162 $book = $this->bookRepo->getBySlug($bookSlug);
163 $bookChildren = $this->bookRepo->getChildren($book);
164 return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
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
173 public function saveSort($bookSlug, Request $request)
175 $book = $this->bookRepo->getBySlug($bookSlug);
176 $this->checkOwnablePermission('book-update', $book);
178 // Return if no map sent
179 if (!$request->has('sort-tree')) {
180 return redirect($book->getUrl());
183 // Sort pages and chapters
185 $updatedModels = collect();
186 $sortMap = json_decode($request->get('sort-tree'));
187 $defaultBookId = $book->id;
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);
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;
204 $updatedModels->push($model);
207 // Store involved books to be sorted later
208 if (!in_array($bookId, $sortedBooks)) {
209 $sortedBooks[] = $bookId;
213 // Add activity for books
214 foreach ($sortedBooks as $bookId) {
215 $updatedBook = $this->bookRepo->getById($bookId);
216 Activity::add($updatedBook, 'book_sort', $updatedBook->id);
219 // Update permissions on changed models
220 $this->bookRepo->buildJointPermissions($updatedModels);
222 return redirect($book->getUrl());
226 * Remove the specified book from storage.
230 public function destroy($bookSlug)
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');
241 * Show the Restrictions view.
243 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
245 public function showRestrict($bookSlug)
247 $book = $this->bookRepo->getBySlug($bookSlug);
248 $this->checkOwnablePermission('restrictions-manage', $book);
249 $roles = $this->userRepo->getRestrictableRoles();
250 return view('books/restrictions', [
257 * Set the restrictions for this book.
260 * @param Request $request
261 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
263 public function restrict($bookSlug, Request $request)
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());