1 <?php namespace BookStack\Http\Controllers;
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;
14 class BookController extends Controller
17 protected $entityRepo;
20 protected $chapterRepo;
24 * BookController constructor.
25 * @param BookRepo $bookRepo
26 * @param PageRepo $pageRepo
27 * @param ChapterRepo $chapterRepo
28 * @param UserRepo $userRepo
30 public function __construct(EntityRepo $entityRepo, BookRepo $bookRepo, PageRepo $pageRepo, ChapterRepo $chapterRepo, UserRepo $userRepo)
32 $this->entityRepo = $entityRepo;
33 // TODO - Remove below
34 $this->bookRepo = $bookRepo;
35 $this->pageRepo = $pageRepo;
36 $this->chapterRepo = $chapterRepo;
37 $this->userRepo = $userRepo;
38 parent::__construct();
42 * Display a listing of the book.
45 public function index()
47 $books = $this->entityRepo->getAllPaginated('book', 10);
48 $recents = $this->signedIn ? $this->entityRepo->getRecentlyViewed('book', 4, 0) : false;
49 $popular = $this->entityRepo->getPopular('book', 4, 0);
50 $this->setPageTitle('Books');
51 return view('books/index', ['books' => $books, 'recents' => $recents, 'popular' => $popular]);
55 * Show the form for creating a new book.
58 public function create()
60 $this->checkPermission('book-create-all');
61 $this->setPageTitle(trans('entities.books_create'));
62 return view('books/create');
66 * Store a newly created book in storage.
68 * @param Request $request
71 public function store(Request $request)
73 $this->checkPermission('book-create-all');
74 $this->validate($request, [
75 'name' => 'required|string|max:255',
76 'description' => 'string|max:1000'
78 $book = $this->bookRepo->createFromInput($request->all());
79 Activity::add($book, 'book_create', $book->id);
80 return redirect($book->getUrl());
84 * Display the specified book.
88 public function show($slug)
90 $book = $this->entityRepo->getBySlug('book', $slug);
91 $this->checkOwnablePermission('book-view', $book);
92 $bookChildren = $this->bookRepo->getChildren($book);
94 $this->setPageTitle($book->getShortName());
95 return view('books/show', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
99 * Show the form for editing the specified book.
103 public function edit($slug)
105 $book = $this->entityRepo->getBySlug('book', $slug);
106 $this->checkOwnablePermission('book-update', $book);
107 $this->setPageTitle(trans('entities.books_edit_named',['bookName'=>$book->getShortName()]));
108 return view('books/edit', ['book' => $book, 'current' => $book]);
112 * Update the specified book in storage.
113 * @param Request $request
117 public function update(Request $request, $slug)
119 $book = $this->entityRepo->getBySlug('book', $slug);
120 $this->checkOwnablePermission('book-update', $book);
121 $this->validate($request, [
122 'name' => 'required|string|max:255',
123 'description' => 'string|max:1000'
125 $book = $this->bookRepo->updateFromInput($book, $request->all());
126 Activity::add($book, 'book_update', $book->id);
127 return redirect($book->getUrl());
131 * Shows the page to confirm deletion
133 * @return \Illuminate\View\View
135 public function showDelete($bookSlug)
137 $book = $this->entityRepo->getBySlug('book', $bookSlug);
138 $this->checkOwnablePermission('book-delete', $book);
139 $this->setPageTitle(trans('entities.books_delete_named', ['bookName'=>$book->getShortName()]));
140 return view('books/delete', ['book' => $book, 'current' => $book]);
144 * Shows the view which allows pages to be re-ordered and sorted.
145 * @param string $bookSlug
146 * @return \Illuminate\View\View
148 public function sort($bookSlug)
150 $book = $this->entityRepo->getBySlug('book', $bookSlug);
151 $this->checkOwnablePermission('book-update', $book);
152 $bookChildren = $this->bookRepo->getChildren($book, true);
153 $books = $this->entityRepo->getAll('book', false);
154 $this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()]));
155 return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
159 * Shows the sort box for a single book.
160 * Used via AJAX when loading in extra books to a sort.
162 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
164 public function getSortItem($bookSlug)
166 $book = $this->entityRepo->getBySlug('book', $bookSlug);
167 $bookChildren = $this->bookRepo->getChildren($book);
168 return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
172 * Saves an array of sort mapping to pages and chapters.
173 * @param string $bookSlug
174 * @param Request $request
175 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
177 public function saveSort($bookSlug, Request $request)
179 $book = $this->entityRepo->getBySlug('book', $bookSlug);
180 $this->checkOwnablePermission('book-update', $book);
182 // Return if no map sent
183 if (!$request->has('sort-tree')) {
184 return redirect($book->getUrl());
187 // Sort pages and chapters
189 $updatedModels = collect();
190 $sortMap = json_decode($request->get('sort-tree'));
191 $defaultBookId = $book->id;
193 // Loop through contents of provided map and update entities accordingly
194 foreach ($sortMap as $bookChild) {
195 $priority = $bookChild->sort;
196 $id = intval($bookChild->id);
197 $isPage = $bookChild->type == 'page';
198 $bookId = $this->entityRepo->exists('book', $bookChild->book) ? intval($bookChild->book) : $defaultBookId;
199 $chapterId = ($isPage && $bookChild->parentChapter === false) ? 0 : intval($bookChild->parentChapter);
200 $model = $this->entityRepo->getById($isPage?'page':'chapter', $id);
202 // Update models only if there's a change in parent chain or ordering.
203 if ($model->priority !== $priority || $model->book_id !== $bookId || ($isPage && $model->chapter_id !== $chapterId)) {
204 $isPage ? $this->pageRepo->changeBook($bookId, $model) : $this->chapterRepo->changeBook($bookId, $model);
205 $model->priority = $priority;
206 if ($isPage) $model->chapter_id = $chapterId;
208 $updatedModels->push($model);
211 // Store involved books to be sorted later
212 if (!in_array($bookId, $sortedBooks)) {
213 $sortedBooks[] = $bookId;
217 // Add activity for books
218 foreach ($sortedBooks as $bookId) {
219 $updatedBook = $this->entityRepo->getById('book', $bookId);
220 Activity::add($updatedBook, 'book_sort', $updatedBook->id);
223 // Update permissions on changed models
224 $this->bookRepo->buildJointPermissions($updatedModels);
226 return redirect($book->getUrl());
230 * Remove the specified book from storage.
234 public function destroy($bookSlug)
236 $book = $this->entityRepo->getBySlug('book', $bookSlug);
237 $this->checkOwnablePermission('book-delete', $book);
238 Activity::addMessage('book_delete', 0, $book->name);
239 Activity::removeEntity($book);
240 $this->bookRepo->destroy($book);
241 return redirect('/books');
245 * Show the Restrictions view.
247 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
249 public function showRestrict($bookSlug)
251 $book = $this->entityRepo->getBySlug('book', $bookSlug);
252 $this->checkOwnablePermission('restrictions-manage', $book);
253 $roles = $this->userRepo->getRestrictableRoles();
254 return view('books/restrictions', [
261 * Set the restrictions for this book.
264 * @param Request $request
265 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
267 public function restrict($bookSlug, Request $request)
269 $book = $this->entityRepo->getBySlug('book', $bookSlug);
270 $this->checkOwnablePermission('restrictions-manage', $book);
271 $this->bookRepo->updateEntityPermissionsFromRequest($request, $book);
272 session()->flash('success', trans('entities.books_permissions_updated'));
273 return redirect($book->getUrl());