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;
12 class BookController extends Controller
17 protected $chapterRepo;
21 * BookController constructor.
22 * @param BookRepo $bookRepo
23 * @param PageRepo $pageRepo
24 * @param ChapterRepo $chapterRepo
25 * @param UserRepo $userRepo
27 public function __construct(BookRepo $bookRepo, PageRepo $pageRepo, ChapterRepo $chapterRepo, UserRepo $userRepo)
29 $this->bookRepo = $bookRepo;
30 $this->pageRepo = $pageRepo;
31 $this->chapterRepo = $chapterRepo;
32 $this->userRepo = $userRepo;
33 parent::__construct();
37 * Display a listing of the book.
40 public function index()
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]);
50 * Show the form for creating a new book.
53 public function create()
55 $this->checkPermission('book-create-all');
56 $this->setPageTitle('Create New Book');
57 return view('books/create');
61 * Store a newly created book in storage.
63 * @param Request $request
66 public function store(Request $request)
68 $this->checkPermission('book-create-all');
69 $this->validate($request, [
70 'name' => 'required|string|max:255',
71 'description' => 'string|max:1000'
73 $book = $this->bookRepo->createFromInput($request->all());
74 Activity::add($book, 'book_create', $book->id);
75 return redirect($book->getUrl());
79 * Display the specified book.
83 public function show($slug)
85 $book = $this->bookRepo->getBySlug($slug);
86 $this->checkOwnablePermission('book-view', $book);
87 $bookChildren = $this->bookRepo->getChildren($book);
89 $this->setPageTitle($book->getShortName());
90 return view('books/show', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
94 * Show the form for editing the specified book.
98 public function edit($slug)
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]);
107 * Update the specified book in storage.
108 * @param Request $request
112 public function update(Request $request, $slug)
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'
120 $book = $this->bookRepo->updateFromInput($book, $request->all());
121 Activity::add($book, 'book_update', $book->id);
122 return redirect($book->getUrl());
126 * Shows the page to confirm deletion
128 * @return \Illuminate\View\View
130 public function showDelete($bookSlug)
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]);
139 * Shows the view which allows pages to be re-ordered and sorted.
140 * @param string $bookSlug
141 * @return \Illuminate\View\View
143 public function sort($bookSlug)
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]);
154 * Shows the sort box for a single book.
155 * Used via AJAX when loading in extra books to a sort.
157 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
159 public function getSortItem($bookSlug)
161 $book = $this->bookRepo->getBySlug($bookSlug);
162 $bookChildren = $this->bookRepo->getChildren($book);
163 return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
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
172 public function saveSort($bookSlug, Request $request)
174 $book = $this->bookRepo->getBySlug($bookSlug);
175 $this->checkOwnablePermission('book-update', $book);
177 // Return if no map sent
178 if (!$request->has('sort-tree')) {
179 return redirect($book->getUrl());
182 // Sort pages and chapters
184 $updatedModels = collect();
185 $sortMap = json_decode($request->get('sort-tree'));
186 $defaultBookId = $book->id;
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);
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;
203 $updatedModels->push($model);
206 // Store involved books to be sorted later
207 if (!in_array($bookId, $sortedBooks)) {
208 $sortedBooks[] = $bookId;
212 // Add activity for books
213 foreach ($sortedBooks as $bookId) {
214 $updatedBook = $this->bookRepo->getById($bookId);
215 Activity::add($updatedBook, 'book_sort', $updatedBook->id);
218 // Update permissions on changed models
219 $this->bookRepo->buildJointPermissions($updatedModels);
221 return redirect($book->getUrl());
225 * Remove the specified book from storage.
229 public function destroy($bookSlug)
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');
240 * Show the Restrictions view.
242 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
244 public function showRestrict($bookSlug)
246 $book = $this->bookRepo->getBySlug($bookSlug);
247 $this->checkOwnablePermission('restrictions-manage', $book);
248 $roles = $this->userRepo->getRestrictableRoles();
249 return view('books/restrictions', [
256 * Set the restrictions for this book.
259 * @param Request $request
260 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
262 public function restrict($bookSlug, Request $request)
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());