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 EntityRepo $entityRepo
26 * @param BookRepo $bookRepo
27 * @param PageRepo $pageRepo
28 * @param ChapterRepo $chapterRepo
29 * @param UserRepo $userRepo
31 public function __construct(EntityRepo $entityRepo, BookRepo $bookRepo, PageRepo $pageRepo, ChapterRepo $chapterRepo, UserRepo $userRepo)
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();
43 * Display a listing of the book.
46 public function index()
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]);
56 * Show the form for creating a new book.
59 public function create()
61 $this->checkPermission('book-create-all');
62 $this->setPageTitle(trans('entities.books_create'));
63 return view('books/create');
67 * Store a newly created book in storage.
69 * @param Request $request
72 public function store(Request $request)
74 $this->checkPermission('book-create-all');
75 $this->validate($request, [
76 'name' => 'required|string|max:255',
77 'description' => 'string|max:1000'
79 $book = $this->bookRepo->createFromInput($request->all());
80 Activity::add($book, 'book_create', $book->id);
81 return redirect($book->getUrl());
85 * Display the specified book.
89 public function show($slug)
91 $book = $this->entityRepo->getBySlug('book', $slug);
92 $this->checkOwnablePermission('book-view', $book);
93 $bookChildren = $this->bookRepo->getChildren($book);
95 $this->setPageTitle($book->getShortName());
96 return view('books/show', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
100 * Show the form for editing the specified book.
104 public function edit($slug)
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]);
113 * Update the specified book in storage.
114 * @param Request $request
118 public function update(Request $request, $slug)
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'
126 $book = $this->bookRepo->updateFromInput($book, $request->all());
127 Activity::add($book, 'book_update', $book->id);
128 return redirect($book->getUrl());
132 * Shows the page to confirm deletion
134 * @return \Illuminate\View\View
136 public function showDelete($bookSlug)
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]);
145 * Shows the view which allows pages to be re-ordered and sorted.
146 * @param string $bookSlug
147 * @return \Illuminate\View\View
149 public function sort($bookSlug)
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]);
160 * Shows the sort box for a single book.
161 * Used via AJAX when loading in extra books to a sort.
163 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
165 public function getSortItem($bookSlug)
167 $book = $this->entityRepo->getBySlug('book', $bookSlug);
168 $bookChildren = $this->bookRepo->getChildren($book);
169 return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
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
178 public function saveSort($bookSlug, Request $request)
180 $book = $this->entityRepo->getBySlug('book', $bookSlug);
181 $this->checkOwnablePermission('book-update', $book);
183 // Return if no map sent
184 if (!$request->has('sort-tree')) {
185 return redirect($book->getUrl());
188 // Sort pages and chapters
190 $updatedModels = collect();
191 $sortMap = json_decode($request->get('sort-tree'));
192 $defaultBookId = $book->id;
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);
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;
209 $updatedModels->push($model);
212 // Store involved books to be sorted later
213 if (!in_array($bookId, $sortedBooks)) {
214 $sortedBooks[] = $bookId;
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);
224 // Update permissions on changed models
225 $this->bookRepo->buildJointPermissions($updatedModels);
227 return redirect($book->getUrl());
231 * Remove the specified book from storage.
235 public function destroy($bookSlug)
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');
246 * Show the Restrictions view.
248 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
250 public function showRestrict($bookSlug)
252 $book = $this->entityRepo->getBySlug('book', $bookSlug);
253 $this->checkOwnablePermission('restrictions-manage', $book);
254 $roles = $this->userRepo->getRestrictableRoles();
255 return view('books/restrictions', [
262 * Set the restrictions for this book.
265 * @param Request $request
266 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
268 public function restrict($bookSlug, Request $request)
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());