1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Repos\EntityRepo;
5 use BookStack\Repos\UserRepo;
6 use Illuminate\Http\Request;
7 use Illuminate\Http\Response;
10 class BookController extends Controller
13 protected $entityRepo;
17 * BookController constructor.
18 * @param EntityRepo $entityRepo
19 * @param UserRepo $userRepo
21 public function __construct(EntityRepo $entityRepo, UserRepo $userRepo)
23 $this->entityRepo = $entityRepo;
24 $this->userRepo = $userRepo;
25 parent::__construct();
29 * Display a listing of the book.
32 public function index()
34 $books = $this->entityRepo->getAllPaginated('book', 10);
35 $recents = $this->signedIn ? $this->entityRepo->getRecentlyViewed('book', 4, 0) : false;
36 $popular = $this->entityRepo->getPopular('book', 4, 0);
37 $this->setPageTitle('Books');
38 return view('books/index', ['books' => $books, 'recents' => $recents, 'popular' => $popular]);
42 * Show the form for creating a new book.
45 public function create()
47 $this->checkPermission('book-create-all');
48 $this->setPageTitle(trans('entities.books_create'));
49 return view('books/create');
53 * Store a newly created book in storage.
55 * @param Request $request
58 public function store(Request $request)
60 $this->checkPermission('book-create-all');
61 $this->validate($request, [
62 'name' => 'required|string|max:255',
63 'description' => 'string|max:1000'
65 $book = $this->entityRepo->createFromInput('book', $request->all());
66 Activity::add($book, 'book_create', $book->id);
67 return redirect($book->getUrl());
71 * Display the specified book.
75 public function show($slug)
77 $book = $this->entityRepo->getBySlug('book', $slug);
78 $this->checkOwnablePermission('book-view', $book);
79 $bookChildren = $this->entityRepo->getBookChildren($book);
81 $this->setPageTitle($book->getShortName());
82 return view('books/show', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
86 * Show the form for editing the specified book.
90 public function edit($slug)
92 $book = $this->entityRepo->getBySlug('book', $slug);
93 $this->checkOwnablePermission('book-update', $book);
94 $this->setPageTitle(trans('entities.books_edit_named',['bookName'=>$book->getShortName()]));
95 return view('books/edit', ['book' => $book, 'current' => $book]);
99 * Update the specified book in storage.
100 * @param Request $request
104 public function update(Request $request, $slug)
106 $book = $this->entityRepo->getBySlug('book', $slug);
107 $this->checkOwnablePermission('book-update', $book);
108 $this->validate($request, [
109 'name' => 'required|string|max:255',
110 'description' => 'string|max:1000'
112 $book = $this->entityRepo->updateFromInput('book', $book, $request->all());
113 Activity::add($book, 'book_update', $book->id);
114 return redirect($book->getUrl());
118 * Shows the page to confirm deletion
120 * @return \Illuminate\View\View
122 public function showDelete($bookSlug)
124 $book = $this->entityRepo->getBySlug('book', $bookSlug);
125 $this->checkOwnablePermission('book-delete', $book);
126 $this->setPageTitle(trans('entities.books_delete_named', ['bookName'=>$book->getShortName()]));
127 return view('books/delete', ['book' => $book, 'current' => $book]);
131 * Shows the view which allows pages to be re-ordered and sorted.
132 * @param string $bookSlug
133 * @return \Illuminate\View\View
135 public function sort($bookSlug)
137 $book = $this->entityRepo->getBySlug('book', $bookSlug);
138 $this->checkOwnablePermission('book-update', $book);
139 $bookChildren = $this->entityRepo->getBookChildren($book, true);
140 $books = $this->entityRepo->getAll('book', false);
141 $this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()]));
142 return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
146 * Shows the sort box for a single book.
147 * Used via AJAX when loading in extra books to a sort.
149 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
151 public function getSortItem($bookSlug)
153 $book = $this->entityRepo->getBySlug('book', $bookSlug);
154 $bookChildren = $this->entityRepo->getBookChildren($book);
155 return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
159 * Saves an array of sort mapping to pages and chapters.
160 * @param string $bookSlug
161 * @param Request $request
162 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
164 public function saveSort($bookSlug, Request $request)
166 $book = $this->entityRepo->getBySlug('book', $bookSlug);
167 $this->checkOwnablePermission('book-update', $book);
169 // Return if no map sent
170 if (!$request->has('sort-tree')) {
171 return redirect($book->getUrl());
174 // Sort pages and chapters
176 $updatedModels = collect();
177 $sortMap = json_decode($request->get('sort-tree'));
178 $defaultBookId = $book->id;
180 // Loop through contents of provided map and update entities accordingly
181 foreach ($sortMap as $bookChild) {
182 $priority = $bookChild->sort;
183 $id = intval($bookChild->id);
184 $isPage = $bookChild->type == 'page';
185 $bookId = $this->entityRepo->exists('book', $bookChild->book) ? intval($bookChild->book) : $defaultBookId;
186 $chapterId = ($isPage && $bookChild->parentChapter === false) ? 0 : intval($bookChild->parentChapter);
187 $model = $this->entityRepo->getById($isPage?'page':'chapter', $id);
189 // Update models only if there's a change in parent chain or ordering.
190 if ($model->priority !== $priority || $model->book_id !== $bookId || ($isPage && $model->chapter_id !== $chapterId)) {
191 $this->entityRepo->changeBook($isPage?'page':'chapter', $bookId, $model);
192 $model->priority = $priority;
193 if ($isPage) $model->chapter_id = $chapterId;
195 $updatedModels->push($model);
198 // Store involved books to be sorted later
199 if (!in_array($bookId, $sortedBooks)) {
200 $sortedBooks[] = $bookId;
204 // Add activity for books
205 foreach ($sortedBooks as $bookId) {
206 $updatedBook = $this->entityRepo->getById('book', $bookId);
207 Activity::add($updatedBook, 'book_sort', $updatedBook->id);
210 // Update permissions on changed models
211 $this->entityRepo->buildJointPermissions($updatedModels);
213 return redirect($book->getUrl());
217 * Remove the specified book from storage.
221 public function destroy($bookSlug)
223 $book = $this->entityRepo->getBySlug('book', $bookSlug);
224 $this->checkOwnablePermission('book-delete', $book);
225 Activity::addMessage('book_delete', 0, $book->name);
226 $this->entityRepo->destroyBook($book);
227 return redirect('/books');
231 * Show the Restrictions view.
233 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
235 public function showRestrict($bookSlug)
237 $book = $this->entityRepo->getBySlug('book', $bookSlug);
238 $this->checkOwnablePermission('restrictions-manage', $book);
239 $roles = $this->userRepo->getRestrictableRoles();
240 return view('books/restrictions', [
247 * Set the restrictions for this book.
250 * @param Request $request
251 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
253 public function restrict($bookSlug, Request $request)
255 $book = $this->entityRepo->getBySlug('book', $bookSlug);
256 $this->checkOwnablePermission('restrictions-manage', $book);
257 $this->entityRepo->updateEntityPermissionsFromRequest($request, $book);
258 session()->flash('success', trans('entities.books_permissions_updated'));
259 return redirect($book->getUrl());